-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSPMainAux.c
More file actions
executable file
·286 lines (243 loc) · 6.72 KB
/
Copy pathSPMainAux.c
File metadata and controls
executable file
·286 lines (243 loc) · 6.72 KB
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
#include <stdio.h>
#include <stdlib.h>
#include "SPMainAux.h"
#include "SPMiniMax.h"
#include "SPFIARParser.h"
#include <string.h>
#include "SPMiniMaxNode.h"
SPFiarGame *curGame;
char theWinner = ' ';
void init() {
char input[SP_MAX_LINE_LENGTH];
bool createNewGame = true;
GAME_STATUS status;
while (true) {
printf("Please enter the difficulty level between [1-7]:\n");
while (true)
{
fgets(input, SP_MAX_LINE_LENGTH, stdin);
if ((strlen(input) > 0) && (input[strlen(input) - 1] == '\n'))
input[strlen(input) - 1] = 0;
if (spParserPraseLine(input).cmd == SP_QUIT)
{
printf("Exiting...\n");
exit(0);
}
if (spParserIsInt(input)) //checking if the level entered is 1-7
{
if (strlen(input) == 1)
if ((input[0] > 48) && (input[0] < 56))
{
while (true)
{
status = gameLoop((input[0]) - 48, createNewGame);
if (status == RESTART)
{
createNewGame = true;
spFiarGameDestroy(curGame);
break;
}
if (status == UNDO_SUCCESS)
{
createNewGame = false;
}
}
break;
}
}
printf("Error: invalid level (should be between 1 to 7)\n");
break;
}
}
}
GAME_STATUS gameLoop(int level, bool createNewGame)
{
if (createNewGame)
{
curGame = spFiarGameCreate(20);
}
theWinner = ' ';
GAME_STATUS gameOverStatus;
if (gameRun(level) == RESTART) //restart
return RESTART;
spFiarGamePrintBoard(curGame); //else- there is a winner (if there isn't - the game continue in the gameRun function)
winnerPrint();
while (true) //game is over- what can the user do next?
{
gameOverStatus = gameOver();
if (gameOverStatus == RESTART) //restart
return RESTART;
else if (gameOverStatus == UNDO_SUCCESS)// undo done, keep playing
return UNDO_SUCCESS;
}//else- have to run gameOver again (undo didn't work / the input was invalid)
}
GAME_STATUS gameRun(int level)
{
while (true)
{
theWinner = spFiarCheckWinner(curGame);
if (theWinner != 0) //if there is a winner- don't continue the game as usual
break;
if (spFiarGameGetCurrentPlayer(curGame) == SP_FIAR_GAME_PLAYER_1_SYMBOL) //else- if now is user's turn
{
spFiarGamePrintBoard(curGame);
if (userTurn(level) == RESTART) //is means that the user pressed "restart"
{
printf("Game restarted!\n");
return RESTART; //back to start- "enter a level.. 1-7..."
}
continue; //next turn
}
if (spFiarGameGetCurrentPlayer(curGame) == SP_FIAR_GAME_PLAYER_2_SYMBOL) //if now is computer's turn
{
compTurn(level);
continue; //next turn
}
}
return THERE_IS_A_WINNER;
}
GAME_STATUS userTurn(int level)
{
char input[SP_MAX_LINE_LENGTH];
SPCommand command;
printf("Please make the next move:\n");
while (true)
{
fgets(input, SP_MAX_LINE_LENGTH, stdin);
if (input[0] == '\0' || input[0] == 0) //null character
{
printf("Error: invalid command\n");
continue;
}
if ((strlen(input) > 0) && (input[strlen(input) - 1] == '\n'))
input[strlen(input) - 1] = 0;
command = spParserPraseLine(input);
if (command.cmd == SP_INVALID_LINE)
printf("Error: invalid command\n");
if (command.cmd == SP_RESTART)
return RESTART;
if (command.cmd == SP_QUIT)
quit(curGame);
if (command.cmd == SP_SUGGEST_MOVE)
printf("Suggested move: drop a disc to column %d\n", suggestMove(level) + 1);
if (command.cmd == SP_UNDO_MOVE)
{
if (undo() != UNDO_SUCCESS)
continue;
break;
}
if (command.cmd == SP_ADD_DISC)
{
if (addDisc(command) == 0)
continue;
break;
}
}
return PLAY_CONT; //user didn't press restart
}
int suggestMove(int level)
{
return spMinimaxSuggestMove(curGame, level);
}
void quit()
{
spFiarGameDestroy(curGame);
printf("Exiting...\n");
exit(0);
}
void compTurn(int level)
{
int move = spMinimaxSuggestMove(curGame, level);
printf("Computer move: add disc to column %d\n", move + 1);
SP_FIAR_GAME_MESSAGE message = spFiarGameSetMove(curGame, move);
if (message != SP_FIAR_GAME_SUCCESS)
{
printf("Error: spFiarGameSetMove has failed");
spFiarGameDestroy(curGame);
exit(0);
}
}
void winnerPrint()
{
if (theWinner == SP_FIAR_GAME_PLAYER_1_SYMBOL)
printf("Game over: you win\nPlease enter \'quit\' to exit or \'restart\' to start a new game!\n");
if (theWinner == SP_FIAR_GAME_PLAYER_2_SYMBOL)
printf("Game over: computer wins\nPlease enter \'quit\' to exit or \'restart\' to start a new game!\n");
if (theWinner == SP_FIAR_GAME_TIE_SYMBOL)
printf("Game over: it\'s a tie\nPlease enter \'quit\' to exit or \'restart\' to start a new game!\n");
}
GAME_STATUS gameOver()
{
char input[SP_MAX_LINE_LENGTH];
fgets(input, SP_MAX_LINE_LENGTH, stdin);
if ((strlen(input) > 0) && (input[strlen(input) - 1] == '\n'))
input[strlen(input) - 1] = 0;
SPCommand command = spParserPraseLine(input);
if (command.cmd == SP_RESTART)
{
printf("Game restarted!\n");
return RESTART;
}
if (command.cmd == SP_QUIT)
quit();
if (command.cmd == SP_INVALID_LINE)
printf("Error: invalid command\n");
if (theWinner != SP_FIAR_GAME_PLAYER_1_SYMBOL) //undo is allowed only when the user didn't win
{
if (command.cmd == SP_UNDO_MOVE)
{
if (undo() != UNDO_SUCCESS)
return GAME_OVER_LOOP; //don't restart, and undo failed
else
return UNDO_SUCCESS; //don't restart, and undo succseeded
}
}
else
printf("Error: the game is over\n");
return GAME_OVER_LOOP; //it means something went wrong and we have to run the gameOver function again
}
GAME_STATUS undo()
{
int col = -1;
col = spArrayListGetLast(curGame->historyArray);
SP_FIAR_GAME_MESSAGE message = spFiarGameUndoPrevMove(curGame);
if (message == SP_FIAR_GAME_INVALID_ARGUMENT)
{
printf("Error: spFiarGameUndoPrevMove has failed");
spFiarGameDestroy(curGame);
exit(0);
}
if (message == SP_FIAR_GAME_NO_HISTORY)
{
printf("Error: cannot undo previous move!\n");
curGame->currentPlayer = SP_FIAR_GAME_PLAYER_1_SYMBOL;
return UNDO_FAIL;
}
printf("Remove disc: remove computer's disc at column %d\n", col + 1);
printf("Remove disc: remove user's disc at column %d\n", spArrayListGetLast(curGame->historyArray) + 1);
spFiarGameUndoPrevMove(curGame);
return UNDO_SUCCESS;
}
int addDisc(SPCommand command)
{
if ((!command.validArg) || (command.arg > 7) || (command.arg < 1))
{
printf("Error: column number must be in range 1-7\n");
curGame->currentPlayer = SP_FIAR_GAME_PLAYER_1_SYMBOL;
return 0;
}
SP_FIAR_GAME_MESSAGE message = spFiarGameSetMove(curGame, command.arg - 1);
if (message == SP_FIAR_GAME_INVALID_ARGUMENT)
{
printf("Error: spFiarGameSetMove has failed");
spFiarGameDestroy(curGame);
exit(0);
}
if (message == SP_FIAR_GAME_INVALID_MOVE)
{
printf("Error: column %d is full\n", command.arg);
curGame->currentPlayer = SP_FIAR_GAME_PLAYER_1_SYMBOL;
return 0;
}
return 1;
}