Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

CSC112 Spring 2016 Examples

2369 views
1
// The alpaca arena!
2
// It goes without saying, you are not allowed to modify anything in this file.
3
#include <vector>
4
#include <iostream>
5
#include <iomanip>
6
#include <cstdlib>
7
#include <time.h>
8
#include <functional>
9
#include <unistd.h>
10
#include "randPack.h"
11
#include "fracas.h"
12
#include "alpaca.h"
13
#include "termmanip.h"
14
#include "swiss.h"
15
#include "cwillis/fracasPack.h"
16
#include "ischomer/ischomer.h"
17
#include "jmetcalf/superPack.h"
18
#include "mcharles/fracasPack.h"
19
#include "mtaylor/FracasPack.h"
20
21
22
using namespace std;
23
24
class SwissFracasPack : public SwissTournament<AlpacaFracasPack>::Contestant
25
{
26
public:
27
SwissFracasPack(function<AlpacaFracasPack*()> genFunc)
28
{
29
//create the first pack
30
this->genFunc = genFunc;
31
pack = genFunc();
32
nameStr = pack->name();
33
}
34
35
36
virtual string name() {
37
return nameStr;
38
}
39
40
41
virtual AlpacaFracasPack * player()
42
{
43
if(pack) {
44
AlpacaFracasPack *res;
45
res = pack;
46
pack = nullptr;
47
return res;
48
}
49
50
return genFunc();
51
}
52
53
private:
54
function<AlpacaFracasPack*()> genFunc;
55
AlpacaFracasPack *pack;
56
string nameStr;
57
};
58
59
60
int main(int argc, char** argv) {
61
//Make our two packs
62
[] ()->AlpacaFracasPack* {return new randPack::FracasPack();};
63
vector<SwissTournament<AlpacaFracasPack>::Contestant *> players;
64
pair<SwissTournament<AlpacaFracasPack>::Contestant*, SwissTournament<AlpacaFracasPack>::Contestant*> match;
65
SwissTournament<AlpacaFracasPack> *tournament;
66
AlpacaFracasPack *p1, *p2;
67
68
srand(time(0));
69
70
//init players
71
players.push_back(new SwissFracasPack([] ()->AlpacaFracasPack* {return new cwillis::FracasPack();}));
72
players.push_back(new SwissFracasPack([] ()->AlpacaFracasPack* {return new ischomer::FracasPack();}));
73
players.push_back(new SwissFracasPack([] ()->AlpacaFracasPack* {return new jmetcalf::FracasPack();}));
74
players.push_back(new SwissFracasPack([] ()->AlpacaFracasPack* {return new mcharles::FracasPack();}));
75
players.push_back(new SwissFracasPack([] ()->AlpacaFracasPack* {return new mtaylor::FracasPack();}));
76
77
78
79
80
//create the tournament
81
tournament = new SwissTournament<AlpacaFracasPack>(players);
82
while(!tournament->tournamentDone())
83
{
84
cout << clearScreen << cursorOff << cursorPosition(1,1);
85
cout.flush();
86
tournament->printLeader();
87
cout << endl << endl;
88
tournament->printPairings();
89
sleep(5);
90
while(!tournament->roundDone()) {
91
//get the match and the players
92
match = tournament->getMatch();
93
p1 = match.first->player();
94
if(match.second==nullptr) {
95
//bye
96
tournament->report(match.first);
97
continue;
98
}
99
p2 = match.second->player();
100
101
cout << clearScreen << cursorPosition(10, 20)
102
<< match.first->name() << " vs " << match.second->name();
103
cout.flush();
104
sleep(1);
105
106
//Create the fracas
107
Fracas *f = new Fracas(p1, p2);
108
cout << clearScreen << cursorOff << cursorPosition(1,1);
109
cout.flush();
110
111
//do the fracas!
112
if(f->go() == p1) {
113
sleep(5);
114
tournament->report(match.first);
115
cout << clearScreen << cursorPosition(10, 20)
116
<< match.first->name() << " wins!";
117
} else {
118
sleep(5);
119
tournament->report(match.second);
120
cout << clearScreen << cursorPosition(10, 20)
121
<< match.second->name() << " wins!";
122
}
123
cout.flush();
124
sleep(1);
125
}
126
tournament->nextRound();
127
}
128
129
130
//print the final outcome
131
cout << cursorOn << clearScreen << cursorPosition(1,1) << "Final Outcome" << endl;
132
tournament->printLeader();
133
cout.flush();
134
135
return 0;
136
}
137
138