forked from kenegozi/kalah-prolog
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgui.pl
400 lines (337 loc) · 10.9 KB
/
gui.pl
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/***********************************************************************
Program : Kalah game in PROLOG
Written by : Ken Egozi
File : gui.pl
Description : gui related facts and predicates to interact
with the user and control application flow
***********************************************************************/
% entry pont - start the game
start :-
set_brushes,
show_window(dlg_main_window).
/**************************************
Visual settings
***************************************/
basic_width(30).
vertical_separator(10).
horizontal_separator(20).
padding(20).
default_pit_colour(lightbrown).
highlight_pit_colour(green).
collected_pit_colour(yellow).
highlighted_kalah_pit_colour(lightblue).
default_bg_colour(brown).
% extracting settings from the database
get_board_settings(BasicWidth/VSep/HSep/Padding) :-
basic_width(BasicWidth),
vertical_separator(VSep),
horizontal_separator(HSep),
padding(Padding).
% setting gui brushes
set_brushes :-
gfx_brush_create(brush_white,255,255,255,solid),
gfx_brush_create(brush_red,255,0,0,solid),
gfx_brush_create(brush_green,0,255,0,solid),
gfx_brush_create(brush_blue,0,0,255,solid),
gfx_brush_create(brush_yellow,255,255,20,solid),
gfx_brush_create(brush_lightblue,155,170,20,solid),
gfx_brush_create(brush_brown,124,106,93,solid),
gfx_brush_create(brush_lightbrown,190,181,174,solid),
gfx_font_create( font_pits, 'Arial', 17, normal ).
% mapping colours to brushes
get_brush(red, brush_red).
get_brush(green, brush_green).
get_brush(blue, brush_blue).
get_brush(yellow, brush_yellow).
get_brush(lightblue, brush_lightblue).
get_brush(brown, brush_brown).
get_brush(lightbrown, brush_lightbrown).
/**************************************
Data calculations
***************************************/
%calculate the needed board size for the game size
board_size(Size, W, H) :-
basic_width(BasicWidth),
vertical_separator(VerticalSeparator),
horizontal_separator(HorizontalSeparator),
padding(Padding),
W is
(Size + 2) * BasicWidth +
(Size + 1) * VerticalSeparator +
2 * Padding,
H is
2 * BasicWidth +
HorizontalSeparator +
2 * Padding.
% dimensions of a pit
pit_size(pit, Width,Width) :-
basic_width(Width).
% dimensions of a kalah
pit_size(kalah, Width,Height) :-
basic_width(Width),
horizontal_separator(Separator),
Height is Width * 2 + Separator.
/**************************
Game board background
***************************/
draw_board_bg:-
game_board(Board),
default_bg_colour(Colour),
get_brush(Colour, Brush),
pits(Size),
board_size(Size, BoardWidth, BoardHeight),
gfx_begin(Board),
gfx((brush=Brush->
rectangle(0, 0, BoardWidth, BoardHeight)
)),
gfx_end(Board).
/**********************************
Draw the pits
***********************************/
% draw a pit with a given colour at a given anchor
draw_pit(P, Left/Top, BasicWidth, Colour) :-
Bottom is Top + BasicWidth,
Right is Left + BasicWidth,
draw_pit(P, Left, Top, Right, Bottom, Colour).
% draw a pit with a given colour at a given location
draw_pit(P, Left, Top, Right, Bottom, Colour) :-
get_brush(Colour, Brush),
gfx((brush=Brush->
ellipse(Left, Top, Right, Bottom)
)),
write_text(P, Left, Top, Right, Bottom).
%drawing a player one pit (lower row)
draw_pit(P, player1, PitNo, BasicWidth/VSep/HSep/Padding, Colour) :-
Top is Padding + BasicWidth + HSep,
Left is Padding + (BasicWidth + VSep) * PitNo,
draw_pit(P, Left/Top, BasicWidth , Colour).
%drawing a player two pit (upper row)
draw_pit(P, player2, PitNo, BasicWidth/VSep/HSep/Padding, Colour) :-
Top is Padding,
Left is Padding + (BasicWidth + VSep) * PitNo,
draw_pit(P, Left/Top, BasicWidth , Colour).
%drawing the player one kalah (lower row)
draw_kalah(P, player1, PitNo, BasicWidth/VSep/HSep/Padding, Colour) :-
Top is Padding,
Bottom is Top + BasicWidth * 2 + HSep,
Left is Padding + (BasicWidth + VSep) * PitNo,
Right is Left + BasicWidth,
draw_pit(P, Left, Top, Right, Bottom, Colour).
%drawing the player two kalah (upper row)
draw_kalah(P, player2, _, BasicWidth/_/HSep/Padding, Colour) :-
Top is Padding,
Bottom is Top + BasicWidth * 2 + HSep,
Left is Padding,
Right is Left + BasicWidth,
draw_pit(P, Left, Top, Right, Bottom, Colour).
% redrawing all pits
draw_all_pits:-
pos(_/P1/P2),!,
draw_all_pits(P1,P2).
%call to drawing all pits
draw_all_pits(P1,P2) :- !,
game_board(Board),
gfx_begin(Board),
draw_pits(P1),
draw_pits(P2),
gfx_end(Board).
%call to drawing a particular player's pits
draw_pits(Pits) :-
get_board_settings(Settings),
default_pit_colour(Colour),
Pits =.. [pits, Player|PitsList],
draw_pits(Player, PitsList, 1, Settings, Colour).
draw_pits(Player, [P], PitNo, Settings, Colour) :- !,
draw_kalah(P, Player, PitNo, Settings, Colour).
draw_pits(Player, [P|Ps], PitNo, Settings, Colour) :-
pit_to_draw(Player,PitNo,PitToDraw),
draw_pit(P, Player, PitToDraw, Settings, Colour),
NextPitNo is PitNo + 1,
draw_pits(Player, Ps, NextPitNo, Settings, Colour).
draw_pit(P, player1, PitNo, BasicWidth/VSep/HSep/Padding, Colour) :-
Top is Padding + BasicWidth + HSep,
Left is Padding + (BasicWidth + VSep) * PitNo,
draw_pit(P, Left/Top, BasicWidth , Colour).
pit_to_draw(player1, PitNo, PitNo):-!.
pit_to_draw(player2, PitNo, PitToDraw):-!,
pits(Size),
PitToDraw is Size - PitNo + 1.
% set memory
set_pits(P) :-
(retract(pits(_)) ; true),!,
assert(pits(P)).
set_level(L) :-
(retract(level(_)) ; true),!,
assert(level(L)).
set_first(F) :-
(retract(first(_)) ; true),!,
assert(first(F)).
set_pit_played(Played) :-
(retract(pit_played(_)) ; true),!,
assert(pit_played(Played)).
clear_special:-
(retract(special(_)) ; true),!.
set_special(null) :-!,
clear_special.
set_special(Special) :-
clear_special,
assert(special(Special)).
set_pos(Pos) :-
(retract(pos(_)); true),
assert(pos(Pos)).
set_game_state(S):-
(retract(game_state(_)) ; true),
assert(game_state(S)).
pit(Pit) :-
pits(Size),
in_range(Pit, 1-Size).
get_pit(X/Y, PitNo) :-
pit(Pit),
in_pit(X/Y, Pit),
Pit=PitNo.
% asserting pit map for fast retreival - for determining mouse location
assert_pit_map :-
(retract(map_player1_pit(_,_)); true),
get_board_settings(Settings),
pits(Size),
assert_pit_map(Size, Settings).
assert_pit_map(0, _) :-!.
assert_pit_map(PitNo, BasicWidth/VSep/HSep/Padding) :-
Top is Padding + BasicWidth + HSep,
Left is Padding + (BasicWidth + VSep) * PitNo,
Bottom is Top + BasicWidth * 2 + HSep,
Right is Left + BasicWidth,
Margin=5,
Top1 is Top + Margin,
Left1 is Left + Margin,
Bottom1 is Bottom - Margin,
Right1 is Right - Margin,
assert(map_player1_pit(PitNo, Top1/Left1/Bottom1/Right1)),
NextPit is PitNo - 1,
assert_pit_map(NextPit, BasicWidth/VSep/HSep/Padding).
% assert initial pos based on game settings
assert_pos :-
pits(P),
create_list(P1PitsList, P, P),
conc(P1PitsList, [0], P1PitsListWithKalah),
P1Pits =.. [pits,player1|P1PitsListWithKalah],
create_list(P2PitsList, P, P),
conc(P2PitsList, [0], P2PitsListWithKalah),
P2Pits =.. [pits,player2|P2PitsListWithKalah],
first(First),
to_player(First, Player),
set_pos(Player/P1Pits/P2Pits).
% true if the XY (of mouse) is in human's pit no. PitNo
in_pit(X/Y, PitNo):-
map_player1_pit(PitNo, Top/Left/Bottom/Right),
Y >= Top, Y =< Bottom,
X >= Left, X =< Right.
%new game initialisation
start_new_game :-
set_game_state(new),
assert_pos,
draw_board_bg,
draw_all_pits,
play.
%show messages
show_game_over_message:-
winner(Player),
player_win_message(Player, Message),
msgbox('Game over', Message, 0, _).
player_win_message(player1, 'You won - the computer is useless').
player_win_message(player2, 'The computer has won - better luck next time').
player_win_message(tie, 'Tie - try again').
add_message(M):-
(retract(messages(Ms)), ! ; Ms=[]),
assert(messages([M|Ms])).
show_messages:-
(retract(messages(Ms)), ! ; Ms=[]),
assert(messages(Ms)),
gfx_begin((dlg_game_board,10003)),
clear_messages_area,
show_messages(Ms, 3),
gfx_end((dlg_game_board,10003)).
%write into the output console (messages area)
show_messages([], _):-!.
show_messages(_, 0):-!.
show_messages([M|Ms], Line):-
write_message(M, Line),
Line1 is Line - 1,
show_messages(Ms, Line1).
write_message(M, Line):-
X is 20,
Y is (Line - 1) * 15,
gfx((font=font_pits->
text(X,Y,M))).
clear_messages_area:-
pits(Size),
board_size(Size, BoardWidth, _),
gfx((brush=brush_white->
rectangle(0, 0, BoardWidth, 50)
)).
%obvious
highlight_played_pit:-
(retract(pit_played(PitPlayed/SeedsInPit)),!,
game_board(Board),
gfx_begin(Board),
get_board_settings(Settings),
default_pit_colour(Colour),
highlight_pit_colour(HighlightColour),
pit_to_draw(player2,PitPlayed,PitToDraw),
draw_pit(SeedsInPit, player2, PitToDraw, Settings, HighlightColour),
sleep(200),
draw_pit(SeedsInPit, player2, PitToDraw, Settings, Colour),
sleep(200),
draw_pit(SeedsInPit, player2, PitToDraw, Settings, HighlightColour),
sleep(200),
draw_pit(SeedsInPit, player2, PitToDraw, Settings, Colour),
gfx_end(Board)
; true).
%obvious
%special is collect, or last seed put in kalah
highlight_special:-
retract(special(special(Turn/PitNo/Seeds/OppositePitNo/OppositeSeeds))),!,
game_board(Board),
gfx_begin(Board),
get_board_settings(Settings),
default_pit_colour(Colour),
collected_pit_colour(HighlightColour),
next_player(Turn, Opposite),
pit_to_draw(Turn,PitNo,PitToDraw),
pit_to_draw(Opposite,OppositePitNo,OppositePitToDraw),
draw_pit(Seeds, Turn, PitToDraw, Settings, HighlightColour),
draw_pit(OppositeSeeds, Opposite, OppositePitToDraw, Settings, HighlightColour),
sleep(200),
draw_pit(Seeds, Turn, PitToDraw, Settings, Colour),
draw_pit(OppositeSeeds, Opposite, OppositePitToDraw, Settings, Colour),
sleep(200),
draw_pit(Seeds, Turn, PitToDraw, Settings, HighlightColour),
draw_pit(OppositeSeeds, Opposite, OppositePitToDraw, Settings, HighlightColour),
sleep(200),
draw_pit(Seeds, Turn, PitToDraw, Settings, Colour),
draw_pit(OppositeSeeds, Opposite, OppositePitToDraw, Settings, Colour),
gfx_end(Board).
highlight_special:-
retract(special(kalah(Turn))),!,
game_board(Board),
gfx_begin(Board),
get_board_settings(Settings),
highlighted_kalah_pit_colour(HighlightColour),
default_pit_colour(Colour),
is_kalah(PitNo),
pos(_/P1/P2),
(Turn=player1,!,
empty_pit(P1, PitNo, Seeds, _)
;
empty_pit(P2, PitNo, Seeds, _)
),
draw_kalah(Seeds, Turn, PitNo, Settings, HighlightColour),
sleep(200),
draw_kalah(Seeds, Turn, PitNo, Settings, Colour),
sleep(200),
draw_kalah(Seeds, Turn, PitNo, Settings, HighlightColour),
sleep(200),
draw_kalah(Seeds, Turn, PitNo, Settings, Colour),
gfx_end(Board).
%do not fail if the was no special n last move
highlight_special:-!.