-
Notifications
You must be signed in to change notification settings - Fork 5
/
gaple_ai.pas
380 lines (336 loc) · 11.1 KB
/
gaple_ai.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
(* GAME AI & ALGORITHM *)
{ player selection algorithm }
function TFormGaple.FirstPlayer(PairValue: integer): integer;
var
i: integer;
begin
// selecting start card
if IncFirstCardPair then
PairValue := PairValue mod 7
else
PairValue := FirstCardPair;
// selecting player who has the start card
for i := 1 to NumberOfCards do
with TDominoCard(FindComponent('DominoCard'+IntToStr(i))) do
if (TopValue = PairValue) and (BottomValue = PairValue) then
begin
DeckGaple.TopValue := TopValue;
DeckGaple.BottomValue := BottomValue;
LabelDeck.Visible := true;
LabelDeck.Caption := 'Deck expecting: '+IntToStr(DeckGaple.TopValue)+'_'+IntToStr(DeckGaple.BottomValue);
result := Tag;
Break;
end
else
result := 1;
end;
procedure TFormGaple.NextPlayerTurn;
var
i: integer;
begin
if GameRunning then
begin
ListBoxCards.Items := StepList;
with TLabel(FindComponent('LabelPlayer'+IntToStr(CurrentPlayer))) do Font.Color := clWhite;
repeat
Inc(CurrentPlayer);
if CurrentPlayer > 4 then CurrentPlayer := 1;
until not NoCardLeft(CurrentPlayer) or GameIsOver;
Application.ProcessMessages;
Sleep(250);
if not GameIsOver then
begin
with TLabel(FindComponent('LabelPlayer'+IntToStr(CurrentPlayer))) do Font.Color := clLime;
if Players[CurrentPlayer].Kind = plComputer then
ComputerPlaying(CurrentPlayer)
else if Players[CurrentPlayer].Kind = plNetwork then
NetworkPlaying(CurrentPlayer)
else
LabelStatus.Caption := 'Waiting for '+Players[CurrentPlayer].Name+' ... ';
end
else
begin
GameRunning := false;
for i := 1 to NumberOfCards do
with TDominoCard(FindComponent('DominoCard'+IntToStr(i))) do
if Visible then ShowDeck := false;
LabelStatus.Caption := 'Game is over.';
GameOverInformation;
Exit;
end;
end;
end;
{ player algorithm }
procedure TFormGaple.HumanPlaying(SelectedCard: TDominoCard);
var
turn_info: string;
answer: integer;
i: integer;
begin
if GameRunning then
begin
// check player turn
if (SelectedCard.Tag = CurrentPlayer) and
(Players[CurrentPlayer].Kind = plHuman) then
begin
// check player status
if not DeckGaple.IsEmpty then
begin
PlayerPass := NoCardMatch(SelectedCard.Tag);
PlayerPlay := not NoCardLeft(SelectedCard.Tag);
// check for deck reorganizing
if (TurnStep mod 4 = 0) then
begin
ReorganizeDeckCards;
DominoDeck.TopValue := DeckGaple.TopValue;
DominoDeck.BottomValue := DeckGaple.BottomValue;
DominoDeck.ShowDeck := false;
end;
end;
// player allowed to play
if PlayerPlay and (not PlayerPass) then
begin
// first turn
if DeckGaple.IsEmpty then
begin
if not CardMatchBoth(SelectedCard) then
begin
WrongCardWarning;
Exit;
end;
FirstCard(SelectedCard);
end
// next turn
else if CardMatchBoth(SelectedCard) then
begin
if (DeckGaple.TopValue = DeckGaple.BottomValue) then
PlaceCardOnTop(SelectedCard)
else
// player must select card
begin
answer := SelectCardQuestion;
if answer = mrYes then
PlaceCardOnTop(SelectedCard)
else if answer = mrNo then
PlaceCardOnBottom(SelectedCard)
else
Exit;
end;
end
else if CardMatchBottom(SelectedCard) then
PlaceCardOnBottom(SelectedCard)
else if CardMatchTop(SelectedCard) then
PlaceCardOnTop(SelectedCard)
// player select unmatch card
else
begin
WrongCardWarning;
LabelStatus.Caption := Players[CurrentPlayer].Name+' is thinking... wrong!';
end;
end
// player has no card to play then pass the turn
else if PlayerPass then
begin
// passing card required
if PassCardRequired then
begin
answer := PassCardQuestion;
if answer = mrYes then DisableCard(SelectedCard);
end
// no passing card required
else
begin
NoCardInformation;
DisableCard(SelectedCard);
end;
end
// player not allowed to play
else if not PlayerPlay then
begin
// by passing turn
Inc(TurnStep);
NoCardInformation;
// write game info
turn_info := IntToStr(TurnStep)+': ';
turn_info := turn_info + 'pass = ';
turn_info := turn_info + IntToStr(DeckGaple.TopValue)+'_';
turn_info := turn_info + IntToStr(DeckGaple.BottomValue);
StepList.Add(turn_info);
// move to next player
NextPlayerTurn;
end;
end
// wrong turn
else
WrongTurnWarning;
// no more turn
if GameIsOver and GameRunning then
begin
// show all disabled cards
GameRunning := false;
for i := 1 to NumberOfCards do
with TDominoCard(FindComponent('DominoCard'+IntToStr(i))) do
if Visible then ShowDeck := false;
LabelStatus.Caption := 'Game is over.';
GameOverInformation;
Exit;
end;
end;
end;
procedure TFormGaple.NetworkPlaying(Player: integer);
begin
end;
procedure TFormGaple.ComputerPlaying(Player: integer);
type
card = record
top_value: integer;
bottom_value: integer;
is_match: boolean;
index: integer;
end;
var
place_on_top: boolean;
card_match,some_match: boolean;
i,highest,lowest: integer;
card_idx,lowest_idx,highest_idx: integer;
play_cards_count: integer;
play_cards: array of card;
begin
if GameRunning then
begin
// check player turn
if (Player = CurrentPlayer) and
(Players[CurrentPlayer].Kind = plComputer) then
begin
Cursor := crHourGlass;
LabelStatus.Caption := Players[Player].Name+' is thinking... ';
Application.ProcessMessages;
Sleep(250);
// check for deck reorganizing
if (TurnStep mod 4 = 0) then
begin
ReorganizeDeckCards;
DominoDeck.TopValue := DeckGaple.TopValue;
DominoDeck.BottomValue := DeckGaple.BottomValue;
DominoDeck.ShowDeck := false;
end;
// store value of available cards
play_cards_count := 0;
for i := 1 to NumberOfCards do
with TDominoCard(FindComponent('DominoCard'+IntToStr(i))) do
begin
if (Tag = Player) and Visible and Enabled then
begin
SetLength(play_cards,play_cards_count+1);
play_cards[play_cards_count].top_value := TopValue;
play_cards[play_cards_count].bottom_value := BottomValue;
play_cards[play_cards_count].index := i;
Inc(play_cards_count);
end;
end;
// search for match cards
some_match := false;
for i := 0 to play_cards_count-1 do
begin
card_match := (play_cards[i].top_value = DeckGaple.TopValue) or
(play_cards[i].bottom_value = DeckGaple.TopValue) or
(play_cards[i].top_value = DeckGaple.BottomValue) or
(play_cards[i].bottom_value = DeckGaple.BottomValue);
play_cards[i].is_match := card_match;
if card_match then some_match := true;
end;
// player allowed to play
if some_match then
begin
// selecting play card algorithm
highest := -1;
card_idx := -1;
highest_idx := -1;
for i := 0 to play_cards_count-1 do
// if first turn, select card which match the deck
if DeckGaple.IsEmpty then
begin
if (play_cards[i].is_match) and
(play_cards[i].top_value = DeckGaple.TopValue) and
(play_cards[i].bottom_value = DeckGaple.BottomValue) then
begin
highest_idx := play_cards[i].index;
card_idx := i;
Break;
end;
end
// else, select card with biggest value
else
begin
if (play_cards[i].is_match) and
((play_cards[i].top_value + play_cards[i].bottom_value) >= highest) then
begin
highest := play_cards[i].top_value + play_cards[i].bottom_value;
highest_idx := play_cards[i].index;
card_idx := i;
end;
end;
// place selected card
if DeckGaple.IsEmpty then
FirstCard(TDominoCard(FindComponent('DominoCard'+IntToStr(highest_idx))))
else if CardMatchBoth(TDominoCard(FindComponent('DominoCard'+IntToStr(highest_idx)))) then
begin
place_on_top := false;
// place on value which there's same value on hand
for i := 0 to play_cards_count-1 do
begin
if play_cards[i].top_value = play_cards[card_idx].top_value then
place_on_top := true
else if play_cards[i].bottom_value = play_cards[card_idx].bottom_value then
place_on_top := false;
end;
if place_on_top then
PlaceCardOnTop(TDominoCard(FindComponent('DominoCard'+IntToStr(highest_idx))))
else
PlaceCardOnBottom(TDominoCard(FindComponent('DominoCard'+IntToStr(highest_idx))));
end
else if CardMatchBottom(TDominoCard(FindComponent('DominoCard'+IntToStr(highest_idx)))) then
PlaceCardOnBottom(TDominoCard(FindComponent('DominoCard'+IntToStr(highest_idx))))
else
PlaceCardOnTop(TDominoCard(FindComponent('DominoCard'+IntToStr(highest_idx))));
// no more turn
if GameIsOver and GameRunning then
begin
// show all disabled cards
GameRunning := false;
for i := 1 to NumberOfCards do
with TDominoCard(FindComponent('DominoCard'+IntToStr(i))) do
if Visible then ShowDeck := false;
LabelStatus.Caption := 'Game is over.';
GameOverInformation;
Exit;
end;
end
// selecting disable card algorithm
else
begin
if play_cards_count > 0 then
begin
lowest := 111;
lowest_idx := -1;
// select card with lowest value
for i := 0 to play_cards_count-1 do
begin
if ((play_cards[i].top_value + play_cards[i].bottom_value) <= lowest) then
begin
lowest := play_cards[i].top_value + play_cards[i].bottom_value;
lowest_idx := play_cards[i].index;
end;
end;
DisableCard(TDominoCard(FindComponent('DominoCard'+IntToStr(lowest_idx))));
end
else
NextPlayerTurn;
end;
end
else
NextPlayerTurn;
Cursor := crDefault;
Application.ProcessMessages;
end;
end;