-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathframework.cpp
More file actions
143 lines (125 loc) · 3.25 KB
/
framework.cpp
File metadata and controls
143 lines (125 loc) · 3.25 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
#include<iostream>
#include"framework.h"
#include <ctime>
#define MAX_TRIES 6 // change to 6
void read_to_vector(std::vector<std::string>& pool, std::string filename){
std::ifstream infile(filename);
std::string bufline;
while(getline(infile, bufline)){
pool.push_back(bufline);
}
return;
}
void make_ht(std::unordered_set<std::string>& ht, std::vector<std::string> pool){
for(int a = 0; a < pool.size(); a++){
ht.insert(pool[a]);
}
return;
}
game::game(std::string QFN, std::string TFN){
read_to_vector(q_pool, QFN);
pool = q_pool; // copy question dict to total pool.
read_to_vector(pool, TFN);
make_ht(ht, pool);
make_ht(q_ht, q_pool);
StrLen = pool[0].size();
correct_index = 0;
for(int a = 0; a < StrLen; a++){
correct_index <<= 2;
correct_index += 2;
}
srand(clock());
}
game::game(std::string QFN){
read_to_vector(q_pool, QFN);
pool = q_pool; // copy question dict to total pool.
make_ht(ht, pool);
make_ht(q_ht, q_pool);
StrLen = pool[0].size();
correct_index = 0;
for(int a = 0; a < StrLen; a++){
correct_index <<= 2;
correct_index += 2;
}
srand(clock());
}
void game::print_status(){
std::cout << "Total pool size: " << pool.size() << std::endl;
std::cout << "Question pool size: " << q_pool.size() << std::endl;
std::cout << "Current ans: " << ans << std::endl;
}
std::string game::result(int val){
std::string returnval;
for(int a = 0; a < StrLen; a++){
if( a==0 )
returnval += "\"";
else
returnval += ","; // print some element
switch(val & 3){
case 0:
returnval += "0"; // change '_' to '0'
break;
case 1:
returnval += "2"; // change '+' to '2'
break;
case 2:
returnval += "1"; // change 'V' to '1'
break;
default:
std::cerr << "Error!" << std::endl;
}
val >>= 2;
}
returnval += "\"\n";
return returnval;
}
void game::random_ans(){
ans = q_pool[rand()%q_pool.size()];
return;
}
void game::start(){
random_ans();
tries = MAX_TRIES;
return;
}
void game::start(std::string q){
set_ans(q); // set ans
return;
}
int game::test_ans(std::string input){
int returnval = 0;
int used[26] = {0};
for(int a = 0; a < StrLen; a++){
used[ans[a]-'a'] += 1;
}
for(int a = 0; a < StrLen; a++){
if(input[a] == ans[a]){
returnval += (2<<(2*a));
used[input[a]-'a'] -= 1;
}
}
for(int a = 0; a < StrLen; a++){
if(input[a] != ans[a] && used[input[a]-'a'] != 0){
returnval += (1<<(2*a));
used[input[a]-'a'] -= 1;
}
}
return returnval;
}
int game::guess(std::string input){
int returnval = test_ans(input);
std::cout << result(returnval);
return returnval;
}
void game::set_ans(int IDX){
ans = q_pool[IDX];
}
void game::set_ans(std::string Ans){
ans = Ans;
}
int game::check_exist(std::string input){
return ht.count(input);
}
int game::q_check_exist(std::string input){
return q_ht.count(input);
}