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 <string> // string
#include <vector> // vector
......@@ -8,6 +5,7 @@
#include <fstream> // ifstream, ofstream
#include "LesData2.h"
#include "player.h"
#include "Tournament.h"
using namespace std;
......@@ -15,23 +13,6 @@ vector<Player*> playerList;
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<int > gSorting;
......@@ -41,20 +22,13 @@ int gRounds = 0;
int gTables = 1;
// Funksjoner:
void addPlayer();
void writePlayer();
void editPlayer();
int playerWin();
Player* findPlayer(const string name);
Player* findPlayerID(const int ID);
void enterResults();
void newTournament();
void viewPlayers();
void skrivMeny();
void writeToFile();
void readFromFile();
void writeToFileTournament();
void readFromFileTournament();
void amntTables();
void viewResults();
......@@ -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
......@@ -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
*
......@@ -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()
<< "\t2 - Add player\n"
<< "\t3 - Enter results\n"
<< "\t4 - Edit player\n"
<< "\t5 - view player list\n"
<< "\t6 - view results\n"
<< "\t5 - View player list\n"
<< "\t6 - View results\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
*
......@@ -541,13 +326,3 @@ void amntTables(){
cout<<"How many tables do you have?\n";
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 @@
using namespace std;
void newPlayer(int id);
int searchByID(int id, int list);
int searchByName(string name);
void sortRanking();
void printRanking();
void newPlayer(int id); //+
int searchByID(int id, int list); //+
int searchByName(string name); //+
void sortRanking(); //+
void printRanking(); //+
void viewPlayers(); //+
#endif //CHECKMATE_FUNCTIONS_H
......@@ -15,19 +15,21 @@ extern vector<Player*>playerList;
void newPlayer(int id)
{
if (searchByID(id,1)!=-1)
Player* newPlayer;
do
{
cout << endl << "A player with this ID already exists, please try again!" << endl;
}
cout << "\n\tFull name: ";
getline(cin, player->name);
player->rating = lesInt("\n\tRating: ", 1, 3000);
player->playerID = lesInt("\n\tPlayer ID: ", 1, 10000);
cout << "\n\tClub: ";
getline(cin, player->club);
id = lesInt("Please offer a valid ID\n",0,10000);
}while(searchByID(id,1)!=-1);
newPlayer = new Player;
newPlayer->playerID = id;
cout << endl << "Last name of player " << id << endl;
getline(cin, newPlayer->lastName);
cout << endl << "First name of player " << id << endl;
getline(cin, newPlayer->firstName);
newPlayer->rating = lesInt("\nRating: ", 1, 3000);
cout << endl << "Club: ";
getline(cin, newPlayer->club);
playerList.push_back(newPlayer);
}
\ No newline at end of file
......@@ -54,6 +54,14 @@ void Player::printPlayer()
<< "Current rank in tournament: " << currentRank(playerID) << endl;
}
void Player::printPlayerTable()
{
cout << endl << lastName << "\t"
<< playerID << "\t"
<< club << "\t"
<< rating;
}
void Player::calculateScore()
{
float tempScore = 0.0f;
......
......@@ -34,6 +34,7 @@ class Player
void setPlayer(string nm, string , string clbNm, int rtng, int plyrID, float score); //+
virtual void printPlayer(); //+
virtual void printPlayerTable();
virtual void calculateScore(); //+
virtual void writeToFilePlayer(ofstream &out);//-
virtual void readFromFilePlayer(ifstream &in);//-
......
......@@ -11,7 +11,6 @@ int searchByID(int id, int list)
vector<Player*> listToUse;
if (list == 1)
{
listToUse = playerList;
}
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