void InitChess(char chess[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
chess[i][j] = ' ';
}
}
}
void PlayerMove(char chess[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("请输入要下棋的坐标位置:>");
scanf("%d %d", &x, &y);
if (x >= 1 && y >= 1 && x <= COL && y <= ROW && chess[y-1][x-1] == ' ')
{
chess[y-1][x-1] = '*';
break;
}
else
{
printf("坐标无效,请重新输入!n");
}
}
}
void ComputerMove(char chess[ROW][COL], int row, int col)
{
while (1)
{
int x = rand() % (col - 1);
int y = rand() % (row - 1);
if (chess[y][x] == ' ')
{
chess[y][x] = '#';
break;
}
}
}
int IsFilled(char chess[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
int flag = 1;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (chess[i][j] == ' ')
flag = 0;
}
}
return flag;
}
char JudState(char chess[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
//是否有人胜出
//行判断
for (i = 0; i < row; i++)
{
if (chess[i][0] == chess[i][1] && chess[i][1] == chess[i][2] && chess[i][0] != ' ')
{
return chess[i][0];
}
}
//列判断
for (j = 0; j < col; j++)
{
if (chess[0][j] == chess[1][j] && chess[1][j] == chess[2][j] && chess[0][j] != ' ')
{
return chess[0][j];
}
}
//对角线判断
if (chess[0][0] == chess[1][1] && chess[1][1] == chess[2][2] && chess[1][1] != ' ' )
{
return chess[1][1];
}
if(chess[0][2] == chess[1][1] && chess[2][0] == chess[1][1] && chess[1][1] != ' ')
{
return chess[1][1];
}
//是平局还是继续
if (IsFilled(chess, ROW, COL))
{
return 'Q';
}
else
{
return 'C';
}
}
完整代码
game.h中
#include
#include
#include
#define ROW 3
#define COL 3
void InitChess(char chess[ROW][COL], int row, int col);
void DisplayBoard(char chess[ROW][COL], int row, int col);
void PlayerMove(char chess[ROW][COL], int row, int col);
void ComputerMove(char chess[ROW][COL], int row, int col);
char JudState(char chess[ROW][COL], int row, int col);
game.c中
#include"game.h"
void InitChess(char chess[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
chess[i][j] = ' ';
}
}
}
void DisplayBoard(char chess[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
printf(" %c ", chess[i][j]);
if (j < col - 1)
printf("|");
}
printf("n");
if(i < row - 1)
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
printf("|");
}
printf("n");
}
}
void PlayerMove(char chess[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("请输入要下棋的坐标位置:>");
scanf("%d %d", &x, &y);
if (x >= 1 && y >= 1 && x <= COL && y <= ROW && chess[y-1][x-1] == ' ')
{
chess[y-1][x-1] = '*';
break;
}
else
{
printf("坐标无效,请重新输入!n");
}
}
}
void ComputerMove(char chess[ROW][COL], int row, int col)
{
while (1)
{
int x = rand() % (col - 1);
int y = rand() % (row - 1);
if (chess[y][x] == ' ')
{
chess[y][x] = '#';
break;
}
}
}
int IsFilled(char chess[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
int flag = 1;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (chess[i][j] == ' ')
flag = 0;
}
}
return flag;
}
char JudState(char chess[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
//是否有人胜出
//行判断
for (i = 0; i < row; i++)
{
if (chess[i][0] == chess[i][1] && chess[i][1] == chess[i][2] && chess[i][0] != ' ')
{
return chess[i][0];
}
}
//列判断
for (j = 0; j < col; j++)
{
if (chess[0][j] == chess[1][j] && chess[1][j] == chess[2][j] && chess[0][j] != ' ')
{
return chess[0][j];
}
}
//对角线判断
if (chess[0][0] == chess[1][1] && chess[1][1] == chess[2][2] && chess[1][1] != ' ' )
{
return chess[1][1];
}
if(chess[0][2] == chess[1][1] && chess[2][0] == chess[1][1] && chess[1][1] != ' ')
{
return chess[1][1];
}
//是平局还是继续
if (IsFilled(chess, ROW, COL))
{
return 'Q';
}
else
{
return 'C';
}
}
test.c中
#include"game.h"
void menu()
{
printf("*********** 三子棋 ***********n");
printf("******************************n");
printf("********** 1. play ***********n");
printf("********** 0. exit ***********n");
printf("******************************n");
}
void game()
{
char ret = 0;
char chess[ROW][COL];//存储棋盘中棋的信息
InitChess(chess, ROW, COL);//初始化棋的信息
DisplayBoard(chess, ROW, COL);//打印棋盘和棋的信息
while (1)
{
PlayerMove(chess, ROW, COL);//玩家下棋
if ((ret = JudState(chess, ROW, COL)) != 'C')
break;
DisplayBoard(chess, ROW, COL);//打印棋盘和棋的信息
ComputerMove(chess, ROW, COL);//电脑下棋
if ((ret = JudState(chess, ROW, COL)) != 'C')
break;
DisplayBoard(chess, ROW, COL);//打印棋盘和棋的信息
}
if ('*' == ret)
{
printf("玩家赢!n");
}
else if ('#' == ret)
{
printf("电脑赢!n");
}
else
{
printf("平局了!n");
}
DisplayBoard(chess, ROW, COL);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));//设置随机种子
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏!n");
break;
default :
printf("选择有误,请重新选择!n");
break;
}
} while (input);
return 0;
}