目录
最终效果展示
人机对战--五子棋
一:test.c
这个文件的作用是实现游戏主体的框架
1.main函数
-
这个函数的目的是让玩家选择
-
1.玩家和玩家对战
-
2.玩家和电脑对战
-
0.退出
-
我们首先使用menu函数打印一个提示,然后用do while语句接收玩家输入的值
-
do while语句保证了无论如何先进行一次选择
-
之后使用switch语句对玩家输入的值进行分类,
-
选择1则进入game_plus函数接口,选择2则进入game函数接口
-
-
代码:
int main() {
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择>");
scanf("%d",&input);
switch (input)
{
case 1:
game_plus();
break;
case 2:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,请重新选择\n");
break;
}
} while (input);
return 0;
}
2.menu函数
- 这个函数的目的很简单,就是打印个菜单让玩家看
void menu()
{
printf("*****************************************\n");
printf("******** 1.player VS player ********\n");
printf("******** 2.player VS computer ********\n");
printf("******** 0.exit ****************\n");
printf("*****************************************\n");
}
3.game函数
- 这个函数实现的功能是人机对战
- 我们先初始化棋盘,然后让玩家下棋,下完棋后展示棋盘,然后判断游戏是否结束,就是判断是否是平局,玩家胜利,还是电脑胜利
- 若以上三种情况均未发生,则游戏继续,让电脑下棋,展示棋盘,然后判断游戏是否结束。
- 使用while语句,当出现玩家胜利,平局,电脑胜利之后直接退出。
- 代码:
void game()
{//二维数组 存储数据
char board[row][col];
// 初始化棋盘
Initboard(board,row,col);
//打印棋盘
Displayboard(board,row,col);
char ret = 0;
while (1)
{
//玩家下棋
int *a=Playermove(board, row, col);
system("cls");
Displayboard(board, row, col);
ret=Iswin(board,row,col);
if (ret != 'C')
{
break;
}
//电脑下棋
Computermove(board, row, col,a);
Displayboard(board, row, col);
ret = Iswin(board, row, col);
if (ret != 'C')
{
break;
}
}
if (ret == '*')
{
printf("玩家胜利!\n");
}
else if (ret == '#')
{
printf("电脑胜利,再接再厉\n");
}
else
{
//printf("%c1\n", ret);
printf("平局,再来一把\n");
}
Displayboard(board, row, col);
}
4.game_plus函数
- 这个函数实现的功能是玩家对战,实现方式和game函数差不多,只是把电脑落子换成玩家落子
代码:
void game_plus()
{//二维数组 存储数据
char board[row][col];
// 初始化棋盘
Initboard(board, row, col);
//打印棋盘
Displayboard(board, row, col);
char ret = 0;
while (1)
{
//玩家1下棋
Player1move(board, row, col);
system("cls");
Displayboard(board, row, col);
ret = Iswin(board, row, col);
if (ret != 'C')
{
break;
}
//玩家2下棋
Player2move(board, row, col);
Displayboard(board, row, col);
ret = Iswin(board, row, col);
if (ret != 'C')
{
break;
}
}
if (ret == '*')
{
printf("玩家1胜利!\n");
}
else if (ret == '#')
{
printf("玩家2胜利!\n");
}
else
{
//printf("%c1\n", ret);
printf("平局,再来一把\n");
}
Displayboard(board, row, col);
}
5.完整代码
#include"game.h"
void game()
{//二维数组 存储数据
char board[row][col];
// 初始化棋盘
Initboard(board,row,col);
//打印棋盘
Displayboard(board,row,col);
char ret = 0;
while (1)
{
//玩家下棋
int *a=Playermove(board, row, col);
system("cls");
Displayboard(board, row, col);
ret=Iswin(board,row,col);
if (ret != 'C')
{
break;
}
//电脑下棋
Computermove(board, row, col,a);
Displayboard(board, row, col);
ret = Iswin(board, row, col);
if (ret != 'C')
{
break;
}
}
if (ret == '*')
{
printf("玩家胜利!\n");
}
else if (ret == '#')
{
printf("电脑胜利,再接再厉\n");
}
else
{
//printf("%c1\n", ret);
printf("平局,再来一把\n");
}
Displayboard(board, row, col);
}
void game_plus()
{//二维数组 存储数据
char board[row][col];
// 初始化棋盘
Initboard(board, row, col);
//打印棋盘
Displayboard(board, row, col);
char ret = 0;
while (1)
{
//玩家1下棋
Player1move(board, row, col);
system("cls");
Displayboard(board, row, col);
ret = Iswin(board, row, col);
if (ret != 'C')
{
break;
}
//玩家2下棋
Player2move(board, row, col);
Displayboard(board, row, col);
ret = Iswin(board, row, col);
if (ret != 'C')
{
break;
}
}
if (ret == '*')
{
printf("玩家1胜利!\n");
}
else if (ret == '#')
{
printf("玩家2胜利!\n");
}
else
{
//printf("%c1\n", ret);
printf("平局,再来一把\n");
}
Displayboard(board, row, col);
}
void menu()
{
printf("*****************************************\n");
printf("******** 1.player VS player ********\n");
printf("******** 2.player VS computer ********\n");
printf("******** 0.exit ****************\n");
printf("*****************************************\n");
}
int main() {
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择>");
scanf("%d",&input);
switch (input)
{
case 1:
game_plus();
break;
case 2:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,请重新选择\n");
break;
}
} while (input);
return 0;
}
二:game.c
这个文件的作用就是函数的实现
1. Initboard函数(初始化棋盘)
- 这个函数的功能就是把我们在game和game_plus函数中创建的二维数组board初始化
- 刚开始没有任何人落子,所以所有值都置为空格
-代码:
void Initboard(char board[row][col], int hang, int lie)
{
int i = 0;
int j = 0;
for (i = 0; i < hang; i++)
{
for (j = 0; j < lie; j++)
{
board[i][j] = ' ';
}
}
}
2.Displayboard函数(展示棋盘)
- 这个函数功能为打印棋盘,我们先打印列索引方便后续玩家下棋
- 然后打印行索引和棋盘内容,每个格子用|分隔
- 每行用—分隔
-
3.Playermove函数(玩家下棋)
- 这个函数实现的就是玩家下棋
- 先提示玩家输入下棋的坐标,给出示例
- 然后检查坐标是否合法,就是检查坐标的值有没有超过棋盘的范围
- 之后再检查坐标是否被占用,就是检查棋盘的那个坐标存放的是否是空格
- 如果是空格,则把board中对应位置的值改成*,代表玩家在这下了棋
- 最后把玩家下棋的坐标保存下来,返回,用于后续的电脑下棋
- Player1move和Player2move函数的实现几乎完全一致,就是把提示信息改了一下,然后用#代表玩家2落子,不再赘述了。
- 代码:
int* Playermove(char board[][col], int hang, int lie)
{
int x = 0;
int y = 0;
printf("玩家下棋\n");
while (1)
{
printf("请输入您要下棋的坐标,示例:1 1\n");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= hang && y >= 1 && y <= lie)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("坐标已被占用,请重新输入\n");
}
}
else
{
printf("坐标非法,请重新输入\n");
}
}
static int a[2] = {0};
a[0] = x - 1;
a[1] = y - 1;
return a;
}
4.Computermove函数(电脑下棋)
- 这个函数实现的就是让电脑下棋,最开始我是让电脑随机下的,通过使用rand函数生成随机值然后再时期对行或列取余,得到合法坐标
- 但后来我觉得这样下棋电脑实在是太笨了,玩家用膝盖都能赢电脑,就稍微优化了一下
- 首先是把随机落子的范围限制一下,因为Computermove函数接受了玩家之前下棋的坐标,我们可以让电脑要下子的范围限制在玩家落子的九宫格范围内,这样比哪都能下就强多了
- 可是这样会有一个问题,当九宫格全满就没法下了,所以我们要写一个函数判断九宫格是否被下满了,如果下满了,我们就让电脑在25宫格中落子,以此类推
- 当然仅仅是这样随机落子,还是显得电脑很蠢,所有我们可以写一个函数优化一下电脑的落子,让其实现堵截玩家,让自己获胜,让自己连成三子等功能
- 然后每次我们先调用这个优化的函数看看电脑能不能堵玩家或让自己获胜等等,如果这些都做不到,再让电脑随机落子
- 代码:
void Computermove(char board[][col], int hang, int lie,int a[2])
{
int x = 0;
int y = 0;
int i = 0;
int x1 = a[0];
int y1 = a[1];
printf("电脑下棋\n");
int re = youhua(board, hang, lie);
if (re == 0)
{
while (1)
{
x = rand() % hang;
y = rand() % lie;
int tmp = 0;
if (board[x][y] == ' ')
{
if (x <= x1 + 1 && x >= x1 - 1 && y <= y1 + 1 && y >= y1 - 1)
{
board[x][y] = '#';
//printf("随机落子\n");
break;
}
else
{
for (i = 1; i <= row / 2; i++)
{
if (Isfull(board, x1, y1, i))
{
if (x <= x1 + i+1 && x >= x1 - (i+1) && y <= y1 + i+1 && y >= y1 - (i+1))
{
board[x][y] = '#';
tmp = 1;
//printf("随机落子\n");
break;
}
}
else
{
break;
}
}
if (tmp)
{
break;
}
}
}
}
}
}
5.youhua函数(让电脑有头脑的下棋)
- 这个函数就是我们之前说的优化函数,也就是让电脑长点脑子下棋
- 这大抵分为5部分
1.判断电脑是否已经有4子,如果是,则让电脑下第五子获得胜利
2.判断玩家是否已经连成4子,如果连成4子,则电脑下子阻挡玩家获得胜利
3.判断电脑是否有三子,有的话连成四子
4.判断玩家是否连成三子,若连成三子,则堵住玩家
5.判断电脑是否连成两子,若连成两子,则下子使电脑连成三子 - 每个部分又可以分成四个部分,分成行,列,对角线,反对角线
- 也是因为这个,我原本想把五个功能放在一个if else语句中实现,可是这样会有顺序问题,可能列电脑已经练成四子了,我们需要它下第五子获得胜利,但是由于行的判断在列前面,它可能会发现行上有两个子,然后去连成三子,导致列的四子不能变成五子
- 这个函数也是写的最久的一个了,有很多细节,里面涉及边界条件时应该如何处理,堵人的时候堵哪里合适,还有一个我没解决的问题,它发现人有三子的时候,因为我前面的便利问题,总是会堵两次,具体细节我就不说了,等哪天我彻底完善了或许会再发一篇博客(不过会看我博客的也没几个)
- 代码:
int youhua(char board[][col], int hang, int lie)
{
int i = 0;
int j = 0;
int k = 0;
int sum1 = 0;
int sum2 = 0;
//判断行电脑是否已经连成四子,如果连成四子,则下第五子获得胜利
for (i = 0; i < hang; i++)
{
for (j = 0; j < lie - 4; j++)
{
for (k = 0; k < 5; k++)
{
if (board[i][j+k] == '*')
{
sum1++;
}
else if (board[i][j + k] == '#')
{
sum2++;
}
}
if (sum2>3)
{
for(k = 0; k < 5; k++)
{
if (board[i][j + k] == ' ')
{
board[i][j + k] = '#';
//printf("行四子连五\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断列电脑是否已经连成四子,如果连成四子,则下第五子获得胜利
for (j = 0; j < lie; j++)
{
for (i = 0; i < hang - 4; i++)
{
for (k = 0; k < 5; k++)
{
if (board[i+k][j] == '*')
{
sum1++;
}
else if (board[i+k][j] == '#')
{
sum2++;
}
}
if (sum2 > 3 )
{
for (k = 0; k < 5; k++)
{
if (board[i+k][j] == ' ')
{
board[i+k][j] = '#';
//printf("列四子连五\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断对角线电脑是否已经连成四子,如果连成四子,则下第五子获得胜利
for (i = 0; i < hang-4; i++)
{
for (j = 0; j < lie - 4; j++)
{
for (k = 0; k < 5; k++)
{
if (board[i+k][j + k] == '*')
{
sum1++;
}
else if (board[i+k][j + k] == '#')
{
sum2++;
}
}
if (sum2 > 3 )
{
for (k = 0; k < 5; k++)
{
if (board[i+k][j + k] == ' ')
{
board[i+k][j + k] = '#';
//printf("对角线四子连五\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断反对角线电脑是否已经连成四子,如果连成四子,则下第五子获得胜利
for (i = 0; i < hang-4 ; i++)
{
for (j = 4; j < lie; j++)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j - k] == '*')
{
sum1++;
}
else if (board[i + k][j - k] == '#')
{
sum2++;
}
}
if (sum2 > 3)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j - k] == ' ')
{
board[i + k][j - k] = '#';
//printf("反对角线四子连五\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断行玩家是否已经连成四子,如果连成四子,则下子阻挡玩家获得胜利
for (i = 0; i < hang; i++)
{
for (j = 0; j < lie - 4; j++)
{
for (k = 0; k < 5; k++)
{
if (board[i][j + k] == '*')
{
sum1++;
}
else if (board[i][j + k] == '#')
{
sum2++;
}
}
if (sum1 > 3)
{
for (k = 0; k < 5; k++)
{
if (board[i][j + k] == ' ')
{
board[i][j + k] = '#';
//printf("行玩家四子,阻挡玩家五子\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断列玩家是否已经连成四子,如果连成四子,则下子阻挡玩家获得胜利
for (j = 0; j < lie; j++)
{
for (i = 0; i < hang - 4; i++)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j] == '*')
{
sum1++;
}
else if (board[i + k][j] == '#')
{
sum2++;
}
}
if (sum1 > 3)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j] == ' ')
{
board[i + k][j] = '#';
//printf("列玩家四子,阻挡玩家五子\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断对角线玩家是否已经连成四子,如果连成四子,则下子阻挡玩家获得胜利
for (i = 0; i < hang - 4; i++)
{
for (j = 0; j < lie - 4; j++)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j + k] == '*')
{
sum1++;
}
else if (board[i + k][j + k] == '#')
{
sum2++;
}
}
if (sum1 > 3)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j + k] == ' ')
{
board[i + k][j + k] = '#';
//printf("对角线玩家四子,阻挡玩家五子\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断反对角线玩家是否已经连成四子,如果连成四子,则下子阻挡玩家获得胜利
for (i = 0; i < hang - 4; i++)
{
for (j = 4; j < lie; j++)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j - k] == '*')
{
sum1++;
}
else if (board[i + k][j - k] == '#')
{
sum2++;
}
}
if (sum1 > 3)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j - k] == ' ')
{
board[i + k][j - k] = '#';
//printf("反对角线玩家四子,阻挡玩家五子\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断行电脑是否有三子,有的话连成四子
for (i = 0; i < hang; i++)
{
for (j = 0; j < lie - 4; j++)
{
for (k = 0; k < 5; k++)
{
if (board[i][j + k] == '*')
{
sum1++;
}
else if (board[i][j + k] == '#')
{
sum2++;
}
}
if (sum2 == 3)
{
if (sum1 == 1)
{
break;
}
for (k = 0; k < 5; k++)
{
if (board[i][j + k] == ' ' && ((board[i][j + k + 1] == '#' || board[i][j + k - 1] == '#'))
&& (j + k + 2) < col && (j + k - 2) > 0)
{
board[i][j + k] = '#';
//printf("行电脑三子,连三成四\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断列电脑是否有三子,有的话连成四子
for (j = 0; j < lie; j++)
{
for (i = 0; i < hang - 4; i++)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j] == '*')
{
sum1++;
}
else if (board[i + k][j] == '#')
{
sum2++;
}
}
if (sum2 == 3)
{
if (sum1 == 1)
{
break;
}
for (k = 0; k < 5; k++)
{
if (board[i + k][j] == ' ' && ((board[i + k + 1][j] == '#' || board[i + k - 1][j] == '#'))
&& (i + k + 2) < row && (i + k - 2) > 0)
{
board[i + k][j] = '#';
//printf("列电脑三子,连三成四\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断对角线电脑是否有三子,有的话连成四子
for (i = 0; i < hang - 4; i++)
{
for (j = 0; j < lie - 4; j++)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j + k] == '*')
{
sum1++;
}
else if (board[i + k][j + k] == '#')
{
sum2++;
}
}
if (sum2 == 3)
{
if (sum1 == 1)
{
break;
}
for (k = 0; k < 5; k++)
{
if (board[i + k][j + k] == ' ' && ((board[i + k + 1][j + k + 1] == '#' || board[i + k - 1][j + k - 1] == '#'))
&& (i + k + 2) < row && (i + k - 2) > 0)
{
board[i + k][j + k] = '#';
//printf("对角线电脑三子,连三成四\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断反对角线电脑是否有三子,有的话连成四子
for (i = 0; i < hang - 4; i++)
{
for (j = 4; j < lie; j++)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j - k] == '*')
{
sum1++;
}
else if (board[i + k][j - k] == '#')
{
sum2++;
}
}
if (sum2 == 3)
{
if (sum1 == 1)
{
break;
}
for (k = 0; k < 5; k++)
{
if (board[i + k][j - k] == ' ' && ((board[i + k + 1][j - k - 1] == '#' || board[i + k - 1][j - k + 1] == '#'))
&& (i + k + 2) < row && (i + k - 2) > 0)
{
board[i + k][j - k] = '#';
//printf("反对角线电脑三子,连三成四\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断行玩家是否连成三子,若连成三子,则堵住玩家
for (i = 0; i < hang; i++)
{
for (j = 0; j < lie - 4; j++)
{
for (k = 0; k < 5; k++)
{
if (board[i][j + k] == '*')
{
sum1++;
}
else if (board[i][j + k] == '#')
{
sum2++;
}
}
if (sum1 == 3)
{
if (sum2 == 1)
{
j++;
break;
}
for (k = 0; k < 5; k++)
{
if (board[i][j + k] == ' ' && (board[i][j + k + 1] == '*' || board[i][j + k - 1] == '*')
&& (j + k + 2) < col && (j + k - 2) > 0)
{
board[i][j + k] = '#';
//printf("行玩家三子,阻挡玩家四子\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断列玩家是否连成三子,若连成三子,则堵住玩家
for (j = 0; j < lie; j++)
{
for (i = 0; i < hang - 4; i++)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j] == '*')
{
sum1++;
}
else if (board[i + k][j] == '#')
{
sum2++;
}
}
if (sum1 == 3)
{
if (sum2 == 1)
{
i++;
break;
}
for (k = 0; k < 5; k++)
{
if (board[i + k][j] == ' ' && (board[i + k + 1][j] == '*' || board[i + k - 1][j] == '*')
&& (i + k + 2) < row && (i + k - 2) > 0)
{
board[i + k][j] = '#';
//printf("列玩家三子,阻挡玩家四子\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断对角线玩家是否连成三子,若连成三子,则堵住玩家
for (i = 0; i < hang - 4; i++)
{
for (j = 0; j < lie - 4; j++)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j + k] == '*')
{
sum1++;
}
else if (board[i + k][j + k] == '#')
{
sum2++;
}
}
if (sum1 == 3)
{
if (sum2 == 1)
{
break;
}
for (k = 0; k < 5; k++)
{
if (board[i + k][j + k] == ' ' && (board[i + k + 1][j + k + 1] == '*' || board[i + k - 1][j + k - 1] == '*')
&& (i + k + 2) < row && (i + k - 2) > 0)
{
board[i + k][j + k] = '#';
//printf("对角线玩家三子,阻挡玩家四子\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断反对角线玩家是否连成三子,若连成三子,则堵住玩家
for (i = 0; i < hang - 4; i++)
{
for (j = 4; j < lie; j++)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j - k] == '*')
{
sum1++;
}
else if (board[i + k][j - k] == '#')
{
sum2++;
}
}
if (sum1 == 3)
{
if (sum2 == 1)
{
break;
}
for (k = 0; k < 5; k++)
{
if (board[i + k][j - k] == ' ' && (board[i + k + 1][j - k - 1] == '*' || board[i + k - 1][j - k + 1] == '*')
&& (i + k + 2) < row && (i + k - 2) > 0)
{
board[i + k][j - k] = '#';
//printf("反对角线玩家三子,阻挡玩家四子\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断行电脑是否连成两子,若连成两子,则下子使电脑连成三子
for (i = 0; i < hang; i++)
{
for (j = 0; j < lie - 4; j++)
{
for (k = 0; k < 5; k++)
{
if (board[i][j + k] == '*')
{
sum1++;
}
else if (board[i][j + k] == '#')
{
sum2++;
}
}
if (sum2 >= 1)
{
if (sum1 > 1)
{
break;
}
for (k = 0; k < 5; k++)
{
if (board[i][j + k] == ' ' && ((board[i][j + k + 1] == '#' || board[i][j + k - 1] == '#'))
&& (j + k + 2)<col&& (j + k - 2)>0)
{
board[i][j + k] = '#';
//printf("行电脑二子,连二成三\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断列电脑是否连成两子,若连成两子,则下子使电脑连成三子
for (j = 0; j < lie; j++)
{
for (i = 0; i < hang - 4; i++)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j] == '*')
{
sum1++;
}
else if (board[i + k][j] == '#')
{
sum2++;
}
}
if (sum2 >= 1)
{
if (sum1 > 1)
{
break;
}
for (k = 0; k < 5; k++)
{
if (board[i + k][j] == ' ' && ((board[i + k + 1][j] == '#' || board[i + k - 1][j] == '#'))
&& (i+k+2)< row && (i+k-2)>0)
{
board[i + k][j] = '#';
//printf("列电脑二子,连二成三\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断对角线电脑是否连成两子,若连成两子,则下子使电脑连成三子
for (i = 0; i < hang - 4; i++)
{
for (j = 0; j < lie - 4; j++)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j + k] == '*')
{
sum1++;
}
else if (board[i + k][j + k] == '#')
{
sum2++;
}
}
if (sum2 >= 1)
{
if (sum1 > 1)
{
break;
}
for (k = 0; k < 5; k++)
{
if (board[i + k][j + k] == ' ' && ((board[i + k + 1][j + k + 1] == '#' || board[i + k - 1][j + k - 1] == '#'))
&& (i + k + 2) < row && (i + k - 2)>0)
{
board[i + k][j + k] = '#';
//printf("对角线电脑二子,连二成三\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断反对角线电脑是否连成两子,若连成两子,则下子使电脑连成三子
for (i = 0; i < hang - 4; i++)
{
for (j = 4; j < lie; j++)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j - k] == '*')
{
sum1++;
}
else if (board[i + k][j - k] == '#')
{
sum2++;
}
}
if (sum2 >= 1)
{
if (sum1 > 1)
{
break;
}
for (k = 0; k < 5; k++)
{
if (board[i + k][j - k] == ' ' && ((board[i + k + 1][j - k - 1] == '#' || board[i + k - 1][j - k + 1] == '#'))
&& (i + k + 2) < row && (i + k - 2) > 0)
{
board[i + k][j - k] = '#';
//printf("反对角线电脑二子,连二成三\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
return 0;
}
6.Isfull函数(判断棋盘的一部分是否下满)
- 这个函数是为了之前电脑下棋优化做辅助的,就是判断棋盘的一部分是不是满了
- 代码:
int Isfull(char board[][col], int x, int y,int num)
{
int i = 0;
int j = 0;
for (i = x-num; i<=x+num; i++)
{
for (j = y-num; j <= y+num; j++)
{
if (i < 0 || j < 0 || i >= row || j >= col)
{
continue;
}
if (board[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}
7.Isdraw函数(判断是否是平局)
- 这个函数就是判断是不是平局,实现也很简单,就是遍历board看看有没有一个空格,如果一个空格也没有,就是平局了
- 代码:
int Isdraw(char board[][col], int hang, int lie)
{
int i = 0;
int j = 0;
for (i = 0; i < hang; i++)
{
for (j = 0; j < lie; j++)
{
if (board[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}
8.Iswin函数(判断玩家胜利还是电脑胜利)
-
这个函数用于判断胜负,就是遍历每行每列每对角线每反对角线,发现有五个*则玩家胜利,发现有五个#则电脑胜利,玩家胜利 return *,电脑胜利return #
-
要是没有一方胜利,就用Isdraw函数判断是不是是平局,是平局则 return Q,如果不是平局就return C代表游戏继续
-
代码:
char Iswin(char board[][col], int hang, int lie)
{
int i = 0;
int j = 0;
int k = 0;
int sum1 = 0;
int sum2 = 0;
//判断行
for (i = 0; i < hang; i++)
{
for (j = 0; j < lie - 4; j++)
{
for (k = 0; k < 5; k++)
{
if (board[i][j + k] == '*')
{
sum1++;
}
else if (board[i][j + k] == '#')
{
sum2++;
}
}
if (sum1 == 5)
{
return '*';
}
else if (sum2 == 5)
{
return '#';
}
sum1 = sum2 = 0;
}
}
//判断列
for (j = 0; j < lie; j++)
{
for (i = 0; i < hang - 4; i++)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j] == '*')
{
sum1++;
}
else if (board[i + k][j] == '#')
{
sum2++;
}
}
if (sum1 == 5)
{
return '*';
}
else if (sum2 == 5)
{
return '#';
}
sum1 = sum2 = 0;
}
}
//判断对角线
for (i = 0; i < hang - 4; i++)
{
for (j = 0; j < lie - 4; j++)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j + k] == '*')
{
sum1++;
}
else if (board[i + k][j + k] == '#')
{
sum2++;
}
}
if (sum1 == 5)
{
return '*';
}
else if (sum2 == 5)
{
return '#';
}
sum1 = sum2 = 0;
}
}
//判断反对角线
for (i = 0; i < hang - 4; i++)
{
for (j = 4; j < lie; j++)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j - k] == '*')
{
sum1++;
}
else if (board[i + k][j - k] == '#')
{
sum2++;
}
}
if (sum1 == 5)
{
return '*';
}
else if (sum2 == 5)
{
return '#';
}
sum1 = sum2 = 0;
}
}
//判断平局
int ret = Isdraw(board,hang,lie);
if (ret == 1)
{
return 'Q';
}
return 'C';
}
9.完整代码
#include"game.h"
//初始化棋盘函数
void Initboard(char board[row][col], int hang, int lie)
{
int i = 0;
int j = 0;
for (i = 0; i < hang; i++)
{
for (j = 0; j < lie; j++)
{
board[i][j] = ' ';
}
}
}
//棋盘展示函数
void Displayboard(char board[row][col], int hang, int lie)
{
int i=0;
int j=0;
for (j = 0; j < lie + 1; j++)
{
printf("%-3d", j);
if (j < lie)
{
printf("|");
}
}
printf("\n");
for (j = 0; j < lie+1; j++)
{
printf("---");
if (j < lie)
{
printf("|");
}
}
printf("\n");
for (i = 0; i < hang; i++)
{
printf("%-3d", i + 1);
for (j = 0; j < lie; j++)
{
printf("| %c ", board[i][j]);
}
printf("\n");
if (i < hang)
{
for (j = 0; j < lie+1; j++)
{
printf("---");
if (j < lie)
{
printf("|");
}
}
printf("\n");
}
}
}
//玩家下棋函数
int* Playermove(char board[][col], int hang, int lie)
{
int x = 0;
int y = 0;
printf("玩家下棋\n");
while (1)
{
printf("请输入您要下棋的坐标,示例:1 1\n");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= hang && y >= 1 && y <= lie)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("坐标已被占用,请重新输入\n");
}
}
else
{
printf("坐标非法,请重新输入\n");
}
}
static int a[2] = {0};
a[0] = x - 1;
a[1] = y - 1;
return a;
}
//玩家1下棋函数
void Player1move(char board[][col], int hang, int lie)
{
int x = 0;
int y = 0;
printf("玩家1下棋\n");
while (1)
{
printf("请输入您要下棋的坐标,示例:1 1\n");
scanf("%d %d", &x, &y);
if (x >= 1 && x <=hang && y >= 1 && y <=lie)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("坐标已被占用,请重新输入\n");
}
}
else
{
printf("坐标非法,请重新输入\n");
}
}
}
//玩家2下棋函数
void Player2move(char board[][col], int hang, int lie)
{
int x = 0;
int y = 0;
printf("玩家2下棋\n");
while (1)
{
printf("请输入您要下棋的坐标,示例:1 1\n");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= hang && y >= 1 && y <= lie)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '#';
break;
}
else
{
printf("坐标已被占用,请重新输入\n");
}
}
else
{
printf("坐标非法,请重新输入\n");
}
}
}
//电脑堵玩家函数
int youhua(char board[][col], int hang, int lie)
{
int i = 0;
int j = 0;
int k = 0;
int sum1 = 0;
int sum2 = 0;
//判断行电脑是否已经连成四子,如果连成四子,则下第五子获得胜利
for (i = 0; i < hang; i++)
{
for (j = 0; j < lie - 4; j++)
{
for (k = 0; k < 5; k++)
{
if (board[i][j+k] == '*')
{
sum1++;
}
else if (board[i][j + k] == '#')
{
sum2++;
}
}
if (sum2>3)
{
for(k = 0; k < 5; k++)
{
if (board[i][j + k] == ' ')
{
board[i][j + k] = '#';
//printf("行四子连五\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断列电脑是否已经连成四子,如果连成四子,则下第五子获得胜利
for (j = 0; j < lie; j++)
{
for (i = 0; i < hang - 4; i++)
{
for (k = 0; k < 5; k++)
{
if (board[i+k][j] == '*')
{
sum1++;
}
else if (board[i+k][j] == '#')
{
sum2++;
}
}
if (sum2 > 3 )
{
for (k = 0; k < 5; k++)
{
if (board[i+k][j] == ' ')
{
board[i+k][j] = '#';
//printf("列四子连五\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断对角线电脑是否已经连成四子,如果连成四子,则下第五子获得胜利
for (i = 0; i < hang-4; i++)
{
for (j = 0; j < lie - 4; j++)
{
for (k = 0; k < 5; k++)
{
if (board[i+k][j + k] == '*')
{
sum1++;
}
else if (board[i+k][j + k] == '#')
{
sum2++;
}
}
if (sum2 > 3 )
{
for (k = 0; k < 5; k++)
{
if (board[i+k][j + k] == ' ')
{
board[i+k][j + k] = '#';
//printf("对角线四子连五\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断反对角线电脑是否已经连成四子,如果连成四子,则下第五子获得胜利
for (i = 0; i < hang-4 ; i++)
{
for (j = 4; j < lie; j++)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j - k] == '*')
{
sum1++;
}
else if (board[i + k][j - k] == '#')
{
sum2++;
}
}
if (sum2 > 3)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j - k] == ' ')
{
board[i + k][j - k] = '#';
//printf("反对角线四子连五\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断行玩家是否已经连成四子,如果连成四子,则下子阻挡玩家获得胜利
for (i = 0; i < hang; i++)
{
for (j = 0; j < lie - 4; j++)
{
for (k = 0; k < 5; k++)
{
if (board[i][j + k] == '*')
{
sum1++;
}
else if (board[i][j + k] == '#')
{
sum2++;
}
}
if (sum1 > 3)
{
for (k = 0; k < 5; k++)
{
if (board[i][j + k] == ' ')
{
board[i][j + k] = '#';
//printf("行玩家四子,阻挡玩家五子\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断列玩家是否已经连成四子,如果连成四子,则下子阻挡玩家获得胜利
for (j = 0; j < lie; j++)
{
for (i = 0; i < hang - 4; i++)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j] == '*')
{
sum1++;
}
else if (board[i + k][j] == '#')
{
sum2++;
}
}
if (sum1 > 3)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j] == ' ')
{
board[i + k][j] = '#';
//printf("列玩家四子,阻挡玩家五子\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断对角线玩家是否已经连成四子,如果连成四子,则下子阻挡玩家获得胜利
for (i = 0; i < hang - 4; i++)
{
for (j = 0; j < lie - 4; j++)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j + k] == '*')
{
sum1++;
}
else if (board[i + k][j + k] == '#')
{
sum2++;
}
}
if (sum1 > 3)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j + k] == ' ')
{
board[i + k][j + k] = '#';
//printf("对角线玩家四子,阻挡玩家五子\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断反对角线玩家是否已经连成四子,如果连成四子,则下子阻挡玩家获得胜利
for (i = 0; i < hang - 4; i++)
{
for (j = 4; j < lie; j++)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j - k] == '*')
{
sum1++;
}
else if (board[i + k][j - k] == '#')
{
sum2++;
}
}
if (sum1 > 3)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j - k] == ' ')
{
board[i + k][j - k] = '#';
//printf("反对角线玩家四子,阻挡玩家五子\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断行电脑是否有三子,有的话连成四子
for (i = 0; i < hang; i++)
{
for (j = 0; j < lie - 4; j++)
{
for (k = 0; k < 5; k++)
{
if (board[i][j + k] == '*')
{
sum1++;
}
else if (board[i][j + k] == '#')
{
sum2++;
}
}
if (sum2 == 3)
{
if (sum1 == 1)
{
break;
}
for (k = 0; k < 5; k++)
{
if (board[i][j + k] == ' ' && ((board[i][j + k + 1] == '#' || board[i][j + k - 1] == '#'))
&& (j + k + 2) < col && (j + k - 2) > 0)
{
board[i][j + k] = '#';
//printf("行电脑三子,连三成四\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断列电脑是否有三子,有的话连成四子
for (j = 0; j < lie; j++)
{
for (i = 0; i < hang - 4; i++)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j] == '*')
{
sum1++;
}
else if (board[i + k][j] == '#')
{
sum2++;
}
}
if (sum2 == 3)
{
if (sum1 == 1)
{
break;
}
for (k = 0; k < 5; k++)
{
if (board[i + k][j] == ' ' && ((board[i + k + 1][j] == '#' || board[i + k - 1][j] == '#'))
&& (i + k + 2) < row && (i + k - 2) > 0)
{
board[i + k][j] = '#';
//printf("列电脑三子,连三成四\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断对角线电脑是否有三子,有的话连成四子
for (i = 0; i < hang - 4; i++)
{
for (j = 0; j < lie - 4; j++)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j + k] == '*')
{
sum1++;
}
else if (board[i + k][j + k] == '#')
{
sum2++;
}
}
if (sum2 == 3)
{
if (sum1 == 1)
{
break;
}
for (k = 0; k < 5; k++)
{
if (board[i + k][j + k] == ' ' && ((board[i + k + 1][j + k + 1] == '#' || board[i + k - 1][j + k - 1] == '#'))
&& (i + k + 2) < row && (i + k - 2) > 0)
{
board[i + k][j + k] = '#';
//printf("对角线电脑三子,连三成四\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断反对角线电脑是否有三子,有的话连成四子
for (i = 0; i < hang - 4; i++)
{
for (j = 4; j < lie; j++)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j - k] == '*')
{
sum1++;
}
else if (board[i + k][j - k] == '#')
{
sum2++;
}
}
if (sum2 == 3)
{
if (sum1 == 1)
{
break;
}
for (k = 0; k < 5; k++)
{
if (board[i + k][j - k] == ' ' && ((board[i + k + 1][j - k - 1] == '#' || board[i + k - 1][j - k + 1] == '#'))
&& (i + k + 2) < row && (i + k - 2) > 0)
{
board[i + k][j - k] = '#';
//printf("反对角线电脑三子,连三成四\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断行玩家是否连成三子,若连成三子,则堵住玩家
for (i = 0; i < hang; i++)
{
for (j = 0; j < lie - 4; j++)
{
for (k = 0; k < 5; k++)
{
if (board[i][j + k] == '*')
{
sum1++;
}
else if (board[i][j + k] == '#')
{
sum2++;
}
}
if (sum1 == 3)
{
if (sum2 == 1)
{
j++;
break;
}
for (k = 0; k < 5; k++)
{
if (board[i][j + k] == ' ' && (board[i][j + k + 1] == '*' || board[i][j + k - 1] == '*')
&& (j + k + 2) < col && (j + k - 2) > 0)
{
board[i][j + k] = '#';
//printf("行玩家三子,阻挡玩家四子\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断列玩家是否连成三子,若连成三子,则堵住玩家
for (j = 0; j < lie; j++)
{
for (i = 0; i < hang - 4; i++)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j] == '*')
{
sum1++;
}
else if (board[i + k][j] == '#')
{
sum2++;
}
}
if (sum1 == 3)
{
if (sum2 == 1)
{
i++;
break;
}
for (k = 0; k < 5; k++)
{
if (board[i + k][j] == ' ' && (board[i + k + 1][j] == '*' || board[i + k - 1][j] == '*')
&& (i + k + 2) < row && (i + k - 2) > 0)
{
board[i + k][j] = '#';
//printf("列玩家三子,阻挡玩家四子\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断对角线玩家是否连成三子,若连成三子,则堵住玩家
for (i = 0; i < hang - 4; i++)
{
for (j = 0; j < lie - 4; j++)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j + k] == '*')
{
sum1++;
}
else if (board[i + k][j + k] == '#')
{
sum2++;
}
}
if (sum1 == 3)
{
if (sum2 == 1)
{
break;
}
for (k = 0; k < 5; k++)
{
if (board[i + k][j + k] == ' ' && (board[i + k + 1][j + k + 1] == '*' || board[i + k - 1][j + k - 1] == '*')
&& (i + k + 2) < row && (i + k - 2) > 0)
{
board[i + k][j + k] = '#';
//printf("对角线玩家三子,阻挡玩家四子\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断反对角线玩家是否连成三子,若连成三子,则堵住玩家
for (i = 0; i < hang - 4; i++)
{
for (j = 4; j < lie; j++)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j - k] == '*')
{
sum1++;
}
else if (board[i + k][j - k] == '#')
{
sum2++;
}
}
if (sum1 == 3)
{
if (sum2 == 1)
{
break;
}
for (k = 0; k < 5; k++)
{
if (board[i + k][j - k] == ' ' && (board[i + k + 1][j - k - 1] == '*' || board[i + k - 1][j - k + 1] == '*')
&& (i + k + 2) < row && (i + k - 2) > 0)
{
board[i + k][j - k] = '#';
//printf("反对角线玩家三子,阻挡玩家四子\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断行电脑是否连成两子,若连成两子,则下子使电脑连成三子
for (i = 0; i < hang; i++)
{
for (j = 0; j < lie - 4; j++)
{
for (k = 0; k < 5; k++)
{
if (board[i][j + k] == '*')
{
sum1++;
}
else if (board[i][j + k] == '#')
{
sum2++;
}
}
if (sum2 > 1)
{
if (sum1 > 1)
{
break;
}
for (k = 0; k < 5; k++)
{
if (board[i][j + k] == ' ' && ((board[i][j + k + 1] == '#' || board[i][j + k - 1] == '#'))
&& (j + k + 2)<col&& (j + k - 2)>0)
{
board[i][j + k] = '#';
//printf("行电脑二子,连二成三\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断列电脑是否连成两子,若连成两子,则下子使电脑连成三子
for (j = 0; j < lie; j++)
{
for (i = 0; i < hang - 4; i++)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j] == '*')
{
sum1++;
}
else if (board[i + k][j] == '#')
{
sum2++;
}
}
if (sum2 > 1)
{
if (sum1 > 1)
{
break;
}
for (k = 0; k < 5; k++)
{
if (board[i + k][j] == ' ' && ((board[i + k + 1][j] == '#' || board[i + k - 1][j] == '#'))
&& (i+k+2)< row && (i+k-2)>0)
{
board[i + k][j] = '#';
//printf("列电脑二子,连二成三\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断对角线电脑是否连成两子,若连成两子,则下子使电脑连成三子
for (i = 0; i < hang - 4; i++)
{
for (j = 0; j < lie - 4; j++)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j + k] == '*')
{
sum1++;
}
else if (board[i + k][j + k] == '#')
{
sum2++;
}
}
if (sum2 > 1)
{
if (sum1 > 1)
{
break;
}
for (k = 0; k < 5; k++)
{
if (board[i + k][j + k] == ' ' && ((board[i + k + 1][j + k + 1] == '#' || board[i + k - 1][j + k - 1] == '#'))
&& (i + k + 2) < row && (i + k - 2)>0)
{
board[i + k][j + k] = '#';
//printf("对角线电脑二子,连二成三\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
//判断反对角线电脑是否连成两子,若连成两子,则下子使电脑连成三子
for (i = 0; i < hang - 4; i++)
{
for (j = 4; j < lie; j++)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j - k] == '*')
{
sum1++;
}
else if (board[i + k][j - k] == '#')
{
sum2++;
}
}
if (sum2 > 1)
{
if (sum1 > 1)
{
break;
}
for (k = 0; k < 5; k++)
{
if (board[i + k][j - k] == ' ' && ((board[i + k + 1][j - k - 1] == '#' || board[i + k - 1][j - k + 1] == '#'))
&& (i + k + 2) < row && (i + k - 2) > 0)
{
board[i + k][j - k] = '#';
//printf("反对角线电脑二子,连二成三\n");
return 1;
}
}
}
sum1 = sum2 = 0;
}
}
return 0;
}
//判断棋盘的一部分是否已满函数
int Isfull(char board[][col], int x, int y,int num)
{
int i = 0;
int j = 0;
for (i = x-num; i<=x+num; i++)
{
for (j = y-num; j <= y+num; j++)
{
if (i < 0 || j < 0 || i >= row || j >= col)
{
continue;
}
if (board[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}
//电脑下棋函数
void Computermove(char board[][col], int hang, int lie,int a[2])
{
int x = 0;
int y = 0;
int i = 0;
int x1 = a[0];
int y1 = a[1];
printf("电脑下棋\n");
int re = youhua(board, hang, lie);
if (re == 0)
{
while (1)
{
x = rand() % hang;
y = rand() % lie;
int tmp = 0;
if (board[x][y] == ' ')
{
if (x <= x1 + 1 && x >= x1 - 1 && y <= y1 + 1 && y >= y1 - 1)
{
board[x][y] = '#';
//printf("随机落子\n");
break;
}
else
{
for (i = 1; i <= row / 2; i++)
{
if (Isfull(board, x1, y1, i))
{
if (x <= x1 + i+1 && x >= x1 - (i+1) && y <= y1 + i+1 && y >= y1 - (i+1))
{
board[x][y] = '#';
tmp = 1;
//printf("随机落子\n");
break;
}
}
else
{
break;
}
}
if (tmp)
{
break;
}
}
}
}
}
}
//判断是否平局函数
int Isdraw(char board[][col], int hang, int lie)
{
int i = 0;
int j = 0;
for (i = 0; i < hang; i++)
{
for (j = 0; j < lie; j++)
{
if (board[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}
//判断输赢函数
char Iswin(char board[][col], int hang, int lie)
{
int i = 0;
int j = 0;
int k = 0;
int sum1 = 0;
int sum2 = 0;
//判断行
for (i = 0; i < hang; i++)
{
for (j = 0; j < lie - 4; j++)
{
for (k = 0; k < 5; k++)
{
if (board[i][j + k] == '*')
{
sum1++;
}
else if (board[i][j + k] == '#')
{
sum2++;
}
}
if (sum1 == 5)
{
return '*';
}
else if (sum2 == 5)
{
return '#';
}
sum1 = sum2 = 0;
}
}
//判断列
for (j = 0; j < lie; j++)
{
for (i = 0; i < hang - 4; i++)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j] == '*')
{
sum1++;
}
else if (board[i + k][j] == '#')
{
sum2++;
}
}
if (sum1 == 5)
{
return '*';
}
else if (sum2 == 5)
{
return '#';
}
sum1 = sum2 = 0;
}
}
//判断对角线
for (i = 0; i < hang - 4; i++)
{
for (j = 0; j < lie - 4; j++)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j + k] == '*')
{
sum1++;
}
else if (board[i + k][j + k] == '#')
{
sum2++;
}
}
if (sum1 == 5)
{
return '*';
}
else if (sum2 == 5)
{
return '#';
}
sum1 = sum2 = 0;
}
}
//判断反对角线
for (i = 0; i < hang - 4; i++)
{
for (j = 4; j < lie; j++)
{
for (k = 0; k < 5; k++)
{
if (board[i + k][j - k] == '*')
{
sum1++;
}
else if (board[i + k][j - k] == '#')
{
sum2++;
}
}
if (sum1 == 5)
{
return '*';
}
else if (sum2 == 5)
{
return '#';
}
sum1 = sum2 = 0;
}
}
//判断平局
int ret = Isdraw(board,hang,lie);
if (ret == 1)
{
return 'Q';
}
return 'C';
}
三:game.h
- 这个文件的作用十分的简单,就是进行头文件的引用和要在函数的声明
- 直接上代码
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
//常数的定义
#define row 15
#define col 15
//函数的声明
//初始化棋盘函数
void Initboard(char board[row][col], int hang, int lie);
//棋盘展示函数
void Displayboard(char board[row][col], int hang, int lie);
//玩家下棋函数
int* Playermove(char board[][col], int hang, int lie);
//玩家1下棋函数
void Player1move(char board[][col], int hang, int lie);
//玩家下棋函数
void Player2move(char board[][col], int hang, int lie);
//电脑下棋函数
void Computermove(char board[][col], int hang, int lie,int a[2]);
//判断输赢函数
char Iswin(char board[][col], int hang, int lie);