简单的c#winform象棋游戏(附带源码)
整体思路:⽤⼆维数组构建棋盘每⼀个数组元素封装为⼀个picturebox附带若⼲属性(例如:棋⼦归属⽅、棋⼦的类型),⽤⼀个抽象基类规定基本的棋⼦移动规则(例如:不能选中空⽩picturebox、该红⽅⾛棋时不能选中蓝⽅棋⼦),具体的棋⼦单独从基类棋⼦类派⽣ 重写派⽣类的⽅法规定其⾛棋规则,和相应填充picturebox的图⽚(例如: 炮不能斜着⾛不能直线吃⼦,翻⼭炮必须吃⼦且移动路径上只能有⼀个棋⼦)
窗体所⽤控件:lable、timer、picturebox
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Windows.Forms;
6using System.Drawing;
7
8namespace象棋_封_
9{
10enum chess_type
11 {
12 blank,
13 jiang,
14 che,
15 ma,
16 pao,
17 xiang,
18 zu,
19 shi
20 };//棋类的枚举类型
21enum player_type
22 {
23 blank,
24 red,
25 blue,
26 };//玩家类别的枚举类型
27abstract class Chess
28 {
29private static Chess[] cover_blue =new Chess[16];//被吃区
30private static int r=0;
31private static Chess[] cover_red = new Chess[16];//被吃区
32private static int f=0;
33protected static int chosenX;
34protected static int chosenY;
35public static bool chosen;
36static int n = 1;
37public static player_type control_side;
38public PictureBox PB;
39public chess_type type;
40public player_type side;
41public Chess()
42 {
43 side = player_type.blank;
44 type = chess_type.blank;
45 }
46public abstract bool Move_judge(object sender, int X, int Y, Chess[][] checkerboard); 47public abstract void Put_picture();
48//public void Assignment(chess_type ct, player_type pt, Image pic)
49//{
50// side = pt;
51// type = ct;
52// PB.Image = pic;
53//}//给棋⼦赋值
54public void Bg_Tored()
55 {
56this.PB.BackColor = Color.Red;
57 }//背景变为红⾊
58public void Bg_Toblank()
59 {
60this.PB.BackColor = Color.Transparent;
61 }//背景变为⽩⾊
62private void Chess_Toblank(ref Chess a, PictureBox PB)
63 {
64this.Bg_Toblank();
65this.PB.Image = null;
66 a = new Chess_blank(PB);
67 }//棋⼦属性清空
68public static void Clearground(Main f, ref Chess[][] checkerboard)//布置战场
69 {
70int i, j;
71 Control[] col_blue=null;
72 Control[] col_red=null;
73 l_side = d;
74 Chess.chosen = false;
75//*********************初始化被吃区**************//
76for (i = 0; i < 16; i++)
77 {
78 cover_blue[i] = new Chess_blank();
79 cover_red[i] = new Chess_blank();
80 }
81for (i = 1; i <= 16; i++)
82 {
83 col_blue = f.Controls.Find("Cover_PBox_blue" + i, false);//检索控件
84 col_red = f.Controls.Find("Cover_PBox_red" + i, false);//检索控件
85 cover_blue[i - 1].PB = col_blue[0] as PictureBox;
86 cover_red[i - 1].PB = col_red[0] as PictureBox;
87 }
88//*********************初始话被吃区**************//
89
90//*****************构建棋盘********************//
91 checkerboard = new Chess[10][];
92for (i = 0; i < 10; i++)
93 {
94 checkerboard[i] = new Chess[9];//10⾏9列
95 }
96for (i = 0; i < 10; i++)
97for (j = 0; j < 9; j++)
98 {
99 checkerboard[i][j] = new Chess_blank();
100 }
101 checkerboard[0][0] = new Chess_che(player_type.blue);
102 checkerboard[0][1] = new Chess_ma(player_type.blue);
103 checkerboard[0][2] = new Chess_xiang(player_type.blue);
104 checkerboard[0][3] = new Chess_shi(player_type.blue);
105 checkerboard[0][4] = new Chess_jiang(player_type.blue);
106 checkerboard[0][5] = new Chess_shi(player_type.blue);
107 checkerboard[0][6] = new Chess_xiang(player_type.blue);
108 checkerboard[0][7] = new Chess_ma(player_type.blue);
109 checkerboard[0][8] = new Chess_che(player_type.blue);
110 checkerboard[2][1] = new Chess_pao(player_type.blue);
111 checkerboard[2][7] = new Chess_pao(player_type.blue);
112 checkerboard[3][0] = new Chess_zu(player_type.blue);
113 checkerboard[3][2] = new Chess_zu(player_type.blue);
114 checkerboard[3][4] = new Chess_zu(player_type.blue);
115 checkerboard[3][6] = new Chess_zu(player_type.blue);
116 checkerboard[3][8] = new Chess_zu(player_type.blue);
117
118
119 checkerboard[6][0] = new Chess_zu(d);
120 checkerboard[6][2] = new Chess_zu(d);
121 checkerboard[6][4] = new Chess_zu(d);
122 checkerboard[6][6] = new Chess_zu(d);
123 checkerboard[6][8] = new Chess_zu(d);
124 checkerboard[7][1] = new Chess_pao(d);
125 checkerboard[7][7] = new Chess_pao(d);
126 checkerboard[9][0] = new Chess_che(d);
127 checkerboard[9][1] = new Chess_ma(d);
128 checkerboard[9][2] = new Chess_xiang(d);
129 checkerboard[9][3] = new Chess_shi(d);
130 checkerboard[9][4] = new Chess_jiang(d);
131 checkerboard[9][5] = new Chess_shi(d);
132 checkerboard[9][6] = new Chess_xiang(d);
133 checkerboard[9][7] = new Chess_ma(d);
134 checkerboard[9][8] = new Chess_che(d);
135for (i = 0; i < 10; i++)
136for (j = 0; j < 9; j++)//control 是⼀个存放控件类的容器
137 {
138 Control[] col = f.Controls.Find("pictureBox" + (i * 9 + j + 1), false);//检索控件139 checkerboard[i][j].PB = col[0] as PictureBox;//类型转换
140 checkerboard[i][j].PB.Location = new Point(170 + 60 * j, 57 * i);
141 }
142for (i = 0; i < 10; i++)
143for (j = 0; j < 9; j++)
144 {
145 checkerboard[i][j].Put_picture();
146 }
147//*****************构建棋盘********************//
148 }
149protected static int Getx(object sender)
150 {
151int x;
152string name = (sender as PictureBox).Name;
153string number = name.Substring(10);//在字符串中从指定位开始检索(这⾥是在x后也就是picturebox控件ID不同的尾号)
154int index = Convert.ToInt32(number);
155 x = (index - 1) / 9;//列
156return x;
157 }
158protected static int Gety(object sender)
159 {
160int y;
161string name = (sender as PictureBox).Name;
162string number = name.Substring(10);//在字符串中从指定位开始检索(这⾥是在x后也就是picturebox控件ID不同的尾号)
163int index = Convert.ToInt32(number);
164 y = (index - 1) % 9;//⾏
165return y;
166 }
167public static void Nochozen_dispose(object sender, Chess[][] checkerboard)
168 {
169 Chess.chosen = true;
170 chosenX = Chess.Getx(sender);
171 chosenY = Chess.Gety(sender);
172if (checkerboard[chosenX][chosenY].side != control_side)//选择的不是当前应该⾛棋的⼀⽅
173 {
174 chosen = false;
175return;
176 }
177if (checkerboard[chosenX][chosenY].side != player_type.blank)//选择的不是空⽩
178 checkerboard[chosenX][chosenY].Bg_Tored();
179return;
180 }
181public static bool Chozen_dispose(object sender, Chess[][] checkerboard)
182 {
183 Chess.chosen = false;
184int x= Getx(sender);
185int y= Gety(sender);
186if (checkerboard[chosenX][chosenY].side == checkerboard[x][y].side)//移动位置与选择位是同⼀阵营
187 {
188 checkerboard[chosenX][chosenY].Bg_Toblank();
189return false;
190 }
191if (checkerboard[chosenX][chosenY].side == player_type.blank)//选中的是空⽩后端字符串转数组
192return false;
193if (checkerboard[chosenX][chosenY].type == chess_type.ma || checkerboard[chosenX][chosenY].type == chess_type.xiang || checkerboard[ chosenX][chosenY].type == chess_type.shi)//马象⼠的移动不能同y或同x
194 {
195if (chosenX == x || chosenY == y)
196 {
197 checkerboard[chosenX][chosenY].Bg_Toblank();
198return false;
199 }
200 }
201else
202 {
203if (chosenX != x && chosenY != y)//其他棋类都必须同x或同y移动
204 {
205 checkerboard[chosenX][chosenY].Bg_Toblank();
206return false;
207 }
208 }
209
210if (!checkerboard[chosenX][chosenY].Move_judge(sender, x, y, checkerboard))
211 {
212 checkerboard[chosenX][chosenY].Bg_Toblank();
213return false;
214 }
215if (Chess.Setmove(checkerboard, x, y))
216return true;
217else
218return false;
219 }
220static bool Setmove(Chess[][] checkerboard,int X,int Y)
221 {
222 n++;
223bool over = false;
224 player_type z=player_type.blank;
225if (checkerboard[X][Y].type == chess_type.jiang)
226 {
227 over = true;
228 z = checkerboard[X][Y].side;
229 }
230if (checkerboard[X][Y].PB.Image!=null)
231 Cover(checkerboard[X][Y]);
232 Chess.Change(ref checkerboard[Chess.chosenX][Chess.chosenY],ref checkerboard[X][Y]); 233 Chess.chosen = false;
234if (over)
235 {
236if (z == player_type.blue)
237 MessageBox.Show("红⽅胜利");
238else
239 MessageBox.Show("蓝⽅胜利");
240 End f = new End(Time.fen + "分" + Time.miao + "秒");
241 f.Show();
242return true;
243 }
244if (n % 2 == 0)
245 l_side = player_type.blue;
246else
247 l_side = d;
248return false;
249 }
250private static void Change(ref Chess a,ref Chess b)//a⼦位移到b⼦
251 {
252 Chess c = new Chess_blank();
253 c.PB = b.PB;
254switch (a.type)
255 {
256case chess_type.zu:
257 b = new Chess_zu(a.side);break;
258case chess_type.pao:
259 b = new Chess_pao(a.side);break;
260case chess_type.che:
261 b = new Chess_che(a.side); break;
262case chess_type.ma:
263 b = new Chess_ma(a.side); break;
264case chess_type.xiang:
265 b = new Chess_xiang(a.side); break;
266case chess_type.shi:
267 b = new Chess_shi(a.side); break;
268case chess_type.jiang:
269 b = new Chess_jiang(a.side); break;
270 }
271 b.type = a.type;
272 b.side = a.side;
273 b.PB = c.PB;
274 b.PB.Image = a.PB.Image;
275 a.Chess_Toblank(ref a,a.PB);
276 }
277private static void Cover(Chess a)
278 {
279if (a.side == player_type.blue)
280 {
281 Change(ref a, ref cover_red[r]);
282 r++;
283 }
284else
285 {
286 Change(ref a, ref cover_blue[f]);
287 f++;
288 }
289
290 }//棋⼦被吃后移动到被吃区
291
292 }
293 }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论