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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

五子棋人机对弈

發(fā)布時(shí)間:2023/12/29 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 五子棋人机对弈 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

五子棋人機(jī)對弈(c/c++)

AI下棋算法部分

一個(gè)簡單的算法:計(jì)算機(jī)進(jìn)行計(jì)算尋找下棋位置

  • 預(yù)設(shè)(可改):
    ·第一步黑棋(AI)先行;

  • 需提供數(shù)據(jù):
    · 15*15的棋盤落子數(shù)據(jù):int board[15][15](0表示無子;1表示play1棋子;2表示play2棋子);
    · 下棋步數(shù)(非必須):int step。

  • #pragma once class AI { private:int record[15][15]; //計(jì)分?jǐn)?shù)組 public:void quantification(); //狀態(tài)判斷int Scoring(int state); //棋盤打分void Do(); //遍歷下棋 };void AI::quantification() {int play1, play2; //play1,2計(jì)數(shù);api_sleep(650); //延時(shí)//上至下掃描for (int a = 0; a < 15; a++) //判斷行成立;a行;b列{for (int b = 0; b < 15; b++)if (board[a][b] == 0){for (play1 = 0, play2 = 0; play1 + b + 1 < 15 && play2 + b + 1 < 15;)if (board[a][play1 + b + 1] == 1 && play2 == 0)play1++;else if (board[a][play2 + b + 1] == 2 && play1 == 0)play2++;else break;int max = play1 > play2 ? play1 : play2;if (board[a][max + b + 1] == 0)if ((max == 2 || max == 3) && board[a][max + b + 2] == board[a][b + 1])record[a][max + b + 1] += 50000;else if (max == 3 && board[a][b + 1] == board[a][b - 1])record[a][b] += 100000;elserecord[a][1 + b + max] += Scoring(max);record[a][b] += Scoring(max);}}for (int a = 0; a < 15; a++) //判斷列成立;a列;b行{for (int b = 0; b < 15; b++)if (board[b][a] == 0){for (play1 = 0, play2 = 0; play1 + b + 1 < 15 && play2 + b + 1 < 15;)//***改if (board[play1 + b + 1][a] == 1 && play2 == 0)play1++;else if (board[play2 + b + 1][a] == 2 && play1 == 0)play2++;else break;int max = play1 > play2 ? play1 : play2;if (board[max + b + 1][a] == 0)if ((max == 2 || max == 3) && board[max + b + 2][a] == board[b + 1][a])record[max + b + 1][a] += 50000;else if (max == 3 && board[b + 1][a] == board[b - 1][a])record[b][a] += 100000;elserecord[1 + b + max][a] += Scoring(max);record[b][a] += Scoring(max);}}for (int a = 0; a < 15; a++) //判斷主對角線成立;a行;b列{for (int b = 0; b < 15; b++)if (board[a][b] == 0){for (play1 = 0, play2 = 0; play1 + b + 1 < 15 && play2 + b + 1 < 15 && play1 + a + 1 < 15 && play2 + a + 1 < 15;)if (board[play1 + a + 1][play1 + b + 1] == 1 && play2 == 0)play1++;else if (board[play2 + a + 1][play2 + b + 1] == 2 && play1 == 0)play2++;else break;int max = play1 > play2 ? play1 : play2;if (board[max + a + 1][max + b + 1] == 0)if ((max == 2 || max == 3) && board[max + a + 2][max + b + 2] == board[a + 1][b + 1])record[max + a + 1][max + b + 1] += 50000;else if (max == 3 && board[a + 1][b + 1] == board[a - 1][b - 1])record[a][b] += 100000;elserecord[1 + a + max][1 + b + max] += Scoring(max);record[a][b] += Scoring(max);}}for (int a = 0; a < 15; a++) //判斷副對角線成立;a行;b列{for (int b = 14; b >= 0; b--)if (board[a][b] == 0){for (play1 = 0, play2 = 0; b - play1 - 1 > 0 && b - play2 - 1 > 0 && play1 + a + 1 < 15 && play2 + a + 1 < 15;)if (board[a + 1 + play1][b - 1 - play1] == 1 && play2 == 0)play1++;else if (board[a + 1 + play2][b - 1 - play2] == 2 && play1 == 0)play2++;else break;int max = play1 > play2 ? play1 : play2;if (board[1 + a + max][b - 1 - max] == 0)if ((max == 2 || max == 3) && board[2 + a + max][b - 2 - max] == board[a + 1][b - 1])record[1 + a + max][b - 1 - max] += 50000;else if (max == 3 && board[a + 1][b - 1] == board[a - 1][b + 1])record[a][b] += 100000;elserecord[1 + a + max][b - 1 - max] += Scoring(max);record[a][b] += Scoring(max);}}//下至上掃描for (int a = 14; a >= 0; a--) //判斷行成立;a行;b列{for (int b = 14; b >= 0; b--)if (board[a][b] == 0){for (play1 = 0, play2 = 0; b - play1 - 1 >= 0 && b - play2 - 1 >= 0;)if (board[a][b - play1 - 1] == 1 && play2 == 0)play1++;else if (board[a][b - play2 - 1] == 2 && play1 == 0)play2++;else break;int max = play1 > play2 ? play1 : play2;if (board[a][b - max - 1] == 0)if ((max == 2 || max == 3) && board[a][b - 2 - max] == board[a][b - 1])record[a][b - 1 - max] += 50000;else if (max == 3 && board[a][b - 1] == board[a][b + 1])record[a][b] += 100000;elserecord[a][b - 1 - max] += Scoring(max);record[a][b] += Scoring(max);}}for (int a = 14; a >= 0; a--) //判斷列成立;a列;b行{for (int b = 14; b >= 0; b--)if (board[b][a] == 0){for (play1 = 0, play2 = 0; b - play1 - 1 >= 0 && b - play2 - 1 >= 0;)if (board[b - play1 - 1][a] == 1 && play2 == 0)play1++;else if (board[b - play2 - 1][a] == 2 && play1 == 0)play2++;else break;int max = play1 > play2 ? play1 : play2;if (board[b - max - 1][a] == 0)if ((max == 2 || max == 3) && board[b - max - 2][a] == board[b - 1][a])record[b - max - 1][a] += 50000;else if (max == 3 && board[b - 1][a] == board[b + 1][a])record[a][b] += 100000;elserecord[b - 1 - max][a] += Scoring(max);record[b][a] += Scoring(max);}}for (int a = 14; a > 0; a--) //判斷主對角線成立;a行;b列{for (int b = 14; b > 0; b--)if (board[a][b] == 0){for (play1 = 0, play2 = 0; b - play1 - 1 > 0 && b - play2 - 1 > 0 && a - play1 - 1 > 0 && a - play2 - 1 > 0;)if (board[a - play1 - 1][b - play1 - 1] == 1 && play2 == 0)play1++;else if (board[a - play2 - 1][b - play2 - 1] == 2 && play1 == 0)play2++;else break;int max = play1 > play2 ? play1 : play2;if (board[a - max - 1][b - max - 1] == 0)if ((max == 2 || max == 3) && board[a - max - 2][b - max - 2] == board[a - 1][b - 1])record[a - max - 1][b - max - 1] += 50000;else if (max == 3 && board[a - 1][b - 1] == board[a + 1][b + 1])record[a][b] += 100000;elserecord[a - 1 - max][b - 1 - max] += Scoring(max);record[a][b] += Scoring(max);}}for (int a = 14; a >= 0; a--) //判斷副對角線成立;a行;b列{for (int b = 14; b >= 0; b--)if (board[a][b] == 0){for (play1 = 0, play2 = 0; b + play1 + 1 < 15 && b + play2 + 1 < 15 && a - play1 - 1 > 1 && a - play2 - 1 > 1;)if (board[a - play1 - 1][b + 1 + play1] == 1 && play2 == 0)play1++;else if (board[a - play2 - 1][b + 1 + play2] == 2 && play1 == 0)play2++;else break;int max = play1 > play2 ? play1 : play2;if (b + 1 + max <= 14 && board[a - max - 1][b + 1 + max] == 0)if ((max == 2 || max == 3) && board[a - max - 2][b + max + 2] == board[a - 1][b + 1])record[a - max - 1][b + 1 + max] += 50000;else if (max == 3 && board[a - 1][b + 1] == board[a + 1][b - 1])record[a][b] += 100000;elserecord[a - 1 - max][b + 1 + max] += Scoring(max);record[a][b] += Scoring(max);}} }int AI::Scoring(int state) //可修改 {switch (state){case 1:return 10;break;case 2:return 250;break;case 3:return 25001;break;case 4:return 1000000;break;case 5:return 1000000;break;default:return 0;break;} }void AI::Do() {for (int a = 0; a < 15; a++)for (int b = 0; b < 15; b++)record[a][b] = 0; //分?jǐn)?shù)清零if (step < 2){board[7][7] = 1;} //第一步下至棋盤中央else{quantification();int place[3] = { 0,0,0 };for (int a = 0; a < 15; a++)for (int b = 0; b < 15; b++)if (place[2] < record[a][b])place[2] = record[a][b], place[0] = a, place[1] = b;while (board[place[0]][place[1]] != 0) //為預(yù)防重復(fù)落棋預(yù)設(shè)(視情況可去掉)if (place[0] < 15)place[0]++;else if (place[1] < 15)place[1]++;else place[0]--;board[place[0]][place[1]] = 1} }

    總結(jié)

    以上是生活随笔為你收集整理的五子棋人机对弈的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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