C#实现智能AI五子棋游戏详解

这篇文章主要为大家详细介绍了如何通过C#实现智能AI五子棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

文章描述

这个程序也记不清是什么时候写的了,犹记得那时我还很年轻,偶然从网上看到了这样一个类似的标题(AI五子棋的实现),进去后看到那个是javascript写的,自己转成了C#,这次又拿出来稍微整理了下,很多人会认为这个标题带点噱头,嗯,我曾经也这么认为。当时写完之后,还在想,这是什么智能AI,不就是换了个算法么。再后来仔细想想,这或许就是现在所说的、智能AI的一个最底层或者说最简单的实现思路,对,是思路。

这篇文章一共分文两篇,这篇不会写关于算法什么的,主要把UI(棋盘绘制)以及页面的相关事件写一下。

开发环境

.NET Framework版本:4.5

开发工具

 Visual Studio 2013

实现代码

 //棋盘大小 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 chessList = new List();
 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(); } } ///  /// 绘制棋盘 ///  ///  ///  private void panel_board_Paint(object sender, PaintEventArgs e) { //绘制横线 for (int i = 1; i  { 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(); }

实现效果

代码解析:棋盘是在Paint事件中动态绘制的,可参考变量boardSize以及cellSize,棋子是添加到资源文件中的两个图片。然后就是最小化后对数据进行还原

到此这篇关于C#实现智能AI五子棋游戏详解的文章就介绍到这了,更多相关C# AI五子棋游戏内容请搜索0133技术站以前的文章或继续浏览下面的相关文章希望大家以后多多支持0133技术站!

以上就是C#实现智能AI五子棋游戏详解的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 其他教程