-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.pl
129 lines (112 loc) · 3.83 KB
/
game.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
%% Author : Reynaud Nicolas (Kaldoran)
:- include('base.pl').
:- include('affichage.pl').
:- include('interaction.pl').
:- include('verif.pl').
:- include('ia.pl').
:- include('ia_offensif.pl').
% Clear the assert prédicate
% -------------------------- %
clear :- clearX, clearO.
clearX :- retract(iPlay(' x ')); true.
clearO :- retract(iPlay(' o ')); true.
%% ------------ Note : X always start the game
% Launch this predicate to play 2 V 2
% ----------------------------------- %
play :-
initialize_game(Board),
clear,
asserta(iPlay(' x ')), asserta(iPlay(' o ')),
play(Board, ' x ', 0).
% Launch this predicate to play Vs AI, AI plays ' o ', you play ' x '
% ----------------------------------------------------------------- %
playX :-
initialize_game(Board),
clear,
asserta(iPlay(' x ')),
askAI(AI),
play(Board, ' x ', AI).
% Launch this predicate to play Vs AI, AI plays ' x ', you play ' o '
% ----------------------------------------------------------------- %
playO :-
initialize_game(Board),
clear,
asserta(iPlay(' o ')),
askAI(AI),
play(Board, ' x ', AI).
% Launch this predicate to watch a battle AI vs AI
% ----------------------------------------------- %
playAIvsAI:-
initialize_game(Board),
clear,
write('For the black pawns. '), askAI(AIB), nl,
write('For the white pawns. '), askAI(AIW), nl,
printBoard(Board),
playAIs(Board, AIB, AIW).
% Recursive predicate for the AI vs AI battle
% ------------------------------------------- %
playAIs(Board, AIB, AIW):-
asserta(iPlay(' o ')),
findPlay(Board, ' x ', 1, Moves, AIB),
write('Black AI had done her move.'), nl,
write('Move : '), write(Moves),
multiMove(Board, Moves, NewBoard),
clearO,
printBoard(NewBoard),
asserta(iPlay(' x ')),
findPlay(NewBoard, ' o ', 1, Moves, AIW),
write('White AI had done her move.'), nl,
write('Move : '), write(Moves),
multiMove(NewBoard, Moves, NewBoard2),
clearX,
printBoard(NewBoard2),
playAIs(NewBoard2, AIB,AIW), !.
% Check if there is a winner on the '+Board', if there is, write the winner
% ------------------------------------------------------------------------- %
play(Board, _, _) :-
isEndGame(Board, Winner),
write('The Winner Is : '),
write(Winner), nl,break.
% Alternative predicate if there is no winner then play on the '+Board' with '+Pawn'
% --------------------------------------------------------------------------------- %
play(Board, Pawn, AI) :-
printBoard(Board),
invert_player(Pawn, EnemyPawn),
(
(
moveLeft(Board, Pawn);
queen(Pawn, Queen), moveLeft(Board, Queen)
) ->
write('Time to play : '), write(Pawn), nl;
write(Pawn), write(' cannot play.'), nl, play(Board, EnemyPawn, AI), !, nl
),
(
iPlay(Pawn),
askMove(From, To, Board, Pawn),
nth(From, Board, PawnMove),
(
isValide(Board, From, To, PawnMove),
move(Board, From, To, PawnMove, NewBoard);
isValideEat(Board, From, Between, To, PawnMove),
removePawn(Board, Between, TmpBoard),
move(TmpBoard, From, To, PawnMove, NewBoard),
printBoard(NewBoard),
(
askEndMove;
play(NewBoard, EnemyPawn, AI)
),
(existNextEat(NewBoard, To, PawnMove) ->
play(NewBoard, Pawn, AI), !;
write('You try to fool me ? I see that there is no more jump possible.'), nl
)
)
;
findPlay(Board, Pawn, 3, Moves, AI),
printIaMove(Moves),
multiMove(Board, Moves, NewBoard),
write('AI had done her move.'), nl,
play(NewBoard, EnemyPawn, AI), !;
write('AI cannot play'), nl,
play(Board, EnemyPawn, AI), !
),
play(NewBoard, EnemyPawn, AI), !.