目录总体思路项目的创建测试结果电脑获胜玩家获胜平局各函数代码的实现初始化二维数组打印棋盘玩家下棋电脑下棋判断电脑是否有位置可以获胜判断玩家是否有位置获胜判断输赢判断和棋声明代码测试代
井字棋棋盘我们总体可以当成一个二维数组来操作,我们分别需要实现初始化二维数组,打印棋盘,玩家下棋,电脑下棋,判断输赢等代码
我们创建了头文件用于放函数的声明,game.c文件放置函数的实现,test.c文件用于测试。
我们在game.c文件中实现函数功能的代码
void InitBoard(char Board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
Board[i][j] = ' ';
}
}
}
void DisplayBoard(char Board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
printf(" %c ", Board[i][j]);
if (j < col - 1)
printf("|");
}
printf("\n");
if (i < row - 1)
{
for (int j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
printf("|");
}
}
printf("\n");
}
}
这里的棋盘我们只打印了井字的形状,如果为了美观,还可以进行封边,这里就留给朋友们自行实现啦。
void PlayMove(char Board[ROW][COL], int row, int col)
{
while (1)
{
int x = 0, y = 0;
printf("请输入坐标:\n");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (Board[x - 1][y - 1] == ' ')
{
Board[x - 1][y - 1] = '#';
break;
}
else
printf("该位置已被占,请重新输入:\n");
}
else
{
printf("输入错误,请重新输入:\n");
}
}
}
在电脑落子之前,先检测有没有下一步可以直接赢的,若有,则落子在此处,电脑获胜,若没有,再检测玩家下一步是否有机会获胜,若玩家下一步可以直接获胜,那么电脑将抢占这一步。
如果上述两种情况均不存在,当发现(1,1)位置是空时,电脑先下这一步。
其他情况均随意落子。
void ComputerMove(char Board[ROW][COL], int row, int col)
{
int x = 0, y = 0;
while (1)
{
if (JudgeComputer(Board, ROW, COL))
{
for (int i = 0; i < row; i++)
{
if (Board[i][0] == Board[i][1] && Board[i][0] == '*' && Board[i][2] == ' ')
{
Board[i][2] = '*';
return;
}
else if (Board[i][0] == Board[i][2] && Board[i][0] == '*' && Board[i][1] == ' ')
{
Board[i][1] = '*';
return;
}
else if (Board[i][1] == Board[i][2] && Board[i][1] == '*' && Board[i][0] == ' ')
{
Board[i][0] = '*';
return;
}
}
for (int j = 0; j < col; j++)
{
if (Board[0][j] == Board[1][j] && Board[0][j] == '*' && Board[2][j] == ' ')
{
Board[2][j] = '*';
return;
}
else if (Board[0][j] == Board[2][j] && Board[0][j] == '*' && Board[1][j] == ' ')
{
Board[1][j] = '*';
return;
}
else if (Board[1][j] == Board[2][j] && Board[1][j] == '*' && Board[0][j] == ' ')
{
Board[0][j] = '*';
return;
}
}
if (Board[0][0] == Board[1][1] && Board[0][0] == '*' && Board[2][2] == ' ')
{
Board[2][2] = '*';
return;
}
else if (Board[0][0] == Board[2][2] && Board[0][0] == '*' && Board[1][1] == ' ')
{
Board[1][1] = '*';
return;
}
else if (Board[1][1] == Board[2][2] && Board[1][1] == '*' && Board[0][0] == ' ')
{
Board[0][0] = '*';
return;
}
else if (Board[0][2] == Board[1][1] && Board[0][2] == '*' && Board[2][0] == ' ')
{
Board[2][0] = '*';
return;
}
else if (Board[0][2] == Board[2][0] && Board[0][2] == '*' && Board[1][1] == ' ')
{
Board[1][1] = '*';
return;
}
else if (Board[2][0] == Board[1][1] && Board[2][0] == '*' && Board[0][2] == ' ')
{
Board[0][2] = '*';
return;
}
}
else if (JudgePlayer(Board, ROW, COL))
{
for (int i = 0; i < row; i++)
{
if (Board[i][0] == Board[i][1] && Board[i][0] == '#' && Board[i][2] == ' ')
{
Board[i][2] = '*';
return;
}
else if (Board[i][0] == Board[i][2] && Board[i][0] == '#' && Board[i][1] == ' ')
{
Board[i][1] = '*';
return;
}
else if (Board[i][1] == Board[i][2] && Board[i][1] == '#' && Board[i][0] == ' ')
{
Board[i][0] = '*';
return;
}
}
for (int j = 0; j < col; j++)
{
if (Board[0][j] == Board[1][j] && Board[0][j] == '#' && Board[2][j] == ' ')
{
Board[2][j] = '*';
return;
}
else if (Board[0][j] == Board[2][j] && Board[0][j] == '#' && Board[1][j] == ' ')
{
Board[1][j] = '*';
return;
}
else if (Board[1][j] == Board[2][j] && Board[1][j] == '#' && Board[0][j] == ' ')
{
Board[0][j] = '*';
return;
}
}
if (Board[0][0] == Board[1][1] && Board[0][0] == '#' && Board[2][2] == ' ')
{
Board[2][2] = '*';
return;
}
else if (Board[0][0] == Board[2][2] && Board[0][0] == '#' && Board[1][1] == ' ')
{
Board[1][1] = '*';
return;
}
else if (Board[1][1] == Board[2][2] && Board[1][1] == '#' && Board[0][0] == ' ')
{
Board[0][0] = '*';
return;
}
else if (Board[0][2] == Board[1][1] && Board[0][2] == '#' && Board[2][0] == ' ')
{
Board[2][0] = '*';
return;
}
else if (Board[0][2] == Board[2][0] && Board[0][2] == '#' && Board[1][1] == ' ')
{
Board[1][1] = '*';
return;
}
else if (Board[2][0] == Board[1][1] && Board[2][0] == '#' && Board[0][2] == ' ')
{
Board[0][2] = '*';
return;
}
}
else
{
if (Board[1][1] == ' ')
{
Board[1][1] = '*';
return;
}
else
{
x = rand() % row;
y = rand() % col;
if (Board[x][y] == ' ')
{
Board[x][y] = '*';
break;
}
}
}
}
}
static int JudgeComputer(char Board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
if (Board[i][0] == Board[i][1] && Board[i][0] == '*' && Board[i][2] == ' ')
return 1;
else if (Board[i][0] == Board[i][2] && Board[i][0] == '*' && Board[i][1] == ' ')
return 1;
else if (Board[i][1] == Board[i][2] && Board[i][1] == '*' && Board[i][0] == ' ')
return 1;
}
for (int j = 0; j < col; j++)
{
if (Board[0][j] == Board[1][j] && Board[0][j] == '*' && Board[2][j] == ' ')
return 1;
else if (Board[0][j] == Board[2][j] && Board[0][j] == '*' && Board[1][j] == ' ')
return 1;
else if (Board[1][j] == Board[2][j] && Board[1][j] == '*' && Board[0][j] == ' ')
return 1;
}
if (Board[0][0] == Board[1][1] && Board[0][0] == '*' && Board[2][2] == ' ')
return 1;
else if (Board[0][0] == Board[2][2] && Board[0][0] == '*' && Board[1][1] == ' ')
return 1;
else if (Board[1][1] == Board[2][2] && Board[1][1] == '*' && Board[0][0] == ' ')
return 1;
else if (Board[0][2] == Board[1][1] && Board[0][2] == '*' && Board[2][0] == ' ')
return 1;
else if (Board[0][2] == Board[2][0] && Board[0][2] == '*' && Board[1][1] == ' ')
return 1;
else if (Board[2][0] == Board[1][1] && Board[2][0] == '*' && Board[0][2] == ' ')
return 1;
else
return 0;
}
static int JudgePlayer(char Board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
if (Board[i][0] == Board[i][1] && Board[i][0] == '#' && Board[i][2] == ' ')
return 1;
else if (Board[i][0] == Board[i][2] && Board[i][0] == '#' && Board[i][1] == ' ')
return 1;
else if (Board[i][1] == Board[i][2] && Board[i][1] == '#' && Board[i][0] == ' ')
return 1;
}
for (int j = 0; j < col; j++)
{
if (Board[0][j] == Board[1][j] && Board[0][j] == '#' && Board[2][j] == ' ')
return 1;
else if (Board[0][j] == Board[2][j] && Board[0][j] == '#' && Board[1][j] == ' ')
return 1;
else if (Board[1][j] == Board[2][j] && Board[1][j] == '#' && Board[0][j] == ' ')
return 1;
}
if (Board[0][0] == Board[1][1] && Board[0][0] == '#' && Board[2][2] == ' ')
return 1;
else if (Board[0][0] == Board[2][2] && Board[0][0] == '#' && Board[1][1] == ' ')
return 1;
else if (Board[1][1] == Board[2][2] && Board[1][1] == '#' && Board[0][0] == ' ')
return 1;
else if (Board[0][2] == Board[1][1] && Board[0][2] == '#' && Board[2][0] == ' ')
return 1;
else if (Board[0][2] == Board[2][0] && Board[0][2] == '#' && Board[1][1] == ' ')
return 1;
else if (Board[2][0] == Board[1][1] && Board[2][0] == '#' && Board[0][2] == ' ')
return 1;
else
return 0;
}
char IsWin(char Board[ROW][COL], int row, int col)
for (int i = 0; i < row; i++)
{
if (Board[i][0] == Board[i][1] && Board[i][2] == Board[i][1] && Board[i][1] != ' ')
{
return Board[i][0];
}
}
for (int j = 0; j < col; j++)
{
if (Board[0][j] == Board[1][j] && Board[1][j] == Board[2][j] && Board[j][1] != ' ')
{
return Board[0][j];
}
}
if (Board[0][0] == Board[1][1] && Board[2][2] == Board[1][1] && Board[2][2] != ' ')
return Board[0][0];
if (Board[0][2] == Board[1][1] && Board[2][0] == Board[1][1] && Board[1][1] != ' ')
return Board[1][1];
if (IsFull(Board, ROW, COL))
{
return 'o';
}
return 'C';
}
static int IsFull(char Board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (Board[i][j] == ' ')
return 0;
}
}
return 1;
}
我们在game.h头文件中进行函数的声明。
void meun();
#define ROW 3
#define COL 3
//游戏主函数
void game();
//初始化二维数组
void InitBoard(char Board[ROW][COL], int row, int col);
//打印棋盘
void DisplayBoard(char Board[ROW][COL], int row, int col);
//玩家下棋
void PlayMove(char Board[ROW][COL], int row, int col);
//电脑下棋
void ComputerMove(char Board[ROW][COL], int row, int col);
//判断赢否
char IsWin(char Board[ROW][COL], int row, int col);
//判断棋盘满了吗
//int IsFull(char Board[ROW][COL], int row, int col);
//判断电脑下一部是否可以直接赢
//int JudgeComputer(char Board[ROW][COL], int row, int col);
//判断玩家下一步是否会赢
//int JudgePlayer(char Board[ROW][COL], int row, int col);
我们在test.c文件中测试该游戏。
#include "game.h"
void game()
{
char ret = 0;
char Board[ROW][COL] = { 0 };
//初始化数组
InitBoard(Board, ROW, COL);
//打印棋盘
printf("棋盘如下:\n");
DisplayBoard(Board, ROW, COL);
while (1)
{
//玩家下棋
printf("玩家落子:\n");
PlayMove(Board, ROW, COL);
//每下一步打印一次棋盘
DisplayBoard(Board, ROW, COL);
//每下一步判断一次胜负
ret = IsWin(Board, ROW, COL);
if (ret != 'C')
{
break;
}
//电脑下棋
printf("电脑落子:\n");
ComputerMove(Board, ROW, COL);
//每下一步打印一次棋盘
DisplayBoard(Board, ROW, COL);
ret = IsWin(Board, ROW, COL);
if (ret != 'C')
{
break;
}
}
if (ret == '#')
printf("玩家获胜\n");
else if (ret == '*')
printf("电脑获胜\n");
else if (ret == 'o')
printf("卧龙凤雏\n");
}
void test()
{
int input = 0;
srand((size_t)time(NULL));
do
{
meun();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("游戏结束\n");
break;
default:
printf("输入错误,请重新输入:\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
此次代码的实现总体来说虽然能够实现井字棋功能,但是不得不承认代码确实不够精简,还有我们可以思考是否可以使电脑更加聪明,落子的每一步都有自己的‘想法',这里也是博主需要不断思考的问题,博主也要不断的学习看能否实现该功能,如果朋友们有好的想法,欢迎与我进行交流,不胜感激,希望大家以后多多支持编程网!
--结束END--
本文标题: C语言开发实现井字棋及电脑落子优化示例详解
本文链接: https://lsjlt.com/news/157735.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0