From 27df47674740beb035964735ea2c2593900c2595 Mon Sep 17 00:00:00 2001
From: TheHresvelgian <sigrunhog@hotmail.com>
Date: Wed, 27 Apr 2022 01:01:24 +0200
Subject: [PATCH] Organizing code

---
 src/kode/Tournament.cpp | 81 +++++++++++++++++++++++++++++++++++++----
 src/kode/Tournament.h   |  5 ++-
 src/kode/player.cpp     | 12 ++++++
 src/kode/player.h       |  1 +
 src/kode/searchByID.cpp |  2 +-
 5 files changed, 91 insertions(+), 10 deletions(-)

diff --git a/src/kode/Tournament.cpp b/src/kode/Tournament.cpp
index d02dede..742fb43 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 cc053c5..fa0b3d7 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 2c00b98..4a5c70d 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 04ebe8c..64e8c88 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 2e84d62..5f41d6d 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++)
-- 
GitLab