/
my_wol.pl
166 lines (135 loc) · 5.49 KB
/
my_wol.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
:- use_module(library(system)).
/* START OF TASK 3 */
/* Available levels: quiet, verbose */
log_level(verbose).
test_strategy(0, _, _) :-
!.
test_strategy(N, AStrategy, BStrategy) :-
format('Comparing ~w (P1) with ~w (P2):~n~n',[AStrategy, BStrategy]),
now(StartTime),
test_strategy(N,AStrategy,BStrategy,Moves,Wins),
now(EndTime),
count_elements(Wins,'draw',R),
format('Number of draws: ~w~n',[R]),
count_elements(Wins,'b',R1),
format('Number of wins for player 1 (blue): ~w~n',[R1]),
count_elements(Wins,'r',R2),
format('Number of wins for player 2 (red): ~w~n',[R2]),
find_max_nonexh(Moves,R3),
format('Longest (non-exhaustive) game: ~w~n',[R3]),
find_min(Moves,R4),
format('Shortest game: ~w~n',[R4]),
average(Moves,R5),
format('Average game length (including exhaustives): ~w~n',[R5]),
R6 is (EndTime-StartTime)*1000/N,
Acc is 1000/(sqrt(N)),
format('Average game time: ~w +/- ~w ms~n~n',[R6,Acc]).
test_strategy(0,_,_,[],[]).
test_strategy(N,AStrategy,BStrategy,[NumMoves|Moves],[Winner|Wins]) :-
N>0,
log_level(LogLevel),
play(LogLevel,AStrategy,BStrategy, NumMoves, Winner),
NewN is N-1,
test_strategy(NewN,AStrategy,BStrategy, Moves,Wins).
find_min([Min],Min).
find_min([H1,H2|T],M) :-
(H1<H2;H1=H2),
find_min([H1|T],M).
find_min([H1,H2|T],M) :-
H1>H2,
find_min([H2|T],M).
/* Assumes all 250 moves games are exhaustive (no situation in which smb just wins in last 250th move, which makes it 250 moves but non-exhaustive. Computer Scientists are supposed to be lazy, it's part of the job. */
find_max_nonexh([Max],Max).
find_max_nonexh([250|T],M) :-
find_max_nonesh(T,M).
find_max_nonexh([H1,H2|T],M) :-
(H1<H2;H1=H2),
find_max_nonexh([H2|T],M).
find_max_nonexh([H1,H2|T],M) :-
H1>H2,
find_max_nonexh([H1|T],M).
sum_and_no([],0,0).
sum_and_no([H|T],NewS,NewN) :-
sum_and_no(T,S,N),
NewS is H+S,
NewN is N+1.
average(L,A) :-
sum_and_no(L,S,N),
A is S/N.
count_elements([],_,0).
count_elements([E|T],E,NewR) :-
!,
count_elements(T,E,R),
NewR is R+1.
count_elements([_|T], E, R) :-
count_elements(T,E,R).
/* START OF TASK 4 */
bloodlust(Color, Board, NewBoard, Move) :-
find_best_move(Color, Board, bloodlust, NewBoard, Move,_).
self_preservation(Color, Board, NewBoard, Move) :-
find_best_move(Color, Board, self_preservation, NewBoard, Move, _).
land_grab(Color, Board, NewBoard, Move) :-
find_best_move(Color, Board, land_grab, NewBoard, Move, _).
minimax(Color, Board, NewBoard, Move) :-
find_best_move(Color, Board, minimax, NewBoard, Move, _).
possible_moves(Alive,OtherPlayerAlive, PossMoves) :-
findall([A,B,MA,MB],(member([A,B], Alive),
neighbour_position(A,B,[MA,MB]),
\+member([MA,MB],Alive),
\+member([MA,MB],OtherPlayerAlive)),
PossMoves).
/* decompose_board(+Color,+Board,-Alives,-OpponentsAlives) */
decompose_board('r', [B,R], R,B).
decompose_board('b', [B,R], B,R).
compose_board('r',Alives,OpponentAlives,[OpponentAlives,Alives]).
compose_board('b',Alives,OpponentAlives,[Alives,OpponentAlives]).
opponent('r','b').
opponent('b','r').
/* Returns best move for the given Strategy. */
find_maximizing_move([],_,_,_,'u','u').
find_maximizing_move([Move|Moves], Color, Board, Strategy, BestMove, BestMoveGoal) :-
decompose_board(Color,Board,Alives,OpponentAlives),
alter_board(Move,Alives,NewAlives),
compose_board(Color,NewAlives,OpponentAlives,NewBoard),
next_generation(NewBoard,NewGeneratedBoard),
calculate_score(Color, NewGeneratedBoard,Strategy,MoveGoal),
find_maximizing_move(Moves,Color,Board,Strategy, OldBestMove, OldBestMoveGoal),
((OldBestMoveGoal=='u';OldBestMoveGoal<MoveGoal) ->
(BestMove=Move, BestMoveGoal=MoveGoal) ;
(BestMove=OldBestMove,BestMoveGoal=OldBestMoveGoal)
).
/* Score is the number of opponent's pieces on the board. */
calculate_score(Color, Board, bloodlust, Score) :-
decompose_board(Color,Board,_,OpponentsAlive),
length(OpponentsAlive, OpponentNodes),
Score is -OpponentNodes.
/* Score is the number of player's pieces on the board. */
calculate_score(Color, Board, self_preservation, Score) :-
decompose_board(Color, Board, Alives, _),
length(Alives, Score).
/* Score is the number of player's pieces - number of opponent's pieces. */
calculate_score(Color, Board, land_grab, Score) :-
decompose_board(Color, Board, Alives, OpponentsAlive),
length(OpponentsAlive, OpponentNodes),
length(Alives, AliveNodes),
Score is AliveNodes-OpponentNodes.
/* Returns minimum possible landgrab that we could get after opponents move,
so in other words we want to maximize landgrab of opponent and take it
with negative value sign. */
/* If opponent has no pieces, minmum landgrab player must gain is number of his pieces */
calculate_score(Color, Board, minimax, Score) :-
decompose_board(Color,Board, Alives, []),
!,
length(Alives, Score).
calculate_score(Color, Board, minimax, Score) :-
opponent(Color, OpponentsColor),
find_best_move(OpponentsColor, Board, land_grab, _, _, OpponentsMaxLandgrab),
Score is -OpponentsMaxLandgrab.
/* Given Color and Board find_best_move finds best move according to given Strategy
and returns this Move and board configuration. */
find_best_move(Color,Board,Strategy, NewBoard, Move, Score) :-
decompose_board(Color,Board,Alive,OpponentsAlive),
possible_moves(Alive, OpponentsAlive, Moves),
find_maximizing_move(Moves, Color, Board, Strategy, Move, Score),
alter_board(Move, Alive, NewAlive),
compose_board(Color, NewAlive, OpponentsAlive, NewBoard).