1.开始界面(菜单函数)void menu()
void menu()
{
printf("*****************************************\n");
printf("************** 1、play! *************\n");
printf("************** 0、exit! *************\n");
printf("*****************************************\n");
}
2.测试玩游戏int main()
{
int select = 0;
srand((unsigned int)time(NULL));//调用整个可以创造随机数
do
{
menu();
printf("请输入你的选项!\n");
scanf("%d", &select);
switch (select)
{
case 1:
game();
break;
case 0:
printf("成功退出!\n");
break;
default :
printf("输入有误,重新输入!\n");
break;
}
} while (select);
return 0;
}
3.初始化棋盘及打印出所需棋盘
void InitBoard(char board[Row][Col], int row, int col)
{
int i = 0, j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
void PrintBoard(char board[Row][Col], int row, int col)
{
int i = 0, j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < row - 1)
printf("|");
}
printf("\n");
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
}
}
}
4.玩家下棋过程的实现
void PlayerChess(char board[Row][Col], int row, int col)
{
while (1)
{
printf("玩家请下棋!\n");
printf("请输入你要下棋的坐标:\n");
int x = 0, y = 0;
scanf("%d %d", &x, &y);
if ((x - 1) < row && (y - 1) < col &&(x-1)>=0 && (y-1)>=0)//输入坐标要合理
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("该位置已被占用,重新输入!\n");
}
}
else
{
printf("输入坐标有误,重新输入!\n");
}
}
}
5.电脑下棋的实现
void ComChess(char board[Row][Col], int row, int col)
{
printf("电脑下棋!\n");
int x = rand() % row;//生成随机数
int y = rand() % col;//生成随机数
while (1)
{
int x = rand() % row;//生成随机数
int y = rand() % col;//生成随机数
if (board[x][y] == ' ')//该位置为空,才可以下棋,否则就继续生成随机数,直到合理
{
board[x][y] = '#';
break;
}
}
}
6.判断棋盘是否下满
static int IsFull(char board[Row][Col], int row, int col)//判断棋盘是否满
{
int i = 0, j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
return 0;
}
}
return 1;
}
7.每步棋下完后判断当前棋盘状态
char IsFinish(char board[Row][Col], int row, int col)
{
int i = 0, j = 0;
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
return board[i][1];
}
for (j = 0; j < col; j++)
{
if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[0][j] != ' ')
return board[0][j];
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
return board[1][1];
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
return board[1][1];
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
return 'C';
}
}
/*if (IsFull(board, Row, Col))
{
return 'Q';
}*/
return 'Q';
}
8.用一个game()函数将所有游戏实现函数组合起来完成游戏
void game()
{
char ch = 0;
char board[Row][Col];
//最开始,数组内容应该全是空格
InitBoard(board, Row, Col);//初始化棋盘为空格
PrintBoard(board, Row, Col);//打印棋盘格
while (1)
{
PlayerChess(board, Row, Col);//玩家下棋
PrintBoard(board, Row, Col);//下完后继续打印棋盘
ch = IsFinish(board, Row, Col);
/*if (ch != 'C')
{
break;
}*/
if (ch != 'C')
{
if (ch == '*')
{
printf("玩家赢!\n");
break;
}
else if (ch == '#')
{
printf("电脑赢!\n");
break;
}
else
{
printf("平局!\n");
break;
}
}
ComChess(board, Row, Col);//电脑下棋
PrintBoard(board, Row, Col);//下完后继续打印棋盘
ch = IsFinish(board, Row, Col);
/*if (ch != 'C')
{
break;
}*/
if (ch != 'C')
{
if (ch == '*')
{
printf("玩家赢!\n");
break;
}
else if (ch =='#')
{
printf("电脑赢!\n");
break;
}
else
{
printf("平局!\n");
break;
}
}
}