-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
347 lines (280 loc) · 11 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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include "stdafx.h"
#include "board.h"
#include "nineman.h"
#include <stdlib.h>
#include <iostream>
#include <time.h>
using namespace std;
int main() {
int CurrentPlayer;
srand(time(NULL)); //so rand() will generate random number
int token = -1; //used to hold player's token choice
int moveToPosition = -1;
int flag = 0; //1 if the player can go again
char humanOrAI; //determine what type of game it's going to be
bool playInput = true; //determine if user made a valid choice (a or h)
char playAgain; //used to ask if user wants to play again
NineManMorris NineManGame;
cout << "NINE MAN MORRIS\n--------------------------------" << endl;
do {
//ask user if they want to play with human or AI
cout << "HUMAN VS HUMAN (Press 'h')\nHUMAN VS AI (Press 'a') ";
cin >> humanOrAI;
humanOrAI = toupper(humanOrAI);
//determine type of game
switch (humanOrAI) {
case 'H': //Human Game
NineManGame.display();
//determine which player goes first
if (rand() % 2 == 0) {
CurrentPlayer = 1;
cout << "Player 1 goes first!\n" << endl;
}
else {
CurrentPlayer = 2;
cout << "Player 2 goes first!\n" << endl;
}
//while the game is not over, keep playing
while (!NineManGame.isGameOver()) {
if (NineManGame.getPhase() == 1) {
//tell player how many tokens they have left in hand
if (CurrentPlayer == 1) {
cout << "\nPlayer " << CurrentPlayer << " you have " << NineManGame.getNumTokensP1() << " tokens in your hand" << endl;
}
else {
cout << "\nPlayer " << CurrentPlayer << " you have " << NineManGame.getNumTokensP2() << " tokens in your hand" << endl;
}
cout << "[Phase 1]: Player " << CurrentPlayer << " which position would you want to place your token: ";
cin >> moveToPosition;
if (NineManGame.isValid(CurrentPlayer, token, moveToPosition)) {
NineManGame.play(CurrentPlayer, -1, moveToPosition, 0);
}
else {
cout << "Invalid Move!" << endl;
flag = 1;
}
}
else if (NineManGame.getPhase() == 2) {
cout << "[Phase 2]: Player " << CurrentPlayer << " which token would you like to move: ";
cin >> token;
cout << endl << "Where would you like to move the token: ";
cin >> moveToPosition;
if (NineManGame.isValid(CurrentPlayer, token, moveToPosition)) {
NineManGame.play(CurrentPlayer, token, moveToPosition, 0);
}
else {
cout << "\nInvalid Move!" << endl;
flag = 1;
}
}
else if (NineManGame.getPhase() == 3) {
cout << "[Phase 3]: Player " << CurrentPlayer << " which token would you like to move: ";
cin >> token;
cout << endl << "Where would you like to move the token: ";
cin >> moveToPosition;
if (NineManGame.isValid(CurrentPlayer, token, moveToPosition)) {
NineManGame.play(CurrentPlayer, token, moveToPosition, 0);
}
else {
cout << "\nInvalid Move!" << endl;
flag = 1;
}
}
//check if there is a triple only if move was valid (indicated by flag still being 0)
if (flag == 0) {
if (NineManGame.checkTriple(CurrentPlayer, moveToPosition)) { //if a triple was formed
int desiredRemove;
cout << "[REMOVAL PHASE] Player " << CurrentPlayer << " please remove an opponent's token: " << endl;
cin >> desiredRemove;
//check if desired token to remove is a valid move
while (!NineManGame.play(CurrentPlayer, desiredRemove, 0, 1)) {
cout << "Invalid token to remove, try again: " << endl;
cin >> desiredRemove;
}
}
}
//display board after move
NineManGame.display();
//switch players only if there is no flag
if (CurrentPlayer == 1 && flag == 0) {
CurrentPlayer = 2;
}
else if (CurrentPlayer == 2 && flag == 0) {
CurrentPlayer = 1;
}
//reset the flag after each turn
flag = 0;
//check if it is time to switch phases
//changes to phase 2
//also have to make sure that we don't keep changing back to phase 2 after we changed to phase 3
if (NineManGame.getNumTokensP1() == 0 && NineManGame.getNumTokensP2() == 0 && NineManGame.getPhase() == 1) {
cout << "------------------------------------------\n\t\tPHASE 2\n------------------------------------------\n" << endl;
NineManGame.setPhase(2);
}
//changes to phase 3 if a player has 3 or less pieces
if (NineManGame.getTokensPlaced(CurrentPlayer) <= 3 && NineManGame.getPhase() == 2) {
cout << "------------------------------------------\n\t\tPHASE 3\n------------------------------------------\n" << endl;
NineManGame.setPhase(3);
}
else if (NineManGame.getTokensPlaced(CurrentPlayer) > 3 && NineManGame.getPhase() == 3) {
cout << "------------------------------------------\n\t\tPHASE 2\n------------------------------------------\n" << endl;
NineManGame.setPhase(2);
}
}
cout << "\n----------GAME OVER----------" << endl;
if (CurrentPlayer == 1) { //player 2 has won and not player 1 because we switched players at the end of the while loop
cout << "\n\n\n\n-------------------------Player 2 has won!--------------------------------" << endl;
}
else {
cout << "\n\n\n\n-------------------------Player 1 has won!--------------------------------" << endl;
}
cout << "Play again? (Press 'y'): "; //ask if user wants to play again
cin >> playAgain;
playAgain = toupper(playAgain);
if (playAgain == 'Y') {
playInput = true;
}
else {
playInput = false;
}
break;
case 'A': //AI Game
NineManGame.display();
//determine which player goes first
if (rand() % 2 == 0) {
CurrentPlayer = 1;
cout << "Player 1 goes first!\n" << endl;
}
else {
CurrentPlayer = 2;
cout << "AI goes first!\n" << endl;
}
while (!NineManGame.isGameOver()) { //keep playing if game isn't over
if (CurrentPlayer == 1) {
if (NineManGame.getPhase() == 1) {
//tell player how many tokens they have left in hand
cout << "Human you have " << NineManGame.getNumTokensP1() << " tokens in your hand" << endl;
cout << "[Phase 1]: Human which position would you want to place your token: ";
cin >> moveToPosition;
if (NineManGame.isValid(CurrentPlayer, token, moveToPosition)) {
NineManGame.play(CurrentPlayer, -1, moveToPosition, 0);
}
else {
cout << "Invalid Move!" << endl;
flag = 1;
}
}
else if (NineManGame.getPhase() == 2) { //check if phase 2
cout << "[Phase 2]: Human which token would you like to move: ";
cin >> token;
cout << endl << "Where would you like to move the token: ";
cin >> moveToPosition;
if (NineManGame.isValid(CurrentPlayer, token, moveToPosition)) {
NineManGame.play(CurrentPlayer, token, moveToPosition, 0);
}
else {
cout << "\nInvalid Move!" << endl;
flag = 1;
}
}
else if (NineManGame.getPhase() == 3) { //check if phase 3
cout << "[Phase 3]: Human which token would you like to move: ";
cin >> token;
cout << endl << "Where would you like to move the token: ";
cin >> moveToPosition;
if (NineManGame.isValid(CurrentPlayer, token, moveToPosition)) {
NineManGame.play(CurrentPlayer, token, moveToPosition, 0);
}
else {
cout << "\nInvalid Move!" << endl;
flag = 1;
}
}
//check if there is a triple only if move was valid (indicated by flag still being 0)
if (flag == 0) {
if (NineManGame.checkTriple(CurrentPlayer, moveToPosition)) { //if a triple was formed
NineManGame.display();
int desiredRemove;
cout << "[REMOVAL PHASE] Human please remove an opponent's token: " << endl;
cin >> desiredRemove;
//check if desired token to remove is a valid move
while (!NineManGame.play(CurrentPlayer, desiredRemove, 0, 1)) {
cout << "Invalid token to remove, try again: " << endl;
cin >> desiredRemove;
}
}
}
//display board after move
NineManGame.display();
//switch players only if there is no flag
if (flag == 0) {
CurrentPlayer = 2;
}
//reset the flag after each turn
flag = 0;
//check if it is time to switch phases
//changes to phase 2
//also have to make sure that we don't keep changing back to phase 2 after we changed to phase 3
if (NineManGame.getNumTokensP1() == 0 && NineManGame.getNumTokensP2() == 0 && NineManGame.getPhase() == 1) {
cout << "------------------------------------------\n\t\tPHASE 2\n------------------------------------------\n" << endl;
NineManGame.setPhase(2);
}
//changes to phase 3 if a player has 3 or less pieces
if (NineManGame.getTokensPlaced(CurrentPlayer) <= 3 && NineManGame.getPhase() == 2) {
cout << "------------------------------------------\n\t\tPHASE 3\n------------------------------------------\n" << endl;
NineManGame.setPhase(3);
}
else if (NineManGame.getTokensPlaced(CurrentPlayer) > 3 && NineManGame.getPhase() == 3) {
cout << "------------------------------------------\n\t\tPHASE 2\n------------------------------------------\n" << endl;
NineManGame.setPhase(2);
}
}
else {
cout << "\n-----------------AI's turn----------------\n" << endl;
//NineManGame.testfunc();
int moveMade;
moveMade = NineManGame.playAI();
cout << "AI made move at " << moveMade << endl;
//check if triple was made from the move
if (NineManGame.checkTriple(2, moveMade)) {
cout << "[REMOVAL PHASE] AI removing a piece..." << endl;
NineManGame.playAIRemove();
}
NineManGame.display();
//switch players
CurrentPlayer = 1;
//check if it is time to switch phases
//changes to phase 2
//also have to make sure that we don't keep changing back to phase 2 after we changed to phase 3
if (NineManGame.getNumTokensP1() == 0 && NineManGame.getNumTokensP2() == 0 && NineManGame.getPhase() == 1) {
cout << "------------------------------------------\n\t\tPHASE 2\n------------------------------------------\n" << endl;
NineManGame.setPhase(2);
}
//changes to phase 3 if a player has 3 or less pieces
if (NineManGame.getTokensPlaced(CurrentPlayer) <= 3 && NineManGame.getPhase() == 2) {
cout << "------------------------------------------\n\t\tPHASE 3\n------------------------------------------\n" << endl;
NineManGame.setPhase(3);
}
else if (NineManGame.getTokensPlaced(CurrentPlayer) > 3 && NineManGame.getPhase() == 3) {
cout << "------------------------------------------\n\t\tPHASE 2\n------------------------------------------\n" << endl;
NineManGame.setPhase(2);
}
}
}
cout << "Play again? (Press 'y'): "; //ask if user wants to play again
cin >> playAgain;
playAgain = toupper(playAgain);
if (playAgain == 'Y') {
playInput = true;
}
else {
playInput = false;
}
break;
default:
cout << "Please enter a valid command\n" << endl;
playInput = true;
}
} while (playInput);
return 0;
}