Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

CSC112 Spring 2016 Examples

2369 views
1
#include <unistd.h>
2
#include <iostream>
3
#include <time.h>
4
#include <cstdlib>
5
#include <vector>
6
#include <sstream>
7
#include "swiss.h"
8
9
using namespace std;
10
11
class ContestInt : public SwissTournament<int>::Contestant {
12
public:
13
ContestInt() {
14
value = rand();
15
}
16
17
virtual std::string name() {
18
ostringstream os;
19
os << value;
20
return os.str();
21
}
22
23
24
virtual int *player() {
25
return &value;
26
}
27
28
int value;
29
};
30
31
32
int main()
33
{
34
SwissTournament<int> *tournament;
35
vector<SwissTournament<int>::Contestant *> players;
36
pair<SwissTournament<int>::Contestant*, SwissTournament<int>::Contestant*> p;
37
int n;
38
39
cout << "How many? ";
40
cin >>n;
41
for(int i=0; i<n; i++) {
42
players.push_back(new ContestInt);
43
}
44
45
srand(time(0));
46
47
tournament = new SwissTournament<int>(players);
48
49
while(!tournament->tournamentDone()) {
50
tournament->printPairings();
51
while(!tournament->roundDone()) {
52
p = tournament->getMatch();
53
ContestInt *c1 = (ContestInt*)p.first;
54
ContestInt *c2 = (ContestInt*)p.second;
55
if(c2 == nullptr or c1->value > c2->value)
56
tournament->report(c1);
57
else
58
tournament->report(c2);
59
}
60
tournament->printLeader();
61
tournament->nextRound();
62
sleep(5);
63
}
64
65
return 0;
66
}
67