forked from oleoneto/TQF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.h
executable file
·1161 lines (891 loc) · 39.7 KB
/
Game.h
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Game.h
// The Q Factor
//
// Created by Leo N. on 11/21/16.
// Copyright © 2016 Leo Neto. All rights reserved.
//
#ifndef Game_h
#define Game_h
#include "Libraries.h"
#include "Questions.h"
#include "Player.h"
#include "Manager.h"
#include "Quiz.h"
class Game{
private:
// Booleans and Numbers
int Qindex;
float ScorePercent;
bool is_Manager;
bool is_Player;
bool nowPlaying;
bool quitting;
bool firstRun;
bool reLoading;
bool didEscape;
bool returningUser;
bool errorLoading;
bool loadedPreviousQuiz;
bool providedValidPath;
bool didPrintQuizInfo;
// Strings
std::string pName, pLang, pAnswer, answer;
std::string Feedback;
std::string PreviousQuizPath;
std::string CurrentQuizPath;
// File Streams
std::ifstream File;
std::ifstream QuizPathIn;
std::ifstream QuizInfoIn;
std::ifstream RemainingQuestionsIn;
std::ofstream Results;
std::ofstream QuizProgress;
std::ofstream QuizPathOut;
std::ofstream QuizInfoOut;
std::ofstream RemainingQuestionsOut;
// Classes
Player *P;
Manager M;
Questions Q;
Questions *QA;
Questions *AR;
Quiz *QuizInfo;
// Heap Arrays
std::string *GameQuestions;
//*************************************************
//*************************************************
//*************************************************
//*************************************************
//*************************************************
//*************************************************
public:
Game(){
//Initializing Booleans
is_Manager = false;
is_Player = false;
nowPlaying = true;
quitting = false;
firstRun = true;
reLoading = false;
didEscape = false;
returningUser = false;
errorLoading = false;
loadedPreviousQuiz = false;
providedValidPath = false;
didPrintQuizInfo = false;
//Initializing Player
P = new Player;
}//constructor
~Game(){
}//desctructor
// Instructions and Options...
//*************************************************
//*************************************************
//*************************************************
void FormatLines(){
std::cout<<"--------------------------------------------------------------------------"<< std::endl;
}//end FormatLines
void Welcome(){
FormatLines();
std::cout<<"Welcome to The Q Factor, an \"open-sauce\" program by Leo Neto."<<std::endl;
FormatLines();
EmptySpace();
}//end Welcome
void CheckReturningUser(){
std::cout<<"Are you starting a new quiz? ";
getline(std::cin, Feedback);
YESorNO();
if(Feedback=="Yes"){
std::cout<<"Alright, let help you set up an new quiz."<<std::endl;
EmptySpace();
}//end if YES - starting a new quiz
else if(Feedback=="No"){
std::cout<<"If you quit in the middle of a quiz, would you like to pick up where you left off? ";
getline(std::cin, Feedback);
YESorNO();
// Picking up where the user left off...
if(Feedback=="Yes"){
std::cout<<"Let me check your previous progress..."<<std::endl;
int number, index;
std::string Path, storage;
std::stringstream temporary;
QuizPathIn.open("LastPath.tqf");
if(QuizPathIn.is_open()){
getline(QuizPathIn, Path);
CurrentQuizPath = Path;
QuizInfo = new Quiz[1];
QuizPathIn.close();
}else{
//std::cout<<"I couln't find any files from last time..."<<std::endl;
errorLoading = true;
}//end if-else path is open
QuizInfoIn.open("QuizInfo.tqf");
if(QuizInfoIn.is_open()){
getline(QuizInfoIn, storage);
QuizInfo[0].setQuizLanguage(storage);
getline(QuizInfoIn, storage);
QuizInfo[0].setQuizTopic(storage);
getline(QuizInfoIn, storage);
temporary << storage;
temporary >> number;
QuizInfo[0].setQuizTotalQuestions(number);
temporary.clear();
getline(QuizInfoIn, storage);
temporary << storage;
temporary >> Qindex;
//Qindex;
QuizInfoIn.close();
returningUser = true;
}else{
std::cout<<"I couldn't find any files from last time..."<<std::endl;
errorLoading = true;
}//end if-else info is open
if(!errorLoading){
LoadPreviousQuiz();
is_Player = true;
}
if(errorLoading){
returningUser = false;
EmptySpace();
std::cout<<"Sorry, quiz progress could not be retrieved from file."<<std::endl;
std::cout<<"I can still run the program if you supply a file with the quiz."<<std::endl;
std::cout<<"Please, provide a path: ";
getline(std::cin, Feedback);
LoadQuestions(Feedback);
EmptySpace();
}
}//end if picking up where user left off
else{
returningUser = false;
EmptySpace();
std::cout<<"Oh, okay! I'll run the default version of the program, then."<<std::endl;
EmptySpace();
LoadDefaultQuestions();
}
}//end else-if not starting a new quiz
}//end CheckReturningUser
void LoadPreviousQuiz(){
int counter, index;
std::string content;
std::stringstream temporary;
File.open("RemainingQuestions.tqf");
counter = 0;
// This is used instead of a fixed number,
// to help with flexibility and portability...
QA = new Questions[QuizInfo[0].getQuizTotalQuestions()];
for(index=0; index<=(QuizInfo[0].getQuizTotalQuestions()); index++){
if(File.is_open()){
getline(File, content);
QA[index].setQuestion(content);
getline(File, content);
QA[index].setOptionA(content);
getline(File, content);
QA[index].setOptionB(content);
getline(File, content);
QA[index].setOptionC(content);
getline(File, content);
QA[index].setOptionD(content);
getline(File, content);
QA[index].setAnswer(content);
errorLoading = false;
returningUser = true;
loadedPreviousQuiz = true;
providedValidPath = true;
}else{
std::cout<<"I couldn't find the file with your progress."<<std::endl;
errorLoading = true;
returningUser = false;
}
}
CurrentQuizPath = "RemainingQuestions.tqf";
File.close();
//is_Player = true;
}//end LoadPreviousQuiz
void FirstInstructions(){
std::cout<<"The Q Factor will allow you to setup quizzes for students, friends,";
std::cout<<"family members"<<std::endl;
}//end FirstInstructions
void PlayerInstructions(){
std::cout << "To answer a question just type any of the letters a, b, c, or d"<<std::endl;
std::cout << "After answering a question, you'll be told whether or not you are correct."<<std::endl;
}//end ShortInstructions()
//*************************************************
//*************************************************
//*************************************************
void GameRole(){
std::cout <<"Please provide some information about yourself."<<std::endl;
std::cout <<"Do you want to run the program as a (P)Player or as a (M)Manager? ";
getline(std::cin, Feedback);
if(Feedback=="both" || Feedback=="Both"){
std::cout<<"I understand you're not Java but please slow down..."<<std::endl;
EmptySpace();
}
if(Feedback=="manager" | Feedback=="Manager" | Feedback =="m"){
Feedback = "M";
}else if(Feedback =="player" | Feedback=="Player" | Feedback =="p"){
Feedback = "P";
}
while(Feedback!="P" & Feedback!="M"){
std::cout << "Try P for Player, or M for Manager: ";
getline(std::cin, Feedback);
}
if(Feedback=="P"){
P->SetPlayer();
PlayerInstructions();
if(!providedValidPath){
LoadDefaultQuestions();
}
}else if(Feedback=="M"){
M.SetManager();
ManagerInstructions();
ManagerPath();
}
}//end GameRole()
//*************************************************
//*************************************************
//*************************************************
//*************************************************
//*************************************************
//*************************************************
void ManagerInstructions(){
if(providedValidPath){
std::cout<<"Let's make sure your quiz is setup correctly."<<std::endl;
}
else{
std::cout<<"As a manager you can set up the program for others."<<std::endl;
std::cout<<"You can choose whether the program runs with the standard 17 questions,"<<std::endl;
std::cout<<"or you can supply your own data file."<<std::endl;
EmptySpace();
std::cout << "You can choose to either:"<<std::endl;
std::cout << "LOAD questions from your own file, or"<<std::endl;
std::cout << "RUN the standard version of the quiz."<<std::endl;
}
}//end ManagerInstructions
void ManagerPath(){
is_Manager = true;
if(!providedValidPath){
// If it is the first time running the program
// ask the manager whether they want to load a custom file
// Set FirstRun to true.
if(firstRun){
EmptySpace();
std::cout <<"What will it be? ";
getline(std::cin, Feedback);
if(Feedback=="Load" | Feedback == "load" | Feedback == "LOAD" | Feedback=="l"){
Feedback = "L";
}else if(Feedback=="Run" | Feedback == "run" | Feedback == "RUN" | Feedback == "r"){
Feedback = "R";
}
while(Feedback !="L" & Feedback != "R"
& Feedback !="l" & Feedback !="r"){
std::cout <<"Type L to load, or R to run: ";
getline(std::cin, Feedback);
}
}//end if FirstRun
// If it is not the first time the program has run
// Ask the manager whether they want to retake the previous quiz
if(!firstRun){
std::cout<<"Do you want to RELOAD the previous quiz? ";
getline(std::cin, Feedback);
YESorNO();
if(Feedback=="Yes"){
reLoading = true;
}else{
std::cout<<"Do you want to load a NEW quiz? ";
getline(std::cin, Feedback);
YESorNO();
if(Feedback=="No"){
std::cout<<"Terminating the program..."<<std::endl;
}else{
Feedback = "L";
}
}
EmptySpace();
}
if(reLoading){
Feedback = "L";
LoadQuestions(PreviousQuizPath);
}
if(!reLoading){
if(Feedback == "L"){
std::cout <<"Type the path of the file (i.e. \"/Users/John/Documents/file.csv\"):"<<std::endl;
getline(std::cin, Feedback);
File.open(Feedback);
std::cout<<""<<std::endl;
while(!File.is_open()){
std::cout <<""<< std::endl;
std::cout <<"File NOT Found."<<std::endl;
std::cout <<"Type the path of the file: ";
getline(std::cin, Feedback);
std::cout<<"The path you chose is: "<<Feedback<<std::endl;
File.open(Feedback);
}
PreviousQuizPath = Feedback;
if(firstRun){
// Close file to open later.
File.close();
LoadQuestions(Feedback);
//SetQuestionsArray();
FormatLines();
std::cout <<"The Program is ready to run..."<<std::endl;
std::cout <<"Now you'll need to set up a player to play the program."<<std::endl;
EmptySpace();
SetPlayer();
is_Player = true;
}
}else{
LoadDefaultQuestions();
//SetQuestionsArray();
SetPlayer();
std::cout <<""<< std::endl;
FormatLines();
std::cout <<"Running default version..."<<std::endl;
}
FormatLines();
}//end if Not Reloading
}//end if NOT loaded from a previous quiz
else{
FormatLines();
std::cout <<"The Program is ready to run..."<<std::endl;
std::cout <<"Now you'll need to set up a player to play the program."<<std::endl;
EmptySpace();
SetPlayer();
is_Player = true;
}
firstRun = false;
}//end ManagerInstructions()
void SetPlayer(){
std::string playername;
std::cout << "What is the player's name? ";
getline(std::cin, playername);
P->setPlayerName(playername);
std::cout <<""<< std::endl;
std::cout << "Alright, "<<P->getPlayerName()<<" is all set to take the quiz."<<std::endl;
}//end SetPlayer
//*************************************************
//*************************************************
//*************************************************
//*************************************************
//*************************************************
//*************************************************
//*************************************************
//*************************************************
//*************************************************
//*************************************************
//*************************************************
//*************************************************
void LoadDefaultQuestions(){
CurrentQuizPath = "code.tqf";
QuizPathOut.open("LastPath.tqf");
if(QuizPathOut.is_open()){
QuizPathOut << CurrentQuizPath <<std::endl;
QuizPathOut.close();
}
int counter, index;
std::string content;
std::stringstream temporary;
//Info: Language, Topic, Total Questions
QuizInfo = new Quiz[1];
File.open("code.tqf");
// This takes care of the first line
// which has the format: English, Biology, 25
// This iteration will store "English, Biology, 25"
// into one single string.
// This string will be parsed into three variables:
// Language, Topic, and TotalQuestions...
// TotalQuestions will be used to iterate through with getline
// the total number of questions times 6, as seen in the for-loop
counter = 0;
if(File.is_open()){
if(counter==0){
getline(File, content);
QuizInfo[0].SetQuizInfo(content);
// Test printing...
// std::cout<<"Loading..."<<std::endl;
// QuizInfo[0].PrintQuizInfo();
counter++;
}
}
while(QuizInfo[0].getQuizTotalQuestions()<=0){
std::cout<<"Something is wrong. The default file was moved."<<std::endl;
std::cout<<"Please provide a valid path to a quiz file: ";
getline(std::cin, Feedback);
File.open(Feedback);
if(File.is_open()){
getline(File, content);
QuizInfo[0].SetQuizInfo(content);
}
// Test printing...
std::cout<<"Data Loaded..."<<std::endl;
QuizInfo[0].PrintQuizInfo();
didPrintQuizInfo=true;
EmptySpace();
}
// This is used instead of a fixed number,
// to help with flexibility and portability...
QA = new Questions[QuizInfo[0].getQuizTotalQuestions()];
for(index=0; index<=(QuizInfo[0].getQuizTotalQuestions()); index++){
// This should not be here... Placing it above the initialization of the for-loop.
// QA = new Questions[QuizInfo[0].getQuizTotalQuestions()];
if(File.is_open()){
getline(File, content);
QA[index].setQuestion(content);
getline(File, content);
QA[index].setOptionA(content);
getline(File, content);
QA[index].setOptionB(content);
getline(File, content);
QA[index].setOptionC(content);
getline(File, content);
QA[index].setOptionD(content);
getline(File, content);
QA[index].setAnswer(content);
// Testing output from input.
// std::cout<<index<<".[LQ]: "<<QA[index].getQuestion()<<std::endl;
errorLoading = false;
providedValidPath = true;
}else{
std::cout<<"Default file NOT in directory."<<std::endl;
while(!File.is_open()){
std::cout<<"Please provide another path for the quiz file: ";
getline(std::cin, Feedback);
File.open(Feedback);
errorLoading = true;
}
}//end if-else
}//end for-loop
CurrentQuizPath = "code.tqf";
File.close();
}//end LoadQuestions
void LoadQuestions(std::string Path){
CurrentQuizPath = Path;
QuizPathOut.open("LastPath.tqf");
QuizPathOut.close();
int counter, index;
std::string content;
std::stringstream temporary;
//Info: Language, Topic, Total Questions
QuizInfo = new Quiz[1];
File.open(Path);
counter = 0;
if(File.is_open()){
if(counter==0){
getline(File, content);
QuizInfo[0].SetQuizInfo(content);
// Test printing...
std::cout<<"Data Loaded..."<<std::endl;
QuizInfo[0].PrintQuizInfo();
didPrintQuizInfo=true;
std::cout<<""<<std::endl;
counter++;
}
}else{
std::cout<<"File NOT found"<<std::endl;
errorLoading = true;
}
while(QuizInfo[0].getQuizTotalQuestions()<=0){
std::cout<<"File NOT found."<<std::endl;
std::cout<<"Please provide a valid path to your quiz file: ";
getline(std::cin, Feedback);
CurrentQuizPath = Feedback;
File.open(Feedback);
if(File.is_open()){
getline(File, content);
QuizInfo[0].SetQuizInfo(content);
}
// Test printing...
std::cout<<"Data Loaded..."<<std::endl;
QuizInfo[0].PrintQuizInfo();
didPrintQuizInfo=true;
EmptySpace();
}
// This is used instead of a fixed number,
// to help with flexibility and portability...
QA = new Questions[QuizInfo[0].getQuizTotalQuestions()];
for(index=0; index<=(QuizInfo[0].getQuizTotalQuestions()); index++){
if(File.is_open()){
getline(File, content);
QA[index].setQuestion(content);
getline(File, content);
QA[index].setOptionA(content);
getline(File, content);
QA[index].setOptionB(content);
getline(File, content);
QA[index].setOptionC(content);
getline(File, content);
QA[index].setOptionD(content);
getline(File, content);
QA[index].setAnswer(content);
errorLoading = false;
providedValidPath = true;
}else{
std::cout<<"File NOT Open."<<std::endl;
errorLoading = true;
}
}
if(QuizPathOut.is_open()){
QuizPathOut << CurrentQuizPath <<std::endl;
QuizPathOut.close();
}
File.close();
}//end LoadQuestions
void ValidateAnswer(std::string useranswer){
while(useranswer!="a" && useranswer!="b" && useranswer!="c" && useranswer!="d"){
if(useranswer=="Q" || useranswer=="q" ||
useranswer=="quit" || useranswer=="Quit" ||
useranswer=="exit" || useranswer=="Exit"){
nowPlaying=false;
quitting=true;
QuitBeforeTime();
break;
}
std::cout<<"Invalid answer. Try either a, b, c, or d: ";
getline(std::cin, useranswer);
}//end while
}//end ValidateAnswer
void VerifyAndShout(){
// If isThisCorrect returns true, increment score.
// If isThisCorrect returns false, do nothing.
if(QA[Qindex].isThisCorrect(pAnswer)){
if(!quitting){
P->IncreaseScore();
if(!is_Manager){
P->isCorrect();
}
}else{
std::cout<<"That was correct, why are you quitting?"<<std::endl;
}
}else{
if(!quitting){
if(!is_Manager){
P->isWrong();
}
}else{
std::cout<<"Wrapping up now..."<<std::endl;
}
}
}//end VerifyAndShout
void EndQuiz(){
if(Qindex==(QuizInfo[0].getQuizTotalQuestions())-1){
nowPlaying = false;
if(is_Manager){
ManagerReport();
}else{
AnotherQuiz();
}
}else{
Qindex++;
}
}//end EndQuiz
void ScoreReport(){
std::cout<<""<<std::endl;
std::cout<<"Final score ";
std::cout<<P->getPlayerScore();
std::cout<<" points."<<std::endl;
std::cout<<""<<std::endl;
FormatLines();
//Percent Calculator...
//ScorePercent = (P->getPlayerScore()/QuizInfo[0].getQuizTotalQuestions())*100;
}//end ScoreReport
void YESorNO(){
while(Feedback!="Yes" && Feedback!="YES" && Feedback!="yes" && Feedback!="Y" && Feedback!="y" && Feedback!="YEAH" &&
Feedback!="Yep" && Feedback!="Yeah" && Feedback!="yeah" && Feedback!="yep" && Feedback!="YEP" &&
Feedback!="No" && Feedback!="NO" && Feedback!="no" && Feedback!="N" && Feedback!="n" &&
Feedback!="Nope" && Feedback!="nope" && Feedback!="NOPE"){
std::cout<<"Invalid input. Try YES, or NO: ";
getline(std::cin, Feedback);
}
if(Feedback=="YES" || Feedback=="YEP" || Feedback=="Yeah" || Feedback=="Yes" || Feedback=="Yep" || Feedback=="yes" || Feedback=="Y" || Feedback=="y" || Feedback=="yeah" || Feedback=="yep" || Feedback=="YEAH"){
Feedback = "Yes";
}else if(Feedback=="No" || Feedback=="NO" || Feedback=="no" || Feedback=="N" || Feedback=="n" || Feedback=="nope" || Feedback=="Nope" || Feedback=="NOPE"){
Feedback = "No";
}
}//end YESorNO
void ManagerReport(){
if(is_Manager){
EmptySpace();
FormatLines();
std::cout<<"Hello again, "<<M.getName()<<"!"<<std::endl;
std::cout<<"The quiz is over. Would you like to save the results to a file? ";
getline(std::cin, Feedback);
YESorNO();
if(Feedback=="Yes"){
// Output stream...
int counter, index;
Results.open("REPORT-"+P->getPlayerName()+".tqf", std::ofstream::app);
if(!Results.is_open()){
Results.open("REPORT-"+P->getPlayerName()+".tqf");
}
if(Results.is_open()){
for(counter=0, index=0; counter<QuizInfo[0].getQuizTotalQuestions(); counter++, index++){
if(counter==0){
Results << "-----------------------------------------------------"<<std::endl;
Results << "Quiz results for " << P->getPlayerName() <<"."<<std::endl;
Results << "Administered by " << M.getName()<<std::endl;
Results << "Quiz Topic: " << QuizInfo[0].getQuizTopic()<<std::endl;
Results << "Total Number of Questions: " << QuizInfo[0].getQuizTotalQuestions()<<std::endl;
Results << "-----------------------------------------------------"<<std::endl;
}
Results << AR[index].getQuestion()<<std::endl;
Results <<"Correct: "<< QA[index].getCorrectAnswer()<<std::endl;
Results <<"Answered: "<<AR[index].getPlayerAnswer()<<std::endl;
Results << " "<<std::endl;
if(counter==(QuizInfo[0].getQuizTotalQuestions())-1){
Results <<"Final Score: "<<P->getPlayerScore()<<" out of "<<QuizInfo[0].getQuizTotalQuestions()<<std::endl;
}
}
Results.close();
EmptySpace();
EmptySpace();
FormatLines();
std::cout<<"Report saved."<<std::endl;
FormatLines();
}else{
std::cout<<"Error writing to file."<<std::endl;
FormatLines();
}
}
//ANOTHER QUIZ...
AnotherQuiz();
}//if is_Manager
}//end ManagerReport
void AnotherQuiz(){
EmptySpace();
std::cout<<"Alright, the quiz is now over. Would you like to setup another quiz? ";
getline(std::cin, Feedback);
YESorNO();
if(Feedback=="Yes"){
nowPlaying = true;
Qindex = 0;
std::cout <<"Type the path of the file (i.e. \"/Users/John/Documents/file.txt\"):"<<std::endl;
getline(std::cin, Feedback);
File.open(Feedback);
EmptySpace();
while(!File.is_open()){
EmptySpace();
std::cout <<"The given file could NOT be found."<<std::endl;
std::cout <<"Please type a valid path for the next quiz: ";
getline(std::cin, Feedback);
std::cout<<"The path you chose is: "<<Feedback<<std::endl;
File.open(Feedback);
}
File.close();
PreviousQuizPath = Feedback;
LoadQuestions(Feedback);
}else{
FormatLines();
std::cout<<"Ok! Ending program now..."<<std::endl;
FormatLines();
EmptySpace();
}
}//end AnotherQuiz
void SetAnswersArray(){
AR = new Questions[QuizInfo[0].getQuizTotalQuestions()];
}//end SetAnswersArray
void AppendAnswer(){
AR[Qindex].setQuestion(QA[Qindex].getQuestion());
AR[Qindex].setPlayerAnswer(pAnswer);
}//end AppendAnswer
void QuitBeforeTime(){
int counter, index;
// If the user quits before time, we'll start the quiz
QuizProgress.open("QuizProgress.tqf");
Results.open("QuizAnswers.tqf");
RemainingQuestionsOut.open("RemainingQuestions.tqf");
QuizInfoOut.open("QuizInfo.tqf");
EmptySpace();
if(QuizProgress.is_open()){
QuizProgress << Qindex <<std::endl;
std::cout<<"Your progress was saved for when you come back..."<<std::endl;
QuizProgress.close();
}else{
std::cout<<"Your progress could not be saved..."<<std::endl;
}
if(Results.is_open()){
for(counter=0; counter<Qindex; counter++, index++){
Results << AR[counter].getQuestion()<<std::endl;
Results <<"Correct: "<< QA[counter].getCorrectAnswer()<<std::endl;
Results <<"Answered: "<<AR[counter].getPlayerAnswer()<<std::endl;
if(counter==(QuizInfo[0].getQuizTotalQuestions())-1){
Results <<"Final Score: "<<P->getPlayerScore()<<" out of "<<QuizInfo[0].getQuizTotalQuestions()<<std::endl;
}
}//end for-loop
Results.close();
std::cout<<"Your answers to this quiz were saved."<<std::endl;
}else{
std::cout<<"Your answers could not be saved..."<<std::endl;
}
if(RemainingQuestionsOut.is_open()){
// Get all the remaining questions, including the current at Qindex
// since the user will not have answered the question from which they quit.
for(counter=Qindex; counter<QuizInfo[0].getQuizTotalQuestions(); counter++){
RemainingQuestionsOut << QA[counter].getQuestion()<< std::endl;
RemainingQuestionsOut << QA[counter].getOptionA() << std::endl;
RemainingQuestionsOut << QA[counter].getOptionB() << std::endl;
RemainingQuestionsOut << QA[counter].getOptionC() << std::endl;
RemainingQuestionsOut << QA[counter].getOptionD() << std::endl;
RemainingQuestionsOut << QA[counter].getCorrectOption() << std::endl;
}//end for-loop
RemainingQuestionsOut.close();
std::cout<<"The remaining questions were saved to a file."<<std::endl;
}else{
std::cout<<"The remaining questions could not be saved..." <<std::endl;
}
if(QuizInfoOut.is_open()){
QuizInfoOut << QuizInfo[0].getQuizLanguage()<<std::endl;
QuizInfoOut << QuizInfo[0].getQuizTopic()<<std::endl;
// Number of questions left to answer...
QuizInfoOut << QuizInfo[0].getQuizTotalQuestions()-Qindex<<std::endl;
// Index of answer from which user quit...
QuizInfoOut << Qindex <<std::endl;
QuizInfoOut.close();
std::cout<<"Quiz information saved..."<<std::endl;
}else{
std::cout<<"Quiz information could not be saved..."<<std::endl;
}
}// QuitBeforeTime...
void FreeHeap(){
delete P;
delete [] QA;
delete [] QuizInfo;
delete [] AR;
}//end FreeHeap
//*************************************************
//*************************************************
//*************************************************
//*************************************************
//*************************************************
//*************************************************
/*************************************************
*************************************************
*************************************************
*************************************************
*************************************************
*************************************************
Game runs here...
How it works:
We start off by outputing an empty line and then call the
PrintQuestions() function which is part of the Questions class.
PrintQuestions() will output a question followed by four possible answers.