C++簡(jiǎn)單五子棋的AI設(shè)計(jì)實(shí)現(xiàn)
更新時(shí)間:2018年09月28日 14:45:44 作者:black_kyatu
這篇文章主要為大家詳細(xì)介紹了C++簡(jiǎn)單五子棋的AI設(shè)計(jì)實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了C++五子棋的AI設(shè)計(jì)實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
設(shè)計(jì)思路:通過接口獲取信息來確定顏色,通過set_chess函數(shù)來確定落點(diǎn)。
- 對(duì)每個(gè)點(diǎn)位給出兩種顏色棋子的打分,分別存在兩個(gè)15*15的數(shù)組里,數(shù)組下標(biāo)代表點(diǎn)的位置。
- 確定最大值所在數(shù)組之后,遍歷該數(shù)組找出所有最大值對(duì)應(yīng)的位置,然后對(duì)這些位置統(tǒng)計(jì)另一種顏色的棋子的分?jǐn)?shù),再選取一次最大值,從而確定要落點(diǎn)的位置。
- 打分函數(shù)的設(shè)計(jì):在四個(gè)方向分別統(tǒng)計(jì)然后相加。對(duì)于某一個(gè)方向的分?jǐn)?shù)統(tǒng)計(jì),則分為正反兩個(gè)方向進(jìn)行,統(tǒng)計(jì)的時(shí)候如果有連成5個(gè)則直接返回一個(gè)最大值(最高分)。其他情況則按不同情況設(shè)置不同的權(quán)重,觸發(fā)結(jié)束某一個(gè)方向上的統(tǒng)計(jì)的事件如下:遇到異色棋子;空白格子超過兩個(gè);遇到棋盤邊界。其中遇到異色棋子和棋盤邊界均視為一邊被堵死,相比空白來說適當(dāng)減分,而1個(gè)空白相比于完全連續(xù)則應(yīng)再適當(dāng)減分,最后取10的次冪,以保證不同情況的優(yōu)先級(jí),即不至于出現(xiàn)因?yàn)橄碌轿恢肁可以形成4個(gè)活2而放棄下可以形成1個(gè)活4的位置B。
具體代碼如下:
#pragma once #ifndef AI_H #define AI_H #include "renju.h" #include <vector> #include <math.h> class Ai { public: Ai(chessboard &bd, state hm) { ms.set_color(hm); this->p_bd = &bd; } chess set_chess(); private: int evaluate(position pos, state color, position (*pf)(position ,bool ));//給出落子位置和方向移動(dòng)函數(shù),返回該落子位置在該方向上的評(píng)分 int point(position pos, state color);//給出一個(gè)落子位置,返回該落子的得分 void whole_points(int points[][15], state color );//給定顏色 ,記錄該顏色棋子下在每一處的得分 int best_posits(const int points[][15], position p_s[], int& count); //給出分?jǐn)?shù)數(shù)組,找出最大值對(duì)應(yīng)的位置(可能不止一個(gè)),返回分?jǐn)?shù)最大值 chess ms; const chessboard *p_bd; }; //確定落子 chess Ai:: set_chess() { int points_b[15][15]; //記錄黑棋各落點(diǎn)分?jǐn)?shù) int points_w[15][15]; //記錄白棋各落點(diǎn)分?jǐn)?shù) position best_b[20]; //記錄黑棋最大分?jǐn)?shù)對(duì)應(yīng)的落點(diǎn)位置 position best_w[20]; //記錄白棋最大分?jǐn)?shù)對(duì)應(yīng)的落點(diǎn)位置 int s_black = 0, s_white = 0; //記錄黑白棋分別的最大分?jǐn)?shù)值 int count_b = 0,count_w = 0; //記錄黑白棋最大分?jǐn)?shù)對(duì)應(yīng)的落點(diǎn)位置個(gè)數(shù) whole_points(points_b, black); whole_points(points_w, white); s_white = best_posits(points_w, best_w,count_w); s_black = best_posits(points_b, best_b,count_b); if( s_black > s_white ) //黑棋最高分高過白棋,在黑棋最高分對(duì)應(yīng)的位置中選出白棋分?jǐn)?shù)最大的位置落子 { sb: int a[20]; for(int i = 0;i < count_b;i++) { a[i] = point(best_b[i],white); } int max_w = MAX(a, count_b); for(int i = 0;i < count_b;i++) { if(a[i] == max_w) { ms.set_point(best_b[i]); return ms; } } } if( s_black < s_white ) //白棋最高分高過黑棋,在白棋最高分對(duì)應(yīng)的位置中選出黑棋分?jǐn)?shù)最大的位置落子 { sw: int a[20]; for(int i = 0;i < count_w;i++) { a[i] = point(best_w[i],black); } int max_b = MAX(a, count_b); for(int i = 0;i < count_w;i++) { if(a[i] == max_b) { ms.set_point(best_w[i]); return ms; } } } if( s_black == s_white ) { if(ms.get_color() == white) goto sw; if(ms.get_color() == black) goto sb; } } //給出分?jǐn)?shù)數(shù)組,找出最大值對(duì)應(yīng)的位置(可能不止一個(gè)),返回分?jǐn)?shù)最大值 int Ai::best_posits(const int points[][15], position p_s[], int& count) { int max_row[15]; int max_all; for(int i = 0;i < 15;i++) max_row[i] = MAX(points[i],15); max_all = MAX(max_row,15); cout<<"maxall"<<max_all; count = 0; for(int i = 0;i < 15;i++) { for(int j =0;j < 15;j++) { if(points[i][j] == max_all) { position x(i,j); p_s[count] = x; count++; } } } return max_all; } //給定顏色 ,記錄該顏色棋子下在每一處的得分 void Ai::whole_points(int points[][15], state color ) { for( int i =0;i < 15;i++) { for(int j = 0;j < 15;j++) { position temp(i,j); points[i][j] = point(temp,color); } } } //位置函數(shù),用于上下移動(dòng)棋子并判斷是否越界 position up(position pos,bool dir) { position r; if(dir) { while(pos.y > 0) { r.x = pos.x; r.y = pos.y - 1; return r; } throw 0; } else { while(pos.y < 14) { r.x = pos.x; r.y = pos.y + 1; return r; } throw 0; } } //位置函數(shù),用于左右移動(dòng)棋子并判斷是否越界 position left(position pos,bool dir) { position r; if(dir) { while(pos.x > 0) { r.x = pos.x - 1; r.y = pos.y; return r; } throw 0; } else { while(pos.x < 14) { r.x = pos.x + 1; r.y = pos.y; return r; } throw 0; } } //位置函數(shù),用于左上右下移動(dòng)棋子并判斷是否越界 position left_up(position pos,bool dir) { position r; if(dir) { while(pos.x > 0 && pos.y > 0) { r.x = pos.x - 1; r.y = pos.y - 1; return r; } throw 0; } else { while(pos.x < 14 && pos.y < 14) { r.x = pos.x + 1; r.y = pos.y + 1; return r; } throw 0; } } //位置函數(shù),用于右上左下移動(dòng)棋子并判斷是否越界 position right_up(position pos,bool dir) { position r; if(dir) { while(pos.x < 14 && pos.y > 0) { r.x = pos.x + 1; r.y = pos.y - 1; return r; } throw 0; } else { while(pos.x > 0 && pos.y < 14) { r.x = pos.x - 1; r.y = pos.y + 1; return r; } throw 0; } } int Ai::evaluate(position pos, state color, position (*pf)(position ,bool )) { int sum = 0; position p_i = pos; int count = 0,mc = 1; bool flag = true; int c_blank = 0; state judge_t; try { do { p_i = pf(p_i, flag); judge_t = p_bd -> viewboard(p_i); if(judge_t == color) { if(c_blank == 1) { count += 1; } else { mc++; if(mc == 5) return 100000000000; count += 2; } } else { if(judge_t == blank) { if(c_blank >= 1) flag = false; else { c_blank++; } } else { count-=2; flag = false; } } }while(flag); } catch(int key) { flag = false; if(c_blank == 0)count-=2; } p_i = pos; int b_blank = 0;//記錄另一半的空白格子 try { do { p_i = pf(p_i, flag); judge_t = p_bd -> viewboard(p_i); if(judge_t == color) { if(b_blank == 1) { count += 1; } else { if(c_blank == 0 && b_blank == 0) mc++; if(mc == 5) return 100000000000; count += 2; } } else { if(judge_t == blank) { if(b_blank >= 1) flag = true; else { b_blank++; } } else { count-=2; flag = true; } } }while(!flag); } catch(int key) { if(b_blank == 0)count-=2; return pow(10,count); } return pow(10,count); } //給出一個(gè)落子位置,返回該落子的得分 int Ai::point(position pos, state color) { if(p_bd -> viewboard(pos) != blank) { return 0; } position (*p_f)(position,bool) = NULL; int sum = 0; p_f = up; sum += evaluate(pos, color, p_f); p_f = left; sum += evaluate(pos, color, p_f); p_f = left_up; sum += evaluate(pos, color, p_f); p_f = right_up; sum += evaluate(pos, color, p_f); return sum; } #endif
其中所需要的頭文件在上一篇文章中有提到:C++語(yǔ)言設(shè)計(jì)實(shí)現(xiàn)五子棋
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
QT+ffmpeg實(shí)現(xiàn)視頻解析的示例詳解
這篇文章主要為大家詳細(xì)介紹了如何利用QT+ffmpeg實(shí)現(xiàn)視頻解析功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Qt有一定幫助,需要的可以參考一下2022-09-09C/C++中g(shù)etline函數(shù)案例總結(jié)
這篇文章主要介紹了C/C++中g(shù)etline函數(shù)案例總結(jié),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09