-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathuMain.pas
221 lines (193 loc) · 4.93 KB
/
uMain.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
unit uMain;
interface
uses
System.Generics.Collections,
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Objects,
FMX.Layouts, FMX.Effects, FMX.Controls.Presentation, FMX.StdCtrls;
type
TForm1 = class(TForm)
GridPanelLayout1: TGridPanelLayout;
rctA1: TRectangle;
Rectangle2: TRectangle;
Rectangle3: TRectangle;
imgA1: TImage;
rctB1: TRectangle;
imgB1: TImage;
rctC1: TRectangle;
imgC1: TImage;
rctA2: TRectangle;
imgA2: TImage;
rctB2: TRectangle;
imgB2: TImage;
rctC2: TRectangle;
imgC2: TImage;
rctA3: TRectangle;
imgA3: TImage;
rctB3: TRectangle;
imgB3: TImage;
rctC3: TRectangle;
imgC3: TImage;
rctNewGame: TRectangle;
btnNewGame: TSpeedButton;
ShadowEffect9: TShadowEffect;
Text1: TText;
rctMode: TRectangle;
btnMode: TSpeedButton;
ShadowEffect1: TShadowEffect;
txtMode: TText;
Label1: TLabel;
ImgX: TImage;
ImgO: TImage;
procedure RectangleClick(Sender: TObject);
procedure btnModeClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Text1Click(Sender: TObject);
procedure btnNewGameClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
Player: Char;
Moves: TQueue<TRectangle>;
GameOver: Boolean;
InfiniteMode: Boolean;
procedure CheckWin;
procedure SwitchPlayer;
procedure ResetGame;
procedure UpdateMode;
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
procedure TForm1.CheckWin;
const
Wins: array[0..7, 0..2] of Integer = (
(0, 1, 2), (3, 4, 5), (6, 7, 8), // Rows
(0, 3, 6), (1, 4, 7), (2, 5, 8), // Columns
(0, 4, 8), (2, 4, 6) // Diagonals
);
var
I: Integer;
Rect1, Rect2, Rect3: TRectangle;
Win: Boolean;
begin
for I := 0 to 7 do
begin
Rect1 := TRectangle(GridPanelLayout1.Children[Wins[I, 0]]);
Rect2 := TRectangle(GridPanelLayout1.Children[Wins[I, 1]]);
Rect3 := TRectangle(GridPanelLayout1.Children[Wins[I, 2]]);
Win := (Rect1.TagString <> '') and (Rect1.TagString = Rect2.TagString) and (Rect1.TagString = Rect3.TagString);
if Win then
begin
Rect1.Fill.Color := TAlphaColors.Mediumaquamarine;
Rect2.Fill.Color := TAlphaColors.Mediumaquamarine;
Rect3.Fill.Color := TAlphaColors.Mediumaquamarine;
Label1.Text := 'Player ' + Rect1.TagString + ' wins!';
GameOver := True;
Exit;
end;
end;
if not InfiniteMode and (Moves.Count = 9) then
begin
Label1.Text := 'Empate!';
GameOver := True;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
I: Integer;
Rect: TRectangle;
Img: TImage;
begin
Player := 'X';
Moves := TQueue<TRectangle>.Create;
Label1.Text := 'Player: ' + Player;
GameOver := False;
UpdateMode;
end;
procedure TForm1.RectangleClick(Sender: TObject);
var
Rect, NextRect: TRectangle;
Img: TImage;
begin
if GameOver then Exit;
Rect := TRectangle(Sender);
if Rect.TagString = '' then
begin
if Player = 'X' then
TImage(Rect.Children[0]).Bitmap.Assign(ImgX.Bitmap)
else
TImage(Rect.Children[0]).Bitmap.Assign(ImgO.Bitmap);
Rect.TagString := Player;
Moves.Enqueue(Rect);
// Se o modo infinito estiver ativado e já houver mais de 7 movimentos, remova o item mais antigo
if InfiniteMode and (Moves.Count > 7) then
begin
Rect := Moves.Dequeue;
TImage(Rect.Children[0]).Bitmap := nil;
Rect.TagString := '';
end;
CheckWin;
if not GameOver then begin
// Se o modo infinito estiver ativado, marque o próximo item a ser removido
if InfiniteMode and (Moves.Count >= 7) then
begin
NextRect := Moves.Peek;
TImage(NextRect.Children[0]).Bitmap.ReplaceOpaqueColor(TAlphaColors.Darkgray);
end;
SwitchPlayer
end;
end;
end;
procedure TForm1.ResetGame;
var
I: Integer;
Rect: TRectangle;
begin
Player := 'X';
Label1.Text := 'Player: ' + Player;
Moves.Clear;
GameOver := False;
for I := 0 to GridPanelLayout1.ChildrenCount - 1 do
begin
if GridPanelLayout1.Children[I] is TRectangle then
begin
Rect := TRectangle(GridPanelLayout1.Children[I]);
TImage(Rect.Children[0]).Bitmap := nil;
Rect.Fill.Color := TAlphaColors.White;
Rect.TagString := '';
end;
end;
end;
procedure TForm1.btnNewGameClick(Sender: TObject);
begin
ResetGame;
end;
procedure TForm1.SwitchPlayer;
begin
if Player = 'X' then
Player := 'O'
else
Player := 'X';
Label1.Text := 'Player: ' + Player;
end;
procedure TForm1.Text1Click(Sender: TObject);
begin
ResetGame;
end;
procedure TForm1.UpdateMode;
begin
InfiniteMode := txtMode.Text = 'INFINITO';
ResetGame;
end;
procedure TForm1.btnModeClick(Sender: TObject);
begin
if txtMode.Text = 'NORMAL' then
txtMode.Text := 'INFINITO'
else
txtMode.Text := 'NORMAL';
UpdateMode;
end;
end.