欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實(shí)現(xiàn)智能AI五子棋游戲詳解

 更新時(shí)間:2022年11月18日 08:17:14   作者:Csharp小記  
這篇文章主要為大家詳細(xì)介紹了如何通過C#實(shí)現(xiàn)智能AI五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

文章描述

這個程序也記不清是什么時(shí)候?qū)懙牧?,猶記得那時(shí)我還很年輕,偶然從網(wǎng)上看到了這樣一個類似的標(biāo)題(AI五子棋的實(shí)現(xiàn)),進(jìn)去后看到那個是javascript寫的,自己轉(zhuǎn)成了C#,這次又拿出來稍微整理了下,很多人會認(rèn)為這個標(biāo)題帶點(diǎn)噱頭,嗯,我曾經(jīng)也這么認(rèn)為。當(dāng)時(shí)寫完之后,還在想,這是什么智能AI,不就是換了個算法么。再后來仔細(xì)想想,這或許就是現(xiàn)在所說的、智能AI的一個最底層或者說最簡單的實(shí)現(xiàn)思路,對,是思路。

這篇文章一共分文兩篇,這篇不會寫關(guān)于算法什么的,主要把UI(棋盤繪制)以及頁面的相關(guān)事件寫一下。

開發(fā)環(huán)境

.NET Framework版本:4.5

開發(fā)工具

 Visual Studio 2013

實(shí)現(xiàn)代碼

 //棋盤大小
        static Size boardSize = new Size(800, 800);
        //單元格大小
        static Size cellSize = new Size(40, 40);
        //棋子大小
        static Size chessSize = new Size(25, 25);

        int xCellCount = boardSize.Height / cellSize.Height;
        int yCellCount = boardSize.Width / cellSize.Width;

        Graphics graphics;
        GraphicsState graphicsState;
        Pen pen = new Pen(Color.Black);
 //記錄下過的棋子
        List<ChessModel> chessList = new List<ChessModel>();
 private void Form_Chess_Load(object sender, EventArgs e)
        {
            Width = boardSize.Width + 100;
            Height = boardSize.Height;
            panel_board.Width = boardSize.Width;
            panel_board.Height = boardSize.Height;

            Location = new Point((Screen.PrimaryScreen.WorkingArea.Width - Width) / 2, (Screen.PrimaryScreen.WorkingArea.Height - Height) / 2);

            graphics = panel_board.CreateGraphics();
            InitData();

        }
        private void Form_Chess_Resize(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Minimized)
            {
                graphicsState = graphics.Save();
            }
        }
        /// <summary>
        /// 繪制棋盤
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panel_board_Paint(object sender, PaintEventArgs e)
        {
            //繪制橫線
            for (int i = 1; i < xCellCount; i++)
            {
                Point pt1 = new Point(cellSize.Width, cellSize.Width * i);
                Point pt2 = new Point(boardSize.Width - cellSize.Width, cellSize.Width * i);
                graphics.DrawLine(pen, pt1, pt2);
            }

            //繪制豎線
            for (int i = 1; i < yCellCount; i++)
            {
                Point pt1 = new Point(cellSize.Height * i, cellSize.Height);
                Point pt2 = new Point(cellSize.Height * i, boardSize.Height - cellSize.Height);
                graphics.DrawLine(pen, pt1, pt2);
            }
            if (graphicsState != null)
            {
                chessList.ForEach(s =>
                {
                    graphics.DrawImage(s.type ? Properties.Resources.黑棋子 : Properties.Resources.白棋子, s.point.X, s.point.Y, chessSize.Width, chessSize.Height);
                });
            }
        }


        private void SetStatus(int x, int y, bool type)
        {
            if (type)
            {
                lb_white_status.Text = string.Format("白棋下在了第{0}行第{1}列", y, x);
            }
            else
            {
                lb_black_status.Text = string.Format("黑棋下在了第{0}行第{1}列", y, x);
            }
        }

        private void Reset()
        {
            graphics = panel_board.CreateGraphics();
            chessList.Clear();
            InitData();
            graphicsState = null;
            panel_board.Refresh();
            panel_board_Paint(null, null);
        }

  private void btn_min_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Minimized;
        }

        private void btn_close_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btn_reset_Click(object sender, EventArgs e)
        {
            Reset();
        }

實(shí)現(xiàn)效果

代碼解析:棋盤是在Paint事件中動態(tài)繪制的,可參考變量boardSize以及cellSize,棋子是添加到資源文件中的兩個圖片。然后就是最小化后對數(shù)據(jù)進(jìn)行還原

到此這篇關(guān)于C#實(shí)現(xiàn)智能AI五子棋游戲詳解的文章就介紹到這了,更多相關(guān)C# AI五子棋游戲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論