前言
近期在准备计算机博弈大赛,前两天把沈阳航空航天大学2017年的全国计算机博弈大赛五子棋的代码找了出来,研究了一下,觉得还是学到了很多东西的,以下是我自己的一些对于这套代码的理解,如有不对的地方,欢迎斧正!
一、源代码
代码下载地址:github: 五子棋源代码.
(源代码有些地方过于冗长,代码稍有改动)
#include "stdio.h"
#include "math.h"
#include "windows.h"
#include "time.h"//使用当前时钟做种子
#include "conio.h"
#define N 15 //定义棋盘大小
#define M N+8 //定义数据棋盘大小
int chess[N][N];//棋盘,用于储存下子位置
int count[M][M];//数据棋盘,用于储存下子位置和盘外数据
char buffer[N * 2 - 1][N * 4 - 3];//缓冲器,用于储存下子位置,盘外数据,和棋盘符号
long sum[M][M] = {
{
0} };//得分棋盘,储存得分情况
int p[20][5];//储存该位置的所有可能的五连珠情况
int player = 1, ai = 2, error = -1;
int num;//双方回合总数
int now;//设置下棋标志
int flag0;//确定己方已下子
int gs;//游戏结束标志
// 基础函数
void RunGame();//进行整个对局
int Menu();//设置开始菜单
void Initialize();//初始化棋盘,数据棋盘,和缓冲器
void Print(int x, int y);//将数据写入数据棋盘,和缓冲器
void Buf_Display();//利用双缓冲将缓冲器数据输出到屏幕
int Judge_Display();//输出胜负情况
//双方对局函数
void PGame();//玩家对局函数
void CGame();//电脑对局函数
//输入函数
void Input(int *x, int *y);//键盘方向键控制,空格键输入
void Mark(int x0, int y0, int x, int y);//标记下子位置
//电脑判断函数
int Basic_condition(int x, int y);//判断下子位置是否出界
void Find_point(int *pp, int *qq);//找到最佳性最优的下子位置
int JUDGE(int x, int y);//判断胜负
void Grade();//计算所有位置的得分
void Base(int i, int j);//坐标为(i-4,j-4)的所有五连珠情况
long Assessment(int num1, int num2);//评分标准
//主程序
int main()
{
system("title 简易五子棋");//设置标题
system("mode con cols=58 lines=29");//设置窗口大小
system("color E0");//设置颜色
Initialize();//初始化棋盘,数据棋盘,和缓冲器
RunGame();//进行整个对局
return 0;
}
/***********************************************************************************************************************************************************/
void RunGame()//进行整个对局
{
switch (Menu())
{
case 1:
while (1)
{
PGame();//玩家对局函数
if (Judge_Display())
{
break;//对局结束
}
CGame();//电脑对局函数
if (Judge_Display())
{
break;//对局结束
}
}//while
break;
case 2:
now = ai;
count[N / 2 + 4][N / 2 + 4] = now; chess[N / 2][N / 2] = now;
Print(N / 2, N / 2);//将数据写入数据棋盘,和缓冲器
num++;
while (1)
{
PGame();//玩家对局函数
if (Judge_Display())
{
break;//对局结束
}
CGame();//电脑对局函数
if (Judge_Display())
{
break;//对局结束
}
}//while
break;
case 3:
now = 1;
count[N / 2 + 4][N / 2 + 4] = now; chess[N / 2][N / 2] = now;
Print(N / 2, N / 2);//将数据写入数据棋盘,和缓冲器
num++;
while (1)
{
CGame();//电脑对局函数
if (Judge_Display())
{
break;//对局结束
}
}
break;
default:;
}
}
/***********************************************************************************************************************************************************/
int Menu()//设置开始菜单
{
int x;
printf("方向键移动,空格键确定\n");
printf("1、玩家先手\n");
printf("2、玩家后手\n");
printf("3、电脑自对弈\n");
scanf("%d", &x);
return x;
}
void Initialize()//初始化棋盘,数据棋盘,和缓冲器
{
int i, j;
//初始化数据棋盘
for (i = 0; i < M; i++)
{
for (j = 0; j < M; j++)
{
if ((i < 4 || i >= M - 4) && (j < 4 || j >= M - 4))
{
count[i][j] = error;
}
}
}
//初始化缓冲器
for (i = 0; i < N * 2 - 1; i++)
{
for (j = 0; j < N * 4 - 3; j++)
{
if (i == 0 && j == 0)//以下为边缘棋盘样式
buffer[i][j] = 'a';//"┏";
else if (i == 0 && j == (N * 4 - 3) - 1)
buffer[i][j] = 'b';//"┓";
else if (i == (N * 2 - 1) - 1 && j == (N * 4 - 3) - 1)
buffer[i][j] = 'c';//"┛";
else if (i == (N * 2 - 1) - 1 && j == 0)
buffer[i][j] = 'd';//"┗";
else if (i == 0 && j % 4 == 0)
buffer[i][j] = 'e';//"┯";
else if (i == (N * 2 - 1) - 1 && j % 4 == 0)
buffer[i][j] = 'f';//"┷";
else if (i % 2 == 0 && j == 0)
buffer[i][j] = 'g';//"┠";
else if (i % 2 == 0 && j == (N * 4 - 3) - 1)
buffer[i][j] = 'h';//"┨";
else if ((i == 0 || i == (N *

本文介绍了作者对沈阳航空航天大学2017年全国计算机博弈大赛五子棋代码的研究,包括初始化棋盘、对局流程、寻找最佳下子位置和实时判断棋局状况。代码中实现了玩家与电脑的对战,以及电脑自对弈功能。通过对棋盘的实时更新和五子连珠的判断,实现了游戏的结束条件。文章强调了基础算法的重要性,并表达了对提高代码棋力的期待。
最低0.47元/天 解锁文章
2781

被折叠的 条评论
为什么被折叠?



