-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserInterface.c
151 lines (149 loc) · 3.9 KB
/
userInterface.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
/*
Purpose: This file will be how the user will interact with the program.
Author: Jonathan Wright
Date: 8/10/2019
*/
#include "log.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "userInterface.h"
#include "commonlib.h"
#include "game.h"
#include "board.h"
#include "settings.h"
/*
Purpose: To loop until the user selects a option
Date: 8/10/2019
Imports: None
Exports: None
*/
void menu(board* gameBoard)
{
char bufferWaster[5];
int selection = -1;
int validity;
do
{
printf("---------------------------------------------------\n");
printf("1: Start a new game\n");
printf("2: View the settings of the game\n");
printf("3: View the current logs\n");
if (gameBoard->secret != TRUE)
{
printf("4: Save the logs to file\n");
}
printf("5: Exit the application\n");
if (gameBoard->editor == TRUE)
{
printf("6: Change MNK Values\n");
}
printf("---------------------------------------------------\n");
scanf("%d",&selection);
fgets(bufferWaster, 5, stdin);
if (strlen(bufferWaster) > 1) /* This is a quick fix for a problem i found
with doubles. */
{
selection = -1;
validity = FALSE;
printf("That is a invalid input.\n");
}
else
{
validity = validChoice(selection, gameBoard->secret, gameBoard->editor);
}
if (validity == TRUE)
{
callSelection(selection, gameBoard);
}
} while (selection != 5);
}
/*
Purpose: Validate Input is a option.
Date: 8/10/2019
Imports: char selection (input to verify.), int secret (conditional compiliation)
int editor (conditional complimation)
Exports: none
*/
int validChoice(int selection, int secret, int editor)
{
char bufferWaster[5]; /* String to waste buffer into */
int validity = FALSE; /* Start By Assuming Invalid */
if (editor == TRUE && secret == TRUE)
{
if (selection > -1 && selection < 7 && selection != 4)
{
validity = TRUE;
}
else
{
errorPrint("Invalid choice! The options are 1 to 5.");
fgets(bufferWaster,5,stdin); /* Waste Buffer */
}
}
else if (editor == TRUE)
{
if (selection > -1 && selection < 7)
{
validity = TRUE;
}
else
{
errorPrint("Invalid choice! The options are 1 to 5.");
fgets(bufferWaster,5,stdin); /* Waste Buffer */
}
}
else if (secret == TRUE)
{
if (selection > -1 && selection < 6 && selection != 4)
{
validity = TRUE;
}
else
{
errorPrint("Invalid choice! The options are 1 to 5.");
fgets(bufferWaster,5,stdin); /* Waste Buffer */
}
}
else if (selection > -1 && selection < 6)
{
validity = TRUE;
}
else
{
errorPrint("Invalid choice! The options are 1 to 5.");
fgets(bufferWaster,5,stdin); /* Waste Buffer */
}
return validity;
}
/*
Purpose: Call the associated function to selection
Date: 8/10/2019
Imports: char selection
Exports: none
*/
void callSelection(int selection, board* gameBoard)
{
switch (selection)
{
case 1: /* Start Game */
startGame(gameBoard);
break;
case 2: /* View Settings */
displaySettings(*gameBoard);
break;
case 3: /* View Logs */
displayLog(gameBoard, stdout);
break;
case 4: /* Save Logs */
saveLog(gameBoard);
break;
case 5: /* Exit */
freeLinkedList(gameBoard->games, *freeGame);
free(gameBoard);
break;
case 6: /* EDITOR */
editor(gameBoard);
break;
}
}