日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人工智能 > ChatGpt >内容正文

ChatGpt

c语言翻转棋ai算法,黑白棋游戏(也叫翻转棋)(AI 版)

發(fā)布時(shí)間:2023/12/14 ChatGpt 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c语言翻转棋ai算法,黑白棋游戏(也叫翻转棋)(AI 版) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

黑白棋(也叫翻轉(zhuǎn)棋)的棋盤是一個(gè)有8*8方格的棋盤。下棋時(shí)將棋下在空格中間,而不是像圍棋一樣下在交叉點(diǎn)上。開始時(shí)在棋盤正中有兩白兩黑四個(gè)棋子交叉放置,黑棋總是先下子。

下子的方法:把自己顏色的棋子放在棋盤的空格上,而當(dāng)自己放下的棋子在橫、豎、斜八個(gè)方向內(nèi)有一個(gè)自己的棋子,則被夾在中間的全部翻轉(zhuǎn)會(huì)成為自己的棋子。并且,只有在可以翻轉(zhuǎn)棋子的地方才可以下子。

引用了QQ游戲黑白棋的圖片。

游戲運(yùn)行截圖如下:

游戲中使用了大量圖片、音樂、資源文件,<

href="http://www.easyx.cn/Files/samples/201203/BlackWhiteChessAI.zip"

style="text-decoration: none; color: rgb(0, 0,

127);">點(diǎn)這里下載該游戲的完整 VC 項(xiàng)目包>。

游戲代碼如下(相關(guān)圖片資源請下載完整 VC 項(xiàng)目包):

///

// 程序名稱:黑白棋AI版

// 編譯環(huán)境:Visual C++ 2010/6.0,EasyX_v20120304(beta)

// 程序編寫:自然向日葵 1164359890@qq.com

// 最后更新:2012-3-24

//

//說明:人機(jī)對戰(zhàn)版

#include // EasyX_2011驚蟄版

#include

#include

#pragma comment(lib, "Winmm.lib")

#define T(c) ((c == 'B') ? 'W' : 'B')

using namespace std;

const int difficult = 6;// 難度

const int move[8][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1},

{-1, -1}, {1, -1}, {1, 1}, {-1, 1}};

// 八個(gè)方向擴(kuò)展

char map[8][8];// 棋盤

IMAGE img[5];// 保存圖片

int black, white;// 雙方的棋子數(shù)

int X, Y;// 白棋的下子點(diǎn)

void load(void);// 加載素材

void print(void);// 畫棋盤

void draw(int, int, char);// 下當(dāng)前子

int judge(int, int, char);// 判斷當(dāng)前是否可以落下

bool baidu(char);// 判斷是否有棋可吃

bool quit(char);// 判斷是否有棋存活

bool ask(void);// 彈出對話框

int D(char, int);// 動(dòng)態(tài)規(guī)劃

void play(void);// 游戲過程

void load(void)// 加載素材

{

// 加載圖片

loadimage(&img[0], "圖片\\空位.bmp");

loadimage(&img[1], "圖片\\黑子.bmp");

loadimage(&img[2], "圖片\\白子.bmp");

loadimage(&img[3], "圖片\\黑子1.bmp");

loadimage(&img[4], "圖片\\白子1.bmp");

// 加載音樂

mciSendString("open 音樂\\背景音樂.wma", NULL, 0, NULL);

mciSendString("open 音樂\\和局.wma", NULL, 0, NULL);

mciSendString("open 音樂\\勝利.wma", NULL, 0, NULL);

mciSendString("open 音樂\\失敗.wma", NULL, 0, NULL);

mciSendString("open 音樂\\下子.wma", NULL, 0, NULL);

// 初始化棋盤

initgraph(340, 340);

IMAGE qipan;

loadimage(&qipan, "圖片\\棋盤.bmp");

putimage(0, 0, &qipan);

setorigin(26, 26);

SetWindowText(GetHWnd(), "黑白棋AI版");

}

void print(void)// 畫棋盤

{

int x, y;

black = white = 0;

for(x = 0; x < 8; x++)

for(y = 0; y < 8; y++)

switch(map[x][y])

{

case 0:

putimage(37 * y, 37 * x, &img[0]);

break;

case 'B':

putimage(37 * y, 37 * x, &img[1]);

black++;

break;

case 'W':

putimage(37 * y, 37 * x, &img[2]);

white++;

break;

}

}

void draw(int x, int y, char a)// 下當(dāng)前子

{

char b = T(a);// 敵方子

int i, x1, y1, x2, y2;

bool sign;

for (i = 0; i < 8; i++)

{

sign = false;

x1 = x + move[i][0];

y1 = y + move[i][1];

while (0 <= x1 && x1 < 8 && 0 <= y1 && y1 < 8 && map[x1][y1])

{

if(map[x1][y1] == b)

sign = true;

else

{

if(sign)

{

x1 -= move[i][0];

y1 -= move[i][1];

x2 = x + move[i][0];

y2 = y + move[i][1];

while (((x <= x2 && x2 <= x1) || (x1 <= x2 && x2 <= x)) && ((y <= y2 && y2 <= y1) || (y1 <= y2 && y2 <= y)))

{

map[x2][y2] = a;

x2 += move[i][0];

y2 += move[i][1];

}

}

break;

}

x1 += move[i][0];

y1 += move[i][1];

}

}

map[x][y] = a;

}

int judge(int x, int y, char a)// 判斷當(dāng)前是否可以落下,同draw函數(shù)

