上篇博客对三子棋小游戏进行了升级,但仅是扩展到了多子棋,电脑算法采用的还是伪随机数的方法,电脑还是个憨憨。
那么今天就对三子棋小游戏中电脑下棋算法进行一次优化吧,多子棋算法优化改变相应模块条件就可以实现。
****************************************
*****1.开始游戏 0.退出游戏*****
****************************************
系统提示:请选择:>>>1
1 2 3
1 | |
---|---|---
2 | |
---|---|---
3 | |
系统提示:请输入您要下的坐标:>>>1 2
1 2 3
1 | * |
---|---|---
2 | |
---|---|---
3 | |
电脑:我走过啦!!!到你啦!!!
1 2 3
1 | * |
---|---|---
2 | |
---|---|---
3 | | #
系统提示:请输入您要下的坐标:>>>2 2
1 2 3
1 | * |
---|---|---
2 | * |
---|---|---
3 | | #
电脑:我走过啦!!!到你啦!!!
1 2 3
1 | * |
---|---|---
2 | * |
---|---|---
3 | # | #
系统提示:请输入您要下的坐标:>>>3 1
1 2 3
1 | * |
---|---|---
2 | * |
---|---|---
3 * | # | #
电脑:我走过啦!!!到你啦!!!
1 2 3
1 | * | #
---|---|---
2 | * |
---|---|---
3 * | # | #
系统提示:请输入您要下的坐标:>>>2 3
1 2 3
1 | * | #
---|---|---
2 | * | *
---|---|---
3 * | # | #
电脑:我走过啦!!!到你啦!!!
1 2 3
1 | * | #
---|---|---
2 # | * | *
---|---|---
3 * | # | #
系统提示:请输入您要下的坐标:>>>1 1
1 2 3
1 * | * | #
---|---|---
2 # | * | *
---|---|---
3 * | # | #
平局!!!嗯嗯,你已经不错了,但还要加油哦!
****************************************
*****1.开始游戏 0.退出游戏*****
****************************************
系统提示:请选择:>>>1
1 2 3
1 | |
---|---|---
2 | |
---|---|---
3 | |
系统提示:请输入您要下的坐标:>>>2 2
1 2 3
1 | |
---|---|---
2 | * |
---|---|---
3 | |
电脑:我走过啦!!!到你啦!!!
1 2 3
1 | |
---|---|---
2 | * |
---|---|---
3 | # |
系统提示:请输入您要下的坐标:>>>1 1
1 2 3
1 * | |
---|---|---
2 | * |
---|---|---
3 | # |
电脑:我走过啦!!!到你啦!!!
1 2 3
1 * | |
---|---|---
2 | * |
---|---|---
3 | # | #
系统提示:请输入您要下的坐标:>>>1 3
1 2 3
1 * | | *
---|---|---
2 | * |
---|---|---
3 | # | #
电脑:我走过啦!!!到你啦!!!
1 2 3
1 * | | *
---|---|---
2 | * |
---|---|---
3 # | # | #
你输了!!!emmm,不过没关系,毕竟你是人类哇!
****************************************
*****1.开始游戏 0.退出游戏*****
****************************************
系统提示:请选择:>>>1
****************************************
*****1.开始游戏 0.退出游戏*****
****************************************
系统提示:请选择:>>>1
1 2 3
1 | |
---|---|---
2 | |
---|---|---
3 | |
系统提示:请输入您要下的坐标:>>>2 2
1 2 3
1 | |
---|---|---
2 | * |
---|---|---
3 | |
电脑:我走过啦!!!到你啦!!!
1 2 3
1 | |
---|---|---
2 | * |
---|---|---
3 | | #
系统提示:请输入您要下的坐标:>>>1 1
1 2 3
1 * | |
---|---|---
2 | * |
---|---|---
3 | | #
电脑:我走过啦!!!到你啦!!!
1 2 3
1 * | |
---|---|---
2 | * |
---|---|---
3 | # | #
系统提示:请输入您要下的坐标:>>>3 1
1 2 3
1 * | |
---|---|---
2 | * |
---|---|---
3 * | # | #
电脑:我走过啦!!!到你啦!!!
1 2 3
1 * | |
---|---|---
2 # | * |
---|---|---
3 * | # | #
系统提示:请输入您要下的坐标:>>>1 3
1 2 3
1 * | | *
---|---|---
2 # | * |
---|---|---
3 * | # | #
恭喜你赢啦!!!哇哦,太棒了!没几个人能战胜我呢!)
****************************************
*****1.开始游戏 0.退出游戏*****
****************************************
系统提示:请选择:>>>
这次程序中添加了两个函数check_computer
、judge_player
且对ComputerMove
和game
两个函数以及头文件
做出了更改,头文件仅添加了这两个新增函数的声明。
下面介绍两个新增函数。
电脑是否能赢,在下过一两个或者多个回合的时候,这个函数真正起到作用,如果能赢就落子。
//判断电脑自己是否有机会赢,如果能赢,就落子,因为自己赢的优先级肯定大于拦截对手,对吧
int check_computer(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
int k = 0;
while (0 == k)
{
//判断电脑横行上是否会赢
for (i = 0; i < row; i++)
{
if ((board[i][0] == board[i][1])
&& (board[i][1] == '#')
&& (board[i][2] == ' '))
{
board[i][2] = '#';
k = 1;
break;
}
if ((board[i][0] == board[i][2])
&& (board[i][0] == '#')
&& (board[i][1] == ' '))
{
board[i][1] = '#';
k = 1;
break;
}
if ((board[i][1] == board[i][2])
&& (board[i][1] == '#')
&& (board[i][0] == ' '))
{
board[i][0] = '#';
k = 1;
break;
}
}
if (k != 0)
break;
//判断电脑列上是否有机会赢
for (j = 0; j < col; j++)
{
if ((board[0][j] == board[1][j])
&& (board[1][j] == '#')
&& (board[2][j] == ' '))
{
board[2][j] = '#';
k = 1;
break;
}
if ((board[0][j] == board[2][j])
&& (board[2][j] == '#')
&& (board[1][j] == ' '))
{
board[1][j] = '#';
k = 1;
break;
}
if ((board[1][j] == board[2][j])
&& (board[2][j] == '#')
&& (board[0][j] == ' '))
{
board[0][j] = '#';
k = 1;
break;
}
}
break;
}
//判断电脑在对角线上是否会赢,又加了一个while是为了让判断对角线的代码成块。
while (0 == k)
{
if ((board[0][0] == board[1][1])
&& (board[1][1] == '#')
&& (board[2][2] == ' '))
{
board[2][2] = '#';
k = 1;
break;
}
if ((board[0][0] == board[2][2])
&& (board[2][2] == '#')
&& (board[1][1] == ' '))
{
board[1][1] = '#';
k = 1;
break;
}
if ((board[1][1] == board[2][2])
&& (board[1][1] == '#')
&& (board[0][0] == ' '))
{
board[0][0] = '#';
k = 1;
break;
}
if ((board[0][2] == board[1][1])
&& (board[0][2] == '#')
&& (board[2][0] == ' '))
{
board[2][0] = '#';
k = 1;
break;
}
if ((board[0][2] == board[2][0])
&& (board[2][0] == '#')
&& (board[1][1] == ' '))
{
board[1][1] = '#';
k = 1;
break;
}
if ((board[1][1] == board[2][0])
&& (board[2][0] == '#')
&& (board[0][2] == ' '))
{
board[0][2] = '#';
k = 1;
break;
}
break;
}
k = judge_player(board, row, col, k);
return k;
}
在判断了上一步之后,电脑下一步不可能赢,那这时候来判断玩家下一步是否能赢,如果玩家下一步能赢,那二话不说,不管三七二十一,咱拦截。如果玩家也不能赢呢,那咱的电脑就只能乖乖的走伪随机数啦。
char judge_player(char board[ROW][COL], int row, int col, int k)
{
int i = 0;
int j = 0;
while (0 == k)
{
//判断玩家在横行上是否会赢
for (i = 0; i < row; i++)
{
if ((board[i][0] == board[i][1])
&& (board[i][1] == '*')
&& (board[i][2] == ' '))
{
board[i][2] = '#';
k = 1;
break;
}
if ((board[i][0] == board[i][2])
&& (board[i][0] == '*')
&& (board[i][1] == ' '))
{
board[i][1] = '#';
k = 1;
break;
}
if ((board[i][1] == board[i][2])
&& (board[i][1] == '*')
&& (board[i][0] == ' '))
{
board[i][0] = '#';
k = 1;
break;
}
}
if (k != 0)
break;
//判断玩家在竖列上是否会赢
for (j = 0; j < col; j++)
{
if ((board[0][j] == board[1][j])
&& (board[1][j] == '*')
&& (board[2][j] == ' '))
{
board[2][j] = '#';
k = 1;
break;
}
if ((board[0][j] == board[2][j])
&& (board[2][j] == '*')
&& (board[1][j] == ' '))
{
board[1][j] = '#';
k = 1;
break;
}
if ((board[1][j] == board[2][j])
&& (board[2][j] == '*')
&& (board[0][j] == ' '))
{
board[0][j] = '#';
k = 1;
break;
}
}
break;
}
//判断玩家在对角线上是否会赢,又加了一个while是为了让判断对角线的代码成块。
while (0 == k)
{
if ((board[0][0] == board[1][1])
&& (board[1][1] == '*')
&& (board[2][2] == ' '))
{
board[2][2] = '#';
k = 1;
break;
}
if ((board[0][0] == board[2][2])
&& (board[2][2] == '*')
&& (board[1][1] == ' '))
{
board[1][1] = '#';
k = 1;
break;
}
if ((board[1][1] == board[2][2])
&& (board[1][1] == '*')
&& (board[0][0] == ' '))
{
board[0][0] = '#';
k = 1;
break;
}
if ((board[0][2] == board[1][1])
&& (board[0][2] == '*')
&& (board[2][0] == ' '))
{
board[2][0] = '#';
k = 1;
break;
}
if ((board[0][2] == board[2][0])
&& (board[2][0] == '*')
&& (board[1][1] == ' '))
{
board[1][1] = '#';
k = 1;
break;
}
if ((board[1][1] == board[2][0])
&& (board[2][0] == '*')
&& (board[0][2] == ' '))
{
board[0][2] = '#';
k = 1;
break;
}
break;
}
return k;
}
电脑下棋需要加一步,获取返回值,如果返回值为N,那么电脑只有乖乖的按照伪随机数来走啦。
//电脑输入
void ComputerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
int z = 0;
printf("电脑:我走过啦!!!到你啦!!!\n\n");
z = check_computer(board, row, col);
while (0 == z)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
改了游戏胜利,平局和失败的输出文案,其余的没动。
void menu(void)
{
printf("****************************************\n");
printf("*****1.开始游戏 0.退出游戏*****\n");
printf("****************************************\n");
}
void game(void)
{
char ret = 0;
char board[ROW][COL] = {
0 };
//初始化棋盘
InitBoard(board, ROW, COL);
//打印棋盘
DisplayBoard(board, ROW, COL);
while (1)
{
//玩家下棋
PlayerMove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
//判断玩家是否胜利
ret = Win(board, ROW, COL);
if (ret != 'C')
{
break;
}
//电脑下棋
ComputerMove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
//判断电脑是否胜利
ret = Win(board, ROW, COL);
if (ret != 'C')
{
break;
}
}
if (ret == '*')
{
printf("恭喜你赢啦!!!哇哦,太棒了!没几个人能战胜我呢!\n");
printf("\n");
}
else if (ret == '#')
{
printf("你输了!!!emmm,不过没关系,毕竟你是人类哇!\n");
printf("\n");
}
else
{
printf("平局!!!嗯嗯,你已经不错了,但还要加油哦!\n");
printf("\n");
}
}
void test(void)
{
int input = 0;
do
{
menu();
printf("\n");
printf("系统提示:请选择:>>>");
scanf("%d", &input);
printf("\n");
switch (input)
{
case 1:
game();
break;
case 0:
printf("破游戏,俺不玩了。\n");
break;
default:
printf("电脑:FBI warning 非法输入,非法输入,请您自觉点。\n\n电脑:乖,咱重新输入!!!\n");
break;
}
} while (input);
}
int main(void)
{
test();
return 0;
}
//宏定义
#define _CRT_SECURE_NO_WARNINGS 1
#define ROW 3
#define COL 3
//引用库函数
#include
#include
//函数声明
void InitBoard(char board[ROW][COL], int row, int col);
void DisplayBoard(char board[ROW][COL], int row, int col);
void PlayerMove(char board[ROW][COL], int row, int col);
void ComputerMove(char board[ROW][COL], int row, int col);
char Win(char board[ROW][COL], int row, int col);
char Full(char board[ROW][COL], int row, int col);
char judge_player(char board[ROW][COL], int row, int col,int k);
int check_computer(char board[ROW][COL], int row, int col);
#include"game.h"
//初始化棋盘
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 < col; i++)
{
if (i == 0)
{
printf(" %d ", i + 1);
}
else
{
printf("%d ", i + 1);
}
}
printf("\n");
for ( int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (j == 0)
{
printf("%d %c |",i+1,board[i][j]);
}
else if (j>0 && j < col - 1)
{
printf(" %c |", board[i][j]);
}
else
printf(" %c ", board[i][j]);
}
printf("\n");
if (i < row - 1)
{
for (int k = 0; k < col; k++)
{
if (k == 0)
{
printf(" ---|");
}
else if (k>0 && k < col - 1)
{
printf("---|");
}
else
{
printf("---");
}
}
}
printf("\n");
}
}
//玩家输入
void PlayerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("系统提示:请输入您要下的坐标:>>>");
scanf("%d%d", &x, &y);
printf("\n");
//判断xy是否合法,一个是是否溢出,一个是是否被占用
if (x > 0 && x < row + 1 && y>0 && y < col + 1)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("电脑:这我下过了,你个憨憨。\n");
printf("\n");
}
}
else
{
printf("电脑:FBI warning 非法输入,非法输入!!!请您自觉点\n\n电脑:乖,咱重新输入!!!\n\n");
}
}
}
//电脑输入
void ComputerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
int z = 0;
printf("电脑:我走过啦!!!到你啦!!!\n\n");
z = check_computer(board, row, col);
while (0 == z)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
//电脑判断自己是否会赢
int check_computer(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
int k = 0;
while (0 == k)
{
//判断电脑横行上是否会赢
for (i = 0; i < row; i++)
{
if ((board[i][0] == board[i][1])
&& (board[i][1] == '#')
&& (board[i][2] == ' '))
{
board[i][2] = '#';
k = 1;
break;
}
if ((board[i][0] == board[i][2])
&& (board[i][0] == '#')
&& (board[i][1] == ' '))
{
board[i][1] = '#';
k = 1;
break;
}
if ((board[i][1] == board[i][2])
&& (board[i][1] == '#')
&& (board[i][0] == ' '))
{
board[i][0] = '#';
k = 1;
break;
}
}
if (k != 0)
break;
//判断电脑列上是否有机会赢
for (j = 0; j < col; j++)
{
if ((board[0][j] == board[1][j])
&& (board[1][j] == '#')
&& (board[2][j] == ' '))
{
board[2][j] = '#';
k = 1;
break;
}
if ((board[0][j] == board[2][j])
&& (board[2][j] == '#')
&& (board[1][j] == ' '))
{
board[1][j] = '#';
k = 1;
break;
}
if ((board[1][j] == board[2][j])
&& (board[2][j] == '#')
&& (board[0][j] == ' '))
{
board[0][j] = '#';
k = 1;
break;
}
}
break;
}
//判断电脑在对角线上是否会赢,又加了一个while是为了让判断对角线的代码成块。
while (0 == k)
{
if ((board[0][0] == board[1][1])
&& (board[1][1] == '#')
&& (board[2][2] == ' '))
{
board[2][2] = '#';
k = 1;
break;
}
if ((board[0][0] == board[2][2])
&& (board[2][2] == '#')
&& (board[1][1] == ' '))
{
board[1][1] = '#';
k = 1;
break;
}
if ((board[1][1] == board[2][2])
&& (board[1][1] == '#')
&& (board[0][0] == ' '))
{
board[0][0] = '#';
k = 1;
break;
}
if ((board[0][2] == board[1][1])
&& (board[0][2] == '#')
&& (board[2][0] == ' '))
{
board[2][0] = '#';
k = 1;
break;
}
if ((board[0][2] == board[2][0])
&& (board[2][0] == '#')
&& (board[1][1] == ' '))
{
board[1][1] = '#';
k = 1;
break;
}
if ((board[1][1] == board[2][0])
&& (board[2][0] == '#')
&& (board[0][2] == ' '))
{
board[0][2] = '#';
k = 1;
break;
}
break;
}
k = judge_player(board, row, col, k);
return k;
}
//下面这个函数就是判断玩家是否有机会赢,如果能赢就堵住他
char judge_player(char board[ROW][COL], int row, int col, int k)
{
int i = 0;
int j = 0;
while (0 == k)
{
//判断玩家在横行上是否会赢
for (i = 0; i < row; i++)
{
if ((board[i][0] == board[i][1])
&& (board[i][1] == '*')
&& (board[i][2] == ' '))
{
board[i][2] = '#';
k = 1;
break;
}
if ((board[i][0] == board[i][2])
&& (board[i][0] == '*')
&& (board[i][1] == ' '))
{
board[i][1] = '#';
k = 1;
break;
}
if ((board[i][1] == board[i][2])
&& (board[i][1] == '*')
&& (board[i][0] == ' '))
{
board[i][0] = '#';
k = 1;
break;
}
}
if (k != 0)
break;
//判断玩家在竖列上是否会赢
for (j = 0; j < col; j++)
{
if ((board[0][j] == board[1][j])
&& (board[1][j] == '*')
&& (board[2][j] == ' '))
{
board[2][j] = '#';
k = 1;
break;
}
if ((board[0][j] == board[2][j])
&& (board[2][j] == '*')
&& (board[1][j] == ' '))
{
board[1][j] = '#';
k = 1;
break;
}
if ((board[1][j] == board[2][j])
&& (board[2][j] == '*')
&& (board[0][j] == ' '))
{
board[0][j] = '#';
k = 1;
break;
}
}
break;
}
//判断玩家在对角线上是否会赢,又加了一个while是为了让判断对角线的代码成块。
while (0 == k)
{
if ((board[0][0] == board[1][1])
&& (board[1][1] == '*')
&& (board[2][2] == ' '))
{
board[2][2] = '#';
k = 1;
break;
}
if ((board[0][0] == board[2][2])
&& (board[2][2] == '*')
&& (board[1][1] == ' '))
{
board[1][1] = '#';
k = 1;
break;
}
if ((board[1][1] == board[2][2])
&& (board[1][1] == '*')
&& (board[0][0] == ' '))
{
board[0][0] = '#';
k = 1;
break;
}
if ((board[0][2] == board[1][1])
&& (board[0][2] == '*')
&& (board[2][0] == ' '))
{
board[2][0] = '#';
k = 1;
break;
}
if ((board[0][2] == board[2][0])
&& (board[2][0] == '*')
&& (board[1][1] == ' '))
{
board[1][1] = '#';
k = 1;
break;
}
if ((board[1][1] == board[2][0])
&& (board[2][0] == '*')
&& (board[0][2] == ' '))
{
board[0][2] = '#';
k = 1;
break;
}
break;
}
return k;
}
//判断胜负
char Win(char board[ROW][COL], int row, int col)
{
int i = 0;
//判断行
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')//行元素一样且不是空格
{
return board[i][1];
}
}
//判断列
for (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')//列元素一样且不是空格
{
return board[1][i];
}
}
//判断对角线
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')//对角线元素一样且不是空格
{
return board[1][1];
}
if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' ')
{
return board[1][1];
}
//判断是否平局
if (1 == Full(board, ROW, COL))//平局判断,函数在下面,已在头文件中声明,位置就无所谓了
{
return 'Q';
}
return 'C';
}
//平局算法函数
char Full(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;
}
#include"game.h"
//游戏菜单
void menu(void)
{
printf("****************************************\n");
printf("*****1.开始游戏 0.退出游戏*****\n");
printf("****************************************\n");
}
void game(void)
{
char ret = 0;
char board[ROW][COL] = {
0 };
//初始化棋盘
InitBoard(board, ROW, COL);
//打印棋盘
DisplayBoard(board, ROW, COL);
while (1)
{
//玩家下棋
PlayerMove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
//判断玩家是否胜利
ret = Win(board, ROW, COL);
if (ret != 'C')
{
break;
}
//电脑下棋
ComputerMove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
//判断电脑是否胜利
ret = Win(board, ROW, COL);
if (ret != 'C')
{
break;
}
}
if (ret == '*')
{
printf("恭喜你赢啦!!!哇哦,太棒了!没几个人能战胜我呢!\n");
printf("\n");
}
else if (ret == '#')
{
printf("你输了!!!emmm,不过没关系,毕竟你是人类哇!\n");
printf("\n");
}
else
{
printf("平局!!!嗯嗯,你已经不错了,但还要加油哦!\n");
printf("\n");
}
}
void test(void)
{
int input = 0;
do
{
menu();
printf("\n");
printf("系统提示:请选择:>>>");
scanf("%d", &input);
printf("\n");
switch (input)
{
case 1:
game();
break;
case 0:
printf("破游戏,俺不玩了。\n");
break;
default:
printf("电脑:FBI warning 非法输入,非法输入,请您自觉点。\n\n电脑:乖,咱重新输入!!!\n");
break;
}
} while (input);
}
int main(void)
{
test();
return 0;
}
这篇博客对三子棋中电脑的算法进行了优化,五子棋和多子棋类似就不写在这里了。
下一篇,简单和较难的三子棋,五子棋,多子棋小游戏的集成。
如果上面的程序或者表述有问题还请各位大佬批评指正哈。