-
Notifications
You must be signed in to change notification settings - Fork 0
/
FinalStage.cpp
110 lines (98 loc) · 2.68 KB
/
FinalStage.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
#include "FinalStage.h"
void FinalStage::copyFinalStage(const FinalStage& finalStage)
{
numTeams = finalStage.numTeams;
teamsInStage = new Team[numTeams];
assert(teamsInStage);
for (int i = 0; i < numTeams; i++)
{
teamsInStage[i] = finalStage.teamsInStage[i];
}
powerOfTwo = finalStage.powerOfTwo;
}
FinalStage::FinalStage(const Team* teams, int num)
{
numTeams = num;
teamsInStage = new Team[numTeams];
assert(teamsInStage);
for (int i = 0; i < numTeams; i++)
{
teamsInStage[i] = teams[i];
}
if (ceil(log2(num)) == floor(log2(num))) powerOfTwo = true;
else powerOfTwo = false;
}
FinalStage::FinalStage(const FinalStage& finalStage)
{
copyFinalStage(finalStage);
}
FinalStage& FinalStage::operator=(const FinalStage& finalStage)
{
if (this != &finalStage)
{
delete[] teamsInStage;
copyFinalStage(finalStage);
}
return *this;
}
FinalStage::~FinalStage()
{
delete[] teamsInStage;
}
bool FinalStage::getPowerOfTwo() const
{
return powerOfTwo;
}
int FinalStage::getNumTeams() const
{
return numTeams;
}
Team* FinalStage::getTeamsInStage() const
{
return teamsInStage;
}
void FinalStage::selectionPowerTwo(const Team* rankedTeams, int numRanked)
{
delete[] teamsInStage;
if (ceil(log2(numRanked)) == floor(log2(numRanked))) numTeams = numRanked;
else numTeams = pow(2, floor(log2(numRanked)));
teamsInStage = new Team[numTeams];
assert(teamsInStage);
for (int i = 0; i < numTeams; i++)
{
teamsInStage[i] = rankedTeams[i];
}
}
void FinalStage::finalStageSort()
{
while (numTeams / 2 != 1)
{
int numberOfMatches = numTeams / 2;
std::cout << "1/" << numberOfMatches << " Stage:" << std::endl;
Team* finalists = new Team[numTeams];
assert(finalists);
for (int i = 0; i < numTeams; i++)
{
finalists[i] = teamsInStage[i];
}
delete[] teamsInStage;
teamsInStage = new Team[numberOfMatches];
assert(teamsInStage);
int* teamsChosen = new int[numberOfMatches];
assert(teamsChosen);
for (int i = 0; i < numTeams; i += 2)
{
std::cout << "Please select which teams will compete against each other: " << std::endl;
std::cin >> teamsChosen[i]; std::cout << " vs. "; std::cin >> teamsChosen[i + 1];
teamsInStage[i / 2 - 1] = finalists[teamsChosen[i]].matchInFinals(finalists[teamsChosen[i + 1]]);
std::cout << teamsInStage[i / 2 - 1].getName() << "has won and will compete in the next round" << std::endl;
}
numTeams /= 2;
delete[] finalists;
delete[] teamsChosen;
finalStageSort();
}
Team Victor;
Victor = teamsInStage[0].matchInFinals(teamsInStage[1]);
std::cout << "Team " << Victor.getName() << "has emerged victorious!" << std::endl;
}