{

if(map[x][y])// 如果當(dāng)前不是空的返回0值

return 0;

char b = T(a);

int i, x1, y1;

int n = 0, sign;

for (i = 0; i < 8; i++)

{

sign = 0;

x1 = x + move[i][0];

y1 = y + move[i][1];

while (0 <= x1 && x1 < 8 && 0 <= y1 && y1 < 8 && map[x1][y1])

{

if(map[x1][y1] == b)

sign++;

else

{

n += sign;

break;

}

x1 += move[i][0];

y1 += move[i][1];

}

}

return n;// 返回可吃棋數(shù)

}

bool baidu(char c)// 判斷是否有棋可吃

{

int x, y;

for(x = 0; x < 8; x++)

for(y = 0; y < 8; y++)

if(judge(x, y, c))

return true;

return false;

}

bool quit(char c)// 判斷是否有棋存活

{

int x, y;

bool b = false, w = false;

for(x = 0; x < 8; x++)

for(y = 0; y < 8; y++)

{

if(map[x][y] == c)

return false;

}

return true;

}

bool ask(void)// 彈出對話框

{

HWND wnd = GetHWnd();

int key;

char str[50];

ostrstream strout(str, 50);

strout <

if (black == white)

strout <

else if(black > white)

strout <

else

strout <

strout <

if(black == white)

key = MessageBox(wnd, str, "和局", MB_YESNO | MB_ICONQUESTION);

else if(black > white)

key = MessageBox(wnd, str, "黑勝", MB_YESNO | MB_ICONQUESTION);

else

key = MessageBox(wnd, str, "白勝", MB_YESNO | MB_ICONQUESTION);

if(key == IDYES)

return true;

else

return false;

}

int D(char c, int step)

{

// 判斷是否結(jié)束遞歸

if (step > difficult)// 約束步數(shù)之內(nèi)

return 0;

if (!baidu(c))

{

if (baidu(T(c)))

return -D(T(c), step);

else

return 0;

}

int i, j, max = 0, temp, x, y;

bool ans = false;

// 建立臨時(shí)數(shù)組

char **t = new char *[8];

for (i = 0; i < 8; i++)

t[i] = new char [8];

for (i = 0; i < 8; i++)

for (j = 0; j < 8; j++)

t[i][j] = map[i][j];

// 搜索解法

for (i = 0; i < 8; i++)

for (j = 0; j < 8; j++)

if (temp = judge(i, j, c))

{

draw(i, j, c);

temp -= D(T(c), step + 1);

if (temp > max || !ans)

{

max = temp;

x = i;

y = j;

ans = true;

}

for (int k = 0; k < 8; k++)

for (int l = 0; l < 8; l++)

map[k][l] = t[k][l];

}

// 撤銷空間

for (i = 0; i < 8; i++)

delete [] t[i];

delete [] t;

// 如果是第一步則標(biāo)識(shí)白棋下子點(diǎn)

if (step == 1)

{

X = x;

Y = y;

}

return max;// 返會(huì)最優(yōu)解

}

void play(void)// 游戲過程

{

MOUSEMSG m;

int x, y;

// 初始化棋子

for(x = 0; x < 8; x++)

for(y = 0; y < 8; y++)

map[x][y] = 0;

map[3][4] = map[4][3] = 'B';

map[3][3] = map[4][4] = 'W';

// 開始游戲

print();

mciSendString("play 音樂\\背景音樂.wma from 0 repeat", NULL, 0, NULL);

do

{

if (baidu('B'))// 如果玩家有下子位置

{

A:

while(true)

{

m = GetMouseMsg();// 獲取鼠標(biāo)消息

if(m.uMsg == WM_LBUTTONDOWN && m.x - 26 < 37 * 8 && m.y - 26 < 37 * 8)

// 如果左鍵點(diǎn)擊

break;

}

x = (m.y - 26) / 37;

y = (m.x - 26) / 37;

if(judge(x, y, 'B'))// 如果當(dāng)前位置有效

{

draw(x, y, 'B');// 下子

mciSendString("play 音樂\\下子.wma from 0", NULL, 0, NULL);

print();

putimage(37 * y, 37 * x, &img[3]);// 標(biāo)識(shí)下子點(diǎn)

}

else

goto A;

if (quit('W'))// 計(jì)算機(jī)是否失敗

break;

}

if (baidu('W'))// 如果計(jì)算機(jī)有下子位置

{

clock_t start;

start = clock();

D('W', 1);// 搜索解法

while (clock() - start < CLOCKS_PER_SEC);

draw(X, Y, 'W');

print();

mciSendString("play 音樂\\下子.wma from 0", NULL, 0, NULL);

putimage(37 * Y, 37 * X, &img[4]);// 標(biāo)識(shí)下子點(diǎn)

if (quit('B'))// 玩家是否失敗

break;

}

}while (baidu('B') || baidu ('W'));

// 播放慶祝音樂

mciSendString("stop 音樂\\背景音樂.wma", NULL, 0, NULL);

if (black > white)

mciSendString("play 音樂\\勝利.wma from 0", NULL, 0, NULL);

else if (black < white)

mciSendString("play 音樂\\失敗.wma from 0", NULL, 0, NULL);

else

mciSendString("play 音樂\\和局.wma from 0", NULL, 0, NULL);

}

// 主函數(shù)

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)

{

load();

do

{

play();

} while(ask());

// 關(guān)閉音樂

mciSendString("close 音樂\\背景音樂.wma", NULL, 0, NULL);

mciSendString("close 音樂\\和局.wma", NULL, 0, NULL);

mciSendString("close 音樂\\勝利.wma", NULL, 0, NULL);

mciSendString("close 音樂\\失敗.wma", NULL, 0, NULL);

mciSendString("close 音樂\\下子.wma", NULL, 0, NULL);

closegraph();

return 0;

}

總結(jié)

以上是生活随笔為你收集整理的c语言翻转棋ai算法,黑白棋游戏(也叫翻转棋)(AI 版)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。