-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
145 lines (142 loc) · 2.9 KB
/
main.cpp
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
#include <iostream>
#include <algorithm>
#include "morpion.h"
int minimax(Board b){
if (b.verification() >= 0){
// si b est une feuille
if (b.verification() == 2){
return -1;
}
else {
return b.verification(); }
}
int val = 0;
if (b.turn() == 1){
// si X de jouer (maximizing player)
val = -1;
for (int i = 0; i < 9; i++){
if (b.can_play(i)){//parmi les coups legaux
Board child = b;
child.play(i);
val = std::max(val, minimax(child));
}
}
}
else {
val = 1;
for (int i = 0; i < 9; i++){
if (b.can_play(i)){//parmi les coups legaux
Board child = b;
child.play(i);
val = std::min(val, minimax(child));
}
}
}
return val;
}
int playO(Board coord){
int val = 1;
int val2 = 1;
int new_move = 0;
for (int i = 0; i < 9; i++){
if (coord.can_play(i) == 1){//parmi les coups legaux
Board child = coord;
child.play(i);
val2 = minimax(child);
if (val2 <= val){
val = val2;
new_move = i;
}
}
}
return new_move;
}
int playX(Board coord){
int val = -1;
int val2 = -1;
int new_move = 0;
for (int i = 0; i < 9; i++){
if (coord.can_play(i) == 1){//parmi les coups legaux
Board child = coord;
child.play(i);
val2 = minimax(child);
if (val2 >= val){
val = val2;
new_move = i;
}
}
}
return new_move;
}
int main(){
int c = 1;
int config = 1;
std::cout << "Avant de jouer, choisissez l'une des deux configurations:\n1)|7|8|9| 2)|1|2|3|\n |4|5|6| |4|5|6|\n |1|2|3| |7|8|9|" << std::endl;
std::cin >> config;
if (config != 2){
config = 1;
}
while (true){
std::cout << "1) player vs player\n2) player vs AI\n3) AI vs player" << std::endl;
std::cin >> c;
Board coord = Board();
coord.configuration(config);
switch (c){
case 2:
while (coord.verification() == -1){
coord.draw();
int n;
std::cin >> n;
coord.play(coord.position(n));
if (coord.verification() != -1){
break;
}
else {
coord.draw();
coord.play(playO(coord));
}
}
break;
case 3:
while (coord.verification() == -1){
coord.draw();
coord.play(playX(coord));
if (coord.verification() != -1){
break;
}
else {
coord.draw();
int n;
std::cin >> n;
coord.play(coord.position(n));
}
}
break;
default:
while (coord.verification() == -1){
coord.draw();
int n;
std::cin >> n;
coord.play(coord.position(n));
}
}
coord.draw();
switch (coord.verification()){
case 0:
std::cout << "match nul" << std::endl;
break;
case 1:
std::cout << "le joueur X gagne" << std::endl;
break;
default:
std::cout << "le joueur O gagne" << std::endl;
}
c = 1;
std::cout << "Rejouer?\n1) oui\n2) non" << std::endl;
std::cin >> c;
if (c == 2){
break;
}
}
return 0;
}