服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#实现简单的五子棋游戏

C#实现简单的五子棋游戏

2022-10-26 13:40Jakie_Zhan C#

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

最近利用业余时间写了一个简单的五子棋游戏,没有利用深层次的面向对象技术,自学一年,代码和程序设计有不妥之处,还望大神指出,先看下实现的功能,三个button按钮,黑棋和白棋选择先出,和重置。

C#实现简单的五子棋游戏

其他的不多说了,直接上全部代码(通过测试)。计算输赢的时候,左斜和右斜用了数学y=kx+b的线性函数计算。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
private Image myImage;
 
/// <summary>
/// 初始化背景数组
/// int[x,y] x为行 y为列
/// </summary>
  private int[,] bgGround = new int[11, 11]; /*{
          {0,0,0,0,0,0,0,0,0,0,0},
          {0,0,0,0,0,0,0,0,0,0,0},
          {0,0,0,0,0,0,0,0,0,0,0},
          {0,0,0,0,0,0,0,0,0,0,0},
          {0,0,0,0,0,0,0,0,0,0,0},
          {0,0,0,0,0,0,0,0,0,0,0},
          {0,0,0,0,0,0,0,0,0,0,0},
          {0,0,0,0,0,0,0,0,0,0,0},
          {0,0,0,0,0,0,0,0,0,0,0},
          {0,0,0,0,0,0,0,0,0,0,0},
          {0,0,0,0,0,0,0,0,0,0,0}
         };*/
  private int CurrentX;//当前bgGround的x行
 
  private int CurrentY;//当前bgGround的y列
 
  private bool IsWhite = false;//判断白棋还是黑棋先
 
  private bool IsOver = false;//记录游戏是否结束
 
  private void Form1_Load(object sender, EventArgs e)
  {
   myImage = new Bitmap(panel1.Width, panel1.Height);
  }
 
  protected override void OnPaint(PaintEventArgs e)
  {
   Draw();
   base.OnPaint(e);
  }
 
  /// <summary>
  /// 画棋盘
  /// </summary>
  private void Draw()
  {
   Graphics g = Graphics.FromImage(myImage);
   g.Clear(this.BackColor);
   g.FillRectangle(Brushes.White,new Rectangle(new Point(10,10),new Size(400,400)));
   //循环次数应比背景bgGround行、列少1
   for (int i = 0; i < 10; i++)
   {
    for (int j = 0; j < 10; j++)
    {
     g.DrawRectangle(new Pen(Brushes.Black), i * 40 + 10, j * 40 + 10, 40, 40);
    }
   }
   
   Graphics gg = panel1.CreateGraphics();
   gg.DrawImage(myImage, 0, 0);
  }
 
  
 
  private void panel1_MouseClick(object sender, MouseEventArgs e)
  {
   if (IsOver)
   {
    return;
   }
   Graphics g = panel1.CreateGraphics();
   //设置当鼠标点击坐标在某一落棋点坐标想x,y的+-10的范围内即可落子
   int x = (e.X - 10) % 40;
   int y = (e.Y - 10) % 40;
   if (x > 30)
   {
    x = (e.X - 10) / 40 + 1;
   }
   else
   {
    x = (e.X - 10) / 40;
   }
   if (y > 30)
   {
    y = (e.Y - 10) / 40 + 1;
   }
   else
   {
    y = (e.Y - 10) / 40;
   }
   if (bgGround[x, y] == 0)
   {
    if (IsWhite)
    {
     DrawChess(g, x, y, new Pen(Brushes.White), Brushes.White, 1);
     IsWhite = false;
     JudgeResult(1);
    }
    else
    {
     DrawChess(g, x, y, new Pen(Brushes.Black), Brushes.Black, 2);
     IsWhite = true;
     JudgeResult(2);
    }
   }
  }
 
  /// <summary>
  /// 判断输赢
  /// </summary>
  /// <param name="flag">1为白棋 2为黑棋</param>
  private void JudgeResult(int flag)
  {
   int x = CurrentX;
   int y = CurrentY;
   int MinXNum = 0;
   int MaxXNum = 0;
   int count = 0;
   if (x > 4)
   {
    MinXNum = x - 4;
    if (x + 4 > 10)
    {
     MaxXNum = 10;
    }
    else
    {
     MaxXNum = x + 4;
    }
   }
   else
   {
    MaxXNum = x + 4;
   }
   int MinYNum = 0;
   int MaxYNum = 0;
   if (y > 4)
   {
    MinYNum = y - 4;
    if (y + 4 > 10)
    {
     MaxYNum = 10;
    }
    else
    {
     MaxYNum = y + 4;
    }
   }
   else
   {
    MaxYNum = y + 4;
   }
   #region //横向
   for (int i = MinXNum; i < MaxXNum+1; i++)
   {
    if (bgGround[i, y] == flag)
    {
     count++;
     if (count > 4)
      goto Label;
    }
    else
    {
     count = 0;
     if (i > MaxXNum - 4)
      break;
    }
   }
   #endregion
   #region //竖向
   for (int i = MinYNum; i < MaxYNum+1; i++)
   {
    if (bgGround[x, i] == flag)
     count++;
    else
    {
     count = 0;
     if (i > MaxYNum - 4)
      break;
    }
    if (count > 4)
     goto Label;
   }
   #endregion
   //左斜
   for (int i = MinXNum; i < MaxXNum+1; i++)
   {
    if (CurrentX + CurrentY - i < 0)
     break;
    if (CurrentX + CurrentY - i <= 10)
    {
     if (bgGround[i, CurrentX + CurrentY - i] == flag)
     {
      count++;
     }
     else
     {
      count = 0;
      if (i > MaxYNum - 4)
       break;
     }
    }
    if (count > 4)
     goto Label;
   }
   //右斜
   for (int i = MinXNum; i < MaxXNum+1; i++)
   {
    if (i < CurrentX - CurrentY)
     break;
    if (i + CurrentY - CurrentX > 10)
     break;
    if (bgGround[i, i + CurrentY - CurrentX] == flag)
    {
     count++;
    }
    else
    {
     count = 0;
     if (i > MaxYNum - 4)
      break;
    }
    if (count > 4)
     goto Label;
   }
 
 
  Label:
   if (flag == 1 && count > 4)
   {
    IsOver = true;
    MessageBox.Show("白棋赢,游戏结束");
    return;
   }
   else if (flag == 2 && count > 4)
   {
    IsOver = true;
    MessageBox.Show("黑棋赢,游戏结束");
    return;
   }
   else
   {
    IsOver = false;
   }
  }
 
  /// <summary>
  /// 画棋子
  /// </summary>
  /// <param name="g"></param>
  /// <param name="x">bgGround中x位置</param>
  /// <param name="y">bgGround中y位置</param>
  /// <param name="p">画笔</param>
  /// <param name="brush">棋子颜色</param>
  /// <param name="flag">1为白棋 2为黑棋</param>
  private void DrawChess(Graphics g, int x, int y, Pen p, Brush brush, int flag)
  {
   CurrentX = x; CurrentY = y;
   bgGround[x, y] = flag;
   g.DrawEllipse(p, x * 40, y * 40, 20, 20);
   g.FillEllipse(brush, x * 40, y * 40, 20, 20);
 
  }
  ///btn_Chess_Click是黑棋先和白棋先按钮的共同事件,设置白棋先button的tag值为1,黑棋先button的tag值为2
  /// <summary>
  /// 判断哪个先下 设置button控件的tag值
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void btn_Chess_Click(object sender, EventArgs e)
  {
   Button btn = sender as Button;
   string tag = btn.Tag.ToString();
   if (tag.Equals("1"))//白棋先
   {
    IsWhite = true;
   }
   else//黑棋先 tag=2
   {
    IsWhite = false;
   }
  }
 
  /// <summary>
  /// 重置
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void btn_reset_Click(object sender, EventArgs e)
  {
   IsOver = false;
   Draw();
   bgGround = new int[11, 11];
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/Jakie_Zhan/article/details/79985923

延伸 · 阅读

精彩推荐