-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_positions.pas
268 lines (250 loc) · 9.47 KB
/
game_positions.pas
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
unit Game_Positions;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, ExtCtrls, StdCtrls, Controls, Graphics, ChessPosition, ChessPiece, Game;
//generic
procedure PositionClick(Sender: TChessPosition);
//specific
procedure PreviousMovementDisplay(Sender: TChessPosition);
procedure CapturePiece(Sender: TChessPosition);
procedure PointCounters(AttackedPiece: TChessPiece; var PlayerScoreInteger: integer; PlayerScorePanel: TPanel; PieceCountersL: PieceCountersLArray);
procedure CreateEnPassant(Sender: TChessPosition);
procedure PieceMovement(Sender: TChessPosition);
procedure PawnPromotion();
procedure EndTurn();
implementation
uses
Collection, Game_Movement;
//GENERIC
procedure PositionClick(Sender: TChessPosition);
begin
PreviousMovementDisplay(Sender);
CapturePiece(Sender);
CreateEnPassant(Sender);
PieceMovement(Sender);
GameF.ChessboardClear(Sender);
if Turn<>'' then PawnPromotion();
EndTurn();
end;
//SPECIFIC
procedure PreviousMovementDisplay(Sender: TChessPosition);
var
i: byte;
begin
for i:=0 to 4 do PreviousMovement[i].Visible:=True;
//MovedPiece
GameF.MovedPiece.Picture:=gSelectedPiece.Picture;
//MovedFrom
Game_Movement.FindPosition(gSelectedPiece.Left, gSelectedPiece.Top);
case FoundPosition.Name of
'A2', 'A4', 'A6', 'A8', 'B1', 'B3', 'B5', 'B7', 'C2', 'C4', 'C6', 'C8', 'D1', 'D3', 'D5', 'D7',
'E2', 'E4', 'E6', 'E8', 'F1', 'F3', 'F5', 'F7', 'G2', 'G4', 'G6', 'G8', 'H1', 'H3', 'H5', 'H7': begin
GameF.MovedFrom.Canvas.Pen.Color:=clWhite;
GameF.MovedFrom.Canvas.Brush.Color:=clBlack;
GameF.MovedFrom.Canvas.Font.Color:=clWhite;
end
else begin
GameF.MovedFrom.Canvas.Pen.Color:=clBlack;
GameF.MovedFrom.Canvas.Brush.Color:=clWhite;
GameF.MovedFrom.Canvas.Font.Color:=clBlack;
end;
end;
GameF.MovedFrom.Canvas.Rectangle(0, 0, GameF.MovedFrom.Width, GameF.MovedFrom.Height);
GameF.MovedFrom.Canvas.TextOut(GameF.MovedFrom.Width div 4, GameF.MovedFrom.Height div 4, FoundPosition.Name);
//MovementType
GameF.MovementType.Picture:=Sender.Picture;
//MovedTo
case Sender.Name of
'A2', 'A4', 'A6', 'A8', 'B1', 'B3', 'B5', 'B7', 'C2', 'C4', 'C6', 'C8', 'D1', 'D3', 'D5', 'D7',
'E2', 'E4', 'E6', 'E8', 'F1', 'F3', 'F5', 'F7', 'G2', 'G4', 'G6', 'G8', 'H1', 'H3', 'H5', 'H7': begin
GameF.MovedTo.Canvas.Pen.Color:=clWhite;
GameF.MovedTo.Canvas.Brush.Color:=clBlack;
GameF.MovedTo.Canvas.Font.Color:=clWhite;
end
else begin
GameF.MovedTo.Canvas.Pen.Color:=clBlack;
GameF.MovedTo.Canvas.Brush.Color:=clWhite;
GameF.MovedTo.Canvas.Font.Color:=clBlack;
end;
end;
GameF.MovedTo.Canvas.Rectangle(0, 0, GameF.MovedTo.Width, GameF.MovedTo.Height);
GameF.MovedTo.Canvas.TextOut(GameF.MovedTo.Width div 4, GameF.MovedTo.Height div 4, Sender.Name);
//CapturedPiece
if Sender.PositionMovement='Attack' then begin
case Turn of
'White': Game_Movement.FindPieceFromArray(BlackPieces, Sender.Left, Sender.Top);
'Black': Game_Movement.FindPieceFromArray(WhitePieces, Sender.Left, Sender.Top);
end;
GameF.CapturedPiece.Picture:=FoundPiece.Picture;
end
else GameF.CapturedPiece.Picture:=nil;
end;
procedure CapturePiece(Sender: TChessPosition);
var
i: byte;
begin
//if piece was attacked continue
if Sender.PositionMovement='Attack' then begin
//search for piece you attacked
for i:=0 to 31 do if (AllPieces[i].Left=Sender.Left) AND (AllPieces[i].Top=Sender.Top) then begin
{REGULAR ATTACK}
//remove attacked piece from game
AllPieces[i].AnchorSide[akTop].Control:=GameF;
AllPieces[i].AnchorSide[akLeft].Control:=GameF;
//point counters
case gSelectedPiece.PieceOwner of
'White': PointCounters(AllPieces[i], Player1ScoreInteger, GameF.Player1ScorePanel, BlackPieceCountersL);
'Black': PointCounters(AllPieces[i], Player2ScoreInteger, GameF.Player2ScorePanel, WhitePieceCountersL);
end;
exit;
end;
{EnPassant Capture}
//white attacks EnPassant
case gSelectedPiece.PieceOwner of
'White': begin
//point counter
inc(Player1ScoreInteger);
GameF.PawnBlackCounterL.Caption:=inttostr(strtoint(GameF.PawnBlackCounterL.caption)-1);
if Player1ScoreInteger=1 then GameF.Player1ScorePanel.Caption:=inttostr(Player1ScoreInteger)+' Point'
else GameF.Player1ScorePanel.Caption:=inttostr(Player1ScoreInteger)+' Points';
//search for attacked EnPassant's pawn
for i:=0 to 7 do if (BlackPawns[i].Left=GameF.Enpassant.Left-75) AND (BlackPawns[i].Top=GameF.Enpassant.Top) then begin
//remove attacked black pawn from game
BlackPawns[i].AnchorSide[akTop].Control:=GameF;
BlackPawns[i].AnchorSide[akLeft].Control:=GameF;
end;
end;
//black attacks EnPassant
'Black': begin
//point counter
inc(Player2ScoreInteger);
GameF.PawnWhiteCounterL.Caption:=inttostr(strtoint(GameF.PawnWhiteCounterL.caption)-1);
if Player2ScoreInteger=1 then GameF.Player2ScorePanel.Caption:=inttostr(Player2ScoreInteger)+' Point'
else GameF.Player2ScorePanel.Caption:=inttostr(Player2ScoreInteger)+' Points';
//search for attacked EnPassant's pawn
for i:=0 to 7 do if (WhitePawns[i].Left=GameF.Enpassant.Left+75) AND (WhitePawns[i].Top=GameF.Enpassant.Top) then begin
//remove attacked white pawn from game
WhitePawns[i].AnchorSide[akTop].Control:=GameF;
WhitePawns[i].AnchorSide[akLeft].Control:=GameF;
end;
end;
end;
end;
end;
procedure PointCounters(AttackedPiece: TChessPiece; var PlayerScoreInteger: integer; PlayerScorePanel: TPanel; PieceCountersL: PieceCountersLArray);
var
i: byte;
begin
case AttackedPiece.PieceType of
'Pawn': begin
inc(PlayerScoreInteger);
PieceCountersL[Pawn].Caption:=inttostr(strtoint(PieceCountersL[Pawn].Caption)-1);
end;
'Rook': begin
inc(PlayerScoreInteger, 5);
PieceCountersL[Rook].Caption:=inttostr(strtoint(PieceCountersL[Rook].Caption)-1);
end;
'Knight': begin
inc(PlayerScoreInteger, 3);
PieceCountersL[Knight].Caption:=inttostr(strtoint(PieceCountersL[Knight].Caption)-1);
end;
'Bishop': begin
inc(PlayerScoreInteger, 3);
PieceCountersL[Bishop].Caption:=inttostr(strtoint(PieceCountersL[Bishop].Caption)-1);
end;
'Queen': begin
inc(PlayerScoreInteger, 9);
PieceCountersL[Queen].Caption:=inttostr(strtoint(PieceCountersL[Queen].Caption)-1);
end;
'King': case Turn of
'White': if not ((QueenBlackType='Blue') and (GameF.QueenBlackCounterL.Caption='1')) then begin
GameF.SurrenderWhite.Visible:=False;
GameF.MessageA.Font.Color:=clWhite;
GameF.MessageA.Caption:=GameF.Player1gName.Caption;
GameF.Victory();
for i:=0 to 4 do PreviousMovement[i].Visible:=False;
end
else begin
GameF.QueenBlack.PieceType:='King';
GameF.QueenBlack.Picture:=GameF.Player2Portrait.Picture;
GameF.QueenBlackCounterL.Caption:='0';
inc(PlayerScoreInteger, 9);
end;
'Black': if not ((QueenWhiteType='Blue') and (GameF.QueenWhiteCounterL.Caption='1')) then begin
GameF.SurrenderBlack.Visible:=False;
GameF.MessageA.Font.Color:=clSilver;
GameF.MessageA.Caption:=GameF.Player2gName.Caption;
GameF.Victory();
for i:=0 to 4 do PreviousMovement[i].Visible:=False;
end
else begin
GameF.QueenWhite.PieceType:='King';
GameF.QueenWhite.Picture:=GameF.Player1Portrait.Picture;
GameF.QueenWhiteCounterL.Caption:='0';
inc(PlayerScoreInteger, 9);
end;
end;
end;
if PlayerScoreInteger=1 then PlayerScorePanel.Caption:=inttostr(PlayerScoreInteger)+' Point'
else PlayerScorePanel.Caption:=inttostr(PlayerScoreInteger)+' Points';
end;
procedure CreateEnPassant(Sender: TChessPosition);
begin
//check for what piece is selected
if gSelectedPiece.PieceType='Pawn' then begin
case gSelectedPiece.PieceOwner of
'White': begin
//if white pawn used special move then create EnPassant
if (gSelectedPiece.Left=Sender.Left-150) AND (PawnWhiteType<>'Red') then begin
GameF.EnPassant.Left:=Sender.Left-75;
GameF.EnPassant.Top:=Sender.Top;
exit;
end;
end;
'Black': begin
//if black pawn used special move then create EnPassant
if (gSelectedPiece.Left=Sender.Left+150) AND (PawnBlackType<>'Red') then begin
GameF.EnPassant.Left:=Sender.Left+75;
GameF.EnPassant.Top:=Sender.Top;
exit;
end;
end;
end;
end;
//remove EnPassant
GameF.EnPassant.Left:=0;
GameF.EnPassant.Top:=0;
end;
procedure PieceMovement(Sender: TChessPosition);
begin
gSelectedPiece.AnchorSide[akTop].Control:=Sender;
gSelectedPiece.AnchorSide[akLeft].Control:=Sender;
gSelectedPiece.PieceHasMoved:=True;
end;
procedure PawnPromotion();
var
i: byte;
begin
//check if pawn has reached the end of the board
for i:=0 to 8 do begin
//white pawn promotion
if (WhitePawns[i].Left=GameF.Chessboard.Left+525) AND (WhitePawns[i].PieceType='Pawn') then begin
GameF.PromotionsShow();
exit;
end;
if (BlackPawns[i].Left=GameF.Chessboard.Left) AND (BlackPawns[i].PieceType='Pawn') then begin
//black pawn promotion
GameF.PromotionsShow();
exit;
end;
end;
end;
procedure EndTurn();
begin
case Turn of
'White': GameF.BlackTurn();
'Black': GameF.WhiteTurn();
end;
end;
end.