CSC112 Spring 2016 Examples
/*1* This is the template for a class which implements a simple Swiss Tournament.2* It is complete with "Bye's" for any time those are needed.3*/4#ifndef SWISS_H5#define SWISS_H6#include <map>7#include <vector>8#include <set>9#include <string>10#include <utility>11#include <algorithm>12#include <iostream>131415template<class player_t>16class SwissTournament17{18public:19//the participants of the tournament20class Contestant21{22public:23Contestant()24{25wins = 0;26losses = 0;27}28virtual std::string name()=0;29virtual player_t *player()=0;3031bool operator<(const Contestant &rhs) const {32if(wins == rhs.wins) return losses<rhs.losses;33return wins>rhs.wins;34}3536private:37std::set<Contestant*> played;38int wins;39int losses;40friend SwissTournament<player_t>;41};4243static bool swissCmp(Contestant *a, Contestant *b) { return *a < *b; }444546//Constructor47SwissTournament(const std::vector<Contestant*> &players) : contestants(players)48{49//arrange the first round at random50round = 1;51match = 0;52exhausted = false;53for(int i=0; i<contestants.size(); i++) {54std::swap(contestants[i], contestants[rand()%contestants.size()]);55}5657//arrange the matches58for(int i=0; i<contestants.size(); i+=2) {59std::pair<Contestant *, Contestant*> m;60m.first = contestants[i];61if(i+1<contestants.size()) {62m.first->played.insert(m.second);63m.second = contestants[i+1];64m.second->played.insert(m.first);65} else {66m.second = nullptr; //the bye67}6869matches.push_back(m);70}71}727374//return the current match75std::pair<Contestant*, Contestant*> getMatch()76{77return matches[match];78}798081//report the winner82void report(Contestant* winner) {83winner->wins++;84if(matches[match].second) {85if(winner == matches[match].first)86matches[match].second->losses++;87else88matches[match].first->losses++;89}9091//rank things92std::sort(contestants.begin(), contestants.end(), swissCmp);9394//on to the next match!95match++;96}979899//true if the round is done100bool roundDone() {101return match >= matches.size();102}103104105//true if the tournament is done106bool tournamentDone() {107std::sort(contestants.begin(), contestants.end(), swissCmp);108109//safety valve110if(exhausted) return true;111112//check to see if there is a tie for first, second, or third place113for(int i=0; i<=2; i++) {114//check to see if there is a tie for first place115if(contestants[i]->wins == contestants[i+1]->wins and116contestants[i]->losses == contestants[i+1]->losses) {117return false;118}119}120121//if we make it here, we're done!122return true;123}124125126//arrange the next round127void nextRound()128{129std::pair<Contestant *, Contestant *> m;130int start;131132if(!roundDone()) {133return; //no fair interrupting a round!134}135136//sort contestants and clear the matches137std::sort(contestants.begin(), contestants.end(), swissCmp);138matches.clear();139140//skip over the cemented spots141start=3;142for(int i=0; i<=2; i++) {143if(contestants[i]->wins == contestants[i+1]->wins and144contestants[i]->losses == contestants[i+1]->losses) {145start = i;146}147}148149//set the matches, detect exhaustion150std::set<int> used;151exhausted = true;152for(int i=start; i<contestants.size(); i++) {153if(used.count(i)) continue; //skip the used154for(int j=i+1; j<contestants.size(); j++) {155if(used.count(j)) continue; //skip the used156if(contestants[i]->played.count(contestants[j])) continue; //no dual face-offs!157//make it so!158m.first = contestants[i];159m.second= contestants[j];160contestants[i]->played.insert(contestants[j]);161contestants[j]->played.insert(contestants[i]);162used.insert(i);163used.insert(j);164exhausted = false;165matches.push_back(m);166break;167}168169//handle byes170if(used.count(i) == 0){171m.first = contestants[i];172m.second = nullptr;173used.insert(i);174matches.push_back(m);175}176}177178//next round!179round++;180match = 0;181}182183184void printPairings() {185std::cout << "Round " << round << std::endl;186for(int i=0; i<matches.size(); i++) {187std::cout << i+1 << ".) "188<< matches[i].first->name() << " vs " ;189if(matches[i].second != nullptr) {190std::cout << matches[i].second->name() << std::endl;191} else {192std::cout << "** BYE **" << std::endl;193}194}195}196197198void printLeader() {199std::sort(contestants.begin(), contestants.end(), swissCmp);200std:: cout << "Leader Board" << std::endl;201for(int i=0; i<contestants.size(); i++) {202std::cout << i+1 << ".) "203<< contestants[i]->name() << "\t"204<< contestants[i]->wins << " - " << contestants[i]->losses << std::endl;205}206}207208private:209int round;210int match;211bool exhausted;212std::vector<Contestant *> contestants;213std::vector<std::pair<Contestant*, Contestant*>> matches;214};215216217#endif218219