This repository was archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
261 lines (217 loc) · 9.66 KB
/
Program.cs
File metadata and controls
261 lines (217 loc) · 9.66 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
using System;
using System.Collections.Generic;
public class Program {
public static void Main(string[] args)
{
Console.Title = "NCEA DTS Assessment";
// Start the game
Program program = new Program();
program.Menu();
}
void Menu()
{
Utils utils = new Utils();
// Title
Console.Clear();
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("\n\n\n");
utils.CentreText(" ██████╗ █████╗ ███╗ ███╗███████╗");
utils.CentreText("██╔════╝ ██╔══██╗████╗ ████║██╔════╝");
utils.CentreText("██║ ██╗ ███████║██╔████╔██║█████╗ ");
utils.CentreText("██║ ╚██╗██╔══██║██║╚██╔╝██║██╔══╝ ");
utils.CentreText("╚██████╔╝██║ ██║██║ ╚═╝ ██║███████╗");
utils.CentreText(" ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝");
utils.CentreText("Max McCarthy\n\n\n");
Console.ResetColor();
// Game start Menu
int menuInput = utils.ArrowMenu(new[] { "Play Quiz", "Create Quiz", "Remove Quiz", "About", "Exit" }, "Please Select an option");
switch (menuInput)
{
case 0:
// Play game
PlayGame();
break;
case 1:
// Make game
MakeGame();
break;
case 2:
// Remove
RemoveGame();
break;
case 3:
// About
About();
break;
case 4:
// Exit
Environment.Exit(0);
break;
}
}
void About()
{
Utils utils = new Utils();
Console.Clear();
utils.Line();
// Give a short explanation of the quiz/game
utils.CentreText("\n\n\nThis quiz was created for my DTS NCEA Assessment.\nI was originally going to make a much bigger game that had stuff like a story, boss fights, and ascii movement\nbut it was going to be too annoying to write all of the pesudocode and document all errors and debugging.\n\nThis is a really simple quiz program that reads questions from an external file.\n\n");
// Show them a link to the GutHub repo even though they can't click on it
Console.ForegroundColor = ConsoleColor.Blue;
utils.CentreText("https://github.com/MaximilianMcC/ncea-assessment");
Console.ResetColor();
// Show a quick table that has all of the controlls
utils.CentreText("\n\n╔════════════════════════════╗");
utils.CentreText("║ Quiz Controlls ║");
utils.CentreText("╟──────────────╥─────────────╢");
utils.CentreText("║ Move up ║ up arrow ║");
utils.CentreText("║ Move down ║ down arrow ║");
utils.CentreText("║ Next item ║ tab ║");
utils.CentreText("║ Select ║ enter ║");
utils.CentreText("╚══════════════╩═════════════╝");
// Go back to the menu
utils.CentreText("\n\n\nPress any key to return to menu...");
Console.ReadKey();
Menu();
}
void PlayGame()
{
// Basic setup
Utils utils = new Utils();
Root json = utils.GetJson();
Console.Clear();
utils.Line();
// Ask the user to select a quiz
string[] quizNames = new string[json.Quiz.Count];
for (int i = 0; i < quizNames.Length; i++) quizNames[i] = json.Quiz[i].Name;
int quizSelect = utils.ArrowMenu(quizNames, "Select a quiz");
int score = 0;
// Loop through all of the questions in the quiz
foreach (QuestionObject question in json.Quiz[quizSelect].Questions)
{
// Put the questions and answers in a arrow menu
Console.Clear();
utils.Line();
int answer = utils.ArrowMenu(question.Answers.ToArray(), question.Question);
// Check for if they gave the correct answer
if (answer == question.Answer)
{
// Correct
Console.ForegroundColor = ConsoleColor.Green;
utils.CentreText("\n\nCorrect!");
score++;
}
else
{
// Incorrect
Console.ForegroundColor = ConsoleColor.Red;
utils.CentreText("\n\nIncorrect!");
}
// Reset the color, give them their total score then wait a few seconds
Console.ResetColor();
utils.CentreText("Total score: " + score);
utils.CentreText("\n\nPress any key to proceed...");
Console.ReadKey();
}
// When they have finished a quiz then display a message
Console.Clear();
utils.Line();
int questionsLength = json.Quiz[quizSelect].Questions.Count;
utils.CentreText($"You completed the quiz\nFinal score: {score}/{questionsLength}");
if (score <= 0) utils.CentreText("You got no questions correct");
else if (score >= questionsLength) utils.CentreText("You got all correct!");
// Bring them back to the menu
utils.CentreText("\n\nPress any key to go back to the main menu...");
Console.ReadKey();
Menu();
}
// Let the user create a quiz
void MakeGame()
{
// Basic setup
Utils utils = new Utils();
Root json = utils.GetJson();
Console.Clear();
// Ask them for the quiz name and make the quiz
string quizName = utils.GetTextInput("Name of the quiz: ");
List<QuestionObject> questions = new List<QuestionObject>();
Quiz quiz = new Quiz() { Name = quizName, Questions = questions } ;
while (true)
{
// Ask them what they want to do
int selection = utils.ArrowMenu(new[] { "Add question", "Finish quiz", "Back to menu" }, "Select an option");
if (selection == 0)
{
Console.Clear();
// Ask them for what the question is and how many answers
string questionName = utils.GetTextInput("Question name: ");
int answersLength = utils.GetNumberInput("How many answers? ");
// Ask them what all of the answers are
List<string> questionAnswers = new List<string>();
for (int i = 0; i < answersLength; i++)
{
questionAnswers.Add(utils.GetTextInput($"Answer {i + 1}) "));
}
// Ask them for what the answer is
int questionAnswer = utils.GetNumberInput("Answer number: ");
questionAnswer--;
// Create and add the question to the quiz
questions.Add(new QuestionObject() {
Question = questionName,
Answers = questionAnswers,
Answer = questionAnswer
});
}
else if (selection == 1)
{
Console.Clear();
// Generate a new quiz with the questions
Root deserializedJson = utils.GetJson();
deserializedJson.Quiz.Add(quiz);
utils.SetJson(deserializedJson);
// Put them into the play game menu
PlayGame();
}
else if (selection == 2)
{
// Go back to the menu
Menu();
}
}
}
void RemoveGame()
{
// Basic setup
Utils utils = new Utils();
Root deserializedJson = utils.GetJson();
Console.Clear();
utils.Line();
// Ask the user to select a quiz
string[] quizNames = new string[deserializedJson.Quiz.Count];
for (int i = 0; i < quizNames.Length; i++) quizNames[i] = deserializedJson.Quiz[i].Name;
int quizSelect = utils.ArrowMenu(quizNames, "Select a quiz to remove");
// Ask them if they are sure they want to delete it
int wantToDelete = utils.ArrowMenu(new[] { "Cancel", "Delete Quiz" }, $"Are you sure you want to delete '{deserializedJson.Quiz[quizSelect]}'?");
if (wantToDelete == 0) Menu();
else if (wantToDelete == 1)
{
// Check for if there is more than 1 quiz
if (deserializedJson.Quiz.Count <= 1)
{
utils.CentreText("\n\nSorry, you can not delete this quiz because it is the only one left.");
}
else
{
// Remove the quiz
deserializedJson.Quiz.Remove(deserializedJson.Quiz[quizSelect]);
utils.SetJson(deserializedJson);
// Tell them that they removed it
utils.CentreText("\n\nThe quiz has successfully been removed.");
}
// Go back to the menu
utils.CentreText("Press any key to return to menu...");
Console.ReadKey();
Menu();
}
}
}