C#实现智能AI五子棋游戏详解
作者:Csharp小记 时间:2021-12-10 06:17:38
文章描述
这个程序也记不清是什么时候写的了,犹记得那时我还很年轻,偶然从网上看到了这样一个类似的标题(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<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();
}
实现效果
代码解析:棋盘是在Paint事件中动态绘制的,可参考变量boardSize
以及cellSize
,棋子是添加到资源文件中的两个图片。然后就是最小化后对数据进行还原
来源:https://mp.weixin.qq.com/s/yz8vakrjuwNnKmVP1v75dw
标签:C#,AI,五子棋,游戏
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
微信公众号开发之设置自定义菜单实例代码【java版】
2022-09-02 00:52:22
![](https://img.aspxhome.com/file/2023/2/88042_0s.png)
解决fastjson泛型转换报错的解决方法
2023-04-02 03:10:44
![](https://img.aspxhome.com/file/2023/5/90965_0s.png)
Java 中的垃圾回收机制详解
2023-01-11 19:32:34
![](https://img.aspxhome.com/file/2023/2/67162_0s.png)
超实用的Java快捷键(总结)
2022-04-06 20:56:51
基于SpringMVC入门案例及讲解
2023-04-06 17:34:43
![](https://img.aspxhome.com/file/2023/6/75786_0s.jpg)
C#关闭指定名字进程的方法
2021-11-23 23:59:42
Java Thread.currentThread().getName() 和 this.getName()区别详解
2021-10-31 01:46:18
![](https://img.aspxhome.com/file/2023/4/61834_0s.png)
SpringBoot 嵌入式web容器的启动原理详解
2021-12-29 23:23:14
![](https://img.aspxhome.com/file/2023/8/64908_0s.png)
Seata AT模式TM处理流程图文示例详解
2022-05-03 02:28:39
![](https://img.aspxhome.com/file/2023/4/71864_0s.png)
浅谈为什么要使用mybatis的@param
2023-07-01 20:12:39
![](https://img.aspxhome.com/file/2023/9/86819_0s.png)
JAVA swing布局管理器实例解析
2022-01-11 05:05:15
![](https://img.aspxhome.com/file/2023/6/94216_0s.png)
一文带你了解C#中的协变与逆变
2022-08-06 22:31:21
JavaWeb动态导出Excel可弹出下载
2021-11-21 11:12:12
![](https://img.aspxhome.com/file/2023/8/91138_0s.jpg)
WinForm导出文件为Word、Excel、文本文件的方法
2022-07-08 20:07:16
Opencv光流运动物体追踪详解
2023-06-21 11:55:31
![](https://img.aspxhome.com/file/2023/4/91994_0s.jpg)
WinForm下 TextBox只允许输入数字的小例子
2023-08-02 15:34:14
java动态导出excel压缩成zip下载的方法
2022-07-13 07:29:27
MyBatis JdbcType 与Oracle、MySql数据类型对应关系说明
2023-08-23 02:23:06
![](https://img.aspxhome.com/file/2023/6/58276_0s.jpg)
Deepin系统安装eclipse2021-03及CDT插件的安装教程
2022-02-12 05:42:33
![](https://img.aspxhome.com/file/2023/1/85471_0s.png)
Java二维数组计算集合总结
2023-02-15 16:38:14
![](https://img.aspxhome.com/file/2023/8/63998_0s.jpg)