C语言五子棋程序代码
时间: 2024-06-18 16:04:34 浏览: 242
五子棋程序是一种经典的人机博弈游戏,C语言编写的五子棋程序可以让我们更好地理解程序设计和算法实现。五子棋程序主要包括以下几个部分:
1. 界面设计:包括棋盘的显示、落子的标识、游戏结果的显示等。
2. 游戏逻辑:包括玩家与电脑的交互、判断胜负、落子的规则等。
3. AI算法:实现电脑落子的策略,可以采用博弈树搜索、最大最小值算法等。
以下是一个简单的C语言五子棋程序示例:
```
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 15
#define COL 15
#define PLAYER 1
#define COMPUTER 2
int chessboard[ROW][COL] = {0};
void initChessboard()
{
int i, j;
for(i=0; i<ROW; i++)
for(j=0; j<COL; j++)
chessboard[i][j] = 0;
}
void printChessboard()
{
int i, j;
for(i=0; i<ROW; i++)
{
for(j=0; j<COL; j++)
{
if(chessboard[i][j] == PLAYER)
printf("O ");
else if(chessboard[i][j] == COMPUTER)
printf("X ");
else
printf("+ ");
}
printf("\n");
}
}
int checkWin(int player)
{
int i, j, k;
for(i=0; i<ROW; i++)
{
for(j=0; j<COL; j++)
{
if(chessboard[i][j] == player)
{
for(k=1; k<5; k++)
{
if(i+k<ROW && chessboard[i+k][j]==player && k==4)
return 1;
else if(i+k>=ROW || chessboard[i+k][j]!=player)
break;
}
for(k=1; k<5; k++)
{
if(j+k<COL && chessboard[i][j+k]==player && k==4)
return 1;
else if(j+k>=COL || chessboard[i][j+k]!=player)
break;
}
for(k=1; k<5; k++)
{
if(i+k<ROW && j+k<COL && chessboard[i+k][j+k]==player && k==4)
return 1;
else if(i+k>=ROW || j+k>=COL || chessboard[i+k][j+k]!=player)
break;
}
for(k=1; k<5; k++)
{
if(i-k>=0 && j+k<COL && chessboard[i-k][j+k]==player && k==4)
return 1;
else if(i-k<0 || j+k>=COL || chessboard[i-k][j+k]!=player)
break;
}
}
}
}
return 0;
}
int getScore(int player)
{
int score = 0;
int i, j, k;
for(i=0; i<ROW; i++)
{
for(j=0; j<COL; j++)
{
if(chessboard[i][j] == player)
{
for(k=1; k<5; k++)
{
if(i+k<ROW && chessboard[i+k][j]==player)
score += 10;
else if(i+k>=ROW || chessboard[i+k][j]!=0)
break;
}
for(k=1; k<5; k++)
{
if(j+k<COL && chessboard[i][j+k]==player)
score += 10;
else if(j+k>=COL || chessboard[i][j+k]!=0)
break;
}
for(k=1; k<5; k++)
{
if(i+k<ROW && j+k<COL && chessboard[i+k][j+k]==player)
score += 10;
else if(i+k>=ROW || j+k>=COL || chessboard[i+k][j+k]!=0)
break;
}
for(k=1; k<5; k++)
{
if(i-k>=0 && j+k<COL && chessboard[i-k][j+k]==player)
score += 10;
else if(i-k<0 || j+k>=COL || chessboard[i-k][j+k]!=0)
break;
}
}
}
}
return score;
}
int getMaxScore(int *x, int *y)
{
int i, j, maxScore = 0, score = 0;
for(i=0; i<ROW; i++)
{
for(j=0; j<COL; j++)
{
if(chessboard[i][j] == 0)
{
score = getScore(COMPUTER);
if(score > maxScore)
{
maxScore = score;
*x = i;
*y = j;
}
}
}
}
return maxScore;
}
void computerPlay()
{
int x, y, maxScore;
maxScore = getMaxScore(&x, &y);
chessboard[x][y] = COMPUTER;
}
int main()
{
srand((unsigned int)time(NULL));
initChessboard();
printChessboard();
while(1)
{
// 玩家落子
int x, y;
printf("请输入你要落子的位置(x y): ");
scanf("%d %d", &x, &y);
while(x < 0 || x >= ROW || y < 0 || y >= COL || chessboard[x][y] != 0)
{
printf("输入有误,请重新输入(x y): ");
scanf("%d %d", &x, &y);
}
chessboard[x][y] = PLAYER;
printChessboard();
if(checkWin(PLAYER))
{
printf("恭喜你,你赢了!\n");
break;
}
// 电脑落子
computerPlay();
printf("电脑落子:\n");
printChessboard();
if(checkWin(COMPUTER))
{
printf("很遗憾,你输了!\n");
break;
}
}
return 0;
}
```
阅读全文