国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++簡單五子棋的AI設計實現

2020-01-26 13:38:40
字體:
來源:轉載
供稿:網友

本文實例為大家分享了C++五子棋的AI設計實現代碼,供大家參考,具體內容如下

設計思路:通過接口獲取信息來確定顏色,通過set_chess函數來確定落點。

  • 對每個點位給出兩種顏色棋子的打分,分別存在兩個15*15的數組里,數組下標代表點的位置。
  • 確定最大值所在數組之后,遍歷該數組找出所有最大值對應的位置,然后對這些位置統計另一種顏色的棋子的分數,再選取一次最大值,從而確定要落點的位置。
  • 打分函數的設計:在四個方向分別統計然后相加。對于某一個方向的分數統計,則分為正反兩個方向進行,統計的時候如果有連成5個則直接返回一個最大值(最高分)。其他情況則按不同情況設置不同的權重,觸發結束某一個方向上的統計的事件如下:遇到異色棋子;空白格子超過兩個;遇到棋盤邊界。其中遇到異色棋子和棋盤邊界均視為一邊被堵死,相比空白來說適當減分,而1個空白相比于完全連續則應再適當減分,最后取10的次冪,以保證不同情況的優先級,即不至于出現因為下到位置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 ));//給出落子位置和方向移動函數,返回該落子位置在該方向上的評分  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); //給出分數數組,找出最大值對應的位置(可能不止一個),返回分數最大值  chess ms;  const chessboard *p_bd;};//確定落子chess Ai:: set_chess(){  int points_b[15][15];    //記錄黑棋各落點分數  int points_w[15][15];    //記錄白棋各落點分數  position best_b[20];    //記錄黑棋最大分數對應的落點位置  position best_w[20];    //記錄白棋最大分數對應的落點位置  int s_black = 0, s_white = 0;    //記錄黑白棋分別的最大分數值  int count_b = 0,count_w = 0;      //記錄黑白棋最大分數對應的落點位置個數  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 )   //黑棋最高分高過白棋,在黑棋最高分對應的位置中選出白棋分數最大的位置落子  {  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 )   //白棋最高分高過黑棋,在白棋最高分對應的位置中選出黑棋分數最大的位置落子  {  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;  }}//給出分數數組,找出最大值對應的位置(可能不止一個),返回分數最大值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);    }  }}//位置函數,用于上下移動棋子并判斷是否越界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;  }}//位置函數,用于左右移動棋子并判斷是否越界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;  }}//位置函數,用于左上右下移動棋子并判斷是否越界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;  }}//位置函數,用于右上左下移動棋子并判斷是否越界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++語言設計實現五子棋

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 米林县| 神农架林区| 扬州市| 扎囊县| 曲周县| 乌鲁木齐市| 高淳县| 文化| 连平县| 张家口市| 漳州市| 临西县| 会昌县| 太原市| 正镶白旗| 南召县| 个旧市| 金平| 乃东县| 宣汉县| 勐海县| 寿阳县| 宕昌县| 龙门县| 长治县| 孟津县| 安阳市| 北流市| 新宾| 河曲县| 习水县| 香港 | 连城县| 平阳县| 自治县| 敦煌市| 毕节市| 河西区| 墨竹工卡县| 赣州市| 武穴市|