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

其中所需要的頭文件在上一篇文章中有提到:
http://blog.csdn.net/black_kyatu/article/details/79327680

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章