Skip to content
Snippets Groups Projects
Commit 27df4767 authored by TheHresvelgian's avatar TheHresvelgian
Browse files

Organizing code

parent c50deb07
No related branches found
No related tags found
No related merge requests found
......@@ -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
{
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();
do {
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);
}
}
}
}
......
......@@ -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); //-
......
......@@ -128,3 +128,15 @@ 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
......@@ -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
......@@ -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++)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment