-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathia.pl
261 lines (209 loc) · 8.86 KB
/
ia.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
%% Author : Reynaud Nicolas (Kaldoran)
% evalEndGame is called for give the Eval of and ending point
% According to the '+Pawn'
% ----------------------------------------------------------- %
evalEndGame(100, Pawn) :-
iPlay(Pawn), !.
evalEndGame(-100, _).
% Evaluate the '+Board' according to the '+Pawn'
% And output the evaluation of the board in '-Value'
% ------------------------------------------------- %
% Heuristic about the number of pawns
% ----------------------------------- %
eval(Board, Value, Pawn, 1) :-
isX(Pawn),
findall(X, (member(X, Board), isX(X)), ResultX),
findall(O, (member(O, Board), isO(O)), ResultO),
length(ResultX, LRX),
length(ResultO, LRO),
Value is LRX - LRO, !.
eval(Board, Value, Pawn, 1) :-
isO(Pawn),
findall(X, (member(X, Board), isX(X)), ResultX),
findall(O, (member(O, Board), isO(O)), ResultO),
length(ResultX, LRX),
length(ResultO, LRO),
Value is LRO - LRX, !.
% Heuristic about the stategic positions
%% -------------------------------------- %%
eval(Board, Value, Pawn, 2) :-
valueBoard(Board, 1, Value, Pawn).
% Heuristic about the fragility defensive of the adversary
% -------------------------------------------------------- %
eval(Board, Value, Pawn, 3) :-
isX(Pawn),
findAllRaid(Pawn, Board, ResultRX),
findAllRaid((X, isO(X)), Board, ResultRO),
Value is ResultRX - ResultRO, !.
eval(Board, Value, Pawn, 3) :-
isO(Pawn),
findAllRaid(Pawn, Board, ResultRX),
findAllRaid((O, isO(O)), Board, ResultRO),
Value is ResultRO - ResultRX, !.
% Evaluate the '+Board' according to the sqares' values
% And output the evaluation of the board in '-Value'
% ----------------------------------------------------- %%
valueBoard([Sqr], _, 0, _):-
\+isPawn(Sqr),!.
valueBoard([Sqr], _, 6, Pawn):-
isSameColor(Sqr,Pawn), !.
valueBoard([_], _, -10, _):-!.
valueBoard([Sqr|RestBoard], Pos, Value, Pawn):-
\+isPawn(Sqr),Pos2 is Pos + 1,
valueBoard(RestBoard, Pos2, Value, Pawn), !.
valueBoard([Sqr|RestBoard], Pos, Value, Pawn):-
isSameColor(Sqr,Pawn),Pos2 is Pos + 1,
valueSqr(Pos,ValueSqr),
valueBoard(RestBoard, Pos2, Value2, Pawn),
Value is Value2 + ValueSqr, !.
valueBoard([_|RestBoard], Pos, Value, Pawn):-
Pos2 is Pos + 1,
valueBoard(RestBoard, Pos2, Value2, Pawn),
Value is Value2 - 10,!.
% Associate values to the squares
% ------------------------------- %
valueSqr(Pos,6):-
member(Pos,[2,4,6,8,10,91,93,95,97,99]),!.
valueSqr(Pos,5):-
member(Pos,[11,30,31,50,51,70,71,90]),!.
valueSqr(Pos,4):-
member(Pos,[13,15,17,19,22,39,42,59,62,79,82,84,86,88]),!.
valueSqr(Pos,3):-
member(Pos,[24,26,28,33,48,53,68,73,75,77]),!.
valueSqr(Pos,2):-
member(Pos,[35,37,44,57,64,66]),!.
valueSqr(Pos,1):-
member(Pos,[46,55]).
% Look for the max of '+Eval' and '+AllMove'
% Matching the '+Eval' best move with his '+AllMove' move
% And put it into '-BestMove'
% ------------------------------------------------------- %
seekMin(_, [BestMove], BestMove) :- !.
seekMin([X, Y|Eval], [_|AllMoves], BestMove) :-
X > Y, seekMin([Y|Eval], AllMoves, BestMove), !.
seekMin([X, _|Eval], [A, _|AllMoves], BestMove) :-
seekMin([X|Eval], [A|AllMoves], BestMove).
% Look on the '+Board' with the '+Pawn' whats the '-BestMove' to do
% In a '+Depth' according to a minmax function
% ----------------------------------------------------------------- %
findPlay(Board, Pawn, Depth, BestMove, AI) :-
findAllMove(Board, Pawn, AllMoves),
simulate(Board, Pawn, AllMoves, Depth, Eval, AI),
writeDebug(Eval),
writeDebug(AllMoves),
seekMin(Eval, AllMoves, BestMove),!.
% Simulate a '+Move' on a '+Board'
% with the '+Pawn' and complete the '-Eval'
% ----------------------------------------- %
simulate(_, _, [], _, [], _) :- !.
simulate(Board, Pawn, [Move|AllMoves], Depth, [EvalBis|Eval], AI) :-
multiMove(Board, Move, NewBoard),
NewDepth is Depth - 1,
invert_player(Pawn, EnemyPawn),
(
iPlay(Pawn), min(NewBoard, EnemyPawn, NewDepth, EvalBis, AI); %% If its the human turn to simulate
max(NewBoard, EnemyPawn, NewDepth, EvalBis, AI)
),
simulate(Board, Pawn, AllMoves, Depth, Eval, AI).
% Min on the '+Board'
% Simulate a move on the '+Board'
% ------------------------------ %
min(Board, Pawn, 0, Eval, AI) :-
eval(Board, Eval, Pawn, AI), !.
min(Board, _, _, Eval, _) :-
isEndGame(Board, Winner),
evalEndGame(Eval, Winner), !.
min(Board, Pawn, Depth, Eval, AI) :-
findAllMove(Board, Pawn, AllMoves),
simulate(Board, Pawn, AllMoves, Depth, EvalList, AI),
min_list(EvalList, Eval).
% Max part of the minmax
% ---------------------- %
max(Board, Pawn, 0, Eval, AI) :-
eval(Board, Eval, Pawn, AI), !.
max(Board, _, _, Eval, _) :-
isEndGame(Board, Winner),
evalEndGame(Eval, Winner).
max(Board, Pawn, Depth, Eval, AI) :-
findAllMove(Board, Pawn, AllMoves),
simulate(Board, Pawn, AllMoves, Depth, EvalList, AI),
max_list(EvalList, Eval).
% Find all available move for a '+Pawn'
% on the '+Board' and complet the '-AllMoves'
% ------------------------------------------ %
findAllMove(Board, Pawn, AllMoves) :-
findall(Place, nth1(Place, Board, ' '), BlankSpace),
findAllQueenMoves(Board, Pawn, AllQueenMoves, BlankSpace),
seekMoves(Board, BlankSpace, Pawn, AllRegularMoves),
append(AllRegularMoves, AllQueenMoves, AllMoves).
% Predicate that actually found all the move
% ------------------------------------------ %
seekMoves(_, [], _, []).
seekMoves(Board, [To|BlankSpace], Pawn, AllMoves) :-
findall([Place, Between], existValideEat(Board, Place, Between, To, Pawn), ResultEat),
findall(Place, existValide(Board, Place, To, Pawn), ResultValide),
allMovesVal(ResultValide, To, AllPawnMoves),
allMovesEat(Board, Pawn, ResultEat, To, AllMovesEat),
append(AllPawnMoves, AllMovesEat, AllMovesGlobale),
seekMoves(Board, BlankSpace, Pawn, AllMovesSeek),
append(AllMovesGlobale, AllMovesSeek, AllMoves), !.
% Look for allMove
% then appply them and complet the '-All' result
% ---------------------------------------------- %
allMovesEat(_, _, [], _, []).
allMovesEat(Board, Pawn, [[From, Between]|Result], To, All) :-
multiMove(Board, [[From, Between, To]], NewBoard),
seekMultiMove(NewBoard, To, Pawn, MultiMove),
append([[From, Between, To]], MultiMove, MultiEat),
allMovesEat(Board, Pawn, Result, To, AllMoves),
append(AllMoves, [MultiEat], All), !.
% Look for all move after a given move
% ------------------------------------ %
seekMultiMove(Board, From, Pawn, [[From, Between, To]|MultiMove]) :-
existValideEatFrom(Board, From, Between, To, Pawn),
multiMove(Board, [[From, Between, To]], NewBoard),
seekMultiMove(NewBoard, To, Pawn, MultiMove).
seekMultiMove(_, _, _, []).
% Complet the '-AllMoves' array
% ----------------------------- %
allMovesVal([], _, []).
allMovesVal([From|Result], To, [[From, To]|AllMoves]) :-
allMovesVal(Result, To, AllMoves).
% Given a '+Pawn' find '-AllQueenMoves' associate to Queen on the '+Board'
% ------------------------------------------------------------------------ %
findAllQueenMoves(Board, Pawn, AllQueenMoves, BlankSpace) :-
queen(Pawn, Queen),
findall(Place, nth(Place, Board, Queen), AllQueen),
allMovesQueen(Board, AllQueen, BlankSpace, Queen, AllQueenMoves).
% Predicate who call test for all Queen
% ------------------------------------- %
allMovesQueen(_, [], _, _, []) :- !.
allMovesQueen(Board, [From|AllQueen], BlankSpace, Queen, AllQueenMoves) :-
seekAllMovesQueen(Board, From, BlankSpace, Queen, AllQMoves),
append(AllQMoves, AQM, AllQueenMoves),
allMovesQueen(Board, AllQueen, BlankSpace, Queen, AQM).
% This predicate actually do the test by looking the moove
% If is a value one between From and To then it add it to '-AllQueenMoves'
% ----------------------------------------------------------------------- %
%% This one do ths for Normal move
seekAllMovesQueen(_, _, [], _, []) :- !.
seekAllMovesQueen(Board, From, [To|BlankSpace], Queen, [[From, To]|AllQueenMoves]) :-
Distance is abs(From - To),
(
mod(Distance, 11) =:= 0;
mod(Distance, 9) =:= 0
),
isValideSpecial(Board, From, To, Queen),
seekAllMovesQueen(Board, From, BlankSpace, Queen, AllQueenMoves), !.
%% This one for Eat done by a queen
seekAllMovesQueen(Board, From, [To|BlankSpace], Queen, [[[From, Between, To]]|AllQueenMoves]) :-
Distance is abs(From - To),
(
mod(Distance, 11) =:= 0;
mod(Distance, 9) =:= 0
),
isValideEat(Board, From, Between, To, Queen),
seekAllMovesQueen(Board, From, BlankSpace, Queen, AllQueenMoves), !.
%% If no move had been found from From to To then try next one
seekAllMovesQueen(Board, From, [_|BlankSpace], Queen, AllQueenMoves) :-
seekAllMovesQueen(Board, From, BlankSpace, Queen, AllQueenMoves).