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

Organizing code

parent 5f629a63
No related branches found
No related tags found
No related merge requests found
......@@ -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;
......
......@@ -28,7 +28,6 @@ class Tournament
vector<int>white;
vector<int>black;
virtual void startNewTournament(); //+
virtual void enterResults(); //+
virtual void endTournament(); //+
virtual void printTables(); //+
......
......@@ -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() {
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;
}
command = lesChar("\nKommando");
}
writeToFileTournament();
writeToFile();
return 0;
menuToPrint=menuStart;
}
/**
* Writes out all info on player
*
*/
void Player::writePlayer() const
else if (gTournaments[0]->currentRound==0)
{
cout << "\n"
<< name << '\n'
<< rating << '\n'
<< playerID << '\n'
<< club << '\n'
<< wins << "\n\n";
menuToPrint=menuSetUp;
}
/**
* Finds player based on name and returns pointer
*
* @param name
* @return Player*
*/
Player *findPlayer(const string name)
else if (gTournaments[0]->nrOfRounds==gTournaments[0]->white.size())
{
for (int i = 0; i < gPlayers.size(); i++)
{
if (gPlayers[i]->playerName(name) == true)
{
return gPlayers[i];
menuToPrint=menuEnd;
}
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)
{
return gPlayers[i];
}
else
return nullptr;
}
menuToPrint=menuDuring;
}
/**
* 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)
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();
getline(in, name);
in >> rating;
in.ignore();
in >> playerID;
in.ignore();
getline(in, club);
}
/**
* 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";
}
#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
#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
......@@ -13,5 +13,8 @@ int searchByName(string name); //+
void sortRanking(); //+
void printRanking(); //+
void viewPlayers(); //+
void editPlayer(); //+
void startNewTournament();//+
void deletePlayer();//-
#endif //CHECKMATE_FUNCTIONS_H
......@@ -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
......
//
// 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment