-
Notifications
You must be signed in to change notification settings - Fork 0
/
FrmChessExample.cs
321 lines (274 loc) · 10.9 KB
/
FrmChessExample.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ChessExample
{
public partial class FrmChessExample : Form
{
private const int CELL_SIZE = 52;
private ChessLogic logic;
private Image darkSquareImg;
private Image lightSquareImg;
private Image whitePawnImg;
private Image whiteRookImg;
private Image whiteKnightImg;
private Image whiteBishopImg;
private Image whiteQueenImg;
private Image whiteKingImg;
private Image blackPawnImg;
private Image blackRookImg;
private Image blackKnightImg;
private Image blackBishopImg;
private Image blackQueenImg;
private Image blackKingImg;
private List<PictureBox>[] pieces;
private Point mousePos;
private Piece draggingPiece;
private int dx;
private int dy;
public FrmChessExample()
{
InitializeComponent();
}
private Point CellToPoint(int row, int col)
{
return new Point(CELL_SIZE * col, CELL_SIZE * row);
}
private Point CellToPoint(Cell position)
{
return CellToPoint(position.Row, position.Col);
}
public Image GetImage(string name)
{
using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name))
{
return Image.FromStream(stream);
}
}
private void CreateBoard()
{
pieces = new List<PictureBox>[2];
pieces[0] = new List<PictureBox>();
pieces[1] = new List<PictureBox>();
darkSquareImg = GetImage("ChessExample.img.Dark Square.png");
lightSquareImg = GetImage("ChessExample.img.Light Square.png");
whitePawnImg = GetImage("ChessExample.img.White Pawn.png");
whiteRookImg = GetImage("ChessExample.img.White Rook.png");
whiteKnightImg = GetImage("ChessExample.img.White Knight.png");
whiteBishopImg = GetImage("ChessExample.img.White Bishop.png");
whiteQueenImg = GetImage("ChessExample.img.White Queen.png");
whiteKingImg = GetImage("ChessExample.img.White King.png");
blackPawnImg = GetImage("ChessExample.img.Black Pawn.png");
blackRookImg = GetImage("ChessExample.img.Black Rook.png");
blackKnightImg = GetImage("ChessExample.img.Black Knight.png");
blackBishopImg = GetImage("ChessExample.img.Black Bishop.png");
blackQueenImg = GetImage("ChessExample.img.Black Queen.png");
blackKingImg = GetImage("ChessExample.img.Black King.png");
//RefreshGameState();
}
private void RefreshGameState()
{
for (int i = 0; i < 2; i++)
for (int j = 0; j < pieces[i].Count; j++)
{
PictureBox picture = pieces[i][j];
Piece piece = picture.Tag as Piece;
if (piece.alive)
picture.Location = new Point(-CELL_SIZE, 0);
else
picture.Location = CellToPoint(piece.position);
}
}
private void FrmChessExample_Load(object sender, EventArgs e)
{
ClientSize = new Size(8 * 52, 8 * 52);
logic = new ChessLogic();
CreateBoard();
}
private void DrawPiece(Graphics g, Piece piece, Point p)
{
if (piece.Color == PieceColor.WHITE)
{
if (piece is Pawn)
g.DrawImage(whitePawnImg, p);
else if (piece is Rook)
g.DrawImage(whiteRookImg, p);
else if (piece is Knight)
g.DrawImage(whiteKnightImg, p);
else if (piece is Bishop)
g.DrawImage(whiteBishopImg, p);
else if (piece is Queen)
g.DrawImage(whiteQueenImg, p);
else if (piece is King)
g.DrawImage(whiteKingImg, p);
}
else
{
if (piece is Pawn)
g.DrawImage(blackPawnImg, p);
else if (piece is Rook)
g.DrawImage(blackRookImg, p);
else if (piece is Knight)
g.DrawImage(blackKnightImg, p);
else if (piece is Bishop)
g.DrawImage(blackBishopImg, p);
else if (piece is Queen)
g.DrawImage(blackQueenImg, p);
else if (piece is King)
g.DrawImage(blackKingImg, p);
}
}
private void DrawPiece(Graphics g, Piece piece)
{
DrawPiece(g, piece, CellToPoint(piece.Position));
}
private void FrmChessExample_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
for (int row = 0; row < 8; row++)
for (int col = 0; col < 8; col++)
g.DrawImage(((row + col) & 1) == 0 ? lightSquareImg : darkSquareImg, CellToPoint(row, col));
for (int j = 0; j < logic.GetPieceCount(PieceColor.WHITE); j++)
{
Piece piece = logic.GetPiece(PieceColor.WHITE, j);
if (piece != draggingPiece)
DrawPiece(g, piece);
}
for (int j = 0; j < logic.GetPieceCount(PieceColor.BLACK); j++)
{
Piece piece = logic.GetPiece(PieceColor.BLACK, j);
if (piece != draggingPiece)
DrawPiece(g, piece);
}
if (mousePos.X >= 0 && mousePos.X <= ClientSize.Width && mousePos.Y >= 0 && mousePos.Y <= ClientSize.Height)
{
if (draggingPiece != null)
DrawPiece(g, draggingPiece, new Point(mousePos.X - dx, mousePos.Y - dy));
int row = mousePos.Y / CELL_SIZE;
int col = mousePos.X / CELL_SIZE;
using (Pen pen = new Pen(Color.Blue, 2))
{
Point p = CellToPoint(row, col);
g.DrawRectangle(pen, new Rectangle(p.X, p.Y, CELL_SIZE, CELL_SIZE));
}
if (logic.IsValidPosition(row, col))
{
Piece piece = logic.GetPiece(row, col);
if (piece != null && piece.Color == logic.CurrentTurn)
{
Cell[] moveList = piece.GetMoveList();
for (int i = 0; i < moveList.Length; i++)
{
Cell position = moveList[i];
using (Pen pen = new Pen(Color.Red, 2))
{
Point p = CellToPoint(position);
g.DrawRectangle(pen, new Rectangle(p.X, p.Y, CELL_SIZE, CELL_SIZE));
}
}
}
}
}
}
private void FrmChessExample_MouseMove(object sender, MouseEventArgs e)
{
mousePos.X = e.X;
mousePos.Y = e.Y;
Invalidate();
}
private void FrmChessExample_MouseDown(object sender, MouseEventArgs e)
{
int x = e.X;
int y = e.Y;
int row = y / CELL_SIZE;
int col = x / CELL_SIZE;
if (logic.IsValidPosition(row, col))
{
Piece piece = logic.GetPiece(row, col);
if (piece != null && piece.Color == logic.CurrentTurn)
{
draggingPiece = piece;
Point p = CellToPoint(row, col);
dx = x - p.X;
dy = y - p.Y;
Invalidate();
}
}
}
private void FrmChessExample_MouseUp(object sender, MouseEventArgs e)
{
if (draggingPiece != null)
{
int x = e.X;
int y = e.Y;
int row = y / CELL_SIZE;
int col = x / CELL_SIZE;
Cell dst = new Cell(row, col);
if (logic.IsValidPosition(row, col))
{
Cell[] moveList = draggingPiece.GetMoveList();
if (moveList.Contains(dst))
{
draggingPiece.Move(dst);
if (logic.GameOver)
{
if (logic.Draw)
MessageBox.Show("Draw!");
else
MessageBox.Show((logic.Winner == PieceColor.WHITE ? "White" : "Black") + " wins!");
}
else if (logic.PawnToPromote != null)
{
if (logic.CurrentTurn == PieceColor.WHITE)
{
btnKnight.Image = whiteKnightImg;
btnBishop.Image = whiteBishopImg;
btnRook.Image = whiteRookImg;
btnQueen.Image = whiteQueenImg;
}
else
{
btnKnight.Image = blackKnightImg;
btnBishop.Image = blackBishopImg;
btnRook.Image = blackRookImg;
btnQueen.Image = blackQueenImg;
}
pnlPieceToPromote.Visible = true;
pnlPieceToPromote.Location = new Point((ClientSize.Width - pnlPieceToPromote.Width) / 2, (ClientSize.Height - pnlPieceToPromote.Height) / 2);
}
}
}
draggingPiece = null;
Invalidate();
}
}
private void btnKnight_Click(object sender, EventArgs e)
{
logic.PawnToPromote.PromoteToKnight();
pnlPieceToPromote.Visible = false;
}
private void btnBishop_Click(object sender, EventArgs e)
{
logic.PawnToPromote.PromoteToBishop();
pnlPieceToPromote.Visible = false;
}
private void btnRook_Click(object sender, EventArgs e)
{
logic.PawnToPromote.PromoteToRook();
pnlPieceToPromote.Visible = false;
}
private void btnQueen_Click(object sender, EventArgs e)
{
logic.PawnToPromote.PromoteToQueen();
pnlPieceToPromote.Visible = false;
}
}
}