-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
438 lines (303 loc) · 9.75 KB
/
main.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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
#include <time.h>
#include "main.h"
#include "user_input.h"
#include "data_parser.h"
#include "singleyll.h"
#include "simon.h"
/* onboard main method to create a command which is then added to command list */
/* using struct from singleyll (linked list) header file */
com_t make_command() {
/* using node struct from singleyll (linked list) to hold new command */
com_t new_com;
/* print statements to show user options to choose from */
printf("\t\tChoose a command to add from the following options\n");
printf("\t\t---------------------------------------------------\n");
usleep(100000);
printf("\t\t\tType 1 For Simon Says Right Arm Up\n");
usleep(100000);
printf("\t\t\tType 2 For Simon Says Left Arm Up\n");
usleep(100000);
printf("\t\t\tType 3 For Simon Says Hands On Head\n");
usleep(100000);
printf("\t\t\tType 4 For Simon Says Dance\n");
usleep(100000);
printf("\t\t\tType 5 For Simon Didn't Say Right Arm Up\n");
usleep(100000);
printf("\t\t\tType 6 For Simon Didn't Say Left Arm Up\n");
usleep(100000);
printf("\t\t\tType 7 For Simon Didn't Say Hands On Head\n");
usleep(100000);
printf("\t\t\tType 8 For Simon Didn't Say Dance\n");
/* using input scan to obtain selection value from user -> then stored in the new commands selection value */
printf("\t\t\tYour Choice: ");
scanf("%1d", &new_com.selection);
/* print statements to confirm new commands addition to the linked list */
usleep(250000);
printf("Command Successfully Added!\n");
usleep(250000);
/* return new command node */
return new_com;
}
/* method to emulate css type design via centering main menu */
void terminal_spacer() {
int ii;
for (ii = 0; ii < 6; ii++) {
printf("\n");
}
}
/* method to add all commands from text file to command list */
void add_file_commands(comlist_t* list, char filename[256]) {
/* declaring a new object of node structure to act as new command */
com_t new_com;
/* declaring valid command strings */
char simon_s_ra[100] = "simon said right arm up\n";
char simon_s_la[100] = "simon said left arm up\n";
char simon_s_hoh[100] = "simon said hands on head\n";
char simon_s_dan[100] = "simon said dance\n";
char simon_ds_ra[100] = "simon didn't say right arm up\n";
char simon_ds_la[100] = "simon didn't say left arm up\n";
char simon_ds_hoh[100] = "simon didn't say hands on head\n";
char simon_ds_dan[100] = "simon didn't say dance\n";
FILE* fptr = fopen(filename, "r");
char file_line[100];
while (fgets(file_line, sizeof(file_line), fptr)) {
if (strcmp(file_line, simon_s_ra) == 0) {
new_com.selection = 1;
add_command(list, new_com);
}
else if (strcmp(file_line, simon_s_la) == 0) {
new_com.selection = 2;
add_command(list, new_com);
}
else if (strcmp(file_line, simon_s_hoh) == 0) {
new_com.selection = 3;
add_command(list, new_com);
}
else if (strcmp(file_line, simon_s_dan) == 0) {
new_com.selection = 4;
add_command(list, new_com);
}
else if (strcmp(file_line, simon_ds_ra) == 0) {
new_com.selection = 5;
add_command(list, new_com);
}
else if (strcmp(file_line, simon_ds_la) == 0) {
new_com.selection = 6;
add_command(list, new_com);
}
else if (strcmp(file_line, simon_ds_hoh) == 0) {
new_com.selection = 7;
add_command(list, new_com);
}
else if (strcmp(file_line, simon_ds_dan) == 0) {
new_com.selection = 8;
add_command(list, new_com);
}
else {
printf("invalid command!\n");
}
}
fclose(fptr);
}
/* default main method */
/* with static variables to support command line input */
int main(int argc, char* argv[]) {
int cla;
int mode;
/* if there is no additional command line argument -> set cla to one (False) */
if (argc == 1) {
cla = 1;
printf("Choose a Mode\n");
printf("Type 1 For Normal Mode\n");
printf("Type 2 For Everything Mode\n");
printf("Your Choice: ");
scanf("%1d", &mode);
}
/* if there is one additional command line argument -> set cla to zero (True) */
else if (argc == 2) {
/* declaring status variable to act as boolean */
int status;
cla = 0;
printf("Choose a Mode\n");
printf("Type 1 For Normal Mode\n");
printf("Type 2 For Everything Mode\n");
printf("Your Choice: ");
scanf("%1d", &mode);
/* parse filename into file reader method to verify its existence */
status = file_reader(argv[1]);
/* if status is equal to zero (True) keep cla as zero (True) */
if (status == 0) {
cla = 0;
data_parse(argv[1]);
usleep(500000);
}
/* else -> kill switch runtime via return zero */
else {
free(argv);
return 0;
}
}
/* else -> kill switch runtime via return zero */
/* firstly printing instructions on how to use program */
else {
system("clear");
terminal_spacer();
terminal_spacer();
printf("\t\t\tGot Stuck? \n");
usleep(100000);
printf("\t\t\tUser Instructions\n");
usleep(100000);
printf("\t\t\tRun Via Typing ./SimonSays into command line\n");
usleep(100000);
printf("\t\t\tRun With a Initial File Via Typing ./SimonSays filename.type\n");
usleep(100000);
printf("\t\t\tAny Additional commandline arguments given after a filename will result in a runtime error!\n");
usleep(100000);
return 0;
}
/* creating linked list */
comlist_t* list = create_comlist();
/* adding commandline argument file commands to list */
/* if commandline file was given and verified */
if (cla == 0) {
add_file_commands(list, argv[1]);
}
/* main menu infinite while loop -> while not break -> where break is exit(zero) */
while (1) {
/* functionality variables declared */
char filename[256];
int status;
int choice;
int node_index;
/* terminal formatted menu */
/* clearing current system terminal content */
system("clear");
/* using terminal spacer funtion */
terminal_spacer();
/* print statements for main menu */
/* using usleep to sleep the processing for aesthetics */
printf(" ==========================\n");
usleep(50000);
printf(" Options: \n");
usleep(50000);
printf(" 1. Import File Of Commands\n");
usleep(50000);
printf(" 2. Give Command\n");
usleep(50000);
printf(" 3. Delete Command By Index\n");
usleep(50000);
printf(" 4. Delete Commands By Amount\n");
usleep(50000);
printf(" 5. Play Simon Says\n");
usleep(50000);
printf(" 6. Display Command List\n");
usleep(50000);
printf(" 7. Exit\n");
usleep(50000);
printf(" ===========================\n");
/* obtaining choice as an integer from the user via terminal input scan */
printf(" Your Choice: ");
scanf("%1d", &choice);
/* switch structure which is parsed int choice */
switch(choice) {
/* if case->choice is equal to one */
case 1:
system("clear");
terminal_spacer();
printf("\t\t\tEnter File Name: ");
scanf("%256s", filename);
/* passing given file to file reader function which verifies it's existence within home search path (working directory) */
status = file_reader(filename);
if (status == 0) {
terminal_spacer();
printf("\t\t\tFile Exists!\n");
/* display to user what the file contains via printing to terminal */
data_parse(filename);
/* add all of these commands to the current command list */
add_file_commands(list, filename);
break;
}
else {
terminal_spacer();
printf("\t\t\tFile Does Not Exist!\n");
break;
}
/* if case->choice is equal to two */
case 2:
system("clear");
terminal_spacer();
add_command(list, make_command());
break;
/* if case->choice is equal to three */
case 3:
system("clear");
terminal_spacer();
printf("\t\t\t Please Enter the Index of the Command you want to Delete: ");
scanf("%1d", &node_index);
delete_command(list, node_index-1);
usleep(250000);
printf("\t\t\t Command Successfully Deleted!\n");
usleep(250000);
break;
case 4:
system("clear");
terminal_spacer();
printf("\t\t\t Please Enter the Amount of Commands You Want To Delete: ");
scanf("%1d", &node_index);
mass_delete_commands(list, node_index);
usleep(250000);
printf("\t\t\t Commands Sucessfully Deleted!\n");
break;
/* if case->choice is equal to four */
case 5:
commands_to_simon(list);
break;
/* if case->choice is equal to five */
case 6:
system("clear");
terminal_spacer();
printf("\t\t\tYour Current Command List: \n");
print_comlist(list);
usleep(250000);
break;
/* if case->choice is equal to six (also exit->contains while loop break->exit(zero)) */
case 7:
system("clear");
terminal_spacer();
terminal_spacer();
usleep(500000);
printf("\t\t\t\t Thankyou For Playing!\n");
usleep(500000);
system("clear");
terminal_spacer();
printf("\t\t\t\t Quiting Game\n");
usleep(500000);
system("clear");
terminal_spacer();
printf("\t\t\t\t Quiting Game.\n");
usleep(500000);
system("clear");
terminal_spacer();
printf("\t\t\t\t Quiting Game..\n");
usleep(500000);
system("clear");
terminal_spacer();
printf("\t\t\t\t Quiting Game...\n");
exit(0);
break;
/* if case->choice is equal to none of above (default) */
default:
printf("\t\t\tInvalid Selection!\n");
break;
}
}
/*freeing allocated memory for command list */
free_comlist(list);
/* main method standard exit return */
return 0;
}