C#实现AI五子棋游戏的示例代码

.NET
320
0
0
2023-06-22
标签   C#
目录
  • 文章描述
  • 开发环境
  • 开发工具
  • 实现代码
  • 实现效果

文章描述

关于简单的介绍,这篇就不赘述了,主要还是来写一下实际的人工下棋操作以及对应的机器操作的算法处理。

还是先大致说一下算法实现方式,我们之前写的五子棋大部分可能主要是基于机器算法做一个拦截操作,即判断横向、竖向、斜向、反斜向的棋子的数量去直接进行拦截。但是这一篇中主要是使用了一个分配权重的算法,根据权重来匹配我是要去拦截你,还是保持自己的胜利。这个权重可以根据自己的需求适当调整(我也是瞎写的)。

万变不离其宗,无论什么算法,肯定到最后都是根据五子棋的玩法,去解析横竖斜的胜率来进行权衡。

开发环境

.NET Framework版本:4.5

开发工具

Visual Studio 2013

实现代码

 /// <summary>
/// 转换棋子的绘制位置
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
private Point GetChessPostion(int x, int y)
{
    return new Point((int)(x * cellSize.Width) - chessSize.Width /, (int)(y * cellSize.Height) - chessSize.Height / 2);
}

 /// <summary>
/// 判断胜负
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="type"></param>
private void IsWin(int x, int y, bool type)
{
    for (int w =; w < winSum; w++)
    {
        if (wins[x, y, w] ==)
        {
            if (!type)
            {
                userWin[w]++;
                aiWin[w] =;
                if (userWin[w] ==)
                {
                    graphics.DrawString("赢", new Font("黑体",.0f), new SolidBrush(Color.Red), GetChessPostion(x, y));
                    if (MessageBox.Show("你赢了,是否重新开始?") == DialogResult.OK)
                    {
                        Reset();
                    }
                    break;
                }
            }
            else
            {
                aiWin[w]++;
                userWin[w] =;
                if (aiWin[w] ==)
                {
                    graphics.DrawString("赢", new Font("黑体",.0f), new SolidBrush(Color.Red), GetChessPostion(x, y));
                    if (MessageBox.Show("你输了,是否重新开始?") == DialogResult.OK)
                    {
                        Reset();
                    }

                    break;
                }
            }
        }
    }
    isUserPlay = !isUserPlay;
}
 /// <summary>
/// 人工下棋操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void panel_board_Click(object sender, EventArgs e)
{
    if (!isUserPlay)
    {
        return;
    }
    MouseEventArgs mouse = e as MouseEventArgs;
    if (mouse.X < cellSize.Width || mouse.X > boardSize.Width - cellSize.Width)
    {
        return;
    }
    if (mouse.Y < cellSize.Height || mouse.Y > boardSize.Height - cellSize.Height)
    {
        return;
    }
    int x = Convert.ToInt(Math.Round((decimal)mouse.X / (decimal)cellSize.Width, MidpointRounding.AwayFromZero));
    int y = Convert.ToInt(Math.Round((decimal)mouse.Y / (decimal)cellSize.Width, MidpointRounding.AwayFromZero));
    Point chessPoint = GetChessPostion(x, y);
    if (!chessList.Exists(s => s.point == chessPoint))
    {
        chessList.Add(new ChessModel { point = chessPoint, type = true });

        graphics.DrawImage(Properties.Resources.黑棋子, chessPoint.X, chessPoint.Y, chessSize.Width, chessSize.Height);

        IsWin(x, y, false);
        SetStatus(x, y, false);
        if (!isUserPlay)
        {
            AIChess();
        }
    }
}
/// <summary>
/// AI下棋操作
/// </summary>
private void AIChess()
{
    int[,] userScore = new int[xCellCount, yCellCount];
    int[,] aiScore = new int[xCellCount, yCellCount];

    int max =;
    Point aiChess = new Point();

    for (int x =; x < xCellCount; x++)
    {
        for (int y =; y < yCellCount; y++)
        {
            if (!chessList.Exists(s => s.point == GetChessPostion(x, y)))
            {
                for (int w =; w < winSum; w++)
                {
                    if (wins[x, y, w] ==)
                    {
                        if (userWin[w] ==)
                        {
                            userScore[x, y] +=;
                        }
                        if (userWin[w] ==)
                        {
                            userScore[x, y] +=;
                        }
                        if (userWin[w] ==)
                        {
                            userScore[x, y] +=;
                        }
                        if (userWin[w] ==)
                        {
                            userScore[x, y] +=;
                        }


                        if (aiWin[w] ==)
                        {
                            aiScore[x, y] +=;
                        }
                        if (aiWin[w] ==)
                        {
                            aiScore[x, y] +=;
                        }
                        if (aiWin[w] ==)
                        {
                            aiScore[x, y] +=;
                        }
                        if (aiWin[w] ==)
                        {
                            aiScore[x, y] +=;
                        }

                    }
                }

                if (userScore[x, y] > max)
                {
                    max = userScore[x, y];

                    aiChess.X = x;
                    aiChess.Y = y;
                }
                else if (userScore[x, y] == max)
                {
                    if (aiScore[x, y] > aiScore[x, y])
                    {
                        aiChess.X = x;
                        aiChess.Y = y;
                    }
                }


                if (aiScore[x, y] > max)
                {
                    max = aiScore[x, y];

                    aiChess.X = x;
                    aiChess.Y = y;
                }
                else if (aiScore[x, y] == max)
                {
                    if (userScore[x, y] > userScore[x, y])
                    {
                        aiChess.X = x;
                        aiChess.Y = y;
                    }
                }
            }
        }
    }
    Point chessPoint = GetChessPostion(aiChess.X, aiChess.Y);
    chessList.Add(new ChessModel() { point = chessPoint, type = false });
    graphics.DrawImage(Properties.Resources.白棋子, chessPoint.X, chessPoint.Y, chessSize.Width, chessSize.Height);

    IsWin(aiChess.X, aiChess.Y, true);
    SetStatus(aiChess.X, aiChess.Y, true);
}

实现效果

代码解析:自己看代码吧,看懂了就拿来优化下,看不懂就直接下载下来玩玩(除了一个思路引导外,好像也没什么用)