最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

文章描述

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

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

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

.NET Framework版本:4.5

開(kāi)發(fā)工具

 Visual Studio 2013

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

 //棋盤(pá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);
 //記錄下過(guò)的棋子
        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>
        /// 繪制棋盤(pán)
        /// </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)效果

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

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

相關(guān)文章

最新評(píng)論

公主岭市| 曲阳县| 水富县| 青州市| 通榆县| 百色市| 漾濞| 古田县| 台江县| 股票| 日照市| 钦州市| 开平市| 大理市| 石泉县| 南漳县| 金乡县| 临沭县| 朝阳县| 商都县| 西藏| 伊吾县| 铜川市| 垫江县| 光山县| 台东市| 朝阳区| 双桥区| 磴口县| 公安县| 北辰区| 龙川县| 会泽县| 渑池县| 个旧市| 克拉玛依市| 安国市| 德化县| 庆城县| 卓资县| 上饶县|