diff --git a/src/kode/Tournament.cpp b/src/kode/Tournament.cpp index d02dede83822cd8fa9f06aef6b2a9371ac34c99b..742fb439511c4900e40aac86e22a26a5e42cc649 100644 --- a/src/kode/Tournament.cpp +++ b/src/kode/Tournament.cpp @@ -67,7 +67,8 @@ void Tournament::printTables() black.clear(); currentRound++; - if(currentRound==1) + + if (currentRound == 0) { vector<Player*> playersPlaying = playerList; for (int i = 0; i < nrOfTables; ++i) @@ -76,17 +77,83 @@ void Tournament::printTables() playersPlaying.pop_back(); black.push_back(playersPlaying[playersPlaying.size()-1]->playerID); playersPlaying.pop_back(); - cout << endl << "Table " << i << ": \t" - << } } else { - vector<Player*> playersPlaying = sortedRankings; - sortRanking(); - do { + createMatchups(); + } + cout << endl << "Table \t\t White Player \t\t Black Player"; + for (int i = 0; i < nrOfTables; ++i) + { + cout << endl << i << "\t\t" + << playerList[searchByID(white[i],1)]->lastName + << "\t\t" << playerList[searchByID(black[i],1)]->lastName; + } +} + + +void Tournament::createMatchups() +{ + Player* currentPlayer; + Player* opponent; + vector<Player*> playersPlaying = sortedRankings; + sortRanking(); + for (int i = 0; i < nrOfTables; i++) + { + currentPlayer = playersPlaying[0]; + for (int j = 0; j < playersPlaying.size(); j++) //finds the players opponent + { + if (!currentPlayer->hasPlayedAgainst(playersPlaying[j]->playerID)) + { + opponent=playersPlaying[j]; + for (int k = j; k < playersPlaying.size(); ++k) + { + playersPlaying[k]=playersPlaying[k+1]; + } + playersPlaying.pop_back(); + for (int k = 0; k < playersPlaying.size(); ++k) + { + playersPlaying[k]=playersPlaying[k+1]; + } + playersPlaying.pop_back(); + j=playersPlaying.size(); //removes the opponents from the list and exists the loop + } - }while(playersPlaying > 0); + } + bool wasOneWhite; + bool wasTwoWhite; + for (int j = currentRound-2; j >= 0; j--) //decides who is white and who is black + { + wasOneWhite = currentPlayer->wasWhite[j]; + wasTwoWhite = opponent->wasWhite[j]; + if (wasOneWhite != wasTwoWhite) + { + if (wasOneWhite) + { + white[i] = opponent->playerID; + opponent->wasWhite.push_back(true); + black[i] = currentPlayer->playerID; + currentPlayer->wasWhite.push_back(false); + j = 0; //exits loop + } + else if(wasTwoWhite) + { + white[i] = currentPlayer->playerID; + currentPlayer->wasWhite.push_back(true); + black[i] = opponent->playerID; + opponent->wasWhite.push_back(false); + j = 0; //exits loop + } + } + if (j==0 && white.size()<=i) //if they have had the same color every round grant the lower player white + { + white[i] = opponent->playerID; + opponent->wasWhite.push_back(true); + black[i] = currentPlayer->playerID; + currentPlayer->wasWhite.push_back(false); + } + } } } diff --git a/src/kode/Tournament.h b/src/kode/Tournament.h index cc053c525205b5906098675a50d2a3df3d23959b..fa0b3d75d62c0d2f01efb97b4256ed8a2d7de9af 100644 --- a/src/kode/Tournament.h +++ b/src/kode/Tournament.h @@ -31,8 +31,9 @@ class Tournament virtual void startNewTournament(); //+ virtual void enterResults(); //- virtual void endTournament(); //- - virtual void printTables(); //- - bool canStartRound();// + virtual void printTables(); //+ + virtual void createMatchups();//+ + bool canStartRound();//+ void writeToFileTournament(ofstream &out); //- void readFromFileTournaments(ifstream &in); //- diff --git a/src/kode/player.cpp b/src/kode/player.cpp index 2c00b988fe34ebe950f86441ee4eb1b3d3bede43..4a5c70dc17b9de3f6419fa2b73c732e0ad1f7f55 100644 --- a/src/kode/player.cpp +++ b/src/kode/player.cpp @@ -127,4 +127,16 @@ int Player::currentRank(int id) } } return currentPlace; +} + +bool Player::hasPlayedAgainst(int id) +{ + for (int i = 0; i < playedAgainst.size(); i++) + { + if (playedAgainst[i]->playerID == id) + { + return true; + } + } + return false; } \ No newline at end of file diff --git a/src/kode/player.h b/src/kode/player.h index 04ebe8cf09ac7dfe336c2422cdc647f222d54561..64e8c888ac94eef39fac41f4632d1e8602ea5530 100644 --- a/src/kode/player.h +++ b/src/kode/player.h @@ -38,6 +38,7 @@ class Player virtual void calculateScore(); //+ virtual void writeToFilePlayer(ofstream &out);//- virtual void readFromFilePlayer(ifstream &in);//- + bool hasPlayedAgainst(int id);//- int currentRank(int id);//+ }; #endif //CHECKMATE_PLAYER_H diff --git a/src/kode/searchByID.cpp b/src/kode/searchByID.cpp index 2e84d62beaef81b5cdc21c2311d38a63b22bb3a1..5f41d6d8b2c9ae447643828c22be168815b091a9 100644 --- a/src/kode/searchByID.cpp +++ b/src/kode/searchByID.cpp @@ -19,7 +19,7 @@ int searchByID(int id, int list) } else { - cout << endl << "Unexpected error, please contact publisher!" << endl; + cout << endl << "Unexpected error!" << endl; return -1; } for (int i = 0; i < listToUse.size(); i++)