forked from kahosato/war_of_life
-
Notifications
You must be signed in to change notification settings - Fork 1
/
war_of_life.pl
352 lines (264 loc) · 8.85 KB
/
war_of_life.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
% version for SICStus 4.x
:- use_module(library(lists)).
:- use_module(library(ordsets)).
:- use_module(library(random)).
:- use_module(library(system)).
%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%% PLAYING THE GAME: BASIC CONTROL
%%%%% play/5
play(ShowFlag, FirstPlayerStrategy, SecondPlayerStrategy, TotalMoves, Winner) :-
start_config(random, Board),
(
ShowFlag == verbose,
format('~nInitial State:~n~n', []),
draw_board(Board),
show_score(verbose, Board)
;
ShowFlag == quiet
),
!,
make_move(Board, ShowFlag, _, 'b', FirstPlayerStrategy, 'r', SecondPlayerStrategy, 0, TotalMoves, Winner).
%%%%% make_move/10
%
% Arguments are as follows:
%
% make_move(Board, ShowBoard, FinalBoard, Player, PlayerStrat, NextPlayer, NextPlayerStrat, Moves, TotalMoves, Winner)
make_move([[],[]], ShowFlag, [[],[]], _, _, _, _, NumMoves, NumMoves, 'draw') :-
!,
show_winner(ShowFlag, 'draw', NumMoves).
make_move(_, ShowFlag, _, _, _, _, _, 250, 250, 'exhaust') :-
!,
show_winner(ShowFlag, 'exhaust', 250).
make_move([[],Reds], ShowFlag, [[],Reds], _, _, _, _, NumMoves, NumMoves, 'r') :-
!,
show_winner(ShowFlag, 'red', NumMoves).
make_move([Blues,[]], ShowFlag, [Blues,[]], _, _, _, _, NumMoves, NumMoves, 'b') :-
!,
show_winner(ShowFlag, 'blue', NumMoves).
make_move(Board, ShowFlag, FinalBoard, Player, Strategy, NextPlayer, NextStrategy, NumMoves, TotalMoves, Winner) :-
NewNumMoves is NumMoves + 1,
move_piece(Player, Strategy, Board, NewBoard, Move),
show_move(ShowFlag, NewNumMoves, Player, Move),
draw_board(ShowFlag, NewBoard),
next_generation(NewBoard, CrankedNewBoard),
draw_board(ShowFlag, CrankedNewBoard),
show_score(ShowFlag, CrankedNewBoard),
!,
make_move(CrankedNewBoard, ShowFlag, FinalBoard, NextPlayer, NextStrategy, Player, Strategy, NewNumMoves, TotalMoves, Winner).
make_move(_, ShowFlag, _, _, _, _, _, TotalMoves, TotalMoves, 'stalemate') :-
show_winner(ShowFlag, 'Stalemate', TotalMoves).
%%%%% alter_board/3
%
% replaces a pair [A,B] with [MA,MB] in Alives; result is NewAlives
% Alives must be ordered; NewAlives will be too.
alter_board([A,B,MA,MB], Alives, NewAlives) :-
ord_del_element(Alives, [A,B], AlivesMinus),
ord_add_element(AlivesMinus, [MA,MB], NewAlives).
%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%% RANDOM MOVE STRATEGY
random_move(Alive, OtherPlayerAlive, Move) :-
findall([A,B,MA,MB],(member([A,B], Alive),
neighbour_position(A,B,[MA,MB]),
\+member([MA,MB],Alive),
\+member([MA,MB],OtherPlayerAlive)),
PossMoves),
length(PossMoves,L),
LP1 is L + 1,
random(1, LP1, Pos),
nth1(Pos, PossMoves, Move).
move_piece('b', random, [AliveBlues, AliveReds], [NewAliveBlues, AliveReds], Move) :-
random_move(AliveBlues, AliveReds, Move),
alter_board(Move, AliveBlues, NewAliveBlues).
move_piece('r', random, [AliveBlues, AliveReds], [AliveBlues, NewAliveReds], Move) :-
random_move(AliveReds, AliveBlues, Move),
alter_board(Move, AliveReds, NewAliveReds).
%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%% SUPPORT FOR OTHER STRATEGIES
move_piece(PieceColour, bloodlust, Board, NewBoard, Move) :-
bloodlust(PieceColour, Board, NewBoard, Move).
move_piece(PieceColour, self_preservation, Board, NewBoard, Move) :-
self_preservation(PieceColour, Board, NewBoard, Move).
move_piece(PieceColour, land_grab, Board, NewBoard, Move) :-
land_grab(PieceColour, Board, NewBoard, Move).
move_piece(PieceColour, safety_in_numbers, Board, NewBoard, Move) :-
safety_in_numbers(PieceColour, Board, NewBoard, Move).
move_piece(PieceColour, minimax, Board, NewBoard, Move) :-
minimax(PieceColour, Board, NewBoard, Move).
move_piece(PieceColour, divide_and_conquer, Board, NewBoard, Move) :-
divide_and_conquer(PieceColour, Board, NewBoard, Move).
%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%% STARTING CONFIGURATIONS
%%%%% start_config/2
start_config(random, [OrdBlues,OrdReds]) :-
!,
findall([R,C], cell(R,C), Cells),
pick(12, Cells, Blues, Rest),
pick(12, Rest, Reds, _),
list_to_ord_set(Blues, OrdBlues),
list_to_ord_set(Reds, OrdReds).
start_config(cross, [[[1,1],[2,2],[3,3],[4,4],[5,5],[6,6],[7,7],[8,8]],
[[1,8],[2,7],[3,6],[4,5],[5,4],[6,3],[7,2],[8,1]]]) :-
!.
start_config(checkers, [[[3,1],[4,2],[3,3],[4,4],[3,5],[4,6],[3,7],[4,8]],
[[5,1],[6,2],[5,3],[6,4],[5,5],[6,6],[5,7],[6,8]]]) :-
!.
start_config(gliders, [[[2,1],[3,2],[3,3],[1,3],[2,3]],
[[6,6],[6,7],[6,8],[7,6],[8,7]]]) :-
!.
start_config(X,X) :-
ground(X).
%%%%% cell/2
%
% backtracks to find all cells
cell(A, B) :-
member(A, [1,2,3,4,5,6,7,8]),
member(B, [1,2,3,4,5,6,7,8]).
%%%%% pick/4
pick(Total, From, Picked, Rest) :-
pick_aux(0, Total, From, Picked, Rest).
%%%%% pick_aux/5
pick_aux(Total, Total, Rest, [], Rest) :-
!.
pick_aux(N, Total, From, [E|Picked], Rest) :-
random_select(E, From, NewFrom),
N1 is N + 1,
pick_aux(N1, Total, NewFrom, Picked, Rest).
%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%% CONWAY CRANK (NEXT GENERATION)
%%%%% next_generation/2
%
% basc control for Conway next generation
next_generation(Board, [NewAliveBlues, NewAliveReds]) :-
findall([A,B,NewW], (cell(A,B),
what_in_cell(Board, A, B, W),
change_cell(Board, A, B, W, NewW)),
ABWs),
findall([A,B], member([A,B,b], ABWs), NewAliveBlues),
findall([A,B], member([A,B,r], ABWs), NewAliveReds).
%%%%% what_in_cell/4
what_in_cell([AliveBlues, _], A, B, 'b') :-
member([A,B], AliveBlues).
what_in_cell([_, AliveReds], A, B, 'r') :-
member([A,B], AliveReds).
what_in_cell([AliveBlues, AliveReds], A, B, ' ') :-
\+ member([A,B], AliveBlues),
\+ member([A,B], AliveReds).
%%%%% cchange_cell/5
change_cell([AliveBlues, AliveReds], A, B, W, NewW) :-
findall(b, (neighbour_position(A,B,[NA,NB]),
member([NA,NB], AliveBlues)),
Bs),
findall(r, (neighbour_position(A,B,[NA,NB]),
member([NA,NB], AliveReds)),
Rs),
length(Bs, BL),
length(Rs, RL),
populate_cell(BL,RL,W,NewW),
!.
%%%%% neighbour_position/3
neighbour_position(A,B,[I,J]) :-
AM1 is A - 1,
AP1 is A + 1,
BM1 is B - 1,
BP1 is B + 1,
L = [AM1,A,AP1],
K = [BM1,B,BP1],
member(I,L),
member(J,K),
\+ (I == A, J == B),
\+ I == 0,
\+ J == 0,
\+ I > 8,
\+ J > 8.
%%%%% populate_cell/4
populate_cell(3,0,' ',b).
populate_cell(0,3,' ',r).
populate_cell(2,1,' ',b).
populate_cell(1,2,' ',r).
populate_cell(NumBlues,NumReds,X,X) :-
2 is NumBlues + NumReds.
populate_cell(NumBlues,NumReds,X,X) :-
3 is NumBlues + NumReds.
populate_cell(_,_,_,' ').
%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%% DRAWING THE BOARD
%%%%% draw_board/1
%
% wrapper for draw_board/2
draw_board(Board) :-
draw_board(verbose, Board).
%%%%% draw_board/2
%
% draws a board, if needed
draw_board(quiet, _) :-
!.
draw_board(verbose, Board) :-
format(' 12345678~n +--------+', []),
draw_cells(1, 1, Board),
format('~n +--------+~n~n', []).
%%%%% draw_cells/3
%
% draw the right colour in cells
% beginning of row
draw_cells(A, 1, ColouredCells) :-
!,
format('~n~w|', [A]),
draw_cell(A, 1, ColouredCells, NewColouredCells),
draw_cells(A, 2, NewColouredCells).
% end of row
draw_cells(A, 8, ColouredCells) :-
!,
draw_cell(A, 8, ColouredCells, NewColouredCells),
format('|', []),
(
A = 8
-> true
; A1 is A + 1,
draw_cells(A1, 1, NewColouredCells)
).
% middle of row
draw_cells(A, B, ColouredCells) :-
draw_cell(A, B, ColouredCells, NewColouredCells),
B1 is B + 1,
draw_cells(A, B1, NewColouredCells).
%%%%% draw_cell/4
%
% draw the right colour in a cell
draw_cell(A, B, [[[A,B]|RestBlues],Reds], [RestBlues,Reds]) :-
!,
format('b', []).
draw_cell(A, B, [Blues,[[A,B]|RestReds]], [Blues,RestReds]) :-
!,
format('r', []).
draw_cell(_, _, ColouredCells, ColouredCells) :-
format(' ', []).
%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%% SHOWING THE SCORE
%%%%% show_score/2
show_score(quiet, _) :-
!.
show_score(verbose, [AliveBlues, AliveReds]) :-
length(AliveBlues, BL),
length(AliveReds, RL),
format('~nblue score = ~w~nredscore = ~w~n~n', [BL,RL]).
%%%%% show_move/4
show_move(quiet, _, _, _) :-
!.
show_move(verbose, Num, Player, Move) :-
format('~w. ~w moves ~w~n~n', [Num,Player,Move]).
%%%%% show_winner/3
show_winner(quiet, _, _) :-
!.
show_winner(verbose, 'Exhaust', Num) :-
format('Game is drawn due to exhaustion after ~w moves!~n~n', [Num]).
show_winner(verbose, 'Draw', Num) :-
format('Game is drawn after ~w moves!~n~n', [Num]).
show_winner(verbose, Winner, Num) :-
format('~w wins after ~w moves!~n~n', [Winner,Num]).