From 1963920e93fac19b9356b47d5947a44b45005496 Mon Sep 17 00:00:00 2001 From: TheHresvelgian <sigrunhog@hotmail.com> Date: Fri, 29 Apr 2022 03:29:32 +0200 Subject: [PATCH] fiksing --- src/kode/Tournament.cpp | 64 ++++++++++++++------------------------- src/kode/deletePlayer.cpp | 4 +-- src/kode/editPlayer.cpp | 4 +-- src/kode/functions.h | 2 +- src/kode/newPlayer.cpp | 5 +-- src/kode/player.cpp | 4 ++- src/kode/player.h | 2 +- src/kode/printRanking.cpp | 2 +- src/kode/searchByID.cpp | 21 +++---------- src/kode/sortRanking.cpp | 24 +++++++-------- 10 files changed, 51 insertions(+), 81 deletions(-) diff --git a/src/kode/Tournament.cpp b/src/kode/Tournament.cpp index 80b74fd..ed3eb72 100644 --- a/src/kode/Tournament.cpp +++ b/src/kode/Tournament.cpp @@ -29,11 +29,14 @@ void Tournament::enterResults() cout << endl << "Enter results for round " << currentRound; for (int i = 0; i < nrOfTables; ++i) { + playerList[searchByID(white[i],playerList)]->playedAgainst.push_back( playerList[searchByID(black[i],playerList)]); + playerList[searchByID(black[i],playerList)]->playedAgainst.push_back(playerList[searchByID(white[i],playerList)]); + playerList[searchByID(white[i],playerList)]->white++; int answer; cout << endl << "Enter results for table " << i+1 << "with " - << playerList[searchByID(white[i],1)]->lastName + << playerList[searchByID(white[i],playerList)]->lastName << " as white and " - << playerList[searchByID(black[i],1)]->lastName + << playerList[searchByID(black[i],playerList)]->lastName << " as black." << endl << "1: white victory" << endl << "2: black victory" @@ -42,16 +45,16 @@ void Tournament::enterResults() switch (answer) { case 1: - playerList[searchByID(white[i],1)]->results.push_back(2); - playerList[searchByID(black[i],1)]->results.push_back(0); + playerList[searchByID(white[i],playerList)]->results.push_back(2); + playerList[searchByID(black[i],playerList)]->results.push_back(0); break; case 2: - playerList[searchByID(white[i],1)]->results.push_back(0); - playerList[searchByID(black[i],1)]->results.push_back(2); + playerList[searchByID(white[i],playerList)]->results.push_back(0); + playerList[searchByID(black[i],playerList)]->results.push_back(2); break; case 3: - playerList[searchByID(white[i],1)]->results.push_back(1); - playerList[searchByID(black[i],1)]->results.push_back(1); + playerList[searchByID(white[i],playerList)]->results.push_back(1); + playerList[searchByID(black[i],playerList)]->results.push_back(1); break; default: cout << "Error!"; @@ -90,6 +93,7 @@ void Tournament::printTables() currentRound++; + if (currentRound == 1) { vector<Player*> playersPlaying = playerList; @@ -109,8 +113,8 @@ void Tournament::printTables() 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; + << playerList[searchByID(white[i],playerList)]->lastName + << "\t\t" << playerList[searchByID(black[i],playerList)]->lastName; } } @@ -126,6 +130,7 @@ void Tournament::createMatchups() currentPlayer = playersPlaying[0]; for (int j = 0; j < playersPlaying.size(); j++) //finds the players opponent { + if (!currentPlayer->hasPlayedAgainst(playersPlaying[j]->playerID)) { opponent=playersPlaying[j]; @@ -143,38 +148,15 @@ void Tournament::createMatchups() } } - bool wasOneWhite; - bool wasTwoWhite; - for (int j = currentRound-2; j >= 0; j--) //decides who is white and who is black + if (currentPlayer->white > opponent->white || currentPlayer->white == opponent->white) { - 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); - } + white.push_back(opponent->playerID); + black.push_back(currentPlayer->playerID); + } + else if (currentPlayer->white < opponent->white) + { + white.push_back(currentPlayer->playerID); + black.push_back(opponent->playerID); } } } diff --git a/src/kode/deletePlayer.cpp b/src/kode/deletePlayer.cpp index 416a43b..65c9ea8 100644 --- a/src/kode/deletePlayer.cpp +++ b/src/kode/deletePlayer.cpp @@ -43,12 +43,12 @@ void deletePlayer() do { id = lesInt("\nPlease type the ID of the player you wish to delete\n",0,10000); - index = searchByID(id,1); + index = searchByID(id,playerList); if (index == -1) { cout << endl << "No player with this ID exists, please try again!" << endl; } - }while(searchByID(id,1)==-1); + }while(searchByID(id,playerList)==-1); playerToDelete = playerList[index]; break; default: diff --git a/src/kode/editPlayer.cpp b/src/kode/editPlayer.cpp index 57b9ef2..135519a 100644 --- a/src/kode/editPlayer.cpp +++ b/src/kode/editPlayer.cpp @@ -48,12 +48,12 @@ void editPlayer() do { id = lesInt("\nPlease type the ID of the player you wish to edit\n",0,10000); - index = searchByID(id,1); + index = searchByID(id,playerList); if (index == -1) { cout << endl << "No player with this ID exists, please try again!" << endl; } - }while(searchByID(id,1)==-1); + }while(searchByID(id,playerList)==-1); playerToEdit = playerList[index]; break; default: diff --git a/src/kode/functions.h b/src/kode/functions.h index 3c71103..519acd7 100644 --- a/src/kode/functions.h +++ b/src/kode/functions.h @@ -8,7 +8,7 @@ using namespace std; void newPlayer(int id); //+ -int searchByID(int id, int list); //+ +int searchByID(int id, vector<Player*> list); //+ int searchByName(string name); //+ void sortRanking(); //+ void printRanking(); //+ diff --git a/src/kode/newPlayer.cpp b/src/kode/newPlayer.cpp index 9f11032..d45bf0a 100644 --- a/src/kode/newPlayer.cpp +++ b/src/kode/newPlayer.cpp @@ -16,13 +16,13 @@ extern vector<Player*>playerList; void newPlayer(int id) { Player* newPlayer; - if (searchByID(id,1)!=-1) + if (searchByID(id,playerList)!=-1) { do { cout << endl << "A player with this ID already exists, please try again!" << endl; id = lesInt("Please offer a valid ID\n",0,10000); - }while(searchByID(id,1)!=-1); + }while(searchByID(id,playerList)!=-1); } newPlayer = new Player; newPlayer->playerID = id; @@ -32,6 +32,7 @@ void newPlayer(int id) getline(cin, newPlayer->firstName); newPlayer->rating = lesInt("\nRating: ", 1, 3000); cout << endl << "Club: "; + newPlayer->white=0; getline(cin, newPlayer->club); playerList.push_back(newPlayer); diff --git a/src/kode/player.cpp b/src/kode/player.cpp index d6e4cbb..eaf5d9f 100644 --- a/src/kode/player.cpp +++ b/src/kode/player.cpp @@ -114,7 +114,7 @@ int Player::currentRank(int id) { int index; sortRanking(); - index = searchByID(id,2); + index = searchByID(id,sortedRankings); if (index = -1) { cout << endl << "Error: no player found!" << endl; @@ -137,8 +137,10 @@ int Player::currentRank(int id) bool Player::hasPlayedAgainst(int id) { + for (int i = 0; i < playedAgainst.size(); i++) { + if (playedAgainst[i]->playerID == id) { return true; diff --git a/src/kode/player.h b/src/kode/player.h index e7f5773..6d12e9a 100644 --- a/src/kode/player.h +++ b/src/kode/player.h @@ -22,12 +22,12 @@ class Player int rating; int playerID; + int white; float score; vector <int> results; //stores the results of each match, 0 for loss, 1 for draw and 2 for win vector <Player*> playedAgainst; //has every person they have played against - vector<bool> wasWhite; //true for each round the player had white pieces, false for black Player(); Player(string name, string surName, string clubName, int rating, int playerID, float score); diff --git a/src/kode/printRanking.cpp b/src/kode/printRanking.cpp index 673cd52..e022819 100644 --- a/src/kode/printRanking.cpp +++ b/src/kode/printRanking.cpp @@ -22,7 +22,7 @@ void printRanking() sortRanking(); float currentScoreTP = sortedRankings[0]->score; int currentPlace = 1; - for (int i = 0; i < sortedRankings.size(); ++i) + for (int i = 0; i < sortedRankings.size(); i++) { if (currentScoreTP > sortedRankings[i]->score) { diff --git a/src/kode/searchByID.cpp b/src/kode/searchByID.cpp index 5f41d6d..c3c9cde 100644 --- a/src/kode/searchByID.cpp +++ b/src/kode/searchByID.cpp @@ -6,25 +6,12 @@ extern vector<Player*> playerList; extern vector<Player*> sortedRankings; -int searchByID(int id, int list) +int searchByID(int id, vector<Player*> list) { - vector<Player*> listToUse; - if (list == 1) - { - listToUse=playerList; - } - else if (list == 2) - { - listToUse = sortedRankings; - } - else - { - cout << endl << "Unexpected error!" << endl; - return -1; - } - for (int i = 0; i < listToUse.size(); i++) + + for (int i = 0; i < list.size(); i++) { - if (listToUse[i]->playerID == id) + if (list[i]->playerID == id) { return i; } diff --git a/src/kode/sortRanking.cpp b/src/kode/sortRanking.cpp index b48e9ff..189ab1f 100644 --- a/src/kode/sortRanking.cpp +++ b/src/kode/sortRanking.cpp @@ -1,7 +1,7 @@ #include <vector> // vector -#include <iostream> // cout, cin #include "player.h" +#include "functions.h" using namespace std; @@ -11,11 +11,11 @@ extern vector<Player*> sortedRankings; void sortRanking() { sortedRankings.clear(); - float currentHigh = 0; - vector<Player *> listUnsorted = playerList; - vector<Player *> leadingPlayers; + float currentHigh; + vector<Player*> listUnsorted = playerList; + vector<Player*> leadingPlayers; - vector<int> listIndex; + vector<int> listIDs; do { @@ -23,18 +23,18 @@ void sortRanking() for (int i = 0; i < listUnsorted.size(); i++) { listUnsorted[i]->calculateScore(); - // cout << endl << listUnsorted[i]->score << " : " << listUnsorted[i]->lastName; if (listUnsorted[i]->score > currentHigh) { leadingPlayers.clear(); + listIDs.clear(); leadingPlayers.push_back(listUnsorted[i]); - listIndex.push_back(i); + listIDs.push_back(listUnsorted[i]->playerID); currentHigh = leadingPlayers[0]->score; } else if (listUnsorted[i]->score == currentHigh) { leadingPlayers.push_back(listUnsorted[i]); - listIndex.push_back(i); + listIDs.push_back(listUnsorted[i]->playerID); } } for (int i = 0; i < leadingPlayers.size(); i++) @@ -43,13 +43,11 @@ void sortRanking() } leadingPlayers.clear(); - cout << listIndex.size(); - for (int i = 0; i < listIndex.size(); ++i) + for (int i = 0; i < listIDs.size(); i++) { - - listUnsorted[listIndex[i]] = listUnsorted[listUnsorted.size() - 1 - i]; + listUnsorted[searchByID(listIDs[i],listUnsorted)] = listUnsorted[listUnsorted.size()-1]; listUnsorted.pop_back(); } - listIndex.clear(); + listIDs.clear(); } while (listUnsorted.size() > 0); } \ No newline at end of file -- GitLab