C# 实现中国象棋【棋盘,棋子】

本文介绍如何使用C#绘制中国象棋棋盘并进行初始化布局,包括绘制棋盘、棋子及布局等步骤,适用于学习参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文是利用C# 实现中国象棋的棋盘绘制,以及初始化布局,并不实现中国象棋的对弈逻辑。仅供学习参考使用。

思路:

  1. 绘制中国象棋棋盘,竖线九条,横线十条。再中间绘制‘楚河’,‘汉界’ 。
  2. 绘制棋子,然后将棋子布局在棋盘上即可。

涉及知识点:

  1. 用户控件:用于实现棋盘的绘制,重写 OnPaint(PaintEventArgs e) 方法。
  2. Matrix:封装表示几何变换的 3x3 仿射矩阵。本例中主要用于旋转绘制反方的‘汉界’。
  3. GraphicsPath:表示一系列相互连接的直线和曲线。本例中主要用于绘制圆形棋子。

效果图如下:

(一)

(二)

核心代码

棋盘核心代码如下:

  1 protected override void OnPaint(PaintEventArgs e)
  2         {
  3             base.OnPaint(e);
  4 
  5             //初始化数组
  6             InitArrPieceInfo();
  7 
  8             Graphics g = e.Graphics;
  9             int width = this.Width;
 10             int height = this.Height;
 11             int padding = this.Padding.All * 20;
 12             int center = height / 2;//垂直中心位置
 13             int s_width = (width - 2 * padding) / 8;//每一条横线的间距
 14             int s_heigth = (height - 2 * padding) / 9;//每一条竖线的间距
 15             int start_x = padding;//起始位置
 16             int start_y = padding;//起始位置
 17             Pen pen = new Pen(Brushes.Black, 1.5f);
 18             Dictionary<string, string[]> dicNums = new Dictionary<string, string[]>();
 19             dicNums.Add("up", new string[9] { "1", "2", "3", "4", "5", "6", "7", "8", "9" });
 20             dicNums.Add("down", new string[9] { "", "", "", "", "", "", "", "", "" });
 21             Font font = new Font("宋体", 12, FontStyle.Regular);
 22             for (int i = 0; i < 9; i++)
 23             {
 24                 //竖线九条
 25                 Point p0 = new Point(start_x + i * s_width, start_y);
 26                 Point p1 = new Point(start_x + i * s_width, start_y + (s_heigth * 4));
 27                 Point p2 = new Point(start_x + i * s_width, start_y + (s_heigth * 5));
 28                 Point p3 = new Point(start_x + i * s_width, start_y + (s_heigth * 9));
 29                 g.DrawLine(pen, p0, p1);
 30                 g.DrawLine(pen, p2, p3);
 31                 //上下的文字
 32                 Point p_up = new Point(start_x + i * s_width - 5, padding / 2);
 33                 Point p_down = new Point(start_x + i * s_width - 5, start_y + (s_heigth * 9) + padding / 3);
 34                 g.DrawString(dicNums["up"][i], font, Brushes.Black, p_up);
 35                 g.DrawString(dicNums["down"][i], font, Brushes.Black, p_down);
 36                 //数组赋值
 37                 for (int j = 0; j < 10; j++)
 38                 {
 39                     Point absLocation = ArrPiece[i, j].AbsoluteLocation;
 40                     absLocation.X = start_x + i * s_width;
 41                     ArrPiece[i, j].AbsoluteLocation = absLocation;
 42                 }
 43             }
 44             for (int i = 0; i < 10; i++)
 45             {
 46                 //横线十条
 47                 Point p0 = new Point(start_x, start_y + i * s_heigth);
 48                 Point p1 = new Point(start_x + s_width * 8, start_y + i * s_heigth);
 49                 g.DrawLine(pen, p0, p1);
 50                 //数组赋值
 51                 for (int j = 0; j < 9; j++)
 52                 {
 53                     Point absLocation = ArrPiece[j, i].AbsoluteLocation;
 54                     absLocation.Y = start_y + i * s_heigth;
 55                     ArrPiece[j, i].AbsoluteLocation = absLocation;
 56                 }
 57             }
 58             //绘制九宫格
 59             for (int i = 0; i < 2; i++)
 60             {
 61                 Point p0 = new Point(start_x + (3 + i * 2) * s_width, start_y);
 62                 Point p1 = new Point(start_x + (5 - i * 2) * s_width, start_y + (s_heigth * 2));
 63                 Point p2 = new Point(start_x + (3 + i * 2) * s_width, start_y + (s_heigth * 7));
 64                 Point p3 = new Point(start_x + (5 - i * 2) * s_width, start_y + (s_heigth * 9));
 65                 g.DrawLine(pen, p0, p1);
 66                 g.DrawLine(pen, p2, p3);
 67             }
 68 
 69             //兵和卒处有拐角,从左往右
 70             for (int i = 0; i < 5; i++)
 71             {
 72                 int p_x = start_x + 2 * i * s_width;
 73                 int p_y = start_y + 3 * s_heigth;
 74                 DrawCorner(g, pen, p_x, p_y);//
 75                 p_y = start_y + 6 * s_heigth;
 76                 DrawCorner(g, pen, p_x, p_y);//
 77             }
 78             //炮处的拐角,从左往右
 79             for (int i = 0; i < 2; i++)
 80             {
 81                 int p_x = start_x + (1 + 6 * i) * s_width;
 82                 int p_y = start_y + 2 * s_heigth;
 83                 DrawCorner(g, pen, p_x, p_y);//
 84                 p_y = start_y + 7 * s_heigth;
 85                 DrawCorner(g, pen, p_x, p_y);//
 86             }
 87             //绘制楚河汉界
 88             Point p_0 = new Point(2 * s_width, center - 25);
 89             Point p_1 = new Point(7 * s_width, center + 20);
 90             font = new Font("方正隶二繁体", 30, FontStyle.Regular);
 91             g.DrawString("楚河", font, Brushes.Black, p_0);
 92             Matrix mtxSave = g.Transform;
 93             Matrix mtxRotate = g.Transform;
 94             mtxRotate.RotateAt(180, p_1);
 95             g.Transform = mtxRotate;
 96             g.DrawString("汉界", font, Brushes.Black, p_1);
 97             g.Transform = mtxSave;
 98             //绘制外边框
 99             g.DrawRectangle(pen, 3, 3, width - 6, height - 6);
100             g.DrawRectangle(pen, 5, 5, width - 10, height - 10);
101             g.DrawRectangle(pen, 7, 7, width - 14, height - 14);
102         }
View Code

