Skip to content
Snippets Groups Projects
Commit f34d6cfb authored by aurora's avatar aurora
Browse files

File-functions, tournament class, pseudocode

parent ebcc2bf6
Branches
No related tags found
No related merge requests found
...@@ -14,7 +14,9 @@ private: ...@@ -14,7 +14,9 @@ private:
int rating; int rating;
int playerID; int playerID;
string club; string club;
int wins; int wins,
draws;
float score;
public: public:
void newPlayer(); void newPlayer();
...@@ -27,17 +29,34 @@ public: ...@@ -27,17 +29,34 @@ public:
void writePlayer() const; void writePlayer() const;
void updatePlayer(); void updatePlayer();
int playerWin() {wins = 0;}; int playerWin() {wins = 0;};
int playerDraws() {draws = 0;};
float playerScore() {score = 0;};
void calculateScore();
void writeToFilePlayer(ofstream & out); void writeToFilePlayer(ofstream & out);
void readFromFilePlayer(ifstream & in); void readFromFilePlayer(ifstream & in);
}; };
class Tournament {
private:
int tableNr[gTables]; //these will only work if gTables is a const?
string player1[gTables],
player2[gTables];
public:
void writeTournament();
void fillTableArray();
void writeToFileTournament(ofstream & out);
void readFromFileTournament(ifstream & in);
};
// vectorer: // vectorer:
// - hvem spiller på hvilket bord // - hvem spiller på hvilket bord
vector <Player *> gPlayers; vector <Player *> gPlayers;
vector <Tournament *> gTournaments;
// globale verdier: // globale verdier:
const int STRLEN = 80; const int STRLEN = 80;
// - tables (variable) int gRounds = 0;
int gTables = 1;
// Funksjoner: // Funksjoner:
void newPlayer(); void newPlayer();
...@@ -46,7 +65,8 @@ void updatePlayer(); ...@@ -46,7 +65,8 @@ void updatePlayer();
int playerWin(); int playerWin();
Player* findPlayer(const string name); Player* findPlayer(const string name);
Player* findPlayerID(const int ID); Player* findPlayerID(const int ID);
void results(); void enterResults();
void viewResults();
void newTournament(); void newTournament();
void viewPlayers(); void viewPlayers();
bool checkPlayers(); bool checkPlayers();
...@@ -60,6 +80,8 @@ int main() { ...@@ -60,6 +80,8 @@ int main() {
char command; char command;
// readFromFile(); // readFromFile();
// ask for amount of tables
skrivMeny(); skrivMeny();
command = lesChar("\nKommando"); command = lesChar("\nKommando");
...@@ -67,7 +89,7 @@ char command; ...@@ -67,7 +89,7 @@ char command;
switch(command) { switch(command) {
case 1: newTournament(); break; case 1: newTournament(); break;
case 2: newPlayer(); break; case 2: newPlayer(); break;
case 3: results(); break; case 3: enterResults(); break;
case 4: updatePlayer(); break; case 4: updatePlayer(); break;
case 5: viewPlayers(); break; case 5: viewPlayers(); break;
default: skrivMeny(); break; default: skrivMeny(); break;
...@@ -193,13 +215,25 @@ void Player::updatePlayer() { ...@@ -193,13 +215,25 @@ void Player::updatePlayer() {
}
/**
* 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;
} }
int Player::playerWin() {
} }
/** /**
* Read one player from file
* *
* @param in - the file being read from * @param in - the file being read from
*/ */
...@@ -213,6 +247,7 @@ getline(in, club); ...@@ -213,6 +247,7 @@ getline(in, club);
} }
/** /**
* Write one player to file
* *
* @param out - the file being written to * @param out - the file being written to
*/ */
...@@ -225,14 +260,116 @@ void Player::writeToFilePlayer(ofstream & out){ ...@@ -225,14 +260,116 @@ void Player::writeToFilePlayer(ofstream & out){
<<"\n"; <<"\n";
} }
void results() { /**
* 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];
}
}
/**
* 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<< setw(STRLEN) <<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
}
}
/**
* 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
}
}
/**
* Calculates and prints out scores for all players in descending order
*
* @see - calculateScore()
*/
void viewResults(){
for(int i=0; i < gPlayers.size();i++){
gPlayers[i]->calculateScore();
}
}
/**
* Create a new tournament, with black and white players and what table they're playing at
* @see writeTournament()
* @see fillTableArray()
*/
void newTournament() { void newTournament() {
gRounds++;
} }
/**
*
*
*/
void viewPlayers() { void viewPlayers() {
for(int i = 0; i < gPlayers.size(); i++) { for(int i = 0; i < gPlayers.size(); i++) {
...@@ -241,6 +378,12 @@ void viewPlayers() { ...@@ -241,6 +378,12 @@ void viewPlayers() {
} }
/**
* Checks if
*
* @return true
* @return false
*/
bool checkPlayers() { bool checkPlayers() {
} }
...@@ -299,3 +442,43 @@ else ...@@ -299,3 +442,43 @@ else
cout<<"\nCouldn't find PlayerList.dta!\n"; 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 readFromFile(){
Tournament* tournamentNew;
ifstream infile("tournamentData.dta");
if(infile){
while(!infile.eof()){
tournamentNew = new Tournament();
tournamentNew ->readFromFileTournament(infile);
gTournaments.push_back(tournamentNew);
}
infile.close();
}
else
cout<<"\nCouldn't find tournamentData.dta!\n";
}
\ No newline at end of file
  • Maintainer

    Added:

    • Reading to/from tournamentData.dta
    • Added tournament class and some of its data
    • Added global variables gRounds and gTables
    • Added vector gTournaments
    • Added pseudocode or things that are necessary to include in certain functions
    • Added proper commenting
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment