Repository navigation
Expand file tree
/
Copy pathInputController.c
More file actions
208 lines (190 loc) · 6.76 KB
/
Copy pathInputController.c
File metadata and controls
208 lines (190 loc) · 6.76 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
/**
* Input Controller Source File
*
* This file implements managing the fetching of commands entered by the user, as well validating and converting the input to the
* relevant command structs.
*
*/
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include "utils.h"
#define NUMBER_OF_CHARS_INPUT 1024
#define SET_OPTION "set"
#define HINT_OPTION "hint"
#define VALIDATE_OPTION "validate"
#define EXIT_OPTION "exit"
#define EDIT_OPTION "edit"
#define MARK_ERROR_OPTION "mark_errors"
#define PRINT_BOARD_OPTION "print_board"
#define GENERATE_OPTION "generate"
#define UNDO_OPTION "undo"
#define REDO_OPTION "redo"
#define SAVE_OPTION "save"
#define NUM_SOLUTIONS_OPTION "num_solutions"
#define AUTOFILL_OPTION "autofill"
#define SOLVE_OPTION "solve"
#define RESET_OPTION "reset"
#define REGEX " \t\r\n"
#include "InputController.h"
#include "GameManager.h"
int isValidCommand;
char *tempInput, *command, *inputString;
int cnt = 0, isLastInput;
/**
* This function returns a boolean which indicate if the game mode is solve
*
* @param gameMode - int, indicate which gameMode
* @return 1 if gameMode == SOLVE_MODE
*/
int isSolveMode(int gameMode) {
return gameMode == SOLVE_MODE;
}
/**
* This function returns a boolean which indicate if the game mode is solve or edit
*
* @param gameMode - int, indicate which gameMode
* @return 1 if gameMode is SOLVE_MODE or EDIT_MODE
*/
int isSolveOrEditMode(int gameMode) {
return gameMode == SOLVE_MODE || gameMode == EDIT_MODE;
}
/**
* This function returns a boolean which indicate if the game mode is edit
*
* @param gameMode - int, indicate which gameMode
* @return 1 if gameMode == EDIT_MODE
*/
int isEditMode(int gameMode) {
return gameMode == EDIT_MODE;
}
/**
* This function empty the string buffer if the input is bigger than 256
*
*/
void emptyBuffer() {
int c;
while ((c = getchar()) != '\n' && c != EOF) {
}
}
/**
* This function validate the input and filling the commandArray with the command and inputs specified by the user
*
* @param gameMode - int, indicate which gameMode
* @param commandArray - int array - describing the command and the relevant input
*/
void validateCommand(int gameMode, int *commandArray) {
if (!strcmp(command, SET_OPTION) && cnt >= 4 && isSolveOrEditMode(gameMode)) {
isValidCommand = 1;
commandArray[0] = SET;
} else if (!strcmp(command, HINT_OPTION) && cnt >= 3 && isSolveMode(gameMode)) {
isValidCommand = 1;
commandArray[0] = HINT;
} else if (!strcmp(command, VALIDATE_OPTION) && cnt >= 1 && isSolveOrEditMode(gameMode)) {
isValidCommand = 1;
commandArray[0] = VALIDATE;
} else if (!strcmp(command, RESET_OPTION) && cnt >= 1 && isSolveOrEditMode(gameMode)) {
isValidCommand = 1;
commandArray[0] = RESET;
} else if (!strcmp(command, EXIT_OPTION) && cnt >= 1) {
isValidCommand = 1;
commandArray[0] = EXIT;
} else if (!strcmp(command, SOLVE_OPTION) && cnt >= 2) {
isValidCommand = 1;
commandArray[0] = SOLVE;
} else if (!strcmp(command, EDIT_OPTION) && cnt >= 1) {
isValidCommand = 1;
commandArray[0] = EDIT;
} else if (!strcmp(command, MARK_ERROR_OPTION) && cnt >= 2 && isSolveMode(gameMode)) {
isValidCommand = 1;
commandArray[0] = MARK_ERROR;
} else if (!strcmp(command, PRINT_BOARD_OPTION) && cnt >= 1 && isSolveOrEditMode(gameMode)) {
isValidCommand = 1;
commandArray[0] = PRINT_BOARD;
} else if (!strcmp(command, GENERATE_OPTION) && cnt >= 3 && isEditMode(gameMode)) {
isValidCommand = 1;
commandArray[0] = GENERATE;
} else if (!strcmp(command, UNDO_OPTION) && cnt >= 1 && isSolveOrEditMode(gameMode)) {
isValidCommand = 1;
commandArray[0] = UNDO;
} else if (!strcmp(command, REDO_OPTION) && cnt >= 1 && isSolveOrEditMode(gameMode)) {
isValidCommand = 1;
commandArray[0] = REDO;
} else if (!strcmp(command, SAVE_OPTION) && cnt >= 1 && isSolveOrEditMode(gameMode)) {
isValidCommand = 1;
commandArray[0] = SAVE;
} else if (!strcmp(command, NUM_SOLUTIONS_OPTION) && cnt >= 1 && isSolveOrEditMode(gameMode)) {
isValidCommand = 1;
commandArray[0] = NUM_SOLUTIONS;
} else if (!strcmp(command, AUTOFILL_OPTION) && cnt >= 1 && isSolveMode(gameMode)) {
isValidCommand = 1;
commandArray[0] = AUTO_FILL;
} else {
printInvalidCommand();
}
}
/**
* This function fetch an input from the user, validate and filling the commandArray if the input is correct.
*
* @param gameMode - int, indicate which gameMode
* @param commandArray - int array, describing the command and the relevant input
* @param pathString - String, for commands save, edit and solve - the pathString indicate for the file location.
*/
void getTurnCommand(int gameMode, int *commandArray, char *pathString) {
strcpy(pathString, "");
command = (char *) malloc(NUMBER_OF_CHARS_INPUT * sizeof(char));
inputString = (char *) malloc(NUMBER_OF_CHARS_INPUT * sizeof(char));
assert(command);
assert(inputString);
isValidCommand = 0;
while (!isValidCommand) {
if (feof(stdin)) {
free(command);
free(inputString);
exitGame();
}
printEnterCommand();
cnt = 0;
fgets(inputString, 270, stdin);
while (strlen(inputString) > 256) {
emptyBuffer();
}
tempInput = strtok(inputString, REGEX);
if (tempInput == NULL) {
continue;
} else {
while (tempInput != NULL) {
switch (cnt) {
case 0:
strcpy(command, tempInput);
break;
case 1:
if (!strcmp(command, SAVE_OPTION) || !strcmp(command, EDIT_OPTION) ||
!strcmp(command, SOLVE_OPTION)) {
strcpy(pathString, tempInput);
} else if (!sscanf(tempInput, "%d", &commandArray[1])) {
commandArray[1] = -1;
}
break;
case 2:
if (!sscanf(tempInput, "%d", &commandArray[2])) {
commandArray[2] = -1;
}
break;
case 3:
if (!sscanf(tempInput, "%d", &commandArray[3])) {
commandArray[3] = -1;
}
default:
break;
}
tempInput = strtok(NULL, REGEX);
++cnt;
}
validateCommand(gameMode, commandArray);
}
}
free(command);
free(inputString);
}