-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.c
224 lines (178 loc) · 6.17 KB
/
game.c
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
/******************************************************************************
** Student name: Elliott Gorrell
** Student number: s3452258
** Course: Advanced Programming Techniques - S1 2018
******************************************************************************/
#include "game.h"
void playGame()
{
Cell board[BOARD_HEIGHT][BOARD_WIDTH];
Player player;
Boolean completedSetup;
initialiseBoard(board);
completedSetup = setupGame(&player, board);
/* If the player successfully setup the board start game play */
if (completedSetup) runGame(&player, board);
/* Reshow the main menu controls since we are going back there */
showMainMenu(&player, board);
}
Boolean setupGame(Player * player, Cell board[BOARD_HEIGHT][BOARD_WIDTH]) {
/* Allow args in command to be seperated with a space or comma */
char delimiter[] = ", \n";
Boolean finished = FALSE;
Boolean boardLoaded = FALSE;
Boolean setupComplete = FALSE;
char buffer[20];
char tmpBuffer[20];
char *command= NULL;
char *arg1 = NULL;
char *arg2 = NULL;
char *arg3 = NULL;
displayBoard(board, NULL);
while ( !finished ) {
Boolean commandSucceeded = FALSE;
printf("What you wanna do:\n");
fgets(buffer,20,stdin);
strncpy(tmpBuffer, buffer, 20);
command = strtok(tmpBuffer,delimiter);
arg1 = strtok(NULL, delimiter);
arg2 = strtok(NULL, delimiter);
arg3 = strtok(NULL, delimiter);
if (command) {
if (strcmp(command, COMMAND_LOAD) == 0) {
commandSucceeded = loadBoardCommand(board, arg1);
if (commandSucceeded) boardLoaded = TRUE;
}
else if (strcmp(command, COMMAND_INIT) == 0) {
if (!boardLoaded) {
printf( RED "You need to load a board first\n" RESET);
}
else{
commandSucceeded = initBoardCommand(arg1, arg2, arg3, player, board);
if (commandSucceeded) {
finished = TRUE;
setupComplete = TRUE;
}
}
}
else if (strcmp(command, COMMAND_QUIT) == 0) {
printf("Sending you back to the main menu...\n\n");
finished = TRUE;
}
else {
printf(RED "Invalid Command\n" RESET);
commandSucceeded = FALSE;
}
}
if (commandSucceeded) displayBoard(board, player);
if ( !strchr(buffer, '\n') ) readRestOfLine();
memset(&buffer[0], 0, sizeof(buffer));
if (command) memset(&command[0], 0, sizeof(command));
if (arg1) memset(&arg1[0], 0, sizeof(arg1));
if (arg2) memset(&arg2[0], 0, sizeof(arg2));
if (arg3) memset(&arg3[0], 0, sizeof(arg3));
}
return setupComplete;
}
void runGame(Player * player, Cell board[BOARD_HEIGHT][BOARD_WIDTH]) {
char buffer[15];
char tmpBuffer[15];
char * command;
Boolean playing = TRUE;
while ( playing ) {
Boolean commandSucceeded = FALSE;
printf("Make a move:\n");
fgets(buffer,15,stdin);
strncpy(tmpBuffer, buffer, 15);
command = strtok(tmpBuffer,"\n\r");
if (command) {
if (strcmp(command, COMMAND_FORWARD) == 0 || strcmp(command, COMMAND_FORWARD_SHORTCUT) == 0) {
PlayerMove moveResult;
moveResult = movePlayerForward(board, player);
switch(moveResult){
case PLAYER_MOVED:
player->moves += 1;
commandSucceeded = TRUE;
break;
case OUTSIDE_BOUNDS:
printf(RED "You can't go outside the map bounds\n" RESET);
break;
case CELL_BLOCKED:
printf(RED "You can't enter a blocked cell\n" RESET);
break;
}
}
else if ( (strcmp(command, COMMAND_TURN_LEFT) == 0) || (strcmp(command, COMMAND_TURN_LEFT_SHORTCUT) == 0) ) {
turnDirection(player, TURN_LEFT);
commandSucceeded = TRUE;
}
else if ( (strcmp(command, COMMAND_TURN_RIGHT) == 0) || (strcmp(command, COMMAND_TURN_RIGHT_SHORTCUT) == 0) ) {
turnDirection(player, TURN_RIGHT);
commandSucceeded = TRUE;
}
else if ( (strcmp(command, COMMAND_QUIT) == 0) ) {
printf("Total moves: %d\n", player->moves);
printf("Quitting game..\n\n");
playing = FALSE;
}
else {
printf(RED "Invalid Command\n" RESET);
}
}
if (commandSucceeded) displayBoard(board, player);
if ( !strchr(buffer, '\n') ) readRestOfLine();
memset(&buffer[0], 0, sizeof(buffer));
memset(&tmpBuffer[0], 0, sizeof(tmpBuffer));
if (command) memset(&command[0], 0, sizeof(command));
}
}
Boolean loadBoardCommand(Cell board[BOARD_HEIGHT][BOARD_WIDTH], char * arg1){
int boardNumber;
if (arg1 == NULL) {
printf(RED "must provide a board number\n" RESET);
return FALSE;
}
/* If we don't have a valid integer we will get returned 0 by atoi so will fall through to else conditional */
boardNumber = atoi(arg1);
if (boardNumber == 1) {
loadBoard(board, BOARD_1);
}
else if (boardNumber == 2) {
loadBoard(board, BOARD_2);
}
else {
printf(RED "Command must be 'load 1' or 'load 2'\n" RESET);
return FALSE;
}
return TRUE;
}
Boolean initBoardCommand(char * arg1, char * arg2, char * arg3, Player * player, Cell board[BOARD_HEIGHT][BOARD_WIDTH]) {
int x;
int y;
char * direction;
Position initialPosition;
Direction startingDirection;
if (arg1 == NULL || arg2 == NULL || arg3 == NULL) {
printf(RED "input for 'init' command must be in the format 'init x,y,direction' or 'init x y direction'\n" RESET);
return FALSE;
}
x = atoi(arg1);
y = atoi(arg2);
direction = arg3;
initialPosition.x = x;
initialPosition.y = y;
if (strcmp(direction, DIRECTION_NORTH) == 0) startingDirection = NORTH;
else if (strcmp(direction, DIRECTION_SOUTH) == 0) startingDirection = SOUTH;
else if (strcmp(direction, DIRECTION_EAST) == 0) startingDirection = EAST;
else if (strcmp(direction, DIRECTION_WEST) == 0) startingDirection = WEST;
else {
printf(RED "Direction must be one of: 'north' 'east' 'south' 'west'\n" RESET);
return FALSE;
}
initialisePlayer(player, &initialPosition, startingDirection);
if ( !placePlayer(board, initialPosition) ) {
printf(RED "FAILED! You cannot place player at a blocked or out of bound cell!\n" RESET);
return FALSE;
}
return TRUE;
}