-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
227 lines (215 loc) · 4.81 KB
/
main.cpp
File metadata and controls
227 lines (215 loc) · 4.81 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
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <exception>
#include <random>
#include <algorithm>
#include <iomanip>
using namespace std;
void error(string s)
{
throw runtime_error(s);
}
ifstream is{"qna.txt"}; //open qna.txt for reading
class Stats
{
int heart;
public:
string name;
Stats(int h,string n)
{
heart=h;
name=n;
}
Stats()
{
heart=3;
}
int dispheart(){return heart;}
void lose(){ heart--;}
};
class Mcq_ques //store question,answer and their explaination
{
public:
vector <string> ques;
vector <char> ans;
vector <string> expl;
};
Mcq_ques shuffle_ques(Mcq_ques M);
istream& operator >>(istream& is,Mcq_ques& M) //input operator to input from file
{//change istream to bad state if input isn't formatted correctly
string s,str;
while(is>>s)
{
if(s!="Ques{")
{
is.clear(ios_base::badbit);
return is;
}
for(is>>s;s!="}~";is>>s)
{
str=str+s+' ';
}
M.ques.push_back(str);
str="\0";
is>>s;
if(s!="Ans{")
{
M.ques.pop_back();
is.clear(ios_base::badbit);
return is;
}
char ch1='\0';
is>>ch1>>s;
if(s!="}~")
{
M.ques.pop_back();
is.clear(ios_base::badbit);
return is;
}
else M.ans.push_back(ch1);
string Ex,Exp;
is>>Ex;
if(Ex!="Expl{")
{
is.clear(ios_base::badbit);
M.ques.pop_back();
M.ans.pop_back();
return is;
}
for(is>>Ex;Ex!="}~";is>>Ex)
{
Exp=Exp+Ex+' ';
}
M.expl.push_back(Exp);
}
return is;
}
ostream& operator <<(ostream& os,Stats& S) //output operator for Stat
{
return os<<setw(10)<<S.name<<'\t'<<S.dispheart()<<"❤️";
}
void printresult();
Stats MC;
Stats Enemy{5,"Skeleton"}; //create Enemy
void battle(Mcq_ques M,int round);
char select_option();
bool game_end();
int main()
try{
if(!is) error("couldn't open the file qna.txt");
Mcq_ques M;
is>>M;
if(is.bad())
error("The input isn't formatted correctly");
M={shuffle_ques(M)}; //shuffle question order
if(M.ques.size()<(MC.dispheart()+Enemy.dispheart()-1))
error("Not enough questions to complete the game");
string mc_name;
cout<<"Enter your name : ";
cin>>mc_name;
MC.name=mc_name;
printresult();
for(int round=0;round<M.ques.size();round++) //run the game till Enemy or MC heart reaches zero
{
battle(M,round);
if(game_end())
return 0;
}
}
catch(exception& e) //catch runtime exceptions and disp error msg
{
cout<<"error: "<<e.what();
return 1;
}
void printresult() //print Name,Stat of both MC and Enemy
{
cout<<"____________________________________________________________"<<'\n'
<<MC<<'\n'<<Enemy<<'\n'
<<"____________________________________________________________"<<'\n';
}
void battle(Mcq_ques M,int round) //runs a round
{//outputs the question and read an option from user
cout<<M.ques[round]<<'\n';
char answer;
answer=select_option();
if(M.ans[round]==answer) //if answer is correct Enemy lose a heart
{
Enemy.lose();
cout<<"Correct!!"<<'\n'
<<M.expl[round]<<'\n';
}
else //else MC loses a heart
{
MC.lose();
cout<<"Wrong!! the correct option was "<<M.ans[round]<<'\n';
cout<<M.expl[round]<<'\n';
}
printresult();
}
char select_option() //convert lower,upper,num to options and read till the input isn't a valid option
{
char option;
bool cond=true;
while(cond)
{
cin.clear();
cin>>option;
switch(option)
{
case '1': case 'A': case 'a':
option='a';
cond=false;
break;
case '2': case 'B': case 'b':
option='b';
cond=false;
break;
case '3': case 'C': case 'c':
option='c';
cond=false;
break;
case '4': case 'D': case 'd':
option='d';
cond=false;
break;
default:
cout<<"Please select a valid option ";
}}
return option;
}
bool game_end() //return true if MC or Enemy heart reaches zero
{
if(MC.dispheart()==0)
{
cout<<"\n -------You lose-------\n";
return 1;
}
else if(Enemy.dispheart()==0)
{
cout<<"\nCongratulations!!"
<<"\n-------You Won-------\n";
return 1;
}
else return 0;
}
Mcq_ques shuffle_ques(Mcq_ques M) //shuffle the order of questions and their answers and explanation
{
Mcq_ques M2{M};
random_device rd;
mt19937 g(rd());
shuffle(M.ques.begin(),M.ques.end(),g);
for(int i=0;i<M.ques.size();i++)
{
for(int j=0;j<M2.ques.size();j++)
{
if(M.ques[i]==M2.ques[j])
{
M.ans[i]=M2.ans[j];
M.expl[i]=M2.expl[j];
}
}
}
return M;
}