-
Notifications
You must be signed in to change notification settings - Fork 3
/
blotto_ml.cpp
212 lines (173 loc) · 5.05 KB
/
blotto_ml.cpp
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
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int roundRobin(string input, string output);
int main(int argc, char *argv[]) {
if (argc < 3) {
cout << "Must include dataset and output file\n";
return -1;
}
string input = argv[1];
string output = argv[2];
ifstream file(input);
if (file.fail()) {
cout << "Unable to read file";
return -1;
}
file.close();
if (argc == 4) {
int rounds = atoi(argv[3]);
cout << "Only running " << rounds << " round(s)\n";
for (int i = 0; i < rounds; i++) {
int ret = roundRobin(input, output);
input = output;
output[0]--;
if (rounds % 2)
output[0] += 2;
if (ret == 1) {
output[0]++;
if (rounds % 2)
output[0] -= 2;
cout << "Finished after " << rounds << " round(s)\n";
return 0;
}
if (ret == -1) {
output[0]++;
if (rounds % 2)
output[0] -= 2;
cout << "Unable to write to " << output << endl;
return -1;
}
if (ret == 0) {
cout << "Uhh not entirely sure what happened here? Try fewer "
"rounds\n";
return 1;
}
}
return 0;
}
bool keepOn = true;
int rounds = 0;
while (keepOn) {
rounds++;
int ret = roundRobin(input, output);
input = output;
output[0]--;
if (rounds % 2)
output[0] += 2;
if (ret == 1) {
keepOn = false;
output[0]++;
if (rounds % 2)
output[0] -= 2;
}
if (ret == -1) {
keepOn = false;
output[0]++;
if (rounds % 2)
output[0] -= 2;
cout << "Unable to write to " << output << endl;
return -1;
}
if (ret == 0) {
keepOn = false;
cout << "Too many iterations, try starting from the second to last "
"file and run with 1 operation.\n";
return 0;
}
}
cout << "Successfully wrote to " << output << endl;
return 0;
}
int roundRobin(string input, string output) {
ifstream player1File(input);
ofstream oFile(output);
if (player1File.fail() || oFile.fail()) {
cout << "Invalid files\n";
return -1;
}
int numOfWins = 0;
int count;
player1File >> count;
oFile << count << endl;
player1File.ignore();
for (int num1 = 0; num1 < count; num1++) {
ifstream file(input);
file >> count;
file.ignore();
int WL = 0;
string player1S;
getline(player1File, player1S);
stringstream player1SS(player1S);
int player1[10] = {0};
int player1T = 0;
for (int i = 0; i < 10; i++) {
player1SS >> player1[i];
player1T += player1[i];
}
if (player1T != 100) {
cout << "Player 1 does not add up to 100\n";
return -1;
}
for (int num = 0; num < count; num++) {
int player2[10] = {0};
int player1Score = 0;
int player2Score = 0;
int player2T = 0;
string player2S;
getline(file, player2S);
stringstream player2SS(player2S);
for (int i = 0; i < 10; i++) {
player2SS >> player2[i];
player2T += player2[i];
}
if (player2T != 100) {
cout << "Must place down 100 soldiers for P2, invalid file\n";
return -1;
}
for (int i = 0; i < 10; i++) {
if (player2[i] > player1[i]) {
player2Score += (i + 1);
} else if (player1[i] > player2[i]) {
player1Score += (i + 1);
}
if (player2Score >= 20) {
WL--;
break;
}
if (player1Score >= 20) {
WL++;
break;
}
}
}
file.close();
if (WL > 0) {
// that is, print out the line IFF it wins more than it loses
// against the test set (all posibilities)
for (int i = 0; i < 10; i++) {
oFile << player1[i] << " ";
}
oFile << 100 << endl;
numOfWins++;
}
}
player1File.close();
oFile.close();
ifstream newFile(output);
string *arr = new string[numOfWins + 1];
for (int i = 0; i <= numOfWins; i++) {
getline(newFile, arr[i]);
}
arr[0] = to_string(numOfWins);
newFile.close();
ofstream overwrite(output);
for (int i = 0; i <= numOfWins; i++) {
overwrite << arr[i] << endl;
}
overwrite.close();
delete[] arr;
return numOfWins;
}