第一次改良,解决了电脑胜利前最后落子,棋盘无法打印出落子位置的问题。
程序代码如下:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MAX_ROW 3
#define MAX_COL 3
//开始选择界面
int Start(int choice)
{
printf("-------------------------------------------\n");
printf("--------------1.Start Game-----------------\n");
printf("--------------2.Exit Game------------------\n");
printf("-------------------------------------------\n");
printf(" 请输入您的选择:");
scanf("%d", &choice);
return choice;
}
char chessboard[MAX_ROW][MAX_COL];
//创建棋盘,并初始化
void init()
{
for (int row = 0; row < MAX_ROW; ++row)
{
for (int col = 0; col < MAX_COL; ++col)
{
chessboard[row][col] = ' ';
}
}
}
//打印棋盘
int ChessBoard()
{
for (int row = 0; row < MAX_ROW; ++row)
{
printf("| %c | %c | %c |\n",chessboard[row][0], chessboard[row][1], chessboard[row][2]);
if (row == 2)
{
printf("| | | |\n");
}
else
{
printf("|___|___|___|\n");
}
}
}
//玩家落子
void PlayerMove()
{
int row, col;
while (1)
{
printf("请玩家输入要落子位置的行和列:");
scanf("%d %d", &row, &col);
if (row >= MAX_ROW || row < 0 || col >= MAX_COL || col < 0)
{
printf("您输入的落子位置有误!\n");
//continue;
}
else if (chessboard[row][col] != ' ')
{
printf("您输入的落子位置已被占用!\n");
//continue;
}
else
{
break;
}
}
chessboard[row][col] = 'x';
if (Winner() != 1)
{
system("cls");
}
}
//电脑随机落子
void ComputerMove()
{
srand(time); //!!!!!!!
int row, col;
printf("电脑落子!\n");
while (1)
{
row = rand() % MAX_ROW;
col = rand() % MAX_COL;
if (chessboard[row][col] == ' ')
{
chessboard[row][col] = 'o';
if (Winner() != 1)
{
system("cls");
}
break;
}
else
{
continue;
}
}
}
//判断是否为和棋,是则返回1,反之返回0
int toDraw()
{
for (int row = 0; row < MAX_ROW; ++row)
{
for (int col = 0; col < MAX_COL; ++col)
{
if (chessboard[row][col] == ' ')
{
return 0;
}
}
}
return 1;
}
//检验游戏是否结束.
//返回'o'则电脑胜,返回'x'则玩家胜,返回"d"则为和棋,返回' '表示未分出胜负游戏继续
char CheckGameOver()
{
//检查行
for (int row = 0; row < MAX_ROW; ++row)
{
if (chessboard[row][0] == chessboard[row][1] && chessboard[row][0] == chessboard[row][2])
{
return chessboard[row][0];
}
}
//检查列
for (int col = 0; col < MAX_COL; ++col)
{
if (chessboard[0][col] == chessboard[1][col] && chessboard[0][col] == chessboard[2][col])
{
return chessboard[0][col];
}
}
//检查对角线
if ((chessboard[0][0] == chessboard[1][1] && chessboard[0][0] == chessboard[2][2]) ||
(chessboard[0][2] == chessboard[1][1] && chessboard[2][0]))
{
return chessboard[1][1];
}
//检查是否和棋
for (int row = 0; row < MAX_ROW; ++row)
{
for (int col = 0; col < MAX_COL; ++col)
{
if (chessboard[row][col] == ' ')
{
return ' ';
}
}
}
return 'd';
}
//判断胜利方
int Winner()
{
char winner;
winner = CheckGameOver();
if (winner == 'x')
{
printf("恭喜!玩家胜利!\n");
}
else if (winner == 'o')
{
printf("很遗憾,电脑胜利.\n");
}
else if (winner == "d")
{
printf("和棋!\n");
}
else
{
return 0;
}
return 1;
}
//开始游戏
void Game()
{
//创建棋盘,并初始化为‘ ’
init();
//打印棋盘
ChessBoard();
while (1)
{
//玩家落子
PlayerMove();
system("cls");
ChessBoard();
//判断游戏是否继续
if (Winner())
{
break;
}
//电脑落子
ComputerMove();
system("cls");
ChessBoard();
//判断游戏是否继续
if (Winner())
{
break;
}
}
}
int main()
{
int choice = 0;
while (1)
{
choice = Start(choice);
if (choice == 1)
{
Game();
break;
}
if (choice == 2)
{
break;
}
else
{
printf("您输入的选择有误,请重新输入!\n");
continue;
}
}
system("pause");
return 0;
}