-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroup.cpp
More file actions
153 lines (138 loc) · 4.05 KB
/
Group.cpp
File metadata and controls
153 lines (138 loc) · 4.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
#include "Group.h"
void Group::swapTeams(int index1, int index2)
{
Team* tempTeam = new Team[numTeams];
assert(tempTeam);
for (int i = 0; i < numTeams; i++)
{
tempTeam[i] = teamsInGroup[i];
}
delete[] teamsInGroup;
teamsInGroup = new Team[numTeams];
assert(teamsInGroup);
for (int i = 0; i < numTeams; i++)
{
if (i == index1) teamsInGroup[index1] = tempTeam[index2];
else if (i == index2) teamsInGroup[index2] = tempTeam[index1];
else teamsInGroup[i] = tempTeam[i];
}
delete[] tempTeam;
}
void Group::playMatch()
{
int teamPoints[4] = { 0, 0, 0, 0 }; // stores information about the points each team within the group has
int pointsDifference[4] = { 0, 0, 0, 0 }; // stores information about the differencePoints each team within the group has
for (int i = 0; i < numTeams - 1; i++)
{
for (int j = i + 1; j < numTeams; j++)
{
if (numTeams == 3)
{
int gameTeam[3][3][2]; // first array stores teams, second array stores the rival team and the third array stores the rounds
std::cout << teamsInGroup[i].getName() << " vs. " << teamsInGroup[j].getName();
std::cout << "First game: "; std::cin >> gameTeam[i][j][0]; std::cout << " - "; std::cin >> gameTeam[j][i][0];
teamPoints[i] += gameTeam[i][j][0]; teamPoints[j] += gameTeam[j][i][0];
pointsDifference[i] += teamPoints[i] - teamPoints[j];
pointsDifference[j] += teamPoints[j] - teamPoints[i];
totalDifference = pointsDifference[i] + pointsDifference[j];
std::cout << "Second game: "; std::cin >> gameTeam[i][j][1]; std::cout << " - "; std::cin >> gameTeam[j][i][1];
teamPoints[i] += gameTeam[i][j][1]; teamPoints[j] += gameTeam[j][i][1];
pointsDifference[i] += teamPoints[i] - teamPoints[j];
pointsDifference[j] += teamPoints[j] - teamPoints[i];
totalDifference = pointsDifference[i] + pointsDifference[j];
}
else
{
int gameTeam[4][4]; // similirat to the above, first array stores teams and second - rival teams
std::cout << teamsInGroup[i].getName() << " vs. " << teamsInGroup[j].getName();
std::cout << "First game: "; std::cin >> gameTeam[i][j]; std::cout << " - "; std::cin >> gameTeam[j][i];
teamPoints[i] += gameTeam[i][j]; teamPoints[j] += gameTeam[j][i];
pointsDifference[i] += teamPoints[i] - teamPoints[j];
pointsDifference[j] += teamPoints[j] - teamPoints[i];
totalDifference = pointsDifference[i] + pointsDifference[j];
}
}
}
for (int i = 0; i < numTeams; i++)
{
teamsInGroup[i].setDifferencePoints(pointsDifference[i]);
}
for (int i = 0; i < numTeams - 1; i++)
{
int min = i;
for (int j = i + 1; j < numTeams; j++)
{
if (teamsInGroup[min].getDifferencePoints() < teamsInGroup[j].getDifferencePoints())
{
min = j;
swapTeams(i, min);
}
}
}
}
void Group::copyGroup(const Group& group)
{
numTeams = group.numTeams;
teamsInGroup = new Team[numTeams];
assert(teamsInGroup);
for (int i = 0; i < numTeams; i++)
{
teamsInGroup[i] = group.teamsInGroup[i];
}
totalDifference = group.totalDifference;
}
Group::Group(const Team* arrTeams, int num)
{
numTeams = num;
teamsInGroup = new Team[numTeams];
assert(teamsInGroup);
for (int i = 0; i < numTeams; i++)
{
teamsInGroup[i].setName(arrTeams[i].getName());
teamsInGroup[i].setPoints(arrTeams[i].getPoints());
}
totalDifference = 0;
}
Group::Group(const Group& group)
{
copyGroup(group);
}
Group& Group::operator=(const Group& group)
{
if (this != &group)
{
delete[] teamsInGroup;
copyGroup(group);
}
return *this;
}
Group::~Group()
{
delete[] teamsInGroup;
}
void Group::setTeams(const Team* arrTeams, int num)
{
if (teamsInGroup != NULL)
{
delete[] teamsInGroup;
}
numTeams = num;
teamsInGroup = new Team[numTeams];
assert(teamsInGroup);
for (int i = 0; i < numTeams; i++)
{
teamsInGroup[i] = arrTeams[i];
}
}
Team* Group::getTeams() const
{
return teamsInGroup;
}
int Group::getDifference() const
{
return totalDifference;
}
int Group::getNumTeams() const
{
return numTeams;
}