-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
236 lines (224 loc) · 7.75 KB
/
main.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
#include <iostream>
using namespace std;
#define num 10000
struct Player {
string name;
double price;
double speed;
double finishing;
double defence;
int team_blonging = -1;
}player[num];
struct Team {
string name;
double money;
int wins = 0;
int looses = 0;
int draws = 0;
int num_player = 0;
int players[1000];
}team[num];
void remove_and_shift_array(int players[], int i, int size_array)
{
for (int j = i; j < size_array-1; j++) {
players[j] = players[j+1];
}
players[size_array-1] = NULL;
}
void swap(int array[], int i, int j)
{
int temp = *(array+i);
*(array+i) = *(array+j);
*(array+j) = temp;
}
void swap_str(string array[], int i, int j)
{
string temp = array[i];
array[i] = array[j];
array[j] = temp;
}
void bubblesort(int wins[], int size_team, int looses[], string name[] )
{
for (int i = 0; i < size_team; i++) {
for (int j = i+1; j < size_team; j++) {
if (*(wins+i) < *(wins+j)) {
swap(wins, i, j);
swap(looses, i, j);
swap_str(name, i, j);
}
else if (*(wins+i) == *(wins+j)) {
if (*(looses+i) > *(looses+j)){
swap(wins, i, j);
swap(looses, i, j);
swap_str(name, i, j);
}
}
}
}
}
int main() {
int playerID = 0;
int teamID = 0;
string key;
while (1) {
cin >> key;
if (key.compare("new") == 0) {
string kind;
cin >> kind;
if (kind.compare("player") == 0) {
cin >> player[playerID].name >> player[playerID].price >> player[playerID].speed >> player[playerID].finishing >> player[playerID].defence;
playerID++;
}
if (kind.compare("team") == 0) {
cin >> team[teamID].name >> team[teamID].money;
teamID++;
}
}
if (key.compare("buy") == 0) {
int player_ID, team_ID;
cin >> player_ID >> team_ID;
if (player_ID > playerID){
cout << "player with the id " << player_ID << " doesnt exist" << endl;
}
else if (team_ID > (teamID)){
cout << "team with the id " << team_ID << " doesnt exist" << endl;
}
else if (team[team_ID-1].money < player[player_ID-1].price) {
cout << "the team cant afford to buy this player" << endl;
}
else if ( player[player_ID-1].team_blonging != -1 ){
cout << "player already has a team" << endl;
}
else{
team[team_ID-1].money -= player[player_ID-1].price;
player[player_ID-1].team_blonging++;
team[team_ID-1].players[team[team_ID-1].num_player] = player_ID;
team[team_ID-1].num_player++;
cout << "player added to the team succesfully"<<endl;
}
}
if (key.compare("sell") == 0) {
int player_ID, team_ID;
cin >> player_ID >> team_ID;
if (team_ID > teamID) {
cout << "team doesnt exist" << endl;
}
else {
int count = 0;
for (int i = 0; i < teamID; i++) {
if( team[team_ID-1].players[i] == player_ID){
cout << "player sold succesfully" << endl;
remove_and_shift_array(team[team_ID-1].players, i, team[team_ID-1].num_player);
player[player_ID-1].team_blonging = -1;
team[team_ID-1].money += player[player_ID-1].price;
team[team_ID-1].num_player--;
count++;
break;
}
}
if (count == 0) {
cout << "team doesnt have this player" << endl;
}
}
}
if (key.compare("match") == 0) {
int team_ID1, team_ID2;
cin >> team_ID1 >> team_ID2;
if ((team_ID1 > teamID ) || (team_ID2 > teamID ) ) {
cout << "team doesnt exist" << endl;
}
else if( (team[team_ID1-1].num_player < 11) || (team[team_ID2-1].num_player < 11) ){
cout << "the game can not be held due to loss of the players" << endl;
}
else{
double power_team_ID1 = 0;
double power_team_ID2 = 0;
for (int i = 0; i < 11; i++) {
power_team_ID1 += player[team[team_ID1-1].players[i]].finishing + player[team[team_ID1-1].players[i]].speed;
power_team_ID2 += player[team[team_ID2-1].players[i]].defence + player[team[team_ID2-1].players[i]].speed;
}
if (power_team_ID1 > power_team_ID2) {
team[team_ID1-1].wins++;
team[team_ID2-1].looses++;
team[team_ID1-1].money += 1000;
}
else if (power_team_ID2 > power_team_ID1) {
team[team_ID2-1].wins++;
team[team_ID1-1].looses++;
team[team_ID2-1].money += 1000;
}
else if(power_team_ID1 == power_team_ID2) {
team[team_ID1-1].draws++;
team[team_ID2-1].draws++;
}
}
}
if (key.compare("show") == 0) {
string kind;
cin >> kind;
if (kind.compare("players") == 0) {
for (int i = 0; i < playerID; i++) {
cout << i+1 << " : " << player[i].name << endl;
}
}
else if (kind.compare("teams") == 0){
for (int i = 0; i < teamID; i++) {
cout << i+1 << " : " << team[i].name << endl;
}
}
else if(kind.compare("player") == 0){
int player_ID;
cin >> player_ID;
if (player_ID > playerID) {
cout << "player doesnt exist" << endl;
}
else{
cout << "player : " << player[player_ID-1].name << endl;
cout << "price : " << player[player_ID-1].price << endl;
cout << "state :" << endl;
cout << "speed : " << player[player_ID-1].speed << endl;
cout << "finishing : " << player[player_ID-1].finishing << endl;
cout << "defence : " << player[player_ID-1].defence << endl;
}
}
else if(kind.compare("team") == 0){
int team_ID;
cin >> team_ID;
if (team_ID > teamID) {
cout << "team doesnt exist" << endl;
}
else{
cout << "team : " << team[team_ID-1].name << endl;
cout << "money : " << team[team_ID-1].money << endl;
cout << "players : ";
for (int i = 0; i < team[team_ID-1].num_player-1 ; i++) {
cout << team[team_ID-1].players[i] << " ";
}
cout << team[team_ID-1].players[team[team_ID-1].num_player-1] << endl;
cout << "state :" << endl;
cout << "win : " << team[team_ID-1].wins << endl;
cout << "draw : " << team[team_ID-1].draws << endl;
cout << "loose : " << team[team_ID-1].looses << endl;
}
}
}
if (key.compare("rank") == 0) {
int team_wins[teamID];
int team_looses[teamID];
string team_name[teamID];
for (int i = 0; i < teamID; i++) {
team_wins[i] = team[i].wins;
team_looses[i] = team[i].looses;
team_name[i] = team[i].name;
}
bubblesort(team_wins, teamID, team_looses, team_name);
for (int i = 0; i < teamID; i++) {
cout << i+1 << ". " << team_name[i] <<endl;
}
}
if (key.compare("end") == 0) {
break;
}
}
return 0;
}