-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chessboard.cs
327 lines (299 loc) · 13 KB
/
Chessboard.cs
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Media;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Chess_Game
{
public partial class Chessboard : Form
{
private Brush LightColor;
private Brush DarkColor;
private Brush Highlighted;
private Brush Transperant;
private SoundPlayer movePiece;
private SoundPlayer capturedPiece;
private SoundPlayer Check;
private SoundPlayer CheckmateHorn;
private SoundPlayer StalemateHorn;
private SoundPlayer TenSecondsHorn;
private ChessGame Game;
private Square Picked;
private Square Dropped;
private Point PickedLocation;
private Dictionary<Piece,Bitmap> PieceImages;//BlackPawn,WhitePawn,BlackRook,WhiteRook,BlackKnight,WhiteKnight,BlackBishop,WhiteBishop
//,BlackKing, WhiteKing, BlackQueen, WhiteQueen;
internal Chessboard(Color Light, Color Dark, ChessGame Game)
{
InitializeComponent();
PieceImages = new Dictionary<Piece, Bitmap>();
PieceImages.Add(Piece.BPAWN, new Bitmap(new Bitmap(@"bp.png"), new Size(64, 64)));
PieceImages.Add(Piece.WPAWN, new Bitmap(new Bitmap(@"wp.png"), new Size(64, 64)));
PieceImages.Add(Piece.BROOK, new Bitmap(new Bitmap(@"br.png"), new Size(64, 64)));
PieceImages.Add(Piece.WROOK, new Bitmap(new Bitmap(@"wr.png"), new Size(64, 64)));
PieceImages.Add(Piece.BKNIGHT, new Bitmap(new Bitmap(@"bkn.png"), new Size(64, 64)));
PieceImages.Add(Piece.WKNIGHT, new Bitmap(new Bitmap(@"wkn.png"), new Size(64, 64)));
PieceImages.Add(Piece.BBISHOP, new Bitmap(new Bitmap(@"bb.png"), new Size(64, 64)));
PieceImages.Add(Piece.WBISHOP, new Bitmap(new Bitmap(@"wb.png"), new Size(64, 64)));
PieceImages.Add(Piece.BKING, new Bitmap(new Bitmap(@"bk.png"), new Size(64, 64)));
PieceImages.Add(Piece.WKING, new Bitmap(new Bitmap(@"wk.png"), new Size(64, 64)));
PieceImages.Add(Piece.BQUEEN, new Bitmap(new Bitmap(@"bq.png"), new Size(64, 64)));
PieceImages.Add(Piece.WQUEEN, new Bitmap(new Bitmap(@"wq.png"), new Size(64, 64)));
LightColor = new SolidBrush(Light);
DarkColor = new SolidBrush(Dark);
Highlighted = new SolidBrush(Color.FromArgb(100, Color.FromName("yellow")));
Transperant = new SolidBrush(Color.FromArgb(50, Color.FromName("white")));
movePiece = new SoundPlayer(Properties.Resources.chop);
capturedPiece = new SoundPlayer(Properties.Resources.knifeSlice);
Check = new SoundPlayer(Properties.Resources.checkHorn);
CheckmateHorn = new SoundPlayer(Properties.Resources.checkmateHorn);
StalemateHorn = new SoundPlayer(Properties.Resources.stalemateHorn);
TenSecondsHorn = new SoundPlayer(Properties.Resources.tenSecondsHorn);
this.Game = Game;
Player1.Text = Game.Player1Name;
Player2.Text = Game.Player2Name;
Game.Promote += Game_Promote;
Game.Started += Started;
Game.CheckmateEvent += Checkmate;
Game.StalemateEvent += Stalemate;
Game.Moved += Piece_Moved;
Game.CapturedPieceEvent += Piece_Captured;
Game.CheckEvent += Game_Check;
Game.TenSeconds += Game_Seconds;
Game.Invalidateboard += Game_Invalidate;
Picked = new Square(0,'z');
Dropped = new Square(0, 'z');
Board.Image = new Bitmap(512,512);
Board_Paint(null,null);
listBox1.DataSource = Game.Moves;
MainTimer.Stop();
Game.PropertyChanged += Time_PropertyChanged;
Binding whiteTimeBinding = new Binding("Text", this.Game, "WhiteTimeLimit", true);
this.Player1Time.DataBindings.Add(whiteTimeBinding);
Binding blackTimeBinding = new Binding("Text", this.Game, "BlackTimeLimit", true);
this.Player2Time.DataBindings.Add(blackTimeBinding);
}
private void Game_Invalidate()
{
Board.Invalidate();
}
private void Game_Seconds()
{
TenSecondsHorn.Play();
}
private object Game_Check(Move move)
{
Check.Play();
return move;
}
private object Piece_Captured(Move move)
{
capturedPiece.Play();
return move;
}
private object Started(Move move)
{
if (!Game.FirstMove)
MainTimer.Start();
return move;
}
private object Checkmate(Move move)
{
Game.BlackTimeLimit = "0.00";
Game.WhiteTimeLimit = "0.00";
Game.WLimit = 0;
Game.BLimit = 0;
CheckmateHorn.Play();
MainTimer.Stop();
return move;
}
private object Stalemate(Move move)
{
Game.BlackTimeLimit = "0.00";
Game.WhiteTimeLimit = "0.00";
Game.WLimit = 0;
Game.BLimit = 0;
StalemateHorn.Play();
MainTimer.Stop();
return move;
}
private object Game_Promote(Move move)
{
// make changes...
Promotion chosenPiece;
if ((int)move.MovedPiece % 2 == 0)
{
MainTimer.Stop();
BlackPiecePromotion blackPromo = new BlackPiecePromotion();
blackPromo.StartPosition = FormStartPosition.CenterParent;
DialogResult y = blackPromo.ShowDialog();
if (y != DialogResult.OK)
chosenPiece = (Promotion)Piece.BPAWN;
chosenPiece = blackPromo.chosenPiece;
}
else
{
MainTimer.Stop();
WhtePiecePromotion whitePromo = new WhtePiecePromotion();
whitePromo.StartPosition = FormStartPosition.CenterParent;
DialogResult y = whitePromo.ShowDialog();
if(y != DialogResult.OK)
chosenPiece= (Promotion)Piece.WPAWN;
chosenPiece = whitePromo.chosenPiece;
}
Board.Invalidate();
MainTimer.Start();
return chosenPiece;
//return ((int)move.MovedPiece % 2 == 0) ? Promotion.BQUEEN : Promotion.WQUEEN;
}
void Time_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case "WhiteTimeLimit":
this.Player1Time.Text = this.Game.WhiteTimeLimit;
break;
case "BlackTimeLimit":
this.Player2Time.Text = this.Game.BlackTimeLimit;
break;
}
}
private object Piece_Moved(Move move)
{
movePiece.Play();
return move;
}
private void Board_MouseDown(object sender, MouseEventArgs e)
{
int sizeUnit = (int)Math.Round(Board.Image.Width / 16.0);
int X = e.X / (2*sizeUnit);
int Y = e.Y / (2 * sizeUnit);
if (Game.Board[X][Y].Occupant == Piece.NONE)
return;
Picked = new Square(Game.Board[X][Y].Rank,
Game.Board[X][Y].File,
Game.Board[X][Y].Occupant);
PickedLocation = new Point(e.Location.X - sizeUnit, e.Location.Y - sizeUnit);
Board.Refresh();
}
private void Board_MouseUp(object sender, MouseEventArgs e)
{
int sizeUnit = (int)Math.Round(Board.Image.Width / 16.0);
if (Picked.Occupant == Piece.NONE)
return;
if (e.X >= Board.Width || e.Y >= Board.Height || e.X < 0 || e.Y < 0)
{
Picked = new Square(0, 'z');
Board.Invalidate();
return;
}
int X = e.X / (2 * sizeUnit);
int Y = e.Y / (2 * sizeUnit);
bool Success = Game.Move(new Move(Picked.File - 'a', 8 - Picked.Rank, X, Y));
if(Success)
Dropped = new Square(Game.Board[X][Y].Rank,
Game.Board[X][Y].File,
Game.Board[X][Y].Occupant);
Picked.Occupant = Piece.NONE ;
Board.Invalidate();
}
private void Board_MouseMove(object sender, MouseEventArgs e)
{
int sizeUnit = (int)Math.Round(Board.Image.Width / 16.0);
if (Picked.Occupant != Piece.NONE)
{
PickedLocation = new Point(e.Location.X - sizeUnit, e.Location.Y - sizeUnit);
if (e.X >= Board.Width)
PickedLocation.X = Board.Width - sizeUnit;
if (e.X < 0)
PickedLocation.X = -sizeUnit;
if (e.Y >= Board.Height)
PickedLocation.Y = Board.Height - sizeUnit;
if (e.Y < 0)
PickedLocation.Y = -sizeUnit;
}
Board.Invalidate();
}
private void Board_Paint(object sender, PaintEventArgs e)
{
int squareWidth = (int)Math.Round(Board.Image.Width / 8.0);
using (Graphics g = Graphics.FromImage(Board.Image))
{
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++)
if ((i + j) % 2 == 0)
g.FillRectangle(LightColor, new Rectangle(squareWidth * i, squareWidth * j, squareWidth, squareWidth));
else
g.FillRectangle(DarkColor, new Rectangle(squareWidth * i, squareWidth * j, squareWidth, squareWidth));
for (int i = 0; i < 8; i++)
{
g.DrawString("" + (8 - i), new Font(FontFamily.GenericSansSerif, 8, FontStyle.Bold),
(i % 2 == 0) ? DarkColor : LightColor, new Point(0, 3 * squareWidth / 64 + squareWidth * i));
g.DrawString("" + (char)('a' + i), new Font(FontFamily.GenericSansSerif, 8, FontStyle.Bold),
(i % 2 == 1) ? DarkColor : LightColor, new Point(54 * squareWidth/64 + squareWidth * i, 498));
}
if(Dropped.Occupant != Piece.NONE)
g.FillRectangle(Highlighted, new Rectangle(squareWidth * (Dropped.File - 'a'), squareWidth * (8 - Dropped.Rank), squareWidth, squareWidth));
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++)
{
if (Game.Board[i][j].Occupant == Piece.NONE)//empty square
continue;
if (Picked.Occupant != Piece.NONE)
if (Game.Board[i][j].Rank == Picked.Rank && Game.Board[i][j].File == Picked.File)
continue;
g.DrawImage(PieceImages[Game.Board[i][j].Occupant], new Point(squareWidth * i, squareWidth * j));
}
if (Picked.Occupant == Piece.NONE)
return;
g.FillRectangle(Highlighted,
new Rectangle(squareWidth * (Picked.File - 'a'), squareWidth * (8 - Picked.Rank), squareWidth, squareWidth));
g.DrawImage(PieceImages[Picked.Occupant], PickedLocation);
}
}
private void ChessBoard_MouseMove(object sender, MouseEventArgs e)
{
Board.Invalidate();
}
private void Board_MouseLeave(object sender, EventArgs e)
{
Board.Invalidate();
}
private void MainTimer_Tick(object sender, EventArgs e)
{
if (Game.WhiteTurn)
{
if (Game.WLimit <= MainTimer.Interval)
{
Game.WhiteTimeLimit = "0.00";
Game.WLimit = 0;
MainTimer.Stop();
MessageBox.Show(Game.Player1Name + " lost by timeout");
}
else
Game.WhiteTimeLimit = Game.TimeToString(Game.WLimit -= MainTimer.Interval);
}
else
{
if (Game.BLimit <= MainTimer.Interval)
{
Game.BlackTimeLimit = "0.00";
Game.BLimit = 0;
MainTimer.Stop();
MessageBox.Show(Game.Player2Name + " lost by timeout");
}
else
Game.BlackTimeLimit = Game.TimeToString(Game.BLimit -= MainTimer.Interval);
}
}
private void Chessboard_FormClosed(object sender, FormClosedEventArgs e)
{
MainTimer.Stop();
}
}
}