-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictactoe.pl
297 lines (245 loc) · 7.31 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
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
/*
* 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.
*
* 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
*
*
* Below predicates define the program. Please go through the README.
* It will help you run the code. Our main purpose is not the code but
* the First Order Logic defined in the PDF given with this code.
* The PDF defines all the rules and coditions to be followed while
* executing this code. Have Fun Playing!!
*/
/*
* minimax(Position, BestNextPosition, Value)
* Position is a postion on Board, Value is its minimax value.
* Best move from Position leads to pos BestNextPosition.
*/
minimax(Position, BestNextPosition, Value) :-
bagof(NextPosition, move(Position, NextPosition), NextPositionList),
best(NextPositionList, BestNextPosition, Value), !.
minimax(Position, _, Value) :-
utility(Position, Value).
best([Position], Position, Value) :-
minimax(Position, _, Value), !.
best([Position1 | PositionList], BestPosition, BestValue) :-
minimax(Position1, _, Value1),
best(PositionList, Position2, Value2),
betterOf(Position1, Value1, Position2, Value2, BestPosition, BestValue).
betterOf(Position0, Value0, _, Value1, Position0, Value0) :-
min_to_move(Position0),
Value0 > Value1, !
;
max_to_move(Position0),
Value0 < Value1, !.
betterOf(_, _, Position1, Value1, Position1, Value1).
/*
* 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, -Value) :-
* True if Value 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('= TicTacToe ='), nl,
write('===================='), nl, nl,
write('Reminder : x starts the game'), nl,
playAskPlayer.
/*
* playAskPlayer
* Ask the player ('x' or 'o') for the human player and start the game with it.
*/
playAskPlayer :-
nl, write('Choose for human player ? (x or o)'), nl,
read(Player), nl,
(
Player \= o, Player \= x, !,
write('Error : not a valid player !'), nl,
playAskPlayer
;
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).