-
Notifications
You must be signed in to change notification settings - Fork 0
/
othello.pl~
154 lines (130 loc) · 5.61 KB
/
othello.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
:- consult('./grid.pl').
:- consult('./play_firstMove.pl').
:- consult('./play_aleatoire.pl').
:- consult('./play_humain.pl').
:- consult('./play_minmax.pl').
:- consult('./play_alphaBeta.pl').
:- consult('./stats.pl').
:- consult('./heuristic.pl').
:- set_prolog_flag(stack_limit, 14 000 000 000). %Pour le developpement, et la recherche en profondeur D=5.
% Initialisation
play() :-
choosePlayers(Player1,Player2),
play(Player1,Player2).
play(Player1,Player2) :-
set(x,0),
set(o,0),
initialGrid(Grid),
displayGrid(Grid,x),
play(Grid,x,Player1,Player2).
choosePlayers(Player1,Player2) :-
nl, write("Player1 (x):"), nl,
tryChoosePlayer(Player1), nl,
write("Player2 (o):"), nl,
tryChoosePlayer(Player2), nl,
write(" -> "),
write(Player1),
write(" vs "),
write(Player2), nl.
tryChoosePlayer(Player) :- write("Choisissez la nature du joueur (choix possibles: humain, firstMove, aleatoire, mMCountTokens, mMCountMoves, mMCountCorners, mMStabilite, mMGlobal, mMUnbalanced, mMBalanced, mMFrontiers, mMWedges, aBCountTokens, aBCountMoves, aBCountCorners, aBStabilite, aBGlobal,aBBalanced, aBUnbalanced, aBFrontiers, aBWedges) :"), read(Player), player(Player).
tryChoosePlayer(Player) :- write("Player inconnu ! "), tryChoosePlayer(Player).
%Tous les modes de jeu possible
player(humain).
player(firstMove).
player(aleatoire).
player(mMCountTokens).
player(mMCountMoves).
player(mMCountCorners).
player(mMStabilite).
player(mMGlobal).
player(mMUnbalanced).
player(mMBalanced).
player(mMFrontiers).
player(mMWedges).
player(aBCountTokens).
player(aBCountMoves).
player(aBCountCorners).
player(aBStabilite).
player(aBGlobal).
player(aBUnbalanced).
player(aBBalanced).
player(aBFrontiers).
player(aBWedges).
% Execution d'un tour de jeu
% Generiser les play et mettre les differents modes de jeux dans des
% fichiers separes
%Verifier que le jeu n'est pas termine
play(Grid, CurrentToken, CurrentPlayerNature, NextPlayerNature) :-
endGame(Grid),
announceWinner(Grid, CurrentToken, CurrentPlayerNature, NextPlayerNature).
%Jouer un tour en fonction de la nature du joueur actuel
play(Grid, CurrentToken, CurrentPlayerNature, NextPlayerNature) :-
canMove(Grid, CurrentToken),
moveToDo(CurrentPlayerNature,CurrentToken,Grid,Line,Column),
doMove(Grid,Line,Column,CurrentToken,NewGrid),
nextPlayer(CurrentToken,NextToken),
displayGrid(NewGrid,NextToken),
play(NewGrid,NextToken,NextPlayerNature,CurrentPlayerNature).
%Passer son tour si le joueur ne peut pas jouer
play(Grid, CurrentToken, CurrentPlayerNature, NextPlayerNature) :-
nextPlayer(CurrentToken,NextToken),
write(CurrentPlayerNature), write(" ("), write(CurrentToken), write(") PASSE SON TOUR!"), nl,
play(Grid,NextToken,NextPlayerNature,CurrentPlayerNature).
% Choix du coup faire selon la nature du joueur
moveToDo(humain,Token,Grid,Line,Column) :- humain(Token,Grid,Line,Column).
moveToDo(aleatoire,Token,Grid,Line,Column) :- aleatoire(Token,Grid,Line,Column).
moveToDo(firstMove,Token,Grid,Line,Column) :- firstMove(Token,Grid,Line,Column).
moveToDo(mMCountTokens,Token,Grid,Line,Column) :- minmax(countTokens,Token,3,Grid,Token,(Line,Column)).
moveToDo(mMCountMoves,Token,Grid,Line,Column) :- minmax(countMoves,Token,3,Grid,Token,(Line,Column)).
moveToDo(mMCountCorners,Token,Grid,Line,Column) :- minmax(countCorners,Token,3,Grid,Token,(Line,Column)).
moveToDo(mMStabilite,Token,Grid,Line,Column) :- minmax(stability,Token,3,Grid,Token,(Line,Column)).
moveToDo(mMGlobal,Token,Grid,Line,Column) :- minmax(global,Token,3,Grid,Token,(Line,Column)).
moveToDo(mMUnbalanced,Token,Grid,Line,Column) :- minmax(unbalanced,Token,3,Grid,Token,(Line,Column)).
moveToDo(mMFrontiers,Token,Grid,Line,Column) :- minmax(countFrontiers,Token,3,Grid,Token,(Line,Column)).
moveToDo(mMWedges,Token,Grid,Line,Column) :- minmax(wedges,Token,3,Grid,Token,(Line,Column)).
moveToDo(aBCountTokens,Token,Grid,Line,Column) :- alphaBeta(countTokens,Token,3,Grid,Token,(Line,Column)).
moveToDo(aBCountMoves,Token,Grid,Line,Column) :- alphaBeta(countMoves,Token,3,Grid,Token,(Line,Column)).
moveToDo(aBCountCorners,Token,Grid,Line,Column) :- alphaBeta(countCorners,Token,3,Grid,Token,(Line,Column)).
moveToDo(aBStabilite,Token,Grid,Line,Column) :- alphaBeta(stability,Token,3,Grid,Token,(Line,Column)).
moveToDo(aBGlobal,Token,Grid,Line,Column) :- alphaBeta(global,Token,3,Grid,Token,(Line,Column)).
moveToDo(aBUnbalanced,Token,Grid,Line,Column) :- alphaBeta(unbalanced,Token,3,Grid,Token,(Line,Column)).
moveToDo(aBFrontiers,Token,Grid,Line,Column) :- alphaBeta(countFrontiers,Token,3,Grid,Token,(Line,Column)).
moveToDo(aBWedges,Token,Grid,Line,Column) :- alphaBeta(wedges,Token,3,Grid,Token,(Line,Column)).
% Passage au joueur oppose
nextPlayer(x,o).
nextPlayer(o,x).
%
canMove(Grid, Token) :-
existingMove(Line,Column),
isValidMove(Grid,Line,Column,Token).
% Conditions de fin du jeu endGame(Grid)
endGame(Grid) :-
\+ canMove(Grid,o),
\+ canMove(Grid,x).
% Les mouvements possibles : letterToNum permet de convertir une lettre
% en chiffre
letterToNum(a,1).
letterToNum(b,2).
letterToNum(c,3).
letterToNum(d,4).
letterToNum(e,5).
letterToNum(f,6).
letterToNum(g,7).
letterToNum(h,8).
existingPosition(1).
existingPosition(2).
existingPosition(3).
existingPosition(4).
existingPosition(5).
existingPosition(6).
existingPosition(7).
existingPosition(8).
% existingMove(l,c) verifie que ligne et colonne rentrees sont
% comprises dans [a,h] et [1,8]
existingMove(Number,Column) :-
existingPosition(Number),
existingPosition(Column).
existingMove(Number,Letter,Number,Column) :-
existingPosition(Number),
letterToNum(Letter,Column),
existingPosition(Column).