赞
踩
*一、项目需求*
五子棋是一种简单的黑白棋,历史悠久,起源于中国,后传入日本,在日本被称为“连珠”,是一种老少皆宜的益智游戏。
人工智能五子棋系统的目标用户是一切想致力于研究人机对弈算法理论的相关研究者和一切想通过此系统进行五子棋人机对弈游戏的人群。目标用户特点:范围广泛,知识储备门槛低,年龄跨度大,具备操作简单的人机交互界面即可。
我们计划实现人人对弈,人机对战以及机机对弈等三种模式以及一些基本游戏功能。
*二、设备要求*
本次实验是运用Microsoft Visual Studio 软件来进行一次智能五子棋游戏的设计, Microsoft Visual StudioVS是一个基本完整的开发工具集,它包括了整个软件生命周期中所需要的大部分工具。Visual Studio是最流行的Windows平台应用程序的集成开发环境。最新版本为 Visual Studio 2019 版本。
人工智能五子棋属于人工智能中人机博弈的一种,人工智能应用广泛,人工智能是促进未来人类科技和生活重大改变的一门学科。
*三、项目设计*
本项目主要设计如下:
*3.1、人机交互界面*
通过鼠标点击调用不同的函数,进而刷新屏幕使之进入不同的界面,让玩家选择自己想要的游戏模式,游戏难度以及先后手,让玩家有更好的游戏体验,为此,我特地添加了悔棋,重新开始,退出游戏等功能,让玩家在玩的过程中拥有更多机会。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yXXzU5bQ-1636549459002)(data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==)]
图3.1.1 人机交互界面关系流程图
*3.2、系统结构设计*
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lraDGBzz-1636549459019)(data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==)]
图3.2.1 系统结构设计
*3.3、数据结构设计*
int Value_black / Value_white[ 17] [ 17] ={
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},
{0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0},
{0, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 1, 0},
{0, 1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 2, 1, 0},
{0, 1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 5, 4, 3, 2, 1, 0},
{0, 1, 2, 3, 4, 5, 6, 6, 6, 6, 6, 5, 4, 3, 2, 1, 0},
{0, 1, 2, 3, 4, 5, 6, 7, 7, 7, 6, 5, 4, 3, 2, 1, 0},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 3, 2, 1, 0},
{0, 1, 2, 3, 4, 5, 6, 7, 7, 7, 6, 5, 4, 3, 2, 1, 0},
{0, 1, 2, 3, 4, 5, 6, 6, 6, 6, 6, 5, 4, 3, 2, 1, 0},
{0, 1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 5, 4, 3, 2, 1, 0},
{0, 1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 2, 1, 0},
{0, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 1, 0},
{0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0},
{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
以下是我定义的各种函数。
void menu(); //主菜单背景 void Difficulty(); //选择人机难度 void choseFrist(); //选择先后手 void ChessBoard(); //棋盘 void DropChess(MOUSEMSG m); //下棋 bool Rules(int i, int j, int x); // 规则(落棋的合法性以及判断输赢) void Newa(); //重新开始后进行初始化 void Regret(int x, int y); //悔棋 void DropChess_People(); //人下棋 void Low(); //初级难度 void Medium(); //中级难度 void Low_J(); void Medium_J(); void Judge_ML(int i,int j,int x); //判断棋盘模型 void Renew_Value_shu(int i,int j,int x,int n); //更新价值表:竖直方向 void Renew_Value_leftup(int i, int j, int x, int n); //更新价值表:左上往右下 void Renew_Value_heng(int i, int j, int x, int n); //更新价值表:水平方向 void Renew_Value_rightup(int i, int j, int x, int n); //更新价值表:右上到左下 HWND chunchun = GetHWnd(); //窗口弹出; int flag = 0; //限制落棋的顺序以及赢棋之后无法再次落棋 int a[20][20] = { 0 }; //存储数组的二维数组 int level; //难度等级 int order = 0; //顺序——先后手 int close = 0; //使退出人机的程序。 int e,f,c,d; //记录悔棋下标。 int level1 = 0; //机器的 int value_black[17][17] = { 0 }; //黑棋的价值表 int value_white[17][17] = { 0 }; //白棋的价值表
四、*项目实现过程*
*4.1、界面设计*
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-H8ueV1vt-1636549459020)(file:///C:\Users\LJC666\AppData\Local\Temp\ksohtml43100\wps8.jpg)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3cI6BSDb-1636549459021)(data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==)]
图4.1.1 总体算法流程图
4.2、游戏裁判算法:判断赢家
当某种颜色的棋子胜利的时候,会有弹窗提示玩家,玩家只有点击“确定”才会重新刷新棋盘,否则点击棋盘是无反应的。
当我判断胜负后能结束游戏时,我会把控制交替下子的变量赋值为另一个数字,使程序进不去落子循环,从而控制赢得游戏之后不能再对棋盘进行改动的操作,而当玩家点击了“确定”之后,程序会立即刷新棋盘,从而使玩家可以进入下一个对局。
int i2, j2; for (i2 = i - 4, j2 = j - 4; i2 <= i + 4 && j2 <= j + 4; i2++, j2++) { if (a[i2][j2] == x) { //左上往右下 sum2 = sum2 + 1; } else { sum2 = 0; } if (sum2 == 5) { pd = 1; } } int i3, j3; for (j3 = j - 4, i3 = i; j3 <= j + 4; j3++) { if (a[i3][j3] == x) { //横 sum3 = sum3 + 1; } else { sum3 = 0; } if (sum3 == 5) { //赢 pd = 1; } } int i4, j4; for (i4 = i - 4, j4 = j + 4; i4 <= i + 4 && j4 >= j - 4; i4++, j4--) { if (a[i4][j4] == x) { //右上往左下 sum4 = sum4 + 1; } else { sum4 = 0; } if (sum4 == 5) { pd = 1; } } if (pd == 1) { return true; } else { return false; } }
4.3、机器价值表算法:
每下一颗棋子时,先判断是否产生输赢,若是没有产生输赢,则判断棋盘类型并根据不同的类型刷新价值表。
例如:每下一颗棋子则在其周围加10,活二在其前后各加100,死二则在其为被堵住的地方加50,活三在其前后加1000,死三则在其为被堵住的地方加500,活四则在其前后分别加10000,死四则在其为被堵住的地方加上2000。
当机器落子时,会遍历黑白双方的价值表,判断此时是应该进攻还是防守,应该让自己赢还是不让对方赢,从而更大程度掌握棋局优势。
五、项目代码
#include<graphics.h> #include<Windows.h> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<mmsystem.h> #pragma comment(lib,"winmm.lib") void menu(); //主菜单背景 void Difficulty(); //选择人机难度 void choseFrist(); //选择先后手 void ChessBoard(); //棋盘 void DropChess(MOUSEMSG m); //下棋 bool Rules(int i, int j, int x); // 规则(落棋的合法性以及判断输赢) void Newa(); //重新开始后进行初始化 void Regret(int x, int y); //悔棋 void DropChess_People(); //人下棋 void Low(); //初级难度 void Medium(); //中级难度 void Low_J(); void Medium_J(); void Judge_ML(int i, int j, int x); //判断棋盘模型 void Renew_Value_shu(int i, int j, int x, int n); //更新价值表:竖直方向 void Renew_Value_leftup(int i, int j, int x, int n); //更新价值表:左上往右下 void Renew_Value_heng(int i, int j, int x, int n); //更新价值表:水平方向 void Renew_Value_rightup(int i, int j, int x, int n); //更新价值表:右上到左下 HWND chunchun = GetHWnd(); //窗口弹出; int flag = 0; //限制落棋的顺序以及赢棋之后无法再次落棋 int a[20][20] = { 0 }; //存储数组的二维数组 int level; //难度等级 int order = 0; //顺序——先后手 int close = 0; //使退出人机的程序。 int e, f, c, d; //记录悔棋下标。 int level1 = 0; //机器的, int value_black[17][17] = { 0 }; //黑棋的价值表 int value_white[17][17] = { 0 }; //白棋的价值表 int main() { //initgraph(700, 600, SHOWCONSOLE); //始化窗口,弄终端界面。 PlaySound(L"C:\\Users\\LJC666\\source\\repos\\txh1.0\\music.wav", NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);//音乐循环播放 //PlaySound(L"music.wav", NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);//音乐循环播放 initgraph(700, 600); //始化窗口,弄终端界面。 menu(); //主菜单界面以及跳转的实现。 while (1) { if (level == 1) { if (order == 1) { if (level1 == 1) { DropChess_People(); Low(); } else if (level1 == 4) { Low_J(); } } else if (order == 2) { Low(); DropChess_People(); } } else if (level == 2) { if (order == 1) { if (level1 == 1) { DropChess_People(); Medium(); } else if (level1 == 4) { Medium_J(); } } else if (order == 2) { Medium(); DropChess_People(); } } else if (level == 0) { break; } if (close == 1) { break; } } //system("pause");//暂停显示界面 //closegraph();//关闭窗口 return 0; } void menu() //主菜单界面,跳转 { IMAGE image; loadimage(&image, L"C:\\Users\\LJC666\\source\\repos\\txh1.0\\x64\\tp\\tp5.jpg", 700, 600); //loadimage(&image, L"./tp/tp5.jpg", 700, 600); putimage(0, 0, &image); settextcolor(BLACK); settextstyle(50, 0, L"华文隶书");//设置字体的风格 setbkmode(TRANSPARENT); //让字的范围跟随背景 outtextxy(250, 150, _T("人机对战")); //在指定区域输出字体 outtextxy(250, 250, _T("人人对战")); outtextxy(250, 350, _T("机机对战")); outtextxy(250, 450, _T("退出游戏")); MOUSEMSG msg; //定义一个鼠标消息 while (1) { msg = GetMouseMsg();//获取鼠标消息 if (msg.x > 250 && msg.x < 450 && msg.y>150 && msg.y < 200) { //点击人机对战的鼠标控制 if (msg.uMsg == WM_LBUTTONDOWN) { level1 = 1; //非机器机器 Difficulty(); //选择人机难度。 break; } } else if (msg.x > 250 && msg.x < 450 && msg.y>250 && msg.y < 300) { //点击人人对战的鼠标控制 if (msg.uMsg == WM_LBUTTONDOWN) { level = 0; level1 = 1; //非机器机器 ChessBoard(); DropChess_People(); break; } } else if (msg.x > 250 && msg.x < 450 && msg.y>350 && msg.y < 400) { //机机对战 if (msg.uMsg == WM_LBUTTONDOWN) { level1 = 4; //机器机器 Difficulty(); //选择难度。 ChessBoard(); break; } } else if (msg.x > 250 && msg.x < 450 && msg.y>450 && msg.y < 500) { //退出游戏 if (msg.uMsg == WM_LBUTTONDOWN) { closegraph(); //关闭图形化界面窗口 close = 1; break; } } } } void Difficulty() //选择人机难度的界面 { cleardevice();//清空屏幕内容 IMAGE immge; loadimage(&immge, L"C:\\Users\\LJC666\\source\\repos\\txh1.0\\tp\\tp4.jpg", 700, 600);//先把图片放在工程项目下,使用L+"绝对路径" //loadimage(&immge, L"./tp/tp4.jpg", 700, 600);//先把图片放在工程项目下,使用L+"绝对路径" putimage(0, 0, &immge); //显示背景图片 settextcolor(BLACK); //设置字体的颜色 settextstyle(50, 0, L"华文琥珀");//设置字体的风格 setbkmode(TRANSPARENT); //让字的范围跟随背景 outtextxy(350, 150, _T("初出茅庐")); //在指定区域输出字体 outtextxy(350, 250, _T("登堂入室")); //outtextxy(350, 350, _T("炉火纯青")); outtextxy(350, 350, _T("返回上一步")); MOUSEMSG mmsge; //定义一个鼠标消息 while (1) { mmsge = GetMouseMsg();//获取鼠标消息 if (mmsge.x > 350 && mmsge.x < 550 && mmsge.y>150 && mmsge.y < 200) { //点击初级难度 if (mmsge.uMsg == WM_LBUTTONDOWN) { level = 1; //choseFrist();//选择先后手的界面 if (level1 == 1) { choseFrist();//选择先后手的界面 } else if (level1 == 4) { ChessBoard(); order = 1; } break; } } else if (mmsge.x > 350 && mmsge.x < 550 && mmsge.y>250 && mmsge.y < 300) { //中等难度 if (mmsge.uMsg == WM_LBUTTONDOWN) { level = 2; if (level1 == 1) { choseFrist();//选择先后手的界面 } else if (level1 == 4) { ChessBoard(); order = 1; } break; } } else if (mmsge.x > 350 && mmsge.x < 550 && mmsge.y>350 && mmsge.y < 400) { //返回上一步 if (mmsge.uMsg == WM_LBUTTONDOWN) { menu(); break; } } } } void choseFrist() //选择先后手 { cleardevice();//清空屏幕内容 IMAGE immg; loadimage(&immg, L"C:\\Users\\LJC666\\source\\repos\\txh1.0\\x64\\tp\\tp6.png", 700, 600);//先把图片放在工程项目下,使用L+"绝对路径" //loadimage(&immg, L"./tp/tp6.png", 700, 600);//先把图片放在工程项目下,使用L+"绝对路径" putimage(0, 0, &immg); //显示背景图片 settextcolor(BLACK); //设置字体的颜色 settextstyle(50, 0, L"华文彩云");//设置字体的风格 setbkmode(TRANSPARENT); //让字的范围跟随背景 outtextxy(200, 150, _T("先手")); //在指定区域输出字体 outtextxy(200, 250, _T("后手")); outtextxy(200, 350, _T("返回上一步")); MOUSEMSG mmsg; //定义一个鼠标消息 while (1) { mmsg = GetMouseMsg();//获取鼠标消息 if (mmsg.x > 200 && mmsg.x < 300 && mmsg.y>150 && mmsg.y < 200) { //先手 if (mmsg.uMsg == WM_LBUTTONDOWN) { order = 1; ChessBoard(); //棋盘,仅绘制; break; } } else if (mmsg.x > 200 && mmsg.x < 300 && mmsg.y>250 && mmsg.y < 300) { //后手 if (mmsg.uMsg == WM_LBUTTONDOWN) { order = 2; ChessBoard(); //棋盘 break; } } else if (mmsg.x > 200 && mmsg.x < 450 && mmsg.y>350 && mmsg.y < 400) { //点击返回上一步的鼠标控制 if (mmsg.uMsg == WM_LBUTTONDOWN) { Difficulty(); break; } } } } void ChessBoard() { cleardevice();//清空屏幕内容 IMAGE img; loadimage(&img, L"C:\\Users\\LJC666\\source\\repos\\txh1.0\\x64\\tp\\tp2.jpg", 700, 600);//先把图片放在工程项目下,使用L+"绝对路径" //loadimage(&img, L"./tp/tp2.jpg", 700, 600);//先把图片放在工程项目下,使用L+"绝对路径" putimage(0, 0, &img); //显示背景图片 settextcolor(BLACK); //设置字体的颜色 settextstyle(30, 0, L"楷体");//设置字体的风格 outtextxy(525, 150, _T("悔棋")); //在指定区域输出字体 outtextxy(525, 250, _T("重新开始")); outtextxy(525, 350, _T("退出游戏")); setlinecolor(BLACK);//将线段设为黑色 int i; int y1 = 100, y2 = 100, x3 = 100, x4 = 100; for (i = 0; i < 17; i++) { line(100, y1, 500, y2); //画横线, y1 = y1 + 25; //每个格子都是25*25. y2 = y2 + 25; line(x3, 100, x4, 500); //画竖线, x3 = x3 + 25; x4 = x4 + 25; } setfillcolor(BLACK); //填充圆的颜色 solidcircle(300, 300, 3); //棋盘的点的位置 solidcircle(175, 175, 3); solidcircle(175, 425, 3); solidcircle(425, 425, 3); solidcircle(425, 175, 3); settextcolor(BLACK); //设置字体的颜色 settextstyle(20, 0, L"幼圆");//设置字体的风格 setbkmode(TRANSPARENT); //让字的范围跟随背景 outtextxy(100, 70, _T("现在轮到:")); setfillcolor(BLACK); //记录下棋者 solidcircle(215, 80, 10); Newa(); } void DropChess_People() { int x, y, X, Y; MOUSEMSG mmsge; //定义一个鼠标消息 while (1) { mmsge = GetMouseMsg();//获取鼠标消息 if (mmsge.x > 525 && mmsge.x < 585 && mmsge.y>150 && mmsge.y < 180) { //点击悔棋的鼠标控制 if (mmsge.uMsg == WM_LBUTTONDOWN) { Regret(e, f); //悔棋操作 Regret(c, d); //悔棋操作 continue; } } else if (mmsge.x > 525 && mmsge.x < 645 && mmsge.y>250 && mmsge.y < 280) { //点击重新开始的鼠标控制 if (mmsge.uMsg == WM_LBUTTONDOWN) { Newa(); //初始化 ChessBoard(); if (level == 0 || order == 1) { DropChess_People(); } break; } } else if (mmsge.x > 525 && mmsge.x < 645 && mmsge.y>350 && mmsge.y < 380) { //点击退出游戏的鼠标控制 if (mmsge.uMsg == WM_LBUTTONDOWN) { Newa(); //初始化 menu(); break; } } else if (mmsge.x > 100 && mmsge.x < 500 && mmsge.y>100 && mmsge.y < 500) { //在棋盘下棋 if (mmsge.uMsg == WM_LBUTTONDOWN || mmsge.uMsg == WM_RBUTTONDOWN) { x = (mmsge.x - 100) % 25; //落棋的位置的限制 y = (mmsge.y - 100) % 25; if (x > 12) { X = ((mmsge.x / 25) + 1) * 25; } else { X = (mmsge.x / 25) * 25; } if (y > 12) { Y = ((mmsge.y / 25) + 1) * 25; } else { Y = (mmsge.y / 25) * 25; } if (flag == 1) { //存后手(白子) e = X; f = Y; } else if (flag == 0) { //存先手(黑子) c = X; d = Y; } DropChess(mmsge); //下棋函数 if (level != 0) { //如果不是人人对战,则跳出循环,使其只执行一次 break; } } } else { if (mmsge.uMsg == WM_LBUTTONDOWN || mmsge.uMsg == WM_RBUTTONDOWN) { MessageBox(chunchun, _T("棋子只能落在棋盘哦"), _T("淑女"), MB_OK); continue; } } } } void DropChess(MOUSEMSG m) //下棋 { int i, j; int x, y, X, Y; x = (m.x - 100) % 25; //落棋的位置的限制 y = (m.y - 100) % 25; if (x > 12) { X = ((m.x / 25) + 1) * 25; } else { X = (m.x / 25) * 25; } if (y > 12) { Y = ((m.y / 25) + 1) * 25; } else { Y = (m.y / 25) * 25; } j = (X - 100) / 25; i = (Y - 100) / 25; if (m.uMsg == WM_LBUTTONDOWN || m.uMsg == WM_RBUTTONDOWN) { if (a[i][j] == 1) { MessageBox(chunchun, _T("这里已经下了黑棋子啦"), _T("淑女"), MB_OK); } else if (a[i][j] == 2) { MessageBox(chunchun, _T("这里已经下了白棋子啦"), _T("淑女"), MB_OK); } } if (m.uMsg == WM_LBUTTONDOWN && a[i][j] == 0 && flag == 1) { //左键白子 a[i][j] = 2; //下白子 value_black[i][j] = 0; value_white[i][j] = 0; setfillcolor(WHITE); solidcircle(X, Y, 10); flag = 0; setfillcolor(BLACK); //记录下棋者 solidcircle(215, 80, 10); if (Rules(i, j, 2)) { //白子胜 MessageBox(chunchun, _T("白棋子胜利"), _T("淑女"), MB_OK); flag = 2; //使不能再下棋 Newa(); //初始化 ChessBoard(); //再次调用棋盘 } Judge_ML(i, j, 2); //判断类型,再调用函数更新价值表 } else if (m.uMsg == WM_RBUTTONDOWN && a[i][j] == 0 && flag == 0) { //右键黑子 a[i][j] = 1; value_black[i][j] = 0; value_white[i][j] = 0; setfillcolor(BLACK); solidcircle(X, Y, 10); flag = 1; setfillcolor(WHITE); //记录下棋者 solidcircle(215, 80, 10); if (Rules(i, j, 1)) { //黑子胜 MessageBox(chunchun, _T("黑棋子胜利"), _T("淑女"), MB_OK); flag = 2; //使不能再下棋 Newa(); //初始化 ChessBoard(); //再次调用棋盘 } Judge_ML(i, j, 1); //判断类型,再调用函数更新价值表 } } bool Rules(int i, int j, int x) //判断输赢 { int pd = 0; int sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0; int i1, j1; for (i1 = i - 4, j1 = j; i1 <= i + 4; i1++) { if (a[i1][j1] == x) { //竖 sum1 = sum1 + 1; } else { sum1 = 0; } if (sum1 == 5) { pd = 1; } } int i2, j2; for (i2 = i - 4, j2 = j - 4; i2 <= i + 4 && j2 <= j + 4; i2++, j2++) { if (a[i2][j2] == x) { //左上往右下 sum2 = sum2 + 1; } else { sum2 = 0; } if (sum2 == 5) { pd = 1; } } int i3, j3; for (j3 = j - 4, i3 = i; j3 <= j + 4; j3++) { if (a[i3][j3] == x) { //横 sum3 = sum3 + 1; } else { sum3 = 0; } if (sum3 == 5) { //赢 pd = 1; } } int i4, j4; for (i4 = i - 4, j4 = j + 4; i4 <= i + 4 && j4 >= j - 4; i4++, j4--) { if (a[i4][j4] == x) { //右上往左下 sum4 = sum4 + 1; } else { sum4 = 0; } if (sum4 == 5) { pd = 1; } } if (pd == 1) { return true; } else { return false; } } void Newa() //初始化 { int i, j; for (i = 0; i < 20; i++) { for (j = 0; j < 20; j++) { a[i][j] = 0; } } flag = 0; for (i = 0; i < 17; i++) { for (j = 0; j < 17; j++) { if (i == 0 || i == 16 || j == 0 || j == 16) { value_black[i][j] = 1; value_white[i][j] = 1; } else if (i == 1 || i == 15 || j == 1 || j == 15) { value_black[i][j] = 2; value_white[i][j] = 2; } else if (i == 2 || i == 14 || j == 2 || j == 14) { value_black[i][j] = 3; value_white[i][j] = 3; } else if (i == 3 || i == 13 || j == 3 || j == 13) { value_black[i][j] = 4; value_white[i][j] = 4; } else if (i == 4 || i == 12 || j == 4 || j == 12) { value_black[i][j] = 5; value_white[i][j] = 5; } else if (i == 5 || i == 11 || j == 5 || j == 11) { value_black[i][j] = 6; value_white[i][j] = 6; } else if (i == 6 || i == 10 || j == 6 || j == 10) { value_black[i][j] = 7; value_white[i][j] = 7; } else if (i == 7 || i == 9 || j == 7 || j == 9) { value_black[i][j] = 8; value_white[i][j] = 8; } else if (i == 8 && j == 8) { value_black[i][j] = 9; value_white[i][j] = 9; } } } } void Regret(int x, int y) //悔棋 { int i, j; j = (x - 100) / 25; i = (y - 100) / 25; IMAGE imge; loadimage(&imge, L"C:\\Users\\LJC666\\source\\repos\\txh1.0\\x64\\tp\\tp10.png", 24, 24);//先把图片放在工程项目下,使用L+"绝对路径" //loadimage(&imge, L"./tp/tp10.png", 24, 24);//先把图片放在工程项目下,使用L+"绝对路径" putimage(x - 12, y - 12, &imge); if (x == 100 && y == 100) { //1 setlinecolor(BLACK);//将线段设为黑色 line(x, y, x + 12, y); //画横线, line(x, y, x, y + 12); } else if (x == 500 && y == 100) { //2 setlinecolor(BLACK);//将线段设为黑色 line(x - 12, y, x, y); //画横线, line(x, y, x, y + 12); } else if (x == 100 && y == 500) { //3 setlinecolor(BLACK);//将线段设为黑色 line(x, y, x, y - 12); //画横线, line(x, y, x + 12, y); } else if (x == 500 && y == 500) { //4 setlinecolor(BLACK);//将线段设为黑色 line(x, y, x, y - 12); //画横线, line(x, y, x - 12, y); } else if (y == 100 && x != 100 && x != 500) { //一 setlinecolor(BLACK);//将线段设为黑色 line(x - 12, y, x + 12, y); //画横线, line(x, y, x, y + 12); } else if (x == 100 && y != 100 && y != 500) { //二 setlinecolor(BLACK);//将线段设为黑色 line(x, y, x + 12, y); //画横线, line(x, y - 12, x, y + 12); } else if (x == 500 && y != 500 && y != 100) { //三 setlinecolor(BLACK);//将线段设为黑色 line(x, y, x - 12, y); //画横线, line(x, y - 12, x, y + 12); } else if (x != 100 && x != 500 && y == 500) { //四 setlinecolor(BLACK);//将线段设为黑色 line(x - 12, y, x + 12, y); //画横线, line(x, y, x, y - 12); } else { //其余位置 setlinecolor(BLACK);//将线段设为黑色 line(x - 12, y, x + 12, y); //画横线, line(x, y - 12, x, y + 12); } a[i][j] = 0; //将记录有无棋子的数组置0 } void Low() //初级难度算法,//传入先后手,0为先手,1为后手 { int numi, numj; while (1) { while (1) { numi = rand(); if (numi >= 0 && numi < 17) { break; } } while (1) { numj = rand(); if (numj >= 0 && numj < 17) { break; } } if (a[numj][numi] == 0) { break; } } if (flag == 1 && order == 1) { //人先下棋,人下黑子,机器白子 setfillcolor(WHITE); solidcircle((numi * 25) + 100, (numj * 25) + 100, 10); //在指定区域里面画实心圆 a[numj][numi] = 2; value_black[numj][numi] = 0; value_white[numj][numi] = 0; flag = 0; setfillcolor(BLACK); //记录下棋者 solidcircle(215, 80, 10); if (Rules(numj, numi, 2)) { //白子胜 MessageBox(chunchun, _T("白棋子胜利"), _T("淑女"), MB_OK); flag = 2; //使不能再下棋 } e = (numi * 25) + 100; f = (numj * 25) + 100; } else if (flag == 0 && order == 2) { //机器先下棋,机器下黑子,人下白子 setfillcolor(BLACK); solidcircle((numi * 25) + 100, (numj * 25) + 100, 10); a[numj][numi] = 1; value_black[numj][numi] = 0; value_white[numj][numi] = 0; flag = 1; setfillcolor(WHITE); //记录下棋者 solidcircle(215, 80, 10); if (Rules(numj, numi, 1)) { //黑子胜 MessageBox(chunchun, _T("白棋子胜利"), _T("淑女"), MB_OK); flag = 2; //使不能再下棋 } c = (numi * 25) + 100; d = (numj * 25) + 100; } } void Judge_ML(int i, int j, int x) //判断棋盘连珠类型(x:白棋为2,黑棋为1) { //printf("fine\n"); int summ1 = 0, summ2 = 0, summ3 = 0, summ4 = 0; int sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0; int i1, j1; int i14, i13, i12; for (i1 = i - 4, j1 = j; i1 <= i + 4; i1++) { //竖 if (a[i1][j1] == x) { summ1 = summ1 + 1; if (summ1 == 4) { i14 = i1; } else if (summ1 == 3) { i13 = i1; } else if (summ1 == 2) { i12 = i1; } if (summ1 > sum1) { sum1 = summ1; } } else { summ1 = 0; } } if (sum1 == 4) { if (a[i14 - 4][j1] == 0 || a[i14 + 1][j1] == 0) { if (a[i14 - 4][j1] == 0 && a[i14 + 1][j1] == 0) { Renew_Value_shu(i14, j1, 0, x); //活四0,+10000 } else { Renew_Value_shu(i14, j1, 6, x); //死四6;+2000 } } } else if (sum1 == 3) { if (a[i13 - 3][j1] == 0 || a[i13 + 1][j1] == 0) { if (a[i13 - 3][j1] == 0 && a[i13 + 1][j1] == 0) { Renew_Value_shu(i13, j1, 3, x); //活三类型3,+1000 } else { Renew_Value_shu(i13, j1, 5, x); //死三类型5,+500 } } } else if (sum1 == 2) { if (a[i12 - 2][j1] == 0 || a[i12 + 1][j1] == 0) { if (a[i12 - 2][j1] == 0 && a[i12 + 1][j1] == 0) { Renew_Value_shu(i12, j1, 2, x); //活2类型2,+100 } else { Renew_Value_shu(i12, j1, 4, x); //死2类型4,+50 } } } else if (sum1 == 1) { Renew_Value_shu(i, j, 1, x); //1颗1,+10 } int i2, j2; //左上往右下 int i24, i23, i22, j24, j23, j22; for (i2 = i - 4, j2 = j - 4; i2 <= i + 4 && j2 <= j + 4; i2++, j2++) { if (a[i2][j2] == x) { summ2 = summ2 + 1; if (summ2 == 4) { i24 = i2; j24 = j2; } else if (summ2 == 3) { i23 = i2; j23 = j2; } else if (summ2 == 2) { i22 = i2; j22 = j2; } if (summ2 > sum2) { sum2 = summ2; } } else { summ2 = 0; } } if (sum2 == 4) { if (a[i24 - 4][j24 - 4] == 0 || a[i24 + 1][j24 + 1] == 0) { if (a[i24 - 4][j24 - 4] == 0 && a[i24 + 1][j24 + 1] == 0) { Renew_Value_leftup(i24, j24, 0, x); //活四0(必杀不赌) } else { Renew_Value_leftup(i24, j24, 6, x); //死四6;+2000 } } } else if (sum2 == 3) { if (a[i23 - 3][j23 - 3] == 0 || a[i23 + 1][j23 + 1] == 0) { if (a[i23 - 3][j23 - 3] == 0 && a[i23 + 1][j23 + 1] == 0) { Renew_Value_leftup(i23, j23, 3, x); //活三类型3,+1000 } else { Renew_Value_leftup(i23, j23, 5, x); //死三类型5,+500 } } } else if (sum2 == 2) { if (a[i22 - 2][j22 - 2] == 0 || a[i22 + 1][j22 + 1] == 0) { if (a[i22 - 2][j22 - 2] == 0 && a[i22 + 1][j22 + 1] == 0) { Renew_Value_leftup(i22, j22, 2, x); //活2类型2,+100 } else { Renew_Value_leftup(i22, j22, 4, x); //死2类型4,+50 } } } else if (sum2 == 1) { Renew_Value_leftup(i, j, 1, x); //1颗1,+10 } int i3, j3; //横 int j34, j33, j32; for (j3 = j - 4, i3 = i; j3 <= j + 4; j3++) { if (a[i3][j3] == x) { summ3 = summ3 + 1; if (summ3 == 4) { j34 = j3; } else if (summ3 == 3) { j33 = j3; } else if (summ3 == 2) { j32 = j3; } if (summ3 > sum3) { sum3 = summ3; } } else { summ3 = 0; } } if (sum3 == 4) { if (a[i3][j34 - 4] == 0 || a[i3][j34 + 1] == 0) { if (a[i3][j34 - 4] == 0 && a[i3][j34 + 1] == 0) { Renew_Value_heng(i3, j34, 0, x); //活四0(必杀不赌) } else { Renew_Value_heng(i3, j34, 6, x); //死四6;+2000 } } } else if (sum3 == 3) { if (a[i3][j33 - 3] == 0 || a[i3][j33 + 1] == 0) { if (a[i3][j33 - 3] == 0 && a[i3][j33 + 1] == 0) { Renew_Value_heng(i3, j33, 3, x); //活三类型3,+1000 } else { Renew_Value_heng(i3, j33, 5, x); //死三类型5,+500 } } } else if (sum3 == 2) { if (a[i3][j32 - 2] == 0 || a[i3][j32 + 1] == 0) { if (a[i3][j32 - 2] == 0 && a[i3][j32 + 1] == 0) { Renew_Value_heng(i3, j32, 2, x); //活2类型2,+100 } else { Renew_Value_heng(i3, j32, 4, x); //死2类型4,+50 } } } else if (sum3 == 1) { Renew_Value_heng(i, j, 1, x); //1颗1,+10 } int i4, j4; //右上往左下 int i44, i43, i42, j44, j43, j42; for (i4 = i - 4, j4 = j + 4; i4 <= i + 4 && j4 >= j - 4; i4++, j4--) { if (a[i4][j4] == x) { summ4 = summ4 + 1; if (summ4 == 4) { i44 = i4; j44 = j4; } else if (summ4 == 3) { i43 = i4; j43 = j4; } else if (summ4 == 2) { i42 = i4; j42 = j4; } if (summ4 > sum4) { sum4 = summ4; } } else { summ4 = 0; } } if (sum4 == 4) { if (a[i44 - 4][j44 + 4] == 0 || a[i44 + 1][j44 + 1] == 0) { if (a[i44 - 4][j44 + 4] == 0 && a[i44 + 1][j44 - 1] == 0) { Renew_Value_rightup(i44, j44, 0, x); //活四0(必杀不赌) } else { Renew_Value_rightup(i44, j44, 6, x); //死四6;+2000 } } } else if (sum4 == 3) { if (a[i43 - 3][j43 + 3] == 0 || a[i43 + 1][j43 - 1] == 0) { if (a[i43 - 3][j43 + 3] == 0 && a[i43 + 1][j43 - 1] == 0) { Renew_Value_rightup(i43, j43, 3, x); //活三类型3,+1000 } else { Renew_Value_rightup(i43, j43, 5, x); //死三类型5,+500 } } } else if (sum4 == 2) { if (a[i42 - 2][j42 + 2] == 0 || a[i42 + 1][j42 - 1] == 0) { if (a[i42 - 2][j42 + 2] == 0 && a[i42 + 1][j42 - 1] == 0) { Renew_Value_rightup(i42, j42, 2, x); //活2类型2,+100 } else { Renew_Value_rightup(i42, j42, 4, x); //死2类型4,+50 } } } else if (sum4 == 1) { Renew_Value_rightup(i, j, 1, x); //1颗1,+10 } } void Renew_Value_shu(int i, int j, int x, int n) //更新价值表:竖直方向,(n:1为黑,2为白) { int i1, j1; for (i1 = 0; i1 < 17; i1++) { for (j1 = 0; j1 < 17; j1++) { //如果已经有棋子了,不改变该点的值 if (a[i1][j1] != 0) { value_black[i1][j1] = -10000; value_white[i1][j1] = -10000; } } } //下棋的话,周边都要加10 if (order == 1) { //人为先手,人是黑棋,机器白棋 if (n == 1) { //黑棋的价值表 value_black[i + 1][j] = value_black[i + 1][j] + 10; value_black[i - 1][j] = value_black[i - 1][j] + 10; value_black[i][j + 1] = value_black[i][j + 1] + 10; value_black[i][j - 1] = value_black[i][j - 1] + 10; value_black[i + 1][j + 1] = value_black[i + 1][j + 1] + 10; value_black[i - 1][j - 1] = value_black[i - 1][j - 1] + 10; value_black[i + 1][j - 1] = value_black[i + 1][j - 1] + 10; value_black[i - 1][j + 1] = value_black[i - 1][j + 1] + 10; } else if (n == 2) { value_white[i + 1][j] = value_white[i + 1][j] + 10; value_white[i - 1][j] = value_white[i - 1][j] + 10; value_white[i][j + 1] = value_white[i][j + 1] + 10; value_white[i][j - 1] = value_white[i][j - 1] + 10; value_white[i + 1][j + 1] = value_white[i + 1][j + 1] + 10; value_white[i - 1][j - 1] = value_white[i - 1][j - 1] + 10; value_white[i + 1][j - 1] = value_white[i + 1][j - 1] + 10; value_white[i - 1][j + 1] = value_white[i - 1][j + 1] + 10; } } else if (order == 2) { //后手 if (n == 1) { value_black[i + 1][j] = value_black[i + 1][j] + 10; value_black[i - 1][j] = value_black[i - 1][j] + 10; value_black[i][j + 1] = value_black[i][j + 1] + 10; value_black[i][j - 1] = value_black[i][j - 1] + 10; value_black[i + 1][j + 1] = value_black[i + 1][j + 1] + 10; value_black[i - 1][j - 1] = value_black[i - 1][j - 1] + 10; value_black[i + 1][j - 1] = value_black[i + 1][j - 1] + 10; value_black[i - 1][j + 1] = value_black[i - 1][j + 1] + 10; } else if (n == 2) { value_white[i + 1][j] = value_white[i + 1][j] + 10; value_white[i - 1][j] = value_white[i - 1][j] + 10; value_white[i][j + 1] = value_white[i][j + 1] + 10; value_white[i][j - 1] = value_white[i][j - 1] + 10; value_white[i + 1][j + 1] = value_white[i + 1][j + 1] + 10; value_white[i - 1][j - 1] = value_white[i - 1][j - 1] + 10; value_white[i + 1][j - 1] = value_white[i + 1][j - 1] + 10; value_white[i - 1][j + 1] = value_white[i - 1][j + 1] + 10; } } if (x == 2) { if (order == 1) { if (n == 1) { value_black[i - 2][j] = value_black[i - 2][j] + 100; value_black[i + 1][j] = value_black[i + 1][j] + 100; } else if (n == 2) { value_white[i - 2][j] = value_white[i - 2][j] + 100; value_white[i + 1][j] = value_white[i + 1][j] + 100; } } else if (order == 2) { if (n == 1) { value_black[i - 2][j] = value_black[i - 2][j] + 100; value_black[i + 1][j] = value_black[i + 1][j] + 100; } else if (n == 2) { value_white[i - 2][j] = value_white[i - 2][j] + 100; value_white[i + 1][j] = value_white[i + 1][j] + 100; } } } else if (x == 3) { if (order == 1) { if (n == 1) { value_black[i - 3][j] = value_black[i - 3][j] + 1000; value_black[i + 1][j] = value_black[i + 1][j] + 1000; } else if (n == 2) { value_white[i - 3][j] = value_white[i - 3][j] + 1000; value_white[i + 1][j] = value_white[i + 1][j] + 1000; } } else if (order == 2) { if (n == 1) { value_black[i - 3][j] = value_black[i - 3][j] + 1000; value_black[i + 1][j] = value_black[i + 1][j] + 1000; } else if (n == 2) { value_white[i - 3][j] = value_white[i - 3][j] + 1000; value_white[i + 1][j] = value_white[i + 1][j] + 1000; } } } else if (x == 4) { if (order == 1) { if (n == 1) { value_black[i - 2][j] = value_black[i - 2][j] + 50; value_black[i + 1][j] = value_black[i + 1][j] + 50; } else if (n == 2) { value_white[i - 2][j] = value_white[i - 2][j] + 50; value_white[i + 1][j] = value_white[i + 1][j] + 50; } } else if (order == 2) { if (n == 1) { value_black[i - 2][j] = value_black[i - 2][j] + 50; value_black[i + 1][j] = value_black[i + 1][j] + 50; } else if (n == 2) { value_white[i - 2][j] = value_white[i - 2][j] + 50; value_white[i + 1][j] = value_white[i + 1][j] + 50; } } } else if (x == 5) { if (order == 1) { if (n == 1) { value_black[i - 3][j] = value_black[i - 3][j] + 500; value_black[i + 1][j] = value_black[i + 1][j] + 500; } else if (n == 2) { value_white[i - 3][j] = value_white[i - 3][j] + 500; value_white[i + 1][j] = value_white[i + 1][j] + 500; } } else if (order == 2) { if (n == 1) { value_black[i - 3][j] = value_black[i - 3][j] + 500; value_black[i + 1][j] = value_black[i + 1][j] + 500; } else if (n == 2) { value_white[i - 3][j] = value_white[i - 3][j] + 500; value_white[i + 1][j] = value_white[i + 1][j] + 500; } } } else if (x == 6) { if (order == 1) { if (n == 1) { value_black[i - 4][j] = value_black[i - 4][j] + 2000; value_black[i + 1][j] = value_black[i + 1][j] + 2000; } else if (n == 2) { value_white[i - 4][j] = value_white[i - 4][j] + 2000; value_white[i + 1][j] = value_white[i + 1][j] + 2000; } } else if (order == 2) { if (n == 1) { value_black[i - 4][j] = value_black[i - 4][j] + 2000; value_black[i + 1][j] = value_black[i + 1][j] + 2000; } else if (n == 2) { value_white[i - 4][j] = value_white[i - 4][j] + 2000; value_white[i + 1][j] = value_white[i + 1][j] + 2000; } } } else if (x == 0) { if (order == 1) { if (n == 1) { value_black[i - 4][j] = value_black[i - 4][j] + 10000; value_black[i + 1][j] = value_black[i + 1][j] + 10000; } else if (n == 2) { value_white[i - 4][j] = value_white[i - 4][j] + 10000; value_white[i + 1][j] = value_white[i + 1][j] + 10000; } } else if (order == 2) { if (n == 1) { value_black[i - 4][j] = value_black[i - 4][j] + 10000; value_black[i + 1][j] = value_black[i + 1][j] + 10000; } else if (n == 2) { value_white[i - 4][j] = value_white[i - 4][j] + 10000; value_white[i + 1][j] = value_white[i + 1][j] + 10000; } } } } void Renew_Value_leftup(int i, int j, int x, int n) //更新价值表:左上往右下,(n:1为黑,2为白) { int i1, j1; for (i1 = 0; i1 < 17; i1++) { for (j1 = 0; j1 < 17; j1++) { //如果已经有棋子了,不改变该点的值 if (a[i1][j1] != 0) { value_black[i1][j1] = -10000; value_white[i1][j1] = -10000; } } } //下棋的话,周边都要加10 if (order == 1) { //人为先手,人是黑棋,机器白棋 if (n == 1) { //黑棋的价值表 value_black[i + 1][j] = value_black[i + 1][j] + 10; value_black[i - 1][j] = value_black[i - 1][j] + 10; value_black[i][j + 1] = value_black[i][j + 1] + 10; value_black[i][j - 1] = value_black[i][j - 1] + 10; value_black[i + 1][j + 1] = value_black[i + 1][j + 1] + 10; value_black[i - 1][j - 1] = value_black[i - 1][j - 1] + 10; value_black[i + 1][j - 1] = value_black[i + 1][j - 1] + 10; value_black[i - 1][j + 1] = value_black[i - 1][j + 1] + 10; } else if (n == 2) { value_white[i + 1][j] = value_white[i + 1][j] + 10; value_white[i - 1][j] = value_white[i - 1][j] + 10; value_white[i][j + 1] = value_white[i][j + 1] + 10; value_white[i][j - 1] = value_white[i][j - 1] + 10; value_white[i + 1][j + 1] = value_white[i + 1][j + 1] + 10; value_white[i - 1][j - 1] = value_white[i - 1][j - 1] + 10; value_white[i + 1][j - 1] = value_white[i + 1][j - 1] + 10; value_white[i - 1][j + 1] = value_white[i - 1][j + 1] + 10; } } else if (order == 2) { //后手 if (n == 1) { value_black[i + 1][j] = value_black[i + 1][j] + 10; value_black[i - 1][j] = value_black[i - 1][j] + 10; value_black[i][j + 1] = value_black[i][j + 1] + 10; value_black[i][j - 1] = value_black[i][j - 1] + 10; value_black[i + 1][j + 1] = value_black[i + 1][j + 1] + 10; value_black[i - 1][j - 1] = value_black[i - 1][j - 1] + 10; value_black[i + 1][j - 1] = value_black[i + 1][j - 1] + 10; value_black[i - 1][j + 1] = value_black[i - 1][j + 1] + 10; } else if (n == 2) { value_white[i + 1][j] = value_white[i + 1][j] + 10; value_white[i - 1][j] = value_white[i - 1][j] + 10; value_white[i][j + 1] = value_white[i][j + 1] + 10; value_white[i][j - 1] = value_white[i][j - 1] + 10; value_white[i + 1][j + 1] = value_white[i + 1][j + 1] + 10; value_white[i - 1][j - 1] = value_white[i - 1][j - 1] + 10; value_white[i + 1][j - 1] = value_white[i + 1][j - 1] + 10; value_white[i - 1][j + 1] = value_white[i - 1][j + 1] + 10; } } if (x == 2) { if (order == 1) { //人为先手,人是黑棋,机器白棋 if (n == 1) { value_black[i - 2][j - 2] = value_black[i - 2][j - 2] + 100; value_black[i + 1][j + 1] = value_black[i + 1][j + 1] + 100; } else if (n == 2) { value_white[i - 2][j - 2] = value_white[i - 2][j - 2] + 100; value_white[i + 1][j + 1] = value_white[i + 1][j + 1] + 100; } } else if (order == 2) { if (n == 1) { value_black[i - 2][j - 2] = value_black[i - 2][j - 2] + 100; value_black[i + 1][j + 1] = value_black[i + 1][j + 1] + 100; } else if (n == 2) { value_white[i - 2][j - 2] = value_white[i - 2][j - 2] + 100; value_white[i + 1][j + 1] = value_white[i + 1][j + 1] + 100; } } } else if (x == 3) { if (order == 1) { if (n == 1) { value_black[i - 3][j - 3] = value_black[i - 3][j - 3] + 1000; value_black[i + 1][j + 1] = value_black[i + 1][j + 1] + 1000; } else if (n == 2) { value_white[i - 3][j - 3] = value_white[i - 3][j - 3] + 1000; value_white[i + 1][j + 1] = value_white[i + 1][j + 1] + 1000; } } else if (order == 2) { if (n == 1) { value_black[i - 3][j - 3] = value_black[i - 3][j - 3] + 1000; value_black[i + 1][j + 1] = value_black[i + 1][j + 1] + 1000; } else if (n == 2) { value_white[i - 3][j - 3] = value_white[i - 3][j - 3] + 1000; value_white[i + 1][j + 1] = value_white[i + 1][j + 1] + 1000; } } } else if (x == 4) { if (order == 1) { if (n == 1) { value_black[i - 2][j - 2] = value_black[i - 2][j - 2] + 50; value_black[i + 1][j + 1] = value_black[i + 1][j + 1] + 50; } else if (n == 2) { value_white[i - 2][j - 2] = value_white[i - 2][j - 2] + 50; value_white[i + 1][j + 1] = value_white[i + 1][j + 1] + 50; } } else if (order == 2) { if (n == 1) { value_black[i - 2][j - 2] = value_black[i - 2][j - 2] + 50; value_black[i + 1][j + 1] = value_black[i + 1][j + 1] + 50; } else if (n == 2) { value_white[i - 2][j - 2] = value_white[i - 2][j - 2] + 50; value_white[i + 1][j + 1] = value_white[i + 1][j + 1] + 50; } } } else if (x == 5) { if (order == 1) { if (n == 1) { value_black[i - 3][j - 3] = value_black[i - 3][j - 3] + 500; value_black[i + 1][j + 1] = value_black[i + 1][j + 1] + 500; } else if (n == 2) { value_white[i - 3][j - 3] = value_white[i - 3][j - 3] + 500; value_white[i + 1][j + 1] = value_white[i + 1][j + 1] + 500; } } else if (order == 2) { if (n == 1) { value_black[i - 3][j - 3] = value_black[i - 3][j - 3] + 500; value_black[i + 1][j + 1] = value_black[i + 1][j + 1] + 500; } else if (n == 2) { value_white[i - 3][j - 3] = value_white[i - 3][j - 3] + 500; value_white[i + 1][j + 1] = value_white[i + 1][j + 1] + 500; } } } else if (x == 6) { if (order == 1) { if (n == 1) { value_black[i - 4][j - 4] = value_black[i - 4][j - 4] + 2000; value_black[i + 1][j + 1] = value_black[i + 1][j + 1] + 2000; } else if (n == 2) { value_white[i - 4][j - 4] = value_white[i - 4][j - 4] + 2000; value_white[i + 1][j + 1] = value_white[i + 1][j + 1] + 2000; } } else if (order == 2) { if (n == 1) { value_black[i - 4][j - 4] = value_black[i - 4][j - 4] + 2000; value_black[i + 1][j + 1] = value_black[i + 1][j + 1] + 2000; } else if (n == 2) { value_white[i - 4][j - 4] = value_white[i - 4][j - 4] + 2000; value_white[i + 1][j + 1] = value_white[i + 1][j + 1] + 2000; } } } else if (x == 0) { if (order == 1) { if (n == 1) { value_black[i - 4][j - 4] = value_black[i - 4][j - 4] + 10000; value_black[i + 1][j + 1] = value_black[i + 1][j + 1] + 10000; } else if (n == 2) { value_white[i - 4][j - 4] = value_white[i - 4][j - 4] + 10000; value_white[i + 1][j + 1] = value_white[i + 1][j + 1] + 10000; } } else if (order == 2) { if (n == 1) { value_black[i - 4][j - 4] = value_black[i - 4][j - 4] + 10000; value_black[i + 1][j + 1] = value_black[i + 1][j + 1] + 10000; } else if (n == 2) { value_white[i - 4][j - 4] = value_white[i - 4][j - 4] + 10000; value_white[i + 1][j + 1] = value_white[i + 1][j + 1] + 10000; } } } } void Renew_Value_heng(int i, int j, int x, int n) //更新价值表:水平方向(n:1为黑,2为白) { int i1, j1; for (i1 = 0; i1 < 17; i1++) { for (j1 = 0; j1 < 17; j1++) { //如果已经有棋子了,不改变该点的值 if (a[i1][j1] != 0) { value_black[i1][j1] = -10000; value_white[i1][j1] = -10000; } } } //下棋的话,周边都要加10 if (order == 1) { //人为先手,人是黑棋,机器白棋 if (n == 1) { //黑棋的价值表 value_black[i + 1][j] = value_black[i + 1][j] + 10; value_black[i - 1][j] = value_black[i - 1][j] + 10; value_black[i][j + 1] = value_black[i][j + 1] + 10; value_black[i][j - 1] = value_black[i][j - 1] + 10; value_black[i + 1][j + 1] = value_black[i + 1][j + 1] + 10; value_black[i - 1][j - 1] = value_black[i - 1][j - 1] + 10; value_black[i + 1][j - 1] = value_black[i + 1][j - 1] + 10; value_black[i - 1][j + 1] = value_black[i - 1][j + 1] + 10; } else if (n == 2) { value_white[i + 1][j] = value_white[i + 1][j] + 10; value_white[i - 1][j] = value_white[i - 1][j] + 10; value_white[i][j + 1] = value_white[i][j + 1] + 10; value_white[i][j - 1] = value_white[i][j - 1] + 10; value_white[i + 1][j + 1] = value_white[i + 1][j + 1] + 10; value_white[i - 1][j - 1] = value_white[i - 1][j - 1] + 10; value_white[i + 1][j - 1] = value_white[i + 1][j - 1] + 10; value_white[i - 1][j + 1] = value_white[i - 1][j + 1] + 10; } } else if (order == 2) { //后手 if (n == 1) { value_black[i + 1][j] = value_black[i + 1][j] + 10; value_black[i - 1][j] = value_black[i - 1][j] + 10; value_black[i][j + 1] = value_black[i][j + 1] + 10; value_black[i][j - 1] = value_black[i][j - 1] + 10; value_black[i + 1][j + 1] = value_black[i + 1][j + 1] + 10; value_black[i - 1][j - 1] = value_black[i - 1][j - 1] + 10; value_black[i + 1][j - 1] = value_black[i + 1][j - 1] + 10; value_black[i - 1][j + 1] = value_black[i - 1][j + 1] + 10; } else if (n == 2) { value_white[i + 1][j] = value_white[i + 1][j] + 10; value_white[i - 1][j] = value_white[i - 1][j] + 10; value_white[i][j + 1] = value_white[i][j + 1] + 10; value_white[i][j - 1] = value_white[i][j - 1] + 10; value_white[i + 1][j + 1] = value_white[i + 1][j + 1] + 10; value_white[i - 1][j - 1] = value_white[i - 1][j - 1] + 10; value_white[i + 1][j - 1] = value_white[i + 1][j - 1] + 10; value_white[i - 1][j + 1] = value_white[i - 1][j + 1] + 10; } } if (x == 2) { if (order == 1) { //人为先手,人是黑棋,机器白棋 if (n == 1) { value_black[i][j - 2] = value_black[i][j - 2] + 100; value_black[i][j + 1] = value_black[i][j + 1] + 100; } else if (n == 2) { value_white[i][j - 2] = value_white[i][j - 2] + 100; value_white[i][j + 1] = value_white[i][j + 1] + 100; } } else if (order == 2) { if (n == 1) { value_black[i][j - 2] = value_black[i][j - 2] + 100; value_black[i][j + 1] = value_black[i][j + 1] + 100; } else if (n == 2) { value_white[i][j - 2] = value_white[i][j - 2] + 100; value_white[i][j + 1] = value_white[i][j + 1] + 100; } } } else if (x == 3) { if (order == 1) { if (n == 1) { value_black[i][j - 3] = value_black[i][j - 3] + 1000; value_black[i][j + 1] = value_black[i][j + 1] + 1000; } else if (n == 2) { value_white[i][j - 3] = value_white[i][j - 3] + 1000; value_white[i][j + 1] = value_white[i][j + 1] + 1000; } } else if (order == 2) { if (n == 1) { value_black[i][j - 3] = value_black[i][j - 3] + 1000; value_black[i][j + 1] = value_black[i][j + 1] + 1000; } else if (n == 2) { value_white[i][j - 3] = value_white[i][j - 3] + 1000; value_white[i][j + 1] = value_white[i][j + 1] + 1000; } } } else if (x == 4) { if (order == 1) { if (n == 1) { value_black[i][j - 2] = value_black[i][j - 2] + 50; value_black[i][j + 1] = value_black[i][j + 1] + 50; } else if (n == 2) { value_white[i][j - 2] = value_white[i][j - 2] + 50; value_white[i][j + 1] = value_white[i][j + 1] + 50; } } else if (order == 2) { if (n == 1) { value_black[i][j - 2] = value_black[i][j - 2] + 50; value_black[i][j + 1] = value_black[i][j + 1] + 50; } else if (n == 2) { value_white[i][j - 2] = value_white[i][j - 2] + 50; value_white[i][j + 1] = value_white[i][j + 1] + 50; } } } else if (x == 5) { if (order == 1) { if (n == 1) { value_black[i][j - 3] = value_black[i][j - 3] + 500; value_black[i][j + 1] = value_black[i][j + 1] + 500; } else if (n == 2) { value_white[i][j - 3] = value_white[i][j - 3] + 500; value_white[i][j + 1] = value_white[i][j + 1] + 500; } } else if (order == 2) { if (n == 1) { value_black[i][j - 3] = value_black[i][j - 3] + 500; value_black[i][j + 1] = value_black[i][j + 1] + 500; } else if (n == 2) { value_white[i][j - 3] = value_white[i][j - 3] + 500; value_white[i][j + 1] = value_white[i][j + 1] + 500; } } } else if (x == 6) { if (order == 1) { if (n == 1) { value_black[i][j - 4] = value_black[i][j - 4] + 2000; value_black[i][j + 1] = value_black[i][j + 1] + 2000; } else if (n == 2) { value_white[i][j - 4] = value_white[i][j - 4] + 2000; value_white[i][j + 1] = value_white[i][j + 1] + 2000; } } else if (order == 2) { if (n == 1) { value_black[i][j - 4] = value_black[i][j - 4] + 2000; value_black[i][j + 1] = value_black[i][j + 1] + 2000; } else if (n == 2) { value_white[i][j - 4] = value_white[i][j - 4] + 2000; value_white[i][j + 1] = value_white[i][j + 1] + 2000; } } } else if (x == 0) { if (order == 1) { if (n == 1) { value_black[i][j - 4] = value_black[i][j - 4] + 10000; value_black[i][j + 1] = value_black[i][j + 1] + 10000; } else if (n == 2) { value_white[i][j - 4] = value_white[i][j - 4] + 10000; value_white[i][j + 1] = value_white[i][j + 1] + 10000; } } else if (order == 2) { if (n == 1) { value_black[i][j - 4] = value_black[i][j - 4] + 10000; value_black[i][j + 1] = value_black[i][j + 1] + 10000; } else if (n == 2) { value_white[i][j - 4] = value_white[i][j - 4] + 10000; value_white[i][j + 1] = value_white[i][j + 1] + 10000; } } } } void Renew_Value_rightup(int i, int j, int x, int n) //更新价值表:右上到左下(n:1为黑,2为白) { int i1, j1; for (i1 = 0; i1 < 17; i1++) { for (j1 = 0; j1 < 17; j1++) { //如果已经有棋子了,不改变该点的值 if (a[i1][j1] != 0) { value_black[i1][j1] = -10000; value_white[i1][j1] = -10000; } } } //下棋的话,周边都要加10 if (order == 1) { //人为先手,人是黑棋,机器白棋 if (n == 1) { //黑棋的价值表 value_black[i + 1][j] = value_black[i + 1][j] + 10; value_black[i - 1][j] = value_black[i - 1][j] + 10; value_black[i][j + 1] = value_black[i][j + 1] + 10; value_black[i][j - 1] = value_black[i][j - 1] + 10; value_black[i + 1][j + 1] = value_black[i + 1][j + 1] + 10; value_black[i - 1][j - 1] = value_black[i - 1][j - 1] + 10; value_black[i + 1][j - 1] = value_black[i + 1][j - 1] + 10; value_black[i - 1][j + 1] = value_black[i - 1][j + 1] + 10; } else if (n == 2) { value_white[i + 1][j] = value_white[i + 1][j] + 10; value_white[i - 1][j] = value_white[i - 1][j] + 10; value_white[i][j + 1] = value_white[i][j + 1] + 10; value_white[i][j - 1] = value_white[i][j - 1] + 10; value_white[i + 1][j + 1] = value_white[i + 1][j + 1] + 10; value_white[i - 1][j - 1] = value_white[i - 1][j - 1] + 10; value_white[i + 1][j - 1] = value_white[i + 1][j - 1] + 10; value_white[i - 1][j + 1] = value_white[i - 1][j + 1] + 10; } } else if (order == 2) { //后手 if (n == 1) { value_black[i + 1][j] = value_black[i + 1][j] + 10; value_black[i - 1][j] = value_black[i - 1][j] + 10; value_black[i][j + 1] = value_black[i][j + 1] + 10; value_black[i][j - 1] = value_black[i][j - 1] + 10; value_black[i + 1][j + 1] = value_black[i + 1][j + 1] + 10; value_black[i - 1][j - 1] = value_black[i - 1][j - 1] + 10; value_black[i + 1][j - 1] = value_black[i + 1][j - 1] + 10; value_black[i - 1][j + 1] = value_black[i - 1][j + 1] + 10; } else if (n == 2) { value_white[i + 1][j] = value_white[i + 1][j] + 10; value_white[i - 1][j] = value_white[i - 1][j] + 10; value_white[i][j + 1] = value_white[i][j + 1] + 10; value_white[i][j - 1] = value_white[i][j - 1] + 10; value_white[i + 1][j + 1] = value_white[i + 1][j + 1] + 10; value_white[i - 1][j - 1] = value_white[i - 1][j - 1] + 10; value_white[i + 1][j - 1] = value_white[i + 1][j - 1] + 10; value_white[i - 1][j + 1] = value_white[i - 1][j + 1] + 10; } } if (x == 2) { if (order == 1) { //人为先手,人是黑棋,机器白棋 if (n == 1) { value_black[i - 2][j + 2] = value_black[i - 2][j + 2] + 100; value_black[i + 1][j - 1] = value_black[i + 1][j - 1] + 100; } else if (n == 2) { value_white[i - 2][j + 2] = value_white[i - 2][j + 2] + 100; value_white[i + 1][j - 1] = value_white[i + 1][j - 1] + 100; } } else if (order == 2) { if (n == 1) { value_black[i - 2][j + 2] = value_black[i - 2][j + 2] + 100; value_black[i + 1][j - 1] = value_black[i + 1][j - 1] + 100; } else if (n == 2) { value_white[i - 2][j + 2] = value_white[i - 2][j + 2] + 100; value_white[i + 1][j - 1] = value_white[i + 1][j - 1] + 100; } } } else if (x == 3) { if (order == 1) { if (n == 1) { value_black[i - 3][j + 3] = value_black[i - 3][j + 3] + 1000; value_black[i + 1][j - 1] = value_black[i + 1][j - 1] + 1000; } else if (n == 2) { value_white[i - 3][j + 3] = value_white[i - 3][j + 3] + 1000; value_white[i + 1][j - 1] = value_white[i + 1][j - 1] + 1000; } } else if (order == 2) { if (n == 1) { value_black[i - 3][j + 3] = value_black[i - 3][j + 3] + 1000; value_black[i + 1][j - 1] = value_black[i + 1][j - 1] + 1000; } else if (n == 2) { value_white[i - 3][j + 3] = value_white[i - 3][j + 3] + 1000; value_white[i + 1][j - 1] = value_white[i + 1][j - 1] + 1000; } } } else if (x == 4) { if (order == 1) { if (n == 1) { value_black[i - 2][j + 2] = value_black[i - 2][j + 2] + 50; value_black[i + 1][j - 1] = value_black[i + 1][j - 1] + 50; } else if (n == 2) { value_white[i - 2][j + 2] = value_white[i - 2][j + 2] + 50; value_white[i + 1][j - 1] = value_white[i + 1][j - 1] + 50; } } else if (order == 2) { if (n == 1) { value_black[i - 2][j + 2] = value_black[i - 2][j + 2] + 50; value_black[i + 1][j - 1] = value_black[i + 1][j - 1] + 50; } else if (n == 2) { value_white[i - 2][j + 2] = value_white[i - 2][j + 2] + 50; value_white[i + 1][j - 1] = value_white[i + 1][j - 1] + 50; } } } else if (x == 5) { if (order == 1) { if (n == 1) { value_black[i - 3][j + 3] = value_black[i - 3][j + 3] + 500; value_black[i + 1][j - 1] = value_black[i + 1][j - 1] + 500; } else if (n == 2) { value_white[i - 3][j + 3] = value_white[i - 3][j + 3] + 500; value_white[i + 1][j - 1] = value_white[i + 1][j - 1] + 500; } } else if (order == 2) { if (n == 1) { value_black[i - 3][j + 3] = value_black[i - 3][j + 3] + 500; value_black[i + 1][j - 1] = value_black[i + 1][j - 1] + 500; } else if (n == 2) { value_white[i - 3][j + 3] = value_white[i - 3][j + 3] + 500; value_white[i + 1][j - 1] = value_white[i + 1][j - 1] + 500; } } } else if (x == 6) { if (order == 1) { if (n == 1) { value_black[i - 4][j + 4] = value_black[i - 4][j + 4] + 2000; value_black[i + 1][j - 1] = value_black[i + 1][j - 1] + 2000; } else if (n == 2) { value_white[i - 4][j + 4] = value_white[i - 4][j + 4] + 2000; value_white[i + 1][j - 1] = value_white[i + 1][j - 1] + 2000; } } else if (order == 2) { if (n == 1) { value_black[i - 4][j + 4] = value_black[i - 4][j + 4] + 2000; value_black[i + 1][j - 1] = value_black[i + 1][j - 1] + 2000; } else if (n == 2) { value_white[i - 4][j + 4] = value_white[i - 4][j + 4] + 2000; value_white[i + 1][j - 1] = value_white[i + 1][j - 1] + 2000; } } } else if (x == 0) { if (order == 1) { if (n == 1) { value_black[i - 4][j + 4] = value_black[i - 4][j + 4] + 10000; value_black[i + 1][j - 1] = value_black[i + 1][j - 1] + 10000; } else if (n == 2) { value_white[i - 4][j + 4] = value_white[i - 4][j + 4] + 10000; value_white[i + 1][j - 1] = value_white[i + 1][j - 1] + 10000; } } else if (order == 2) { if (n == 1) { value_black[i - 4][j + 4] = value_black[i - 4][j + 4] + 10000; value_black[i + 1][j - 1] = value_black[i + 1][j - 1] + 10000; } else if (n == 2) { value_white[i - 4][j + 4] = value_white[i - 4][j + 4] + 10000; value_white[i + 1][j - 1] = value_white[i + 1][j - 1] + 10000; } } } } void Medium() //中级难度 { int i, j; int ii1_b, jj1_b, ii1_w, jj1_w; int ii1 = 0, jj1 = 0; int max = 0; int max_w = 0, max_b = 0; for (i = 0; i < 17; i++) { for (j = 0; j < 17; j++) { if (a[i][j] != 0) { //下过棋子的位置不再下棋子。 continue; } else { if (value_white[i][j] > max_w) { max_w = value_white[i][j]; ii1_w = i; jj1_w = j; } if (value_black[i][j] > max_b) { max_b = value_black[i][j]; ii1_b = i; jj1_b = j; } } } } if (max_w >= max_b) { //根据两个价值表比较进攻与防守哪个更有利。 max = max_w; ii1 = ii1_w; jj1 = jj1_w; } else { max = max_b; ii1 = ii1_b; jj1 = jj1_b; } if (order == 1 && flag == 1) { //机器白子 a[ii1][jj1] = 2; //下白子 setfillcolor(WHITE); solidcircle((jj1 * 25) + 100, (ii1 * 25) + 100, 10); flag = 0; setfillcolor(BLACK); //记录下棋者 solidcircle(215, 80, 10); if (Rules(ii1, jj1, 2)) { //白子胜 MessageBox(chunchun, _T("白棋子胜利"), _T("淑女"), MB_OK); flag = 2; //使不能再下棋 Newa(); //初始化 ChessBoard(); //再次调用棋盘 } Judge_ML(ii1, jj1, 2); //判断类型,再调用函数更新价值表 e = (jj1 * 25) + 100; f = (ii1 * 25) + 100; } else if (order == 2 && flag == 0) { a[ii1][jj1] = 1; //下黑子 setfillcolor(BLACK); solidcircle((jj1 * 25) + 100, (ii1 * 25) + 100, 10); flag = 1; setfillcolor(WHITE); //记录下棋者 solidcircle(215, 80, 10); if (Rules(ii1, jj1, 1)) { //黑子胜 MessageBox(chunchun, _T("黑棋子胜利"), _T("淑女"), MB_OK); flag = 2; //使不能再下棋 Newa(); //初始化 ChessBoard(); //再次调用棋盘 } Judge_ML(ii1, jj1, 1); //判断类型,再调用函数更新价值表 c = (jj1 * 25) + 100; d = (ii1 * 25) + 100; } } void Medium_J() //中级难度 { printf("中等函数\n"); int i, j; int ii1_b, jj1_b, ii1_w, jj1_w; int ii1 = 0, jj1 = 0; int max = 0; int max_w = 0, max_b = 0; for (i = 0; i < 17; i++) { for (j = 0; j < 17; j++) { if (a[i][j] != 0) { //下过棋子的位置不再下棋子。 continue; } else { if (value_white[i][j] > max_w) { max_w = value_white[i][j]; ii1_w = i; jj1_w = j; } if (value_black[i][j] > max_b) { max_b = value_black[i][j]; ii1_b = i; jj1_b = j; } } } } if (max_w >= max_b) { //根据两个价值表比较进攻与防守哪个更有利。 max = max_w; ii1 = ii1_w; jj1 = jj1_w; } else { max = max_b; ii1 = ii1_b; jj1 = jj1_b; } if (flag == 1) { //机器白子 a[ii1][jj1] = 2; //下白子 setfillcolor(WHITE); solidcircle((jj1 * 25) + 100, (ii1 * 25) + 100, 10); flag = 0; setfillcolor(BLACK); //记录下棋者 solidcircle(215, 80, 10); Judge_ML(ii1, jj1, 2); //判断类型,再调用函数更新价值表 if (Rules(ii1, jj1, 2)) { //白子胜 MessageBox(chunchun, _T("白棋子胜利"), _T("淑女"), MB_OK); flag = 2; //使不能再下棋 Newa(); //初始化 ChessBoard(); //再次调用棋盘 } } else if (flag == 0) { a[ii1][jj1] = 1; //下黑子 setfillcolor(BLACK); solidcircle((jj1 * 25) + 100, (ii1 * 25) + 100, 10); flag = 1; setfillcolor(WHITE); //记录下棋者 solidcircle(215, 80, 10); Judge_ML(ii1, jj1, 1); //判断类型,再调用函数更新价值表 if (Rules(ii1, jj1, 1)) { //黑子胜 MessageBox(chunchun, _T("黑棋子胜利"), _T("淑女"), MB_OK); flag = 2; //使不能再下棋 Newa(); //初始化 ChessBoard(); //再次调用棋盘 } } } void Low_J() //初级难度算法,//传入先后手,0为先手,1为后手 { int numi, numj; while (1) { while (1) { numi = rand(); if (numi >= 0 && numi < 17) { break; } } while (1) { numj = rand(); if (numj >= 0 && numj < 17) { break; } } if (a[numj][numi] == 0) { break; } } if (flag == 1) { //人先下棋,人下黑子,机器白子 setfillcolor(WHITE); solidcircle((numi * 25) + 100, (numj * 25) + 100, 10); //在指定区域里面画实心圆 a[numj][numi] = 2; value_black[numj][numi] = 0; value_white[numj][numi] = 0; flag = 0; setfillcolor(BLACK); //记录下棋者 solidcircle(215, 80, 10); if (Rules(numj, numi, 2)) { //白子胜 MessageBox(chunchun, _T("白棋子胜利"), _T("淑女"), MB_OK); flag = 2; //使不能再下棋 Newa(); //初始化 ChessBoard(); //再次调用棋盘 } } else if (flag == 0) { //机器先下棋,机器下黑子,人下白子 setfillcolor(BLACK); solidcircle((numi * 25) + 100, (numj * 25) + 100, 10); a[numj][numi] = 1; value_black[numj][numi] = 0; value_white[numj][numi] = 0; flag = 1; setfillcolor(WHITE); //记录下棋者 solidcircle(215, 80, 10); if (Rules(numj, numi, 1)) { //黑子胜 MessageBox(chunchun, _T("白棋子胜利"), _T("淑女"), MB_OK); flag = 2; //使不能再下棋 Newa(); //初始化 ChessBoard(); //再次调用棋盘 } } }
六、最后,以上插入的图片以及音乐,大家随意发挥,我就不给了,还有一个美中不足就是,人机悔棋的一个小缺陷就是没能返回上一个对局的价值表,有兴趣的可以改进一下。尝试着做一个小游戏会在某种程度上对自己的能力起到一定程度的帮助,本期分享就到这里啦~
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。