forked from ChicoState/SportStats
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
74 lines (67 loc) · 2.05 KB
/
Game.cpp
File metadata and controls
74 lines (67 loc) · 2.05 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
#include "Game.h"
#include "Player.h"
#include <string>
#include <vector>
#include <fstream>
//private helper to open a file and load the roster as Name Number format
std::vector <Player> Game::get_roster_from_file(std::string file){
const char * f_cstr = file.c_str();
std::ifstream read;
std::vector <Player> list;
read.open(f_cstr, std::ios::in );
if( read.is_open() ){
std::string name;
int number;
while(read>>name>>number){
Player current(name,number);
list.push_back(current);
}
}
read.close();
return list;
}
// Initiates rosters from the file names provided for team 1 and team 2 and
// begins the game in period 1
Game::Game(std::string team1, std::string team2){
roster[0] = get_roster_from_file(team1);
roster[1] = get_roster_from_file(team2);
current_period = 0;
if( roster[0].empty() || roster[1].empty() )
throw "Team files NOT successfully loaded.\n";
}
// Matches the player by a string match of their name (non-greedy)
Player Game::get_player_by_name(std::string find){
for(int team=0; team < TEAMS; team++){
for(int i=0; i<roster[team].size(); i++){
if( find == roster[team][i].get_name() )
return roster[team][i];
}
}
return DEFAULT_PLAYER;
}
// Records a player's goal in the current period for the team containing
// the player name provided (or does nothing if no matching names)
void Game::add_goal(std::string player_name){
for(int team=0; team < TEAMS; team++){
for(int i=0; i<roster[team].size(); i++){
if( player_name == roster[team][i].get_name() ){
roster[team][i].tally_goal();
goal_t new_goal;
new_goal.scorer=roster[team][i];
new_goal.period = current_period;
new_goal.team = team+1;
goals.push_back(new_goal);
}
}
}
notifyObservers();
}
// Progresses the current period to the subsequent period #, with no limit
// for the number of periods
void Game::next_period(){
current_period++;
}
// Retrieves the list of goal data in chronological order
std::vector <goal_t> Game::get_goals(){
return goals;
}