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

Organizing code

parent 0b9975ff
No related branches found
No related tags found
No related merge requests found
//
// 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"
using namespace std;
extern vector<Tournament *> gTournaments;
void Tournament::startNewTournament()
{
}
void Tournament::nextRound()
{
}
void Tournament::endTournament()
{
}
/**
* Writes all data about the tournament rounds to file
*/
void Tournament::writeToFileTournament(ofstream &out)
{
ofstream outfile("tournamentData.dta");
if (outfile)
{
for (int i = 0; i < gTournaments.size(); i++)
gTournaments[i]->writeToFileTournament(outfile);
outfile.close();
}
else
cout << "\nCouldn't find tournamentData.dta\n"; // maybe unneccesary, considering it's also checked when reading data from file
}
/**
* Reads all data about the tournament rounds from file
*/
void Tournament::readFromFileTournaments(ifstream &in)
{
Tournament *tournamentNew;
ifstream infile("tournamentData.dta");
if (infile)
{
while (!infile.eof())
{
tournamentNew = new Tournament();
//tournamentNew->readFromFileTournaments(infile);
gTournaments.push_back(tournamentNew);
}
infile.close();
} else {
cout<<"\nCouldn't find tournamentData.dta!\n";
}
infile.close();
}
\ No newline at end of file
//
// Created by sigru on 26.04.2022.
//
#ifndef CHECKMATE_TOURNAMENT_H
#define CHECKMATE_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"
using namespace std;
extern vector<Player*> playerList;
extern vector<Player*> sortedRankings;
class Tournament
{
public :
int nrOfTables;
int currentRound;
int nrOfRounds;
vector<int>white;
vector<int>black;
virtual void startNewTournament(); //-
virtual void nextRound(); //-
virtual void endTournament(); //-
void writeToFileTournament(ofstream &out); //-
void readFromFileTournaments(ifstream &in); //-
};
#endif //CHECKMATE_TOURNAMENT_H
#include <iostream> // cout, cin #include <iostream> // cout, cin
#include <string> // string #include <string> // string
#include <vector> // vector #include <vector> // vector
...@@ -8,6 +5,7 @@ ...@@ -8,6 +5,7 @@
#include <fstream> // ifstream, ofstream #include <fstream> // ifstream, ofstream
#include "LesData2.h" #include "LesData2.h"
#include "player.h" #include "player.h"
#include "Tournament.h"
using namespace std; using namespace std;
...@@ -15,23 +13,6 @@ vector<Player*> playerList; ...@@ -15,23 +13,6 @@ vector<Player*> playerList;
vector<Player*> sortedRankings; vector<Player*> sortedRankings;
class Tournament
{
private:
vector <int *> tableNr;
vector <string *> player1;
vector <string *> player2;
public:
//void Tournament(){};
void writeTournament();
void fillTableArray();
void writeToFileTournament(ofstream &out);
void readFromFileTournaments(ifstream &in);
};
// vectorer:
// - hvem spiller på hvilket bord
vector<Player *> gPlayers;
vector<Tournament *> gTournaments; vector<Tournament *> gTournaments;
vector<int > gSorting; vector<int > gSorting;
...@@ -41,20 +22,13 @@ int gRounds = 0; ...@@ -41,20 +22,13 @@ int gRounds = 0;
int gTables = 1; int gTables = 1;
// Funksjoner: // Funksjoner:
void addPlayer();
void writePlayer(); void writePlayer();
void editPlayer(); void editPlayer();
int playerWin(); int playerWin();
Player* findPlayer(const string name);
Player* findPlayerID(const int ID);
void enterResults(); void enterResults();
void newTournament(); void newTournament();
void viewPlayers(); void viewPlayers();
void skrivMeny(); void skrivMeny();
void writeToFile();
void readFromFile();
void writeToFileTournament();
void readFromFileTournament();
void amntTables(); void amntTables();
void viewResults(); void viewResults();
...@@ -202,80 +176,6 @@ void Tournament::writeTournament() ...@@ -202,80 +176,6 @@ void Tournament::writeTournament()
} }
} }
/**
* Fills the tableNr array with numbers [1,2,3,...,gTables]
*
*/
void Tournament::fillTableArray()
{
// tableNr[0] = 1;
for (int i = 1; i < gTables; i++)
{
tableNr[i] = tableNr[i - 1] + 1;
}
}
/**
* Write one tournament round to file
*
* @param out - the file being written to
*/
void Tournament::writeToFileTournament(ofstream & out){
for(int i=0; i< gTables; i++){
out<< player1[i]
<< setw(STRLEN) <<player2[i]
<< setw(STRLEN) <<tableNr[i]
<< "\n";
}
}
/*
/**
* Read one tournament round from file
*
* @param in - file being read from
*
void Tournament::readFromFileTournament(ifstream & in)
{
for (int i = 0; i < gTables; i++)
{
in >> player1[i];
in.ignore(); // Might be getting some
in >> player2[i];
in.ignore(); // Difficulties here because of spacing between names
in >> tableNr[i];
in.ignore(); // Can't use getline() because player1 and player2 are arrays
}
}*/
/**
* Calculates and prints out scores for all players in descending order
*
* @see - calculateScore()
*/
void Player::viewResults(int i){
for(int j= i+1; j< gPlayers.size();i++){ //Sorts result
if(gPlayers[i]->score < gPlayers[j]->score){
gSorting[i] = gPlayers[j]->score;
}
}
for(int k=0; k<gPlayers.size(); k++){ //For MVP-purposes no scores can be
//identical
cout<< gSorting[k] <<"\t"; //Prints result
for(int j=0; j<gPlayers.size(); j++){
if(gPlayers[j]->score == gSorting[k]){
cout<< gPlayers[j]->name;
}
cout<<"\n";
}
}
}
/** /**
* *
* NON-CLASS FUNCTIONS * NON-CLASS FUNCTIONS
...@@ -342,22 +242,6 @@ void newTournament() ...@@ -342,22 +242,6 @@ void newTournament()
} }
} }
/**
* Add a player
*
* @see - Player::newPlayer()
*/
void addPlayer()
{
Player *newPlayer;
newPlayer = new Player;
newPlayer->newPlayer(newPlayer);
gPlayers.push_back(newPlayer);
cout << "\n\nPlayer added!\n\n";
}
/** /**
* edit info about a player * edit info about a player
* *
...@@ -415,18 +299,7 @@ void editPlayer() ...@@ -415,18 +299,7 @@ void editPlayer()
} }
} }
/**
*
*
*/
void viewPlayers()
{
for (int i = 0; i < gPlayers.size(); i++)
{
cout << gPlayers[i] << "\n\n";
}
}
/** /**
...@@ -440,99 +313,11 @@ void skrivMeny() ...@@ -440,99 +313,11 @@ void skrivMeny()
<< "\t2 - Add player\n" << "\t2 - Add player\n"
<< "\t3 - Enter results\n" << "\t3 - Enter results\n"
<< "\t4 - Edit player\n" << "\t4 - Edit player\n"
<< "\t5 - view player list\n" << "\t5 - View player list\n"
<< "\t6 - view results\n" << "\t6 - View results\n"
<< "\tQ - Quit\n\n"; << "\tQ - Quit\n\n";
} }
/**
* Writes all data belonging to the Player class to the file
*/
void writeToFile()
{
ofstream outfile("PlayerList.dta");
if (outfile)
{
for (int i = 0; i < gPlayers.size(); i++)
gPlayers[i]->writeToFilePlayer(outfile);
outfile.close();
}
else
cout << "\nCouldn't find PlayerList.dta\n"; // maybe unneccesary, considering it's also checked when reading data from file
}
/**
* Reads all data belonging to the Player class from the file
*/
void readFromFile()
{
Player *playerNew;
ifstream infile("PlayerList.dta");
if (infile)
{
while (!infile.eof())
{
playerNew = new Player();
playerNew->readFromFilePlayer(infile);
gPlayers.push_back(playerNew);
}
infile.close();
}
else
cout << "\nCouldn't find PlayerList.dta!\n";
}
/**
* Writes all data about the tournament rounds to file
*/
void writeToFileTournament()
{
ofstream outfile("tournamentData.dta");
if (outfile)
{
for (int i = 0; i < gTournaments.size(); i++)
gTournaments[i]->writeToFileTournament(outfile);
outfile.close();
}
else
cout << "\nCouldn't find tournamentData.dta\n"; // maybe unneccesary, considering it's also checked when reading data from file
}
/**
* Reads all data about the tournament rounds from file
*/
void readFromFileTournament()
{
Tournament *tournamentNew;
ifstream infile("tournamentData.dta");
if (infile)
{
while (!infile.eof())
{
tournamentNew = new Tournament();
//tournamentNew->readFromFileTournaments(infile);
gTournaments.push_back(tournamentNew);
}
infile.close();
} else {
cout<<"\nCouldn't find tournamentData.dta!\n";
}
infile.close();
}
/** /**
* Asks user for number of tables * Asks user for number of tables
* *
...@@ -541,13 +326,3 @@ void amntTables(){ ...@@ -541,13 +326,3 @@ void amntTables(){
cout<<"How many tables do you have?\n"; cout<<"How many tables do you have?\n";
gTables = lesInt("Tables:",1,200); gTables = lesInt("Tables:",1,200);
} }
void viewResults(){
for(int i=0; i < gPlayers.size();i++){
gPlayers[i]->calculateScore();
//Calculates score for every player
for(int i=0; i<gPlayers.size(); i++){
gPlayers[i]->viewResults(i);}
}}
...@@ -7,10 +7,11 @@ ...@@ -7,10 +7,11 @@
using namespace std; using namespace std;
void newPlayer(int id); void newPlayer(int id); //+
int searchByID(int id, int list); int searchByID(int id, int list); //+
int searchByName(string name); int searchByName(string name); //+
void sortRanking(); void sortRanking(); //+
void printRanking(); void printRanking(); //+
void viewPlayers(); //+
#endif //CHECKMATE_FUNCTIONS_H #endif //CHECKMATE_FUNCTIONS_H
...@@ -15,19 +15,21 @@ extern vector<Player*>playerList; ...@@ -15,19 +15,21 @@ extern vector<Player*>playerList;
void newPlayer(int id) void newPlayer(int id)
{ {
Player* newPlayer;
if (searchByID(id,1)!=-1) do
{ {
cout << endl << "A player with this ID already exists, please try again!" << endl; 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);
cout << "\n\tFull name: "; newPlayer = new Player;
getline(cin, player->name); newPlayer->playerID = id;
cout << endl << "Last name of player " << id << endl;
player->rating = lesInt("\n\tRating: ", 1, 3000); getline(cin, newPlayer->lastName);
cout << endl << "First name of player " << id << endl;
player->playerID = lesInt("\n\tPlayer ID: ", 1, 10000); getline(cin, newPlayer->firstName);
newPlayer->rating = lesInt("\nRating: ", 1, 3000);
cout << "\n\tClub: "; cout << endl << "Club: ";
getline(cin, player->club); getline(cin, newPlayer->club);
playerList.push_back(newPlayer);
} }
\ No newline at end of file
...@@ -54,6 +54,14 @@ void Player::printPlayer() ...@@ -54,6 +54,14 @@ void Player::printPlayer()
<< "Current rank in tournament: " << currentRank(playerID) << endl; << "Current rank in tournament: " << currentRank(playerID) << endl;
} }
void Player::printPlayerTable()
{
cout << endl << lastName << "\t"
<< playerID << "\t"
<< club << "\t"
<< rating;
}
void Player::calculateScore() void Player::calculateScore()
{ {
float tempScore = 0.0f; float tempScore = 0.0f;
......
...@@ -34,6 +34,7 @@ class Player ...@@ -34,6 +34,7 @@ class Player
void setPlayer(string nm, string , string clbNm, int rtng, int plyrID, float score); //+ void setPlayer(string nm, string , string clbNm, int rtng, int plyrID, float score); //+
virtual void printPlayer(); //+ virtual void printPlayer(); //+
virtual void printPlayerTable();
virtual void calculateScore(); //+ virtual void calculateScore(); //+
virtual void writeToFilePlayer(ofstream &out);//- virtual void writeToFilePlayer(ofstream &out);//-
virtual void readFromFilePlayer(ifstream &in);//- virtual void readFromFilePlayer(ifstream &in);//-
......
...@@ -11,7 +11,6 @@ int searchByID(int id, int list) ...@@ -11,7 +11,6 @@ int searchByID(int id, int list)
vector<Player*> listToUse; vector<Player*> listToUse;
if (list == 1) if (list == 1)
{ {
listToUse = playerList;
} }
else if (list == 2) else if (list == 2)
{ {
......
//
// Created by sigru on 25.04.2022.
//
#ifndef CHECKMATE_TOURNAMENT_H
#define CHECKMATE_TOURNAMENT_H
#endif //CHECKMATE_TOURNAMENT_H
#include <iostream> // cout, cin
#include <vector> // vector
#include "player.h"
#include "Tournament.h"
using namespace std;
vector<Player*> playerList;
/**
* Prints a list of all players with identification
*/
void viewPlayers()
{
cout << endl << "Last name" << "\t"
<< "Player ID" << "\t"
<< "Club" << "\t"
<< "Rating";
for (int i = 0; i < playerList.size(); i++)
{
playerList[i]->printPlayerTable();
}
}
\ 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