AI五子棋第一篇-利用最简单的C语言实现
Posted Ja_King_ZH
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AI五子棋第一篇-利用最简单的C语言实现相关的知识,希望对你有一定的参考价值。
一.如何实现
1.说明:由于本文只是对初学C语言的人学习,所以将不会涉及任何算法,电脑将采用随机下子的方式。(后期会为大家介绍Alpha-Beta剪枝算法实现人工智能AI)
2.主要部分:
(1)菜单
(2)打印棋盘
(3)玩家下子
(4)电脑下子
(5)判断输赢
二.实现代码及分析
(1)菜单的制作
运用do…while循环调用菜单,根据用户选择实现玩游戏和退出游戏
(2)棋盘的初始化和打印
棋盘采用标准的15*15的格子,我们可以宏定义ROW和COL分别为15和15来表示行和列。
分别封装两个函数对棋盘进行初始化和打印
初始化:(这里将棋盘初始化为空格,可初始化为其他)
打印棋盘:
效果如下:
(3)玩家下子
void PlayerMove(char board[ROW][COL], int row, int col)
int x, y;
while (1)
printf("玩家走:\\n");
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] = 'O';
break;
else
printf("坐标被占用,请重新输入\\n");
else
printf("坐标非法,请重新输入\\n");
玩家下子主要采用的思路是,判断棋盘上是否有子,已经输入的坐标是否合法。
(4)电脑下子
void ComputerMove(char board[ROW][COL], int row, int col)
int x, y;
printf("电脑走\\n");
while (1)
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
board[x][y] = 'X';
break;
与玩家下子相同,此处采用的随机下子,后期可进行优化(比如:极大极小值算法、Alpha-Beta剪枝算法等)
(5)判断输赢
通过遍历整个棋盘,观察是否有连五子情况出现,代码如下:
char iswin(char board[ROW][COL], int row, int col)
//行
int ren=0,dian=0,i,j;
for (i = 0;i < row;i++)
ren = 0;
for (j = 0;j < col;j++)
if (board[i][j] == 'O')
ren++;
else
ren = 0;
if (ren >= 5)
return 'o';
for (i = 0;i < row;i++)
dian = 0;
for (j = 0;j < col;j++)
if (board[i][j] == 'X')
dian++;
else
dian = 0;
if (dian >= 5)
return 'x';
//列
ren = dian = 0;
for (i = 0;i < row;i++)
ren = 0;
for (j = 0;j < col;j++)
if (board[j][i] == 'O')
ren++;
else
ren = 0;
if (ren >= 5)
return 'o';
for (i = 0;i < row;i++)
dian = 0;
for (j = 0;j < col;j++)
if (board[j][i] == 'X')
dian++;
else
dian = 0;
if (dian >= 5)
return 'x';
//右下
ren = dian =i=j= 0;
for (int k = 0;k < row;k++)
i = k;
j = 0;
ren = 0;
while (i < row && j < col)
if (board[i][j] == 'O')
ren++;
else
ren = 0;
i++;
j++;
if (ren >= 5)
return 'o';
i = j = 0;
for (int k = 0;k < row;k++)
i = k;
j = 0;
dian = 0;
while (i < row && j < col)
if (board[i][j] == 'X')
dian++;
else
dian = 0;
i++;
j++;
if (dian >= 5)
return 'x';
//右上
for (int k = row;k >=0;k--)
j = col;
i = k;
ren = 0;
while (i >=0 && j >=0)
if (board[i][j] == 'O')
ren++;
else
ren = 0;
i--;
j--;
if (ren >= 5)
return 'o';
for (int k = row;k >= 0;k--)
i = k;
j = col;
dian = 0;
while (i >= 0 && j >= 0)
if (board[i][j] == 'X')
dian++;
else
dian = 0;
i--;
j--;
if (dian >= 5)
return 'o';
for (int k = row;k >= 0;k--)
i = k;
j = 0;
dian = 0;
while (i >= 0 && j <col)
if (board[i][j] == 'O')
dian++;
else
dian = 0;
i--;
j++;
if (dian >= 5)
return 'o';
for (int k = row;k >= 0;k--)
i = k;
j = 0;
dian = 0;
while (i >= 0 && j <col)
if (board[i][j] == 'X')
dian++;
else
dian = 0;
i--;
j++;
if (dian >= 5)
return 'x';
for (int k = 0;k < row;k++)
i = k;
j = col;
dian = 0;
while (i < row && j >= 0)
if (board[i][j] == 'O')
dian++;
else
dian = 0;
i++;
j--;
if (dian >= 5)
return 'o';
for (int k = 0;k <row;k++)
i = k;
j = col;
dian = 0;
while (i <row && j >= 0)
if (board[i][j] == 'X')
dian++;
else
dian = 0;
i++;
j--;
if (dian >= 5)
return 'x';
for (int k = row;k >= 0;k--)
i = k;
j = 0;
dian = 0;
while (i >= 0 && j <col)
if (board[i][j] == 'O')
dian++;
else
dian = 0;
i--;
j++;
if (dian >= 5)
return 'o';
for (int k = row;k >= 0;k--)
i = k;
j = 0;
dian = 0;
while (i >= 0 && j <col)
if (board[i][j] == 'X')
dian++;
else
dian = 0;
i--;
j++;
if (dian >= 5)
return 'x';
for (int k = 0;k < row;k++)
i = k;
j = col;
dian = 0;
while (i < row && j >= 0)
if (board[i][j] == 'O')
dian++;
else
dian = 0;
i++;
j--;
if (dian >= 5)
return 'o';
for (int k = 0;k <row;k++)
i = k;
j = col;
dian = 0;
while (i <row && j >= 0)
if (board[i][j] == 'X')
dian++;
else
dian = 0;
i++;
j--;
if (dian >= 5)
return 'x';
return 'c';
//左
三.整个代码
(1)test.c文件下代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
printf("**************************\\n");
printf("******* 1. play *********\\n");
printf("******* 0. exit *********\\n");
printf("**************************\\n");
void game()
//棋盘数组
char board[ROW][COL],ret;
//初始化棋盘
InitBoard(board, ROW, COL);
//打印棋盘
PrintBoard(board, ROW, COL);
//下棋
int a;
printf("1.先手 0.后手");
scanf("%d", &a);
if(a==1)
PlayerMove(board, ROW, COL);
else
ComputerMove(board, ROW, COL);
while (1)
if (a == 0)
//PlayerMove(board, ROW, COL);
PlayerMove(board, ROW, COL);
PrintBoard(board, ROW, COL);
ret=iswin(board, ROW, COL);
if (ret != 'c')
break;
//ComputerMove(board, ROW, COL);
ComputerMove(board, ROW, COL);
PrintBoard(board, ROW, COL);
ret = iswin(board, ROW, COL);
if (ret != 'c')
break;
else
//ComputerMove(board, ROW, COL);
ComputerMove(board, ROW, COL);
PrintBoard(board, ROW, COL);
ret = iswin(board, ROW, COL);
if (ret != 'c')
break;
//PlayerMove(board, ROW, COL);
PlayerMove(board, ROW, COL);
PrintBoard(board, ROW, COL);
ret = iswin(board, ROW, COL);
if (ret != 'c')
break;
if (ret == 'o')
printf("玩家赢\\n");
else if (ret == 'x')
printf("电脑赢\\n");
else
printf("平局\\n");
int main()
int input;
srand((unsigned int)time(NULL));
do
menu();
scanf("%d", &input);
switch (input)
case 1:
game();
break;
case 0:
printf("退出游戏\\n");
break;
default:
printf("选择错误\\n");
break;
while (input);
return 0;
(2)game.c下的代码
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void InitBoard(char board[ROW][COL], int row, int col)
for C语言实现简单的三子棋