diff --git a/src/kode/Tournament.cpp b/src/kode/Tournament.cpp index 932e8603948fa2133fdeb8bc7b1dc6fb416f6cb7..80817878ae6a41592f857f54c516ff1f0b68bb45 100644 --- a/src/kode/Tournament.cpp +++ b/src/kode/Tournament.cpp @@ -19,34 +19,6 @@ extern vector<Tournament *> gTournaments; extern vector<Player*> playerList; extern vector<Player*> sortedRankings; -void Tournament::startNewTournament() -{ - char answer; - do { - cout << endl << "Are you certain you wish to start a new tournament?" - << "This will delete all existing tournament data, including the player list."; - answer = lesChar(" Y/N\n"); - answer= tolower(answer); - }while (answer != 'y' && answer != 'n'); - if (answer == 'n') - { - return; - } - playerList.clear(); - sortedRankings.clear(); - gTournaments.clear(); - - Tournament* newTournament; - newTournament = new Tournament; - - newTournament->nrOfTables = lesInt("\nHow many tables should the tournament have?\n",1,1000); - newTournament->currentRound = 0; - newTournament->nrOfRounds = lesInt("\nHow many rounds should the tournament have?\n",1,20); - gTournaments.push_back(newTournament); -} - - - void Tournament::enterResults() { cout << endl << "Enter results for round " << currentRound; diff --git a/src/kode/Tournament.h b/src/kode/Tournament.h index 03fe46b1056fd81b17b1d4438c562562d68a7c8a..3fcc8c2516a90c884ed5ab0a0de53826f7f75e39 100644 --- a/src/kode/Tournament.h +++ b/src/kode/Tournament.h @@ -28,7 +28,6 @@ class Tournament vector<int>white; vector<int>black; - virtual void startNewTournament(); //+ virtual void enterResults(); //+ virtual void endTournament(); //+ virtual void printTables(); //+ diff --git a/src/kode/checkmate.cpp b/src/kode/checkmate.cpp index 4c45c8b2d6a0dc3a21e33da4edc3c073783df58b..f565c369e6914797fcc2a3210b1da980e66364f0 100644 --- a/src/kode/checkmate.cpp +++ b/src/kode/checkmate.cpp @@ -6,6 +6,7 @@ #include "LesData2.h" #include "player.h" #include "Tournament.h" +#include "functions.h" using namespace std; @@ -14,166 +15,78 @@ vector<Player*> sortedRankings; vector<Tournament *> gTournaments; -vector<int > gSorting; -// globale verdier: -const int STRLEN = 80; -int gRounds = 0; -int gTables = 1; +string menuToPrint; +string menuStart = "\nS: Start a new tournament\nQ: Quit the program"; +string menuSetUp = "\nV: View player list\nE: Edit a player\nA: Add player\nD: Delete player\nS: Start a new tournament\nQ: Quit the program"; +string menuDuring = "\nV: View player list\nR: View rankings\nE: Edit a player\nN: Enter results and advance to the next round\nS: Start a new tournament\nQ: Quit the program"; +string menuEnd = "\nS: Start a new tournament\nR: View rankings\nV: View player list\nQ: Quit the program"; -// Funksjoner: -void writePlayer(); -void editPlayer(); -int playerWin(); -void enterResults(); -void newTournament(); -void viewPlayers(); -void skrivMeny(); -void amntTables(); -void viewResults(); +int main() +{ + char command; -int main() { - -char command; - readFromFile(); - readFromFileTournament(); - - amntTables(); - - skrivMeny(); - command = lesChar("\nKommando"); + //readFromFile(); + //readFromFileTournament(); - while (command != 'Q') + do { - switch (command) + if (gTournaments.empty()) { - case 1: newTournament(); break; - case 2: addPlayer(); break; - case 3: enterResults(); break; - case 4: editPlayer(); break; - case 5: viewPlayers(); break; - case 6: viewResults(); break; - default: skrivMeny(); break; + menuToPrint=menuStart; } - command = lesChar("\nKommando"); - } - - writeToFileTournament(); - writeToFile(); - - return 0; -} - - - -/** - * Writes out all info on player - * - */ -void Player::writePlayer() const -{ - - cout << "\n" - << name << '\n' - << rating << '\n' - << playerID << '\n' - << club << '\n' - << wins << "\n\n"; -} - -/** - * Finds player based on name and returns pointer - * - * @param name - * @return Player* - */ -Player *findPlayer(const string name) -{ - for (int i = 0; i < gPlayers.size(); i++) - { - if (gPlayers[i]->playerName(name) == true) + else if (gTournaments[0]->currentRound==0) { - return gPlayers[i]; + menuToPrint=menuSetUp; } - else - return nullptr; - } -} - -string Player::returnName(){ - return name; -} - -/** - * Finds player based on PlayerID and returns pointer - * - * @param ID - * @return Player* - */ -Player *findPlayerID(const int ID) -{ - for (int i = 0; i < gPlayers.size(); i++) - { - if (gPlayers[i]->playerId(ID) == true) + else if (gTournaments[0]->nrOfRounds==gTournaments[0]->white.size()) { - return gPlayers[i]; + menuToPrint=menuEnd; } else - return nullptr; - } -} - -/** - * Calculates the score for a single player - * - */ -void Player::calculateScore() -{ - - for (int i = 1; i < wins; i++) - { - score += 1; - } - for (int i = 1; i < draws; i++) - { - score += 0.5; - } -} - -/** - * Read one player from file - * - * @param in - the file being read from - */ -void Player::readFromFilePlayer(ifstream &in) -{ + { + menuToPrint=menuDuring; + } - getline(in, name); - in >> rating; - in.ignore(); - in >> playerID; - in.ignore(); - getline(in, club); -} + cout << endl << menuToPrint; + command = lesChar("\nCommand: "); + command= tolower(command); + switch (command) + { + case 's': + startNewTournament(); + break; + case 'v': + viewPlayers(); + break; + case 'e': + editPlayer(); + break; + case 'a': + int id; + id = lesInt("\nWhat is the new player's ID?\n",0,10000); + newPlayer(id); + break; + case 'd': + deletePlayer(); + break; + case 'r': + printRanking(); + break; + case 'n': + gTournaments[0]->enterResults(); + default: + cout << endl << "Please enter a valid command."; + break; + } + }while (command != 'Q'); + // writeToFileTournament(); + // writeToFile(); -/** - * Writes out all data for one tournament round - * - */ -void Tournament::writeTournament() -{ - cout << setw(STRLEN) << "White player" - << setw(STRLEN) << "Black player" - << setw(STRLEN) << "Table number\n"; - for (int i = 0; i > gTables; i++) - { - cout << setw(STRLEN) << player1[i] - << setw(STRLEN) << player2[i] - << setw(STRLEN) << tableNr[i]; - } + return 0; } /** @@ -182,109 +95,3 @@ void Tournament::writeTournament() * */ -/** - * Enter results for a specific round - * - */ -void enterResults() -{ - - int round; - int table; - char result; - - cout << "Which round do you want to enter results for? (0 -" << gRounds + 1 << ")"; - round = lesInt("Round:", 0, (gRounds + 1)); - - cout << "Which table would you like to enter results for? (1 -" << gTables << ")\n"; - table = lesInt("Table:", 0, (gTables + 1)); - - // Find players - - cout << "\nEnter result for " << /*name of player*/ "(W(in), L(ose), D(raw)):"; - result = lesChar("Result:"); - if (result == 'W') - { - //+1 to wins int of player ] - } - else if (result == 'D') - { - //+1 to draws int of player - } -} - -/** - * edit info about a player - * - * @see - Player::updatePlayer - */ -void editPlayer() -{ - int choise, - ID; - string search; - // choose wether to enter info or look through list - choise = lesInt("\n\t1. Enter info or 2. List", 1, 2); - cout << "\n\n"; - // enter info - if (choise == 1) - { - choise = 0; // resets variable - // choose search by name or playerID - choise = lesInt("\n\t1. search by name or 2. search by playerID", 1, 2); - if (choise == 1) // if name - { - // user enters name to search for - cout << "\n\tName: "; - getline(cin, search); - // findPlayer searches through all players in vector - // if none has the name - if (findPlayer(search) == nullptr) - { - cout << "\n\tName not recognized!\n"; - } - else - { // if player found, write out info - cout << "\n\tPlayer found!\n"; - findPlayer(search)->writePlayer(); - } - } // if choose playerID - else if (choise == 2) - { - ID = lesInt("\n\tPlayerID: ", 1, 1000); - if (findPlayerID(ID) == nullptr) - { - cout << "\n\tPlayerID not recognized!"; - } - else - { - cout << "\n\tPlayer found!\n"; - findPlayerID(ID)->writePlayer(); - } - } - cout << "\n\n"; - } - else if (choise == 2) - { - viewPlayers(); - } -} - - - - -/** - * writes the programs menu options - */ -void skrivMeny() -{ - - cout << "\nMenu: \n" - << "\t1 - Generate tournament set-up\n" - << "\t2 - Add player\n" - << "\t3 - Enter results\n" - << "\t4 - Edit player\n" - << "\t5 - View player list\n" - << "\t6 - View results\n" - << "\tQ - Quit\n\n"; -} diff --git a/src/kode/deletePlayer.cpp b/src/kode/deletePlayer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..70bb947cf3edf3e8986f5de9a824ebe3eb445440 --- /dev/null +++ b/src/kode/deletePlayer.cpp @@ -0,0 +1,89 @@ +#include <iostream> // cout, cin +#include <string> // STRING-KLASSEN + +#include "player.h" +#include "LesData2.h" +#include "functions.h" + +using namespace std; + +extern vector<Player*>playerList; + +void deletePlayer() +{ + int choice = 0; + int id; + int index = -1; + string search; + Player* playerToDelete= nullptr; + do { + cout << endl << "Choose what you wish to search by"; + choice = lesInt("\n1: search by name \nor \n2: search by playerID\n", 1, 2); + switch (choice) + { + case 1: + do + { + cout << "\nPlease type either the player's first or last name\n"; + getline(cin, search); + index = searchByName(search); + if (index == -1) + { + cout << endl << "Name not found, please try again."; + } + } while(index == -1); + playerToDelete = playerList[index]; + break; + case 2: + do + { + id = lesInt("\nPlease type the ID of the player you wish to edit\n",0,10000); + index = searchByID(id,1); + if (index == -1) + { + cout << endl << "No player with this ID exists, please try again!" << endl; + } + }while(searchByID(index,1)==-1); + playerToDelete = playerList[index]; + break; + default: + cout << endl << "Invalid input, please try again"; + break; + } + }while(playerToDelete == nullptr); + + bool toDelete = true; + do { + choice = 0; + cout << endl << "This is the player the program has found: "; + playerToDelete->printPlayer(); + choice = lesInt("Do you wish to delete this player?\n1: yes\n2: no\n",1,2); + switch (choice) + { + case 1: + //delte + break; + case 2: + toDelete=false; + break; + default: + cout << endl << "Invalid input!"; + break; + } + + } while (toDelete); + choice = lesInt("\nDo you wish to delete another player?\n1: yes\n2: no",1,2); + switch (choice) + { + case 1: + deletePlayer(); + break; + case 2: + return; + break; + default: + cout << endl << "Invalid input, returning to menu."; + return; + break; + } +} \ No newline at end of file diff --git a/src/kode/editPlayer.cpp b/src/kode/editPlayer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1229449d193ed2c2c0b87890626d053a4f2e666c --- /dev/null +++ b/src/kode/editPlayer.cpp @@ -0,0 +1,113 @@ +#include <iostream> // cout, cin +#include <string> // string +#include <vector> // vector +#include <iomanip> // setw +#include <fstream> // ifstream, ofstream +#include "LesData2.h" +#include "player.h" +#include "Tournament.h" +#include "functions.h" + +using namespace std; + +extern vector<Player*> playerList; +/** + * edit info about a player + */ +void editPlayer() +{ + int choice = 0; + int id; + int index = -1; + string search; + Player* playerToEdit = nullptr; + do { + cout << endl << "Choose what you wish to search by"; + choice = lesInt("\n1: search by name \nor \n2: search by playerID\n", 1, 2); + switch (choice) + { + case 1: + do + { + cout << "\nPlease type either the player's first or last name\n"; + getline(cin, search); + index = searchByName(search); + if (index == -1) + { + cout << endl << "Name not found, please try again."; + } + } while(index == -1); + playerToEdit = playerList[index]; + break; + case 2: + do + { + id = lesInt("\nPlease type the ID of the player you wish to edit\n",0,10000); + index = searchByID(id,1); + if (index == -1) + { + cout << endl << "No player with this ID exists, please try again!" << endl; + } + }while(searchByID(index,1)==-1); + playerToEdit = playerList[index]; + break; + default: + cout << endl << "Invalid input, please try again"; + break; + } + }while(playerToEdit == nullptr); + + cout << endl << "Player found!"; + playerToEdit->printPlayer(); + choice=0; + bool edit = true; + int input; + do + { + input = 0; + cout << endl << "What do you wish to edit?"; + choice = lesInt("\n1: First name\n2:Last name\n3: Club\n4: Rating\n5: ID\n",1,5); + switch (choice) + { + case 1: + cout << endl << "What should the first name be?" << endl; + getline(cin,playerToEdit->firstName); + break; + case 2: + cout << endl << "What should the last name be?" << endl; + getline(cin,playerToEdit->lastName); + break; + case 3: + cout << endl << "What club does the player belong to?" << endl; + getline(cin,playerToEdit->club); + break; + case 4: + input = lesInt("\nWhat is the player's new rating?\n",0,3000); + playerToEdit->rating = input; + break; + case 5: + input = lesInt("\nWhat is the player's ID?\n",0,10000); + playerToEdit->playerID = input; + break; + default: + cout << endl << "Error: not a valid command!"; + break; + } + choice = 0; + cout << endl << "Do you wish to continue editing this player?"; + choice = lesInt("\n1: yes\n2: no\n",1,2); + if (choice == 2) + { + choice = 0; + choice = lesInt("\nDo you wish to edit another player?\n1: yes\n2: no\n",1,2); + if (choice==1) + { + editPlayer(); + } + else if(choice==2) + { + edit=false; + } + } + }while(edit); +} \ No newline at end of file diff --git a/src/kode/functions.h b/src/kode/functions.h index e78289d7dea56031bf9ac57e3e2212df75e4e707..e319466b6f58519cf5b0b382b238c52227b0f84f 100644 --- a/src/kode/functions.h +++ b/src/kode/functions.h @@ -13,5 +13,8 @@ int searchByName(string name); //+ void sortRanking(); //+ void printRanking(); //+ void viewPlayers(); //+ +void editPlayer(); //+ +void startNewTournament();//+ +void deletePlayer();//- #endif //CHECKMATE_FUNCTIONS_H diff --git a/src/kode/player.cpp b/src/kode/player.cpp index 4a5c70dc17b9de3f6419fa2b73c732e0ad1f7f55..cb309854aefaf3ddcaff65f8dfbc52c6d7aa5d2b 100644 --- a/src/kode/player.cpp +++ b/src/kode/player.cpp @@ -46,7 +46,7 @@ void Player::setPlayer(string nm, string srNm, string clbNm, int rtng, int plyrI */ void Player::printPlayer() { - cout << lastName << ", " << firstName + cout << endl << lastName << ", " << firstName << endl << "Club: " << club << endl << "Rating: " << rating << endl << "ID: " << playerID << endl diff --git a/src/kode/startNewTournament.cpp b/src/kode/startNewTournament.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a41f2c46c2e8905832d73eb6291c27d43f70ccb8 --- /dev/null +++ b/src/kode/startNewTournament.cpp @@ -0,0 +1,46 @@ +// +// Created by sigru on 26.04.2022. +// + +#include "Tournament.h" + +#include <iostream> // cout, cin +#include <string> // string +#include <vector> // vector +#include <iomanip> // setw +#include <fstream> // ifstream, ofstream +#include "LesData2.h" +#include "player.h" +#include "functions.h" + +using namespace std; + +extern vector<Tournament *> gTournaments; +extern vector<Player*> playerList; +extern vector<Player*> sortedRankings; + +void startNewTournament() +{ + char answer; + do { + cout << endl << "Are you certain you wish to start a new tournament?" + << "This will delete all existing tournament data, including the player list."; + answer = lesChar(" Y/N\n"); + answer= tolower(answer); + }while (answer != 'y' && answer != 'n'); + if (answer == 'n') + { + return; + } + playerList.clear(); + sortedRankings.clear(); + gTournaments.clear(); + + Tournament* newTournament; + newTournament = new Tournament; + + newTournament->nrOfTables = lesInt("\nHow many tables should the tournament have?\n",1,1000); + newTournament->currentRound = 0; + newTournament->nrOfRounds = lesInt("\nHow many rounds should the tournament have?\n",1,20); + gTournaments.push_back(newTournament); +} \ No newline at end of file