棋子核心代码如下:

 1         protected override void OnPaint(PaintEventArgs e)
 2         {
 3             base.OnPaint(e);
 4             Graphics g = e.Graphics;
 5             GraphicsPath gPath = new GraphicsPath();
 6             // Set a new rectangle to the same size as the button's ClientRectangle property.
 7             Rectangle rectangle = this.ClientRectangle;
 8             g.DrawEllipse(new Pen(this.FlatAppearance.BorderColor), rectangle);
 9             gPath.AddEllipse(rectangle);
10 
11             // Set the button's Region property to the newly created  circle region.
12             this.Region = new Region(gPath);
13             Rectangle inRect = new Rectangle(2, 2, this.Width - 4, this.Height - 3);
14             g.FillEllipse(new SolidBrush(this.BackColor), rectangle);
15             g.DrawEllipse(new Pen(Color.Black,2), inRect);
16 
17             Font font = new Font("楷体", 25, FontStyle.Regular);
18             g.DrawString(this.Text, font, new SolidBrush(this.ForeColor), 0,5);
19         }
View Code


源码下载链接

 

转载于:https://www.cnblogs.com/hsiang/p/7282449.html

功能介绍:<br/> * 本上传软件仅为爱好编程的同仁C#学习之用. <br/> <br/> a. 支持单人/双人游戏; 在开局中任意时刻可以切换单人/双人状态. <br/> b. 音效支持;有三首背景音乐,前台走棋音乐多样,如果您仔细观察的话,连拖动旗子的声音也有了:).<br/> c. 能够自定义残局; 通过*.ini配置文件增加了多个残局棋局.<br/> d. 保存. 能够实现动态保存功能,在下棋过程中能够保存当前下棋棋盘布局状态; 并在任意时刻恢复您保存的状态.<br/> e. 防止作弊. 程式严格控制了没个旗子的走发,比如:卒在过诃之前只能够进攻,过了诃才能够左右移动以及不能够一方连续走棋. <br/> f. 支持键盘鼠标两种操作方式; 双人下棋是一人使用鼠标,一人使用键盘操作最佳!<br/> g. 比较好的智能提示. 即使对象棋规则不台熟悉的人也可以很好的根据提示走棋,比如:当一方走棋后,它会自动提示另一方再走棋;当拿起旗子后,它回自动提示该位置是否可以落棋, 当落棋后它会提示您走了哪个旗子. 下棋结束会有得分和分析当前旗子损失率等.<br/> h. 有点不足的是电脑走棋比较苯,不过这也无妨影响学习大碍. 程式里面用到了许多C#技术细节方面,比如: Graphics,Sound,Repaint Control(Change picture to round),KeyDownPress,MouseClick/Move/Down/Drag picture,game save(Serialization),operate setting file etc.<br/><br/>扩展功能:<br/> i. 重新设计了所有旗子, 选择新的旗盘背景, 换了一个更清爽的面.(界面配色并不是件很容易的事情, 这样的棋类游戏长时间容易使眼睛疲劳, 首先要做到选择的色彩不刺激眼睛,其实大部分色彩都比较刺激眼睛,尤其是纯三基色(红/黄/蓝), 还要使界面做得漂亮). <br/> j. 增加”回放” 功能. 当下完旗子时,可以重新回味一下, 刚杀完的一盘旗,可以寻找不足和重新感受一下胜利的喜悦! 这个功能比较复杂!<br/> k. 又看了一下电脑走旗, 感觉确实比较难处理, 没有高人指点这个算法确实比较难, 应该比以前聪明了一些, 但是还是比较笨, 打算有空去找个现在的电脑走旗组件替换上, 自己的电脑走旗算法慢慢研究(当时是因为实在找不到现在的组件, 自己了个较笨的,如果哪位朋友能够提供组件,在次深表感谢!!!).<br/> l. 扩展走旗的步数容量, 有些网友, 对的都是高手, 产生数组越界, 这次从 200 扩展到了500, 当然<br/> 您还可以扩展到更大,因为源代码已经开放).<br/> m. 增加图像缓存功能.<br/> (开发语言: C#语言)<br/><br/> * 本上传软件仅为爱好编程的同仁C#学习之用. <br/> <br/><br/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值