-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercice6.cpp
294 lines (272 loc) · 7.55 KB
/
exercice6.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
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
/**
Stones game , exercice6.cpp
Objectif: La gestion des etudiants
@author Motassim Othman
01/11/19
*/
#include <iostream>
#include <ctime>
#include <conio.h>
using namespace std;
struct Player
{
int type;
string name;
bool turn{false};
int *historique = new int[1];
int numturns{0};
bool isWinner{false};
};
int cailloux;
Player players[2];
void checkWinner(Player &p1, Player &p2)
{
if (p1.turn)
{
p1.isWinner = true;
}
else
{
p2.isWinner = true;
}
}
void initialiser(int nc, Player &player)
{
cailloux = nc;
player.turn = true;
}
void utilisateur_joue(Player &p, int dif)
{
int nap;
if (dif == 0)
{
do
{
do
{
cout << "How many stones would u want to pick ? " << endl;
cin >> nap;
} while ((nap < 1 || nap > 3));
} while (nap > cailloux);
}
else if (dif == 1)
{
clock_t start = clock();
do
{
do
{
cout << "How many stones would u want to pick ? " << endl;
cout << "You have 3 seconds to pick " << endl;
while (!_kbhit()) //Check for keyboard hit
{
//Check if 3 sec timer expired or not
if (((clock() - start) / CLOCKS_PER_SEC) >= 3)
{
cout << "salina" << endl;
nap = 1;
goto lkhr;
}
}
cin >> nap;
} while ((nap < 1 || nap > 3));
} while (nap > cailloux);
}
lkhr:
cailloux -= nap;
p.numturns++;
p.historique = (int *)realloc(p.historique, sizeof(int) * (p.numturns));
p.historique[p.numturns - 1] = nap;
p.turn = false;
}
void machine_joue(Player &m, int dif)
{
int nap;
if (dif == 0)
{
do
{
nap = 1 + rand() % 3;
} while (nap > cailloux);
cailloux -= nap;
cout << m.name << " took " << nap << " Stones " << endl;
}
else if (dif == 1)
{
if ((cailloux - 1) % 4 != 0 && cailloux > 1)
{
do
{
do
{
nap = 1 + rand() % 3;
} while (nap > cailloux);
} while (((cailloux - nap) - 1) % 4 != 0);
}
else
{
do
{
nap = 1 + rand() % 3;
} while (nap > cailloux);
}
cailloux -= nap;
cout << m.name << " took " << nap << " Stones " << endl;
}
m.numturns++;
m.historique = (int *)realloc(m.historique, sizeof(int) * (m.numturns));
m.historique[m.numturns - 1] = nap;
m.turn = false;
}
void showMenu()
{
cout << "=======================================================================================================" << endl;
cout << "==================================== Welcome to the game ! ============================================" << endl;
cout << "=======================================================================================================" << endl;
cout << " 'Play a game against AI' === 1 - Player vs AI " << endl;
cout << " 'Play a game against another player' ==== 2 - Player vs Player " << endl;
cout << " 'Watch two smart machines play' ===== 3 - AI vs AI " << endl;
cout << "=======================================================================================================" << endl;
cout << " Press 0 to exit " << endl;
cout << "=======================================================================================================" << endl;
}
void play(Player &plyr1, Player &plyr2, int dif = -1)
{
int nc, fp;
cout << "Player 1's name : " << endl;
cin >> plyr1.name;
cout << "Player 2's name : " << endl;
cin >> plyr2.name;
do
{
cout << "Specify the number of stones" << endl;
cin >> nc;
} while (nc < 1);
do
{
cout << "Specify first player to play " << endl;
cout << "1 - " << plyr1.name << endl;
cout << "2 - " << plyr2.name << endl;
cin >> fp;
} while (fp < 1 || fp > 2);
switch (fp)
{
case 1:
initialiser(nc, plyr1);
break;
case 2:
initialiser(nc, plyr2);
break;
}
players[0] = plyr1;
players[1] = plyr2;
while (cailloux > 0)
{
for (int i = 0; i < 2; i++)
{
if (players[i].turn)
{
if (players[i].type == 0)
{
if (i == 0)
{
players[1].turn = true;
}
else
{
players[0].turn = true;
}
cout << "============================ " << players[i].name << "'s turn : ================================" << endl;
cout << "number of stones available : " << cailloux << endl;
utilisateur_joue(players[i], dif);
if (cailloux == 0)
break;
}
else
{
if (i == 0)
{
players[1].turn = true;
}
else
{
players[0].turn = true;
}
cout << "============================ " << players[i].name << "'s turn : ================================" << endl;
cout << "number of stones available : " << cailloux << endl;
machine_joue(players[i], dif);
if (cailloux == 0)
break;
}
}
}
}
if (cailloux == 0)
{
checkWinner(players[0], players[1]);
}
for (int i = 0; i < 2; i++)
{
if (players[i].isWinner)
{
cout << "=======================================================================================================" << endl;
cout << "\t\tThe winner is : " << players[i].name << endl;
cout << "=======================================================================================================" << endl;
}
}
}
int main()
{
srand(time(NULL));
Player winner;
int nc;
int fp;
int choice;
do
{
showMenu();
cin >> choice;
} while (choice < 0 || choice > 3);
switch (choice)
{
case 1:
{
Player plyr;
plyr.type = 0;
Player mchn;
mchn.type = 1;
int dif;
do
{
cout << "Chose difficuly \n0 - Easy \n1 - Hard" << endl;
cin >> dif;
} while (dif < 0 || dif > 1);
play(plyr, mchn, dif);
break;
}
case 2:
{
Player plyr1;
plyr1.type = 0;
Player plyr2;
plyr2.type = 0;
play(plyr1, plyr2);
break;
}
case 3:
{
Player ai1;
ai1.type = 1;
Player ai2;
ai2.type = 1;
int dif;
do
{
cout << "Chose difficulty \n0 - Easy \n1 - Hard" << endl;
cin >> dif;
} while (dif < 0 || dif > 1);
play(ai1, ai2, dif);
break;
}
}
}