-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
167 lines (160 loc) · 4.64 KB
/
Copy pathmain.cpp
File metadata and controls
167 lines (160 loc) · 4.64 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
#include<iostream>
#include<vector>
#include<ctime>
#include"framework.h"
#include"AI.h"
#define MainMode 0
/*
int human_play(){
game Game;
std::string str;
while(std::cin >> str){
if(Game.check_exist(str)){
Game.guess(str);
}
else{
std::cout << "Not in Dictionary!" << std::endl;
}
}
return 0;
}
int ai_solve(){
//int main(){
while(1){
std::cout << "Initialize new AI..." << std::endl;
AI ai;
clock_t start_clock = clock();
int solu = ai.solution(1, 1);
std::cout << "Best start: " << ai[solu] << std::endl;
std::cout << "Time used: " << ((double)(clock()-start_clock)) / CLOCKS_PER_SEC << std::endl;
std::string str;
while(std::cin >> str){
if(ai.response(str)){
break;
}
start_clock = clock();
solu = ai.solution(1, 1);
std::cout << "Best start: " << ai[solu] << std::endl;
std::cout << "Time used: " << ((double)(clock()-start_clock)) / CLOCKS_PER_SEC << std::endl;
}
}
return 0;
}
*/
void test(std::string QFN){
game Game(QFN);
int total_guess = 0;
std::vector<int> dist(11, 0);
for(int a = 0; a < 2315; a++){
Game.set_ans(a);
AI ai(QFN);
int count = 0;
while(1){
int solu = ai.solution(1, 1);
int res = Game.test_ans(ai[solu]);
total_guess++;
count++;
std::string resstr;
for(int b = 0; b < ai.getLen(); b++){
switch(res&3){
case 0:
resstr += "0";
break;
case 1:
resstr += "2";
break;
case 2:
resstr += "1";
break;
}
res >>= 2;
}
if(ai.response(resstr)){
dist[count]++;
break;
}
}
if(a%100 == 0 && a != 0){
std::cout << "AVG Guess @ " << a << ": " << ((double)total_guess)/a << std::endl;
for(int b = 1; b <= 10; b++){
std::cout << "Words using " << b << " times of guessing: " << dist[b] << std::endl;
}
}
}
std::cout << "AVG Guess @ " << 2315 << ": " << ((double)total_guess)/2315 << std::endl;
for(int a = 1; a <= 10; a++){
std::cout << "Words using " << a << " times of guessing: " << dist[a] << std::endl;
}
return;
}
void HWMain(std::string QFN, std::string IFname, std::string OFname){
std::vector<std::string> Qs;
std::ifstream Infile(IFname);
std::string bufline;
while(getline(Infile, bufline)){
Qs.push_back(bufline);
}
Infile.close();
std::ofstream Outfile(OFname);
for(int a = 0; a < Qs.size(); a++){
Outfile << Qs[a] << std::endl;
game Game(QFN);
AI ai(QFN);
Game.start(Qs[a]);
int count = 1;
while(1){
int solu = ai.solution(1, 1);
Outfile << count << "; " << ai[solu] << "; " ;
int res = Game.test_ans(ai[solu]);
Outfile << Game.result(res);
std::string resstr;
for(int b = 0; b < ai.getLen(); b++){
switch(res&3){
case 0:
resstr += "0";
break;
case 1:
resstr += "2";
break;
case 2:
resstr += "1";
break;
}
res >>= 2;
}
if(ai.response(resstr)){
Outfile << count << std::endl;
break;
}
count++;
}
}
Outfile.close();
}
int main(int argc, char* argv[]){
switch(MainMode){
case 0:
if(argc != 4){
std::cerr << "Usage: " << argv[0] << " <Wordlist> <Answer_list> <Output_file>" << std::endl;
}
else{
std::string QFN(argv[1]);
std::string Questions(argv[2]);
std::string Outfile(argv[3]);
HWMain(QFN, Questions, Outfile);
}
break;
case 1:
if(argc != 2){
std::cerr << "Usage: " << argv[0] << " <Wordlist> <Answer_list> <Output_file>" << std::endl;
}
else{
std::string QFN(argv[1]);
test(QFN);
}
break;
default:
break;
}
return 0;
}