-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenu.cpp
384 lines (371 loc) · 13.6 KB
/
Menu.cpp
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
//
// Created by Slayton on 12/2/2017.
//
#include "Menu.h"
// MENU METHODS
Menu::Menu() {
prompt = "Menu not properly constructed";
}
Menu::Menu(stack<Menu*>* menusInput) : menus(menusInput) {
}
Menu::Menu(stack<Menu*>* menusInput, Codex* codexInput) : menus(menusInput), codex(codexInput) {
}
void Menu::populateMenu(string promptInput, vector<pair<string, string>> optionsInput) {
// used instead of constructor to allow for writing inside
// the subclass, rather than putting unique text inside the initializer list.
prompt = promptInput;
options = optionsInput;
}
string Menu::ensureProperInput() {
bool running = true;
string userInput;
while(running) {
cout << prompt;
for(int i = 0; i < options.size(); ++i) {
cout << "\n\t" + options.at(i).first + ": " + options.at(i).second;
}
cout << "\ninput: ";
cin >> userInput;
for(int i = 0; i < options.size(); ++i) {
if(userInput == options.at(i).first)
return userInput;
}
if(userInput == "q")
return "back";
cout << "The input: <" + userInput + "> is not one of the options, repeating prompt\n";
}
}
string Menu::ensureProperInput(string promptInput, vector<pair<string, string>> optionsInput) {
bool running = true;
string userInput;
while(running) {
cout << promptInput;
for(int i = 0; i < optionsInput.size(); ++i) {
cout << "\n\t" + optionsInput.at(i).first + ": " + optionsInput.at(i).second;
}
cout << "\ninput: ";
cin >> userInput;
for(int i = 0; i < optionsInput.size(); ++i) {
if(userInput == optionsInput.at(i).first)
return userInput;
}
if(userInput == "q")
return "back";
cout << "The input: <" + userInput + "> is not one of the options, repeating prompt\n";
}
}
bool Menu::run() { // Will change to virtual when I put this in header
}
void Menu::halt() {
string userInput;
cout << "\nType 'c' to continue:";
cin >> userInput;
}
// STATISTIC MENU METHODS
StatisticsMenu::StatisticsMenu(stack<Menu*>* menuInput, Codex* codexInput) : Menu(menuInput, codexInput) {
populateMenu("Statistics of: " + codexInput->getLangaugeName(),
{make_pair("b", "Back to language menu")});
}
bool StatisticsMenu::run() {
codex->printStatistics();
string command = ensureProperInput();
if(command == "b")
return false;
return true;
}
// GRAMMAR MENU METHODS
GrammarMenu::GrammarMenu(stack<Menu*>* menuInput, Codex* codexInput) : Menu(menuInput, codexInput) {
populateMenu("Grammar Menu of " + codexInput->getLangaugeName(),
{make_pair("w", "View Word Types"),
make_pair("p", "View Productions"),
make_pair("at", "Add Word Type"),
make_pair("ap", "Add Production"),
make_pair("b", "Back to Language menu")});
// Add remove wordtype and production type
}
bool GrammarMenu::run() {
string input;
string symbolName;
string command = ensureProperInput();
if(command == "b")
return false;
if(command == "w") {
cout << "Displaying Symbols:\n";
codex->getGrammar()->printSymbols();
}
if(command == "p") {
cout << "Displaying Productions:\n";
codex->getGrammar()->printNonterminalsAndProductions();
}
if(command == "at") {
cout << "Type the name of your new wordtype (note the first letter must be uppercase):";
cin >> command;
if(command.at(0) > 91) {
cout << "Sorry, the first letter must be uppercase\n";
return true;
}
symbolName = command;
command = ensureProperInput("Would you like the symbol to be preterminal?", {make_pair("y", "Make preterminal"), make_pair("n", "Make nonterminal")});
if(command == "back")
return true;
if(command == "y")
codex->getGrammar()->addSymbol(symbolName, "Preterminal");
if(command == "n")
codex->getGrammar()->addSymbol(symbolName, "Nonterminal");
cout << "Symbol: " + symbolName + " created.\n";
}
if(command == "ap") {
cout << "What symbol do you want to add a production to?: ";
cin >> symbolName;
Symbol* s = codex->getGrammar()->getSymbol(symbolName);
if(s == nullptr) {
cout << "Returning to grammar menu\n";
return true;
}
else {
input = "";
while(command != "q") {
cout << "Enter the name of a symbol to add to the production (or q to finish):";
cin >> command;
if(command != "q") {
if(input != "") {
input += ",";
}
input += command;
cout << "Production so far: " + input + "\n";
}
}
codex->getGrammar()->getSymbol(symbolName)->addProduction(input, codex->getGrammar()->getSymbols());
cout << "Production added\n";
}
}
return true;
}
// DICTIONARY MENU METHODS
DictionaryMenu::DictionaryMenu(stack<Menu*>* menuInput, Codex* codexInput) : Menu(menuInput, codexInput) {
populateMenu("Dictionary Menu of " + codexInput->getLangaugeName(),
{make_pair("v", "View All Words"),
make_pair("s", "Search Language"),
make_pair("a", "Add to Language"),
make_pair("sen", "Sentence Functions"),
make_pair("b", "Back to Language menu")});
}
bool DictionaryMenu::run() {
string soundInput;
string typeInput;
string command = ensureProperInput();
if(command == "v") {
codex->printDictionary();
}
if(command == "s") {
cout << "What is the sound of the word you're looking for: ";
cin >> soundInput;
cout << "What is the type of the word you're looking for: ";
cin >> typeInput;
cout << codex->getWord(soundInput, typeInput)->toString() + "\n";
}
if(command == "a") {
menus->push(new AddWordMenu(menus, codex));
}
if(command == "sen") {
menus->push(new SentanceMenu(menus, codex));
}
if(command == "r") {
cout << "Input the sound of the word you want to erase: ";
cin >> soundInput;
cout << "Input the type of the word you want to erase: ";
cin >> typeInput;
codex->removeWord(soundInput, typeInput);
}
if(command == "b")
return false;
return true;
}
// ADD WORD MENU METHODS
AddWordMenu::AddWordMenu(stack<Menu*>* menuInput, Codex* codexInput) : Menu(menuInput, codexInput) {
populateMenu("Add Word Menu of " + codexInput->getLangaugeName(),
{make_pair("m", "Manually Add a Word to the Language"),
make_pair("r", "Randomly Generate a Word to Add"),
make_pair("b", "Back to Dictionary menu")});
}
bool AddWordMenu::run() {
string command = ensureProperInput();
string formattedWordString;
vector<Word*> list;
string wordSound;
string wordType;
string wordMeaning;
if(command == "m") {
cout << "Manually creating words:\n";
while(command != "q") {
cout << "Enter the c to continue or q to quit";
cin >> command;
if(command == "c") {
cout << "Enter the sound of the word (must be all lowercase): ";
cin >> wordSound;
cout << "Enter the type of the word (first letter must be uppercase): ";
cin >> wordType;
cout << "Enter the meaning of the word: ";
cin >> wordMeaning;
formattedWordString = wordSound + "-" + wordType + "-" + wordMeaning;
codex->createWord(formattedWordString);
}
}
cout << "Completed manually entering words\n";
}
if(command == "r") {
cout << "Randomly generating words";
while(command != "q") {
cout << "Enter the c to continue or q to quit: ";
cin >> command;
if(command == "c") {
wordSound = "";
formattedWordString = "";
list = codex->chooseWordsToCombine();
for(int i = 0; i < list.size(); ++i) {
wordSound += list.at(i)->getSound();
}
cout << "The new word [" + wordSound + "] is composed of: \n";
for(int i = 0; i < list.size(); ++i) {
cout << list.at(i)->toString() + "\n";
}
cout << "Meanings in a row: ";
for(int i = 0; i < list.size(); ++i) {
cout << list.at(i)->getMeaning() + ", ";
}
cout << "\nEnter the type of the word (first letter must be uppercase): ";
cin >> wordType;
cout << "Enter the meaning of the word: ";
cin >> wordMeaning;
formattedWordString = wordSound + "-" + wordType + "-" + wordMeaning;
for(int i = 0; i < list.size(); ++i) {
formattedWordString += "|" + list.at(i)->toRootFormat();
}
cout << formattedWordString + "\n";
codex->createWord(formattedWordString);
}
}
}
if(command == "b")
return false;
return true;
}
// ADD WORD MENU METHODS
SentanceMenu::SentanceMenu(stack<Menu*>* menuInput, Codex* codexInput) : Menu(menuInput, codexInput) {
populateMenu("Add Word Menu of " + codexInput->getLangaugeName(),
{make_pair("g", "Generate a Sentance Randomly"),
make_pair("w", "Write a Sentance and have it Translated"),
make_pair("b", "Back to Dictionary menu")});
}
bool SentanceMenu::run() {
char input[50];
string command = ensureProperInput();
if(command == "g") {
cout << "\n" + codex->getGrammar()->generateRandomStringFromSymbol("Sentence") + "\n";
return true;
}
if(command == "w") {
cout << "Note, you may only use the words already in the language, those being:\n";
codex->printDictionary();
cout << "If your sentence is grammatically incorrect it may be rejected.\nInput your sentence here:";
cin.clear();
fflush(stdin);
cin.getline(input,sizeof(input));
cout << endl;
codex->translate(input);
cout << endl;
return true;
}
if(command == "b")
return false;
return true;
}
// LANGUAGE MENU METHODS
LanguageMenu::LanguageMenu(stack<Menu*>* menuInput, Codex* codexInput) : Menu(menuInput, codexInput) {
populateMenu("Language Menu of: " + codexInput->getLangaugeName(),
{make_pair("s", "View Language Statistics"),
make_pair("g", "Load Grammar Options"),
make_pair("d", "Load Dictionary Options"),
make_pair("f", "Save Language to File"),
make_pair("b", "Back to main menu")});
}
bool LanguageMenu::run() {
string command = ensureProperInput();
if(command == "b")
return false;
if(command == "s") {
menus->push(new StatisticsMenu(menus, codex));
}
if(command == "g") {
menus->push(new GrammarMenu(menus, codex));
}
if(command == "d") {
menus->push(new DictionaryMenu(menus, codex));
}
if(command == "f") {
command = ensureProperInput("What do you want the folder to be called:",
{make_pair("a", "Save to Folder with Language Name"),
make_pair("b", "Save to Folder Inputted")});
if(command == "a")
codex->writeCodexToFile(codex->getLangaugeName());
if(command == "b") {
cout << "Input the name of the folder: ";
cin >> command;
codex->writeCodexToFile(command);
}
cout << "Language has been saved";
}
return true;
}
// MAIN MENU METHODS
MainMenu::MainMenu(stack<Menu*>* menuInput) : Menu(menuInput) {
populateMenu("Main Menu", {make_pair("n", "Create New Language"),
make_pair("l", "Load Existing Language from File"),
make_pair("q", "Exit")});
}
bool MainMenu::run() {
string command = ensureProperInput();
if(command == "q")
return false;
if(command == "n") {
codex = createNewLanguage();
menus->push(new LanguageMenu(menus, codex));
}
if(command == "l") {
codex = loadExistingLanguage();
menus->push(new LanguageMenu(menus, codex));
}
return true;
}
Codex* MainMenu::createNewLanguage() { //INCOMPLETE
/* Walks user through the steps of creating a new language [giving it a name,
* choosing a language type, and giving a description] and then creates a codex
* with the specifications and returns it.
*/
char input[50];
Codex* c = new Codex();
string line;
cout << "What is the name of this new language: ";
cin >> line;
cout << "Give a brief description: ";
c->setName(line);
cin.clear();
fflush(stdin);
cin.getline(input,sizeof(input));
c->setDescription(input);
return c;
}
Codex* MainMenu::loadExistingLanguage() {
/* Allows the user to type in the name of a language text file and uses the files
* in it to create a language. Tells them if they're missing a specific file or if
* the files were corrupted in a specific way.
*/
string line;
cout << "What is the language called: ";
cin >> line;
codex = new Codex(line);
codex->initializeScrivener();
codex->initializeGrammar(line);
codex->initializeDictionary(line);
return codex;
}