-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictactoe.pl~
268 lines (208 loc) · 7.06 KB
/
tictactoe.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
/*
* This is a simple prolog program for simulations of tic-tac-toe game.
*
* This particular program is to be used only for the Logic in Computer Science
* assignment. Not for external use, could be dangerous.
*
* This is a group project wherein participants are as follows :-
* 1. Atul Rai 2016A7PS0724G
* 2. Hrishikesh Dahiya 2016A7PS0057G
* 3. Mounil Memaya 2016A7PS0077G
* 4. Satyajeet Jena 2016A7PS0054G
* 5. Ayush Gupta 2016A7PS0024G
* 6. Bhavish Singh 2016A7PS0726G
* Copyright for these participants.
*
*
* Here goes your much awaited (the)code
*
* Below predicates define the winning conds
*/
/*
* minimax(Position, BestNextPosition, Val)
* Position is a pos, Val is its minimax value.
* Best move from Position leads to pos BestNextPosition.
*/
minimax(Position, BestNextPosition, Val) :-
bagof(NextPosition, move(Position, NextPosition), NextPositionList),
best(NextPositionList, BestNextPosition, Val), !.
minimax(Position, _, Val) :-
utility(Position, Val).
best([Position], Position, Val) :-
minimax(Position, _, Val), !.
best([Position1 | PositionList], BestPosition, BestVal) :-
minimax(Position1, _, Val1),
best(PositionList, Position2, Val2),
betterOf(Position1, Val1, Position2, Val2, BestPosition, BestVal).
betterOf(Position0, Val0, _, Val1, Position0, Val0) :-
min_to_move(Position0),
Val0 > Val1, !
;
max_to_move(Position0),
Val0 < Val1, !.
betterOf(_, _, Position1, Val1, Position1, Val1).
/*% move(+Position, -NextPosition)
% True if there is a legal (according to rules) move from Position to NextPosition.*/
move([X1, play, Board], [X2, win, NextBoard]) :-
nextPlayer(X1, X2),
move_aux(X1, Board, NextBoard),
winPosition(X1, NextBoard), !.
move([X1, play, Board], [X2, draw, NextBoard]) :-
nextPlayer(X1, X2),
move_aux(X1, Board, NextBoard),
drawPosition(X1,NextBoard), !.
move([X1, play, Board], [X2, play, NextBoard]) :-
nextPlayer(X1, X2),
move_aux(X1, Board, NextBoard).
/*% move_aux(+Player, +Board, -NextBoard)f20160057
% True if NextBoard is Board with an empty case replaced by Player mark.*/
move_aux(P, [0|Bs], [P|Bs]).
move_aux(P, [B|Bs], [B|B2s]) :-
move_aux(P, Bs, B2s).
/*% min_to_move(+Position)
% True if the next player to play is the MIN player.*/
min_to_move([o, _, _]).
/*% max_to_move(+Position)
% True if the next player to play is the MAX player.*/
max_to_move([x, _, _]).
/*% utility(+Position, -Val) :-
% True if Val the the result of the evaluation function at Position.
% We will only evaluate for final pos.
% So we will only have MAX win, MIN win or draw.
% We will use 1 when MAX win
% -1 when MIN win
% 0 otherwise.*/
utility([o, win, _], 1).
utility([x, win, _], -1).
utility([_, draw, _], 0).
/*% winPosition(+Player, +Board)
% True if Player win in Board.*/
winPosition(P, [X1, X2, X3, X4, X5, X6, X7, X8, X9]) :-
equal(X1, X2, X3, P) ;
equal(X4, X5, X6, P) ;
equal(X7, X8, X9, P) ;
equal(X1, X4, X7, P) ;
equal(X2, X5, X8, P) ;
equal(X3, X6, X9, P) ;
equal(X1, X5, X9, P) ;
equal(X3, X5, X7, P).
/*% drawPosition(+Player, +Board)
% True if the game is a draw.*/
drawPosition(_,Board) :-
\+ member(0, Board).
/*% equal(+W, +X, +Y, +Z).
% True if W = X = Y = Z.*/
equal(X, X, X, X).
/*% bestMove(+Position, -NextPosition)
% Compute the best Next Position from Position Position
% with minimax or alpha-beta algorithm.*/
bestMove(Position, NextPosition) :-
minimax(Position, NextPosition, _).
/*% play
% Start the game.*/
play :-
nl,
write('===================='), nl,
write('= Prolog TicTacToe ='), nl,
write('===================='), nl, nl,
write('Rem : x starts the game'), nl,
playAskColor.
/*% playAskColor
% Ask the color for the human player and start the game with it.*/
playAskColor :-
nl, write('Color for human player ? (x or o)'), nl,
read(Player), nl,
(
Player \= o, Player \= x, !,
write('Error : not a valid color !'), nl,
playAskColor
;
EmptyBoard = [0, 0, 0, 0, _, 0, 0, 0, 0],
show(EmptyBoard), nl,
play([x, play, EmptyBoard], Player)
).
/*% play(+Position, +HumanPlayer)
% If next player to play in pos is equal to HumanPlayer -> Human must play
% Ask to human what to do.*/
play([Player, play, Board], Player) :- !,
nl, write('Next move ?'), nl,
read(Position), nl,
(
humanMove([Player, play, Board], [NextPlayer, State, NextBoard], Position), !,
show(NextBoard),
(
State = win, !,
nl, write('End of game : '),
write(Player), write(' win !'), nl, nl
;
State = draw, !,
nl, write('End of game : '),
write(' draw !'), nl, nl
;
play([NextPlayer, play, NextBoard], Player)
)
;
write('-> Bad Move !'), nl,
play([Player, play, Board], Player)
).
/*% play(+Position, +HumanPlayer)
% If it is not human who must play -> Computer must play
% Compute the best move for computer with minimax or alpha-beta.*/
play([Player, play, Board], HumanPlayer) :-
nl, write('Computer play : '), nl, nl,
% Compute the best move
bestMove([Player, play, Board], [NextPlayer, State, BestSuccBoard]),
show(BestSuccBoard),
(
State = win, !,
nl, write('End of game : '),
write(Player), write(' win !'), nl, nl
;
State = draw, !,
nl, write('End of game : '), write(' draw !'), nl, nl
;
play([NextPlayer, play, BestSuccBoard], HumanPlayer)
).
/*% nextPlayer(X1, X2)
% True if X2 is the next player to play after X1.*/
nextPlayer(o, x).
nextPlayer(x, o).
/*% When human play*/
humanMove([X1, play, Board], [X2, State, NextBoard], Position) :-
nextPlayer(X1, X2),
set1(Position, X1, Board, NextBoard),
(
winPosition(X1, NextBoard), !, State = win ;
drawPosition(X1,NextBoard), !, State = draw ;
State = play
).
/*% set1(+Elem, +Position, +List, -resultList).
% Set Elem at Position in List => Result in resultList.
% Rem : counting starts at 1.*/
set1(1, E, [X|Ls], [E|Ls]) :- !, X = 0.
set1(P, E, [X|Ls], [X|L2s]) :-
number(P),
P1 is P - 1,
set1(P1, E, Ls, L2s).
/*% show(+Board)
% Show the board to current output.*/
show([X1, X2, X3, X4, X5, X6, X7, X8, X9]) :-
write(' '), show2(X1),
write(' | '), show2(X2),
write(' | '), show2(X3), nl,
write(' -----------'), nl,
write(' '), show2(X4),
write(' | '), show2(X5),
write(' | '), show2(X6), nl,
write(' -----------'), nl,
write(' '), show2(X7),
write(' | '), show2(X8),
write(' | '), show2(X9), nl.
/*% show2(+Term)
% Write the term to current outupt
% Replace 0 by ' '.*/
show2(X) :-
X = 0, !,
write(' ').
show2(X) :-
write(X).