-
Notifications
You must be signed in to change notification settings - Fork 0
/
match.cpp
455 lines (411 loc) · 9.27 KB
/
match.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#include "match.h"
match::match(team home, team away)
{
this->home_goals = 0;
this->away_goals = 0;
this->home_corners = 0;
this->away_corners = 0;
this->home_free_kicks = 0;
this->away_free_kicks = 0;
this->home_penalties = 0;
this->away_penalties = 0;
this->home_fouls = 0;
this->away_fouls = 0;
this->home_possesion = 0;
this->away_possesion = 0;
this->home_shots = 0;
this->away_shots = 0;
this->home_passes = 0;
this->away_passes = 0;
this->home_passes_completed = 0;
this->away_passes_completed = 0;
this->game_time = 0;
this->in_possesion = HOME;
this->home = home;
this->away = away;
this->position = MID;
this->acting = home.players[9];
this->last_actor = home.players[9];
this->last_event = NONE;
this->current_event = PASS;
//srand(time(NULL));
this->next_event = match_events::new_event(current_event, rand() % 100);
}
teams match::dispossess(teams in_possesion)
{
if (in_possesion == HOME)
{
return AWAY;
}
else
{
return HOME;
}
}
player match::player_with_ball(positions position , team in_possession)
{
//srand(time(NULL));
int probability = rand() % 100;
switch (position)
{
case GK:
return in_possession.players[1];
case DEF:
//srand(time(NULL));
if (probability < 80)
{
int player_index = (rand() % 4) + 2;
return in_possession.players[player_index];
}
else if (probability < 90)
{
int player_index = (rand() % 3) + 6;
return in_possession.players[player_index];
}
else
{
int player_index = (rand() % 3) + 9;
return in_possession.players[player_index];
}
case MID:
//srand(time(NULL));
if (probability < 25)
{
int player_index = (rand() % 4) + 2;
return in_possession.players[player_index];
}
else if (probability < 50)
{
int player_index = (rand() % 3) + 6;
return in_possession.players[player_index];
}
else
{
int player_index = (rand() % 3) + 9;
return in_possession.players[player_index];
}
case FWD:
//srand(time(NULL));
if (probability < 15)
{
int player_index = (rand() % 4) + 2;
return in_possession.players[player_index];
}
else if (probability < 35)
{
int player_index = (rand() % 3) + 6;
return in_possession.players[player_index];
}
else
{
int player_index = (rand() % 3) + 9;
return in_possession.players[player_index];
}
}
}
positions match::play_position(events current_event , events next_event)
{
switch (current_event)
{
case SAVE:
return GK;
case INTERCEPT:
return DEF;
case BLOCK:
return DEF;
case TACKLE:
return DEF;
case PASS:
return MID;
case CROSS :
return MID;
case SKILL:
if (next_event == SHOOT)
{
return FWD;
}
else
{
return MID;
}
case SHOOT :
return FWD;
case CORNER:
return MID;
case FREE_KICK:
if (next_event == SHOOT)
{
return FWD;
}
else
{
return MID;
}
case PENALTY:
return FWD;
}
}
void match::show_squads()
{
std::cout <<"\n"<< home.display_name() + "\t\t\t\t\t" + away.display_name() + "\n";
std::cout << "coach \t\t\t\t\t\t\t coach \n";
std::cout << home.players[0].display_name() + "\t\t\t\t\t\t\t\t\t" + away.players[0].display_name() + "\n";
std::cout << "players \t\t\t\t\t\t\t\t\t players\n";
for (int i = 1; i < 12; i++)
{
std::cout <<i<<". " + home.players[i].display_name() + "\t\t\t\t\t\t" << i <<". " + away.players[i].display_name() + "\n\n";
}
}
void match::stats()
{
std::cout <<"\n\t\t\t" + home.display_name() + "\t\t\t\t" + away.display_name() + "\n";
std::cout << "goals " <<"\t\t\t" <<home_goals << "\t\t\t\t\t\t" << away_goals << "\n";
std::cout << "possession " <<"\t\t"<< home_possesion << "\t\t\t\t\t\t" << away_possesion << "\n";
//should probably be a variable
//integer division by zero also occurs
std::cout << "possession(%) " <<"\t\t"<< (home_possesion * 100 / (home_possesion + away_possesion)) << "\t\t\t\t\t\t" << (away_possesion * 100 / (home_possesion + away_possesion)) << "\n";
std::cout << "shots " <<"\t\t\t"<< home_shots << "\t\t\t\t\t\t" << away_shots << "\n";
std::cout << "fouls " <<"\t\t\t"<< home_fouls << "\t\t\t\t\t\t" << away_fouls << "\n";
std::cout << "free kicks " <<"\t\t"<< home_free_kicks << "\t\t\t\t\t\t" << away_free_kicks << "\n";
std::cout << "penalties " <<"\t\t"<< home_penalties << "\t\t\t\t\t\t" << away_penalties << "\n";
std::cout << "passes " <<"\t\t\t"<< home_passes << "\t\t\t\t\t\t" << away_passes << "\n";
std::cout << "passes completed " <<"\t"<< home_passes_completed << "\t\t\t\t\t\t" << away_passes_completed << "\n";
std::cout << "corners " <<"\t\t"<< home_corners << "\t\t\t\t\t\t" << away_corners << "\n";
}
void match::play()
{
//srand(time(NULL));
while (game_time <= FULL_TIME * 5 )
{
srand(rand() * time(0) * time(0));
if (game_time == HALF_TIME * 5)
{
current_event = PASS;
in_possesion = AWAY;
acting = away.players[9];
std::cout << "\n HALF TIME \n\n";
}
comment = commentary::text(last_event, current_event, next_event, game_time, last_actor, acting);
if (comment != "") {
std::cout << "'" << (game_time / 5) << " " << comment;
};
switch (current_event)
{
case NONE:
//should rethink , this not wise
break;
case TACKLE:
position = play_position(current_event, next_event);
if (in_possesion == HOME)
{
acting = player_with_ball(position, away);
}
else
{
acting = player_with_ball(position, home);
}
dispossess(in_possesion);
break;
case INTERCEPT:
last_actor = acting;
position = play_position(current_event, next_event);
if (in_possesion == HOME)
{
acting = player_with_ball(position, away);
home_passes_completed -= 1;
}
else
{
acting = player_with_ball(position, home);
away_passes_completed -= 1;
}
dispossess(in_possesion);
break;
case BLOCK:
last_actor = acting;
position = play_position(current_event, next_event);
if (in_possesion == HOME)
{
acting = player_with_ball(position, away);
}
else
{
acting = player_with_ball(position, home);
}
dispossess(in_possesion);
break;
case PASS:
last_actor = acting;
position = play_position(current_event, next_event);
if (in_possesion == HOME)
{
srand(rand() * time(0) * rand() * time(0));
acting = player_with_ball(position, home);
home_passes += 1;
home_passes_completed += 1;
}
else
{
acting = player_with_ball(position, away);
away_passes += 1;
away_passes_completed += 1;
}
break;
case CROSS:
last_actor = acting;
position = play_position(current_event, next_event);
if (in_possesion == HOME)
{
acting = player_with_ball(position, home);
}
else
{
acting = player_with_ball(position, away);
}
break;
case SHOOT:
last_actor = acting;
position = play_position(current_event, next_event);
if (in_possesion == HOME)
{
acting = player_with_ball(position, home);
home_shots += 1;
}
else
{
acting = player_with_ball(position, away);
away_shots += 1;
}
break;
case SAVE:
last_actor = acting;
position = play_position(current_event, next_event);
if (in_possesion == HOME)
{
/*acting = player_with_ball(position, away);*/
acting = away.players[1];
}
else
{
acting = player_with_ball(position, home);
}
dispossess(in_possesion);
break;
case SKILL:
last_actor = acting;
position = play_position(current_event, next_event);
if (in_possesion == HOME)
{
acting = player_with_ball(position, home);
}
else
{
acting = player_with_ball(position, away);
}
break;
case FOUL:
//modify tackle so the team on the ball doesn't switch if it results in a foul
//-*use the next_event
dispossess(in_possesion);
if (in_possesion == HOME)
{
home_fouls += 1;
}
else
{
away_fouls += 1;
}
break;
case YELLOW_CARD:
if (in_possesion == HOME)
{
}
else
{
}
break;
case RED_CARD:
if (in_possesion == HOME)
{
}
else
{
}
break;
case PENALTY:
last_actor = acting;
if (in_possesion == HOME)
{
//acting = player_with_ball(position, home);
home_penalties += 1;
}
else
{
//acting = player_with_ball(position, away);
away_penalties += 1;
}
break;
case FREE_KICK:
//last_actor = acting;
if (in_possesion == HOME)
{
home_free_kicks += 1;
acting = player_with_ball(position, home);
}
else
{
away_free_kicks += 1;
acting = player_with_ball(position, away);
}
break;
case CORNER:
last_actor = acting;
dispossess(in_possesion);
if (in_possesion == HOME)
{
home_corners += 1;
acting = player_with_ball(position, home);
}
else
{
away_corners += 1;
acting = player_with_ball(position, away);
}
break;
case MISS:
if (in_possesion == HOME)
{
}
else
{
}
dispossess(in_possesion);
break;
case GOAL:
if (in_possesion == HOME)
{
home_goals += 1;
}
else
{
away_goals += 1;
}
dispossess(in_possesion);
break;
}
//last_actor = acting;
last_event = current_event;
current_event = next_event;
next_event = match_events::new_event(current_event, rand() % 100);
position = play_position(current_event, next_event);
if (in_possesion == HOME)
{
home_possesion += 1;
}
else
{
away_possesion += 1;
}
game_time++;
}
}