-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterface.h
More file actions
191 lines (173 loc) · 3.7 KB
/
Interface.h
File metadata and controls
191 lines (173 loc) · 3.7 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
#ifndef HACK_INTERFACE_H
#define HACK_INTERFACE_H
#include <iostream>
#include "Question.h"
#include "Player.h"
#include "Enemy.h"
#include "Level.h"
#include <thread>
using namespace std;
#define NORTH 0
#define EAST 1
#define SOUTH 2
#define WEST 3
class Interface {
private:
vector<Question> questions;
int qn;
long timelimit;
bool isGoodAnswer(string s,int n) {
int c = (int)s[0];
c |= 32;
c -= 96;
return c<=n && c>0 ;
}
public:
Interface(vector<Question> qs) : qn(0) {
questions = qs;
}
int promptMoveLoop(){
string ans = "";
int dir = -1;
cout << "which way do you want to move (w,a,s,d keys): ";
cin >> ans;
while(ans.size()!=1 || !(ans=="w" || ans=="a" || ans=="s" || ans=="d" || ans=="q")) {
cout << "Invalid Input: ";
cin >> ans;
}
if(ans=="w"){
dir = NORTH;
}
if(ans=="a"){
dir = WEST;
}
if(ans=="d"){
dir = EAST;
}
if(ans=="s"){
dir = SOUTH;
}
if(ans == "q"){
dir == -1;
}
return dir;
}
int promptMovement(){
string ans = "";
int dir = -1;
cout << "which way do you want to move (w,a,s,d keys): ";
cin >> ans;
while(ans.size()!=1 || !(ans=="w" || ans=="a" || ans=="s" || ans=="d")) {
cout << "Invalid Input: ";
cin >> ans;
}
if(ans=="w"){
dir = NORTH;
}
if(ans=="a"){
dir = WEST;
}
if(ans=="d"){
dir = EAST;
}
if(ans=="s"){
dir = SOUTH;
}
return dir;
}
//returns num rounds passed
int fight(Player &p, Enemy &e, Level &l) {
vector<string> abilities = p.getAbilities();
int rounds = 0;
while(p.getHealth()>=0 && !e.is_dead()) {
cout << e.get_type() << ": "<<e.health<<"/"<<e.get_max_health()<<endl;
cout << "Player: "<<p.getHealth() << "/"<<p.getMaxHealth()<<endl;
int n = abilities.size();
for(int i = 0; i < abilities.size(); ++i){
cout << abilities[i] << "("<<(char)(i+97)<<"), ";
}
string ans = "";
cin >> ans;
while(ans.size() !=1 || !isGoodAnswer(ans,n)) {
cout << "Invalid input, try again: ";
cin >> ans;
}
Question q = questions[qn];
if(ask(q)){
switch((int)ans[0]){
case 'a':{
l.printLevel();
int d = promptMovement();
string dir = "up";
if(d==SOUTH){
dir = "down";
}
if(d==EAST){
dir = "right";
}
if(d==WEST){
dir = "left";
}
l.movePlayer(dir);
return rounds;
}
case 'b':{
p.shield();
}
case 'c':{
int d = p.attack(&e);
cout << "you attack for" << d <<endl;
}
}
}
cout << e.get_type()<<" attacks you "<<endl;
int d = e.randomize_attack();
cout << "you take "<< d << endl;
p.damage(d);
++rounds;
}
if(p.getHealth()<0){
cout << "you DEAD"<< endl;
}
else if(e.is_dead()){
p.addExperience(e.experience);
}
return rounds;
}
bool ask(Question question){
//prompt
cout << question.getPrompt() << endl;
bool isMultipleChoice = question.getIsMcq();
if(isMultipleChoice){
string ans = "";
int num = question.getNumAnswers();
cout << "select answer (a-"<< (char)(num+97) << "):" << endl;
for(int i = 0; i < num; ++i) {
cout << "("<<(char)(i+97)<<") " << question.getAnswer(i) << endl;
}
cin >> ans;
while(ans.size() !=1 || !isGoodAnswer(ans,num)) {
cout << "Invalid input, try again: ";
cin >> ans;
}
cout << ((int)(ans[0]&(~96))) << endl;
return question.isCorrect((int)(ans[0]&(~96))-1);
}
else {
}
return false;
}
bool ask(Question question, long timeSeconds) {
if(timeSeconds < 0){
ask(question);
}
auto start = chrono::steady_clock::now();
bool out = ask(question);
if((long)chrono::duration_cast<chrono::seconds>(chrono::steady_clock::now()-start).count() < timeSeconds ) {
return out;
}
cout << "Time Limit passed"<< endl;
return false;
}
};
#endif