-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
334 lines (253 loc) · 7.06 KB
/
Game.cpp
File metadata and controls
334 lines (253 loc) · 7.06 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include <string>
#include <iostream>
#include <deque>
#include <unordered_map>
using namespace std;
class Game {
deque<string> buffer;
class keyword {
string key;
public:
string getKey() {
return key;
}
keyword(string in) :
key(in) {
}
keyword() {
}
bool operator()(string in) {
for (int i = 0; i < in.length(); ++i) {
if (!(in[i] >= 'A' && in[i] <= 'Z'
|| in[i] >= 'a' && in[i] <= 'z')) {
in = in.substr(0, i) + in.substr(i + 1, in.length());
--i;
}
}
return in == key;
}
};
class is_player {
public:
bool operator ()(string s, char a, char b) {
return s[0] >= a && s[0] <= b;
}
};
template<typename Pred>
class comparator {
char upper, lower;
Pred function;
public:
comparator(char upper_in, char lower_in) :
upper(upper_in), lower(lower_in) {
}
//comparator (string target){string target};
bool operator ()(string in) {
return function(in, upper, lower);
}
};
deque<keyword> keywords_array;
public:
int outs;
int score[2];
string teams[2];
string bases[3];
string bases_duplicate[3];
//string batter;
bool is_name;
int team;
bool new_batter;
unordered_map<string, int> equivalences;
comparator<is_player> is_player_comp = comparator<is_player>('A', 'Z');
Game(string teams_in[2]) :
is_name(false), outs(0), team(0) {
for (int i = 0; i < 2; ++i) {
teams[i] = *(teams_in + i);
score[i] = 0;
}
const static double BASE_OUT_SCORE[8][3] = { { 0.461, 0.243, 0.095 }, {
0.831, 0.489, 0.214 }, { 1.068, 0.644, 0.305 }, { 1.373, 0.908,
0.343 }, { 1.426, 0.865, 0.413 }, { 1.798, 1.140, 0.471 }, {
1.920, 1.352, 0.570 }, { 2.282, 1.520, 0.73 } };
keyword out("out");
keywords_array.push_back(out);
//keyword swinging ("out"); keywords_array.push_back(swinging);
keyword popped("popped");
keywords_array.push_back(popped);
keyword double_play("double");
keywords_array.push_back(double_play);
keyword caught("caught");
keywords_array.push_back(caught);
keyword advanced("advanced");
keywords_array.push_back(advanced);
keyword stole("stole");
keywords_array.push_back(stole);
keyword pinch_ran("ran");
keywords_array.push_back(pinch_ran);
///////////////////////////////////////////////////////////////////
//keywords array below must also be in equivalences array
keyword walked("walked");
keywords_array.push_back(walked);
keyword hbp("pitch");
keywords_array.push_back(hbp);
keyword reached("reached");
keywords_array.push_back(reached);
keyword singled("singled");
keywords_array.push_back(singled);
keyword doubled("doubled");
keywords_array.push_back(doubled);
keyword tripled("tripled");
keywords_array.push_back(tripled);
keyword homered("homered");
keywords_array.push_back(homered);
keyword scored("scored");
keywords_array.push_back(scored);
keyword second("second");
keywords_array.push_back(second);
keyword third("third");
keywords_array.push_back(third);
equivalences.insert(pair<string, int>("singled", 0));
equivalences.insert(pair<string, int>("hit", 0));
equivalences.insert(pair<string, int>("doubled", 1));
equivalences.insert(pair<string, int>("tripled", 2));
equivalences.insert(pair<string, int>("walked", 0));
equivalences.insert(pair<string, int>("reached", 0));
equivalences.insert(pair<string, int>("second", 1));
equivalences.insert(pair<string, int>("third", 2));
equivalences.insert(pair<string, int>("homered", 3));
equivalences.insert(pair<string, int>("scored", 3));
}
string look_player(int start, int direction) {
for (int i = start;; i = i + direction) {
if (is_player_comp(buffer[i])) {
return buffer[i];
}
}
}
bool is_legit_out(int start, int direction) {
for (int i = start + 1;; i = i + direction) {
if (keywords_array[0](buffer[i])) {
return false;
}
if (is_player_comp(buffer[i])) {
return true;
}
}
}
string get_player(int start) {
string player = look_player(start, 1);
if (is_legit_out(start, 1) == false) {
outs--;
}
for (int j = 0; j < 3; ++j) {
if (bases[j] == player) {
break;
}
if (j == 2)
new_batter = true;
}
return player;
return "";
}
int get_bases(int start) {
for (int i = start;; --i) {
for (int j = keywords_array.size() - equivalences.size();
j < keywords_array.size(); ++j) {
if (keywords_array[j](buffer[i])) {
return equivalences[keywords_array[j].getKey()];
} else if (is_player_comp(buffer[i])) {
for (int k = 0; k < 3; ++k) {
if (bases[k] == buffer[i])
return k;
}
}
}
}
return 0;
}
void clean_bases(string player) {
for (int i = 0; i < 3; ++i) {
if (bases_duplicate[i] == player)
bases_duplicate[i] = "";
}
}
void assign_bases(int base, string player) {
if (base <= 2)
bases_duplicate[base] = player;
else {
score[team]++;
clean_bases(player);
}
}
void update_bases() {
for (int i = 0; i < 3; ++i) {
bases[i] = bases_duplicate[i];
}
for (int i = 0; i < 3; ++i) {
for (int j = i + 1; j < 3; ++j) {
if (bases[i] == bases[j])
bases[i] = "";
}
}
}
void reset() {
for (int i = 0; i < 3; ++i) {
bases[i] = "";
bases_duplicate[i] = "";
}
buffer.clear();
team = (team + 1) % 2;
}
void calc_situation() {
for (int i = buffer.size() - 1; i >= 0; --i) {
for (int j = 0; j < keywords_array.size(); ++j) {
if (keywords_array[j](buffer[i]) == true && j >= 0 && j <= 3) {
clean_bases(get_player(i));
outs++;
break;
} else if (keywords_array[j](buffer[i]) == true && j >= 4
&& j <= 14) {
assign_bases(get_bases(i), get_player(i));
break;
}
}
if (buffer[i][buffer[i].length() - 1] == '.' && new_batter) {
update_bases();
for (int i = 0; i < 3; ++i) {
cout << bases[i] << endl;
}
cout << outs << endl;
cout << score[0] << endl;
cout << score[1] << endl;
cout << teams[team] << endl;
cout << "============" << endl;
new_batter = false;
}
}
}
void calc_base_out() {
}
void process_string(string s) {
if (is_name) {
is_name = false;
buffer[0] = buffer[0] + " " + s.substr(0, s.length() - 1);
} else {
for (int i = 0; i < s.length(); ++i) {
if (s[i] == ',' && is_player_comp(s)) {
is_name = true;
break;
}
}
buffer.push_front(s);
}
for (int i = 0; i < s.length(); ++i) {
s[i] = tolower(s[i]);
}
//find
if (s.find("run") != string::npos) {
calc_situation();
reset();
}
// return s;
}
};