• 大小: 24KB
    文件类型: .cpp
    金币: 1
    下载: 0 次
    发布日期: 2022-10-08
  • 语言: C/C++
  • 标签: 同化棋  计算概论  

资源简介

自写同化棋代码,内含决策AI,注释也是挺详细的。 发上来造福后人。

资源截图

代码片段和文件信息

#include 
#include 
#include 
#include 
#include 
#include 
#include
#include
using namespace std;
//全局变量~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const int maxdepth = 4;//搜索深度
const int maxID = 35;
static int delta[24][2] = { { 11 }{ 01 }{ -11 }{ -10 }
{ -1-1 }{ 0-1 }{ 1-1 }{ 10 }
{ 20 }{ 21 }{ 22 }{ 12 }
{ 02 }{ -12 }{ -22 }{ -21 }
{ -20 }{ -2-1 }{ -2-2 }{ -1-2 }
{ 0-2 }{ 1-2 }{ 2-2 }{ 2-1 }
};
int gridInfo[7][7] = { 0 }; // 先x后y,记录棋盘状态//主棋盘
int currPeoColor; // 人所执子颜色(1为黑,-1为白,棋盘状态亦同)
int turnID;//回合数

//打印图形界面 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
string fuhao(int i int j int p[7][7])//获得棋盘中指定坐标交点位置的字符,通过制表符拼成棋盘
{
if (p[i][j] == 1)//1为黑子
return “○“;
else if (p[i][j] == -1)//-1为白子
return “●“;
else
return “  “;
}
void outqipan()//实现打印界面
{
system(“cls“);

int blacknum = 0;
int whitenum = 0;
cout <<“  |“<< “1“ << “  2“ << “  3“ << “  4“ << “  5“ << “  6“ << “  7“ <<“  Y“<< endl;
cout << “-------------------------------“ << endl;
for (int i = 0; i<7; i++)
{
cout << i + 1 << “ |“;
for (int j = 0; j<7; j++)
{
if (gridInfo[i][j] == 1) blacknum++;
if (gridInfo[i][j] == -1) whitenum++;
cout << fuhao(i j gridInfo)<<“ |“;
}
cout << endl;
cout<<“  “<<“------------------------------“ ;
cout< }
cout << “X|“ << endl;
cout<<“**********                **********“< cout << “*黑方:“ < cout< cout<<“**********                **********“<}

//提子走子~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
inline bool inMap(int x int y)// 判断是否在地图内
{
if (x < 0 || x > 6 || y < 0 || y > 6)
return false;
return true;
}

bool step(int x0 int y0 int x1 int y1 int qipan[7][7] int color)//实现在指定的棋盘上下棋
{
if (color == 0) return false;
if (x1 == -1) return true;
if (!inMap(x0 y0)) return false;
if (qipan[x0][y0] != color) return false;
int dx dy x y;
dx = abs((x0 - x1)) dy = abs((y0 - y1));
if ((dx == 0 && dy == 0) || dx > 2 || dy > 2) // 保证不会移动到原来位置,而且移动始终在5×5区域内
return false;
if (qipan[x1][y1] != 0) // 保证移动到的位置为空
return false;
if (dx == 2 || dy == 2) // 如果走的是5×5的外围,则不是复制粘贴
qipan[x0][y0] = 0;

qipan[x1][y1] = color;
for (int dir = 0; dir<8; dir++)
{
x = x1 + delta[dir][0];
y = y1 + delta[dir][1];
if (!inMap(x y))
continue;
if (qipan[x][y] == -color)
{
qipan[x][y] = color;
}
}
return true;
}

//判断游戏结束~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

评论

共有 条评论

相关资源