C++簡單五子棋的AI設(shè)計實現(xiàn)
本文實例為大家分享了C++五子棋的AI設(shè)計實現(xiàn)代碼,供大家參考,具體內(nèi)容如下
設(shè)計思路:通過接口獲取信息來確定顏色,通過set_chess函數(shù)來確定落點。
- 對每個點位給出兩種顏色棋子的打分,分別存在兩個15*15的數(shù)組里,數(shù)組下標(biāo)代表點的位置。
- 確定最大值所在數(shù)組之后,遍歷該數(shù)組找出所有最大值對應(yīng)的位置,然后對這些位置統(tǒng)計另一種顏色的棋子的分?jǐn)?shù),再選取一次最大值,從而確定要落點的位置。
- 打分函數(shù)的設(shè)計:在四個方向分別統(tǒng)計然后相加。對于某一個方向的分?jǐn)?shù)統(tǒng)計,則分為正反兩個方向進行,統(tǒng)計的時候如果有連成5個則直接返回一個最大值(最高分)。其他情況則按不同情況設(shè)置不同的權(quán)重,觸發(fā)結(jié)束某一個方向上的統(tǒng)計的事件如下:遇到異色棋子;空白格子超過兩個;遇到棋盤邊界。其中遇到異色棋子和棋盤邊界均視為一邊被堵死,相比空白來說適當(dāng)減分,而1個空白相比于完全連續(xù)則應(yīng)再適當(dāng)減分,最后取10的次冪,以保證不同情況的優(yōu)先級,即不至于出現(xiàn)因為下到位置A可以形成4個活2而放棄下可以形成1個活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 ));//給出落子位置和方向移動函數(shù),返回該落子位置在該方向上的評分
int point(position pos, state color);//給出一個落子位置,返回該落子的得分
void whole_points(int points[][15], state color );//給定顏色 ,記錄該顏色棋子下在每一處的得分
int best_posits(const int points[][15], position p_s[], int& count); //給出分?jǐn)?shù)數(shù)組,找出最大值對應(yīng)的位置(可能不止一個),返回分?jǐn)?shù)最大值
chess ms;
const chessboard *p_bd;
};
//確定落子
chess Ai:: set_chess()
{
int points_b[15][15]; //記錄黑棋各落點分?jǐn)?shù)
int points_w[15][15]; //記錄白棋各落點分?jǐn)?shù)
position best_b[20]; //記錄黑棋最大分?jǐn)?shù)對應(yīng)的落點位置
position best_w[20]; //記錄白棋最大分?jǐn)?shù)對應(yīng)的落點位置
int s_black = 0, s_white = 0; //記錄黑白棋分別的最大分?jǐn)?shù)值
int count_b = 0,count_w = 0; //記錄黑白棋最大分?jǐn)?shù)對應(yīng)的落點位置個數(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 ) //黑棋最高分高過白棋,在黑棋最高分對應(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 ) //白棋最高分高過黑棋,在白棋最高分對應(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ù)組,找出最大值對應(yīng)的位置(可能不止一個),返回分?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ù),用于上下移動棋子并判斷是否越界
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ù),用于左右移動棋子并判斷是否越界
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ù),用于左上右下移動棋子并判斷是否越界
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ù),用于右上左下移動棋子并判斷是否越界
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);
}
//給出一個落子位置,返回該落子的得分
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++語言設(shè)計實現(xiàn)五子棋
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:C++實現(xiàn)學(xué)校運動會管理系統(tǒng)
欄 目:C語言
下一篇:C++實現(xiàn)校園運動會報名系統(tǒng)
本文標(biāo)題:C++簡單五子棋的AI設(shè)計實現(xiàn)
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/666.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10用C++實現(xiàn)DBSCAN聚類算法
- 01-10全排列算法的非遞歸實現(xiàn)與遞歸實現(xiàn)的方法(C++)
- 01-10C++大數(shù)模板(推薦)
- 01-10淺談C/C++中的static與extern關(guān)鍵字的使用詳解
- 01-10深入C/C++浮點數(shù)在內(nèi)存中的存儲方式詳解
- 01-10深入理解C/C++混合編程


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10C#中split用法實例總結(jié)


