Unity 五子棋游戏设计和简单AI(3)

在把五子棋基本胜利逻辑完成之后,我们这次就着手开始AI的设计。主要的思路是使用贪心算法,评估函数来进行一个五子棋AI的设计。

AI设计思路:遍历棋盘中的每一个位置,通过计算每个位置的分数,从而得出表面上的“最优解”,举个例子:一个位置既能达成活三又能堵住对面的棋子,那么它的分数就比单单一个活三的分数要高。计算分数的方式我们可以参考之前检测五子的代码。

1代码:代码中的"*"指代棋子 _指那个位置是空的。依次检查棋子的周围是否有相同的类型,之后挑选出分数最高的,作为返回值。

 public int CheckOneLine(int[] pos, int[] offest , int chessColor )
    {
        string temp = "*";
    
        for (int x = pos[0] + offest[0], y = pos[1] + offest[1], rem=0; (x < 15 && x > 0) && (y > 0 && y < 15)&&rem<=5; x=x+ offest[0], y += offest[1],rem++)
        {

            if (ChessBoard.Instance.grid[x, y] == (int)chessColor)
            {
                temp += "*";

            }
            else if (ChessBoard.Instance.grid[x, y] == 0)
            {
                temp += "_";
               
            }
            else  break;
        }
        
        for (int x = pos[0] - offest[0], y = pos[1] - offest[1],rem=0; (x < 15 && x > 0) && (y > 0 && y < 15)&&rem<=5; x -= offest[0], y -= offest[1],rem++)
        {
            if (ChessBoard.Instance.grid[x, y] == (int)chessColor)
            {
                temp = "*" + temp;
             

            }
            else if (ChessBoard.Instance.grid[x, y] == 0)
            {
                temp = "_" + temp;
                break;
            }
            else break;
        }
        int MaxScore = 0;
        foreach (var item in toScore)//返回一个最大值
        {
            if (temp.Contains((item.Key)) && toScore[item.Key] > MaxScore)
            {
                MaxScore = toScore[item.Key];
            }

        }
        return MaxScore;
    }

2 这里我们使用字典作为分数和棋谱的储存器,在开始初始化,分数和棋谱读者可以自行调整,添加。


    Dictionary<string, int> Score = new Dictionary<string, int>();
    private void Start()
    {
        Score.Add("_*_", 20);
        Score.Add("_**_", 120);
        Score.Add("_**", 50);
        Score.Add("**_", 50);
        Score.Add("_***_", 720);
        Score.Add("_***", 600);
        Score.Add("***_", 600);
        Score.Add("****_", 720);
        Score.Add("**_**_", 720);
        Score.Add("**_*_", 720);
        Score.Add("_*_**", 720);
        Score.Add("_****_", 5000);
        Score.Add("*****", 100000);
    }

3计算总分,注意不仅要计算自己棋子的分数还要计算对方的分数,以便做到防守。

   int SetScore(int[] pos)//计算总分
    {
        int score = 0;
        score += CheckOneLine(pos, new int[] { 1, 0 },1);
        score += CheckOneLine(pos, new int[] { 0, 1 },1);
        score += CheckOneLine(pos, new int[] { 1, 1 },1);
        score += CheckOneLine(pos, new int[] { 1, -1 },1);//计算白子总分

        score += CheckOneLine(pos, new int[] { 1, 0 },2);
        score += CheckOneLine(pos, new int[] { 0, 1 },2);
        score += CheckOneLine(pos, new int[] { 1, 1 },2);
        score += CheckOneLine(pos, new int[] { 1, -1 },2);//计算黑子总分
        return score;
    }

4最后,遍历棋盘得到分数最高的那个点,注意一些细节,这样我们的五子棋AI就完成了!

 public override void PlayChess()
    {
        if ( chessType != gametable.Instance.turn) return;
        int MaxX=7,MaxY=7, MaxScore = 80;
        if (gametable .Instance.grid[MaxX, MaxY] != 0) MaxScore = 0;
        for (int x = 0; x <= 14; x++)
        {
            for (int y = 0; y <= 14; y++)
            {
                if (gametable.Instance.grid[x, y] != 0) continue;
                int score = SetScore(new int[] { x, y });
                if (score > MaxScore)
                {
                    MaxX = x;
                    MaxY = y;
                    MaxScore = score;

                }
            }
        }
        gametable.Instance.Play(new int[] { MaxX, MaxY });

    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值