-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinal Project.cpp
More file actions
273 lines (234 loc) · 7.05 KB
/
Final Project.cpp
File metadata and controls
273 lines (234 loc) · 7.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
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
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <string>
#include <iomanip>
#include <limits>
using namespace std;
struct Player {
string name;
int totalPoints = 0;
int gamesPlayed = 0;
double averagePoints() const {
return gamesPlayed ? (double)totalPoints / gamesPlayed : 0.0;
}
};
struct Team {
string name;
vector<Player> players;
int totalPoints = 0;
int gamesPlayed = 0;
int wins = 0, losses = 0;
void addPlayer(const string& playerName) {
players.push_back({ playerName });
}
void updatePlayerScore(const string& playerName, int points) {
for (auto& p : players) {
if (p.name == playerName) {
p.totalPoints += points;
p.gamesPlayed++;
return;
}
}
cout << "Player not found: " << playerName << endl;
}
double averagePoints() const {
return gamesPlayed ? (double)totalPoints / gamesPlayed : 0.0;
}
void printStats() const {
cout << "Team: " << name << " | Wins: " << wins << " | Losses: " << losses
<< " | Avg Points: " << averagePoints() << "\n";
for (const auto& p : players) {
cout << " Player: " << p.name << " | Avg Points: " << p.averagePoints() << "\n";
}
}
};
class League {
string sport;
vector<Team> teams;
public:
League(const string& sportName = "") : sport(sportName) {}
void addTeam(const Team& t) {
teams.push_back(t);
}
int getTeamCount() const {
return teams.size();
}
Team& getTeam(int index) {
return teams[index];
}
void listTeams() const {
for (int i = 0; i < teams.size(); ++i) {
cout << i << ": " << teams[i].name << "\n";
}
}
void playGame() {
if (teams.size() < 2) {
cout << "Not enough teams.\n";
return;
}
listTeams();
int t1, t2;
cout << "Enter index of Team 1: "; cin >> t1;
cout << "Enter index of Team 2: "; cin >> t2;
if (t1 < 0 || t2 < 0 || t1 >= teams.size() || t2 >= teams.size() || t1 == t2) {
cout << "Invalid team indices.\n";
return;
}
int t1Score = 0, t2Score = 0;
cout << "Enter total score for " << teams[t1].name << ": ";
cin >> t1Score;
cout << "Enter total score for " << teams[t2].name << ": ";
cin >> t2Score;
Team& team1 = teams[t1];
Team& team2 = teams[t2];
team1.totalPoints += t1Score;
team2.totalPoints += t2Score;
team1.gamesPlayed++;
team2.gamesPlayed++;
if (t1Score > t2Score) {
team1.wins++;
team2.losses++;
}
else {
team2.wins++;
team1.losses++;
}
// Individual scoring
for (Team* team : { &team1, &team2 }) {
cout << "Enter player scores for " << team->name << ":\n";
for (auto& p : team->players) {
int pts;
cout << " " << p.name << ": ";
cin >> pts;
p.totalPoints += pts;
p.gamesPlayed++;
}
}
cout << "Game recorded.\n";
}
void saveLeague(const string& filename) const {
ofstream out(filename);
if (!out) {
cout << "Error saving to file.\n";
return;
}
out << sport << "\n" << teams.size() << "\n";
for (const auto& team : teams) {
out << team.name << "\n" << team.wins << " " << team.losses << " " << team.gamesPlayed << " " << team.totalPoints << "\n";
out << team.players.size() << "\n";
for (const auto& player : team.players) {
out << player.name << "\n" << player.totalPoints << " " << player.gamesPlayed << "\n";
}
}
out.close();
}
void loadLeague(const string& filename) {
ifstream in(filename);
if (!in) {
cout << "Error loading file.\n";
return;
}
getline(in, sport);
int teamCount;
in >> teamCount;
in.ignore();
teams.clear();
for (int i = 0; i < teamCount; ++i) {
Team team;
getline(in, team.name);
in >> team.wins >> team.losses >> team.gamesPlayed >> team.totalPoints;
int playerCount;
in >> playerCount;
in.ignore();
for (int j = 0; j < playerCount; ++j) {
Player p;
getline(in, p.name);
in >> p.totalPoints >> p.gamesPlayed;
in.ignore();
team.players.push_back(p);
}
teams.push_back(team);
}
cout << "League loaded successfully.\n";
}
void printLeagueStats() const {
cout << "\n=== League: " << sport << " ===\n";
for (const auto& t : teams) {
t.printStats();
}
}
};
int main() {
League league;
string choice;
while (true) {
cout << "\nMenu:\n"
<< "1. Create League\n"
<< "2. Load League from File\n"
<< "3. Add Team\n"
<< "4. Add Player to Team\n"
<< "5. Play Game\n"
<< "6. Show League Stats\n"
<< "7. Save League\n"
<< "8. Exit\n"
<< "Choice: ";
cin >> choice;
if (choice == "1") {
string sport;
cout << "Enter sport name: ";
cin.ignore();
getline(cin, sport);
league = League(sport);
}
else if (choice == "2") {
string filename;
cout << "Enter filename: ";
cin >> filename;
league.loadLeague(filename);
}
else if (choice == "3") {
string teamName;
cout << "Enter team name: ";
cin.ignore();
getline(cin, teamName);
league.addTeam({ teamName });
}
else if (choice == "4") {
league.listTeams();
int teamIndex;
cout << "Select team index: ";
cin >> teamIndex;
string playerName;
cout << "Enter player name: ";
cin.ignore(numeric_limits<streamsize>::max(), '\n');
getline(cin, playerName);
if (teamIndex >= 0 && teamIndex < league.getTeamCount()) {
league.getTeam(teamIndex).addPlayer(playerName);
}
else {
cout << "Invalid team index.\n";
}
}
else if (choice == "5") {
league.playGame();
}
else if (choice == "6") {
league.printLeagueStats();
}
else if (choice == "7") {
string filename;
cout << "Enter filename to save: ";
cin >> filename;
league.saveLeague(filename);
}
else if (choice == "8") {
break;
}
else {
cout << "Invalid choice.\n";
}
}
return 0;
}