Skip to content
Snippets Groups Projects
Commit b3cdc68d authored by lokesvelland's avatar lokesvelland
Browse files

fikset på players og slikt

parent 7ef3b109
Branches
No related tags found
No related merge requests found
......@@ -7,8 +7,8 @@
#include "LesData2.h"
using namespace std;
class Player {
class Player
{
private:
string name;
int rating;
......@@ -19,13 +19,25 @@ private:
float score;
public:
void newPlayer();
bool playerName(const string pname) { if(pname == name) {
void newPlayer(Player *player);
bool playerName(const string pname)
{
if (pname == name)
{
return true;
} else return false; };
bool playerId(const int ID) { if(ID == playerID) {
}
else
return false;
};
bool playerId(const int ID)
{
if (ID == playerID)
{
return true;
} else return false; };
}
else
return false;
};
void writePlayer() const;
void updatePlayer();
int playerWin() { wins = 0; };
......@@ -36,11 +48,13 @@ public:
void readFromFilePlayer(ifstream &in);
};
class Tournament {
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();
......@@ -74,8 +88,8 @@ void skrivMeny();
void writeToFile();
void readFromFile();
int main() {
int main()
{
char command;
// readFromFile();
......@@ -85,8 +99,10 @@ char command;
skrivMeny();
command = lesChar("\nKommando");
while(command != 'Q') {
switch(command) {
while (command != 'Q')
{
switch (command)
{
case 1: newTournament(); break;
case 2: addPlayer(); break;
case 3: enterResults(); break;
......@@ -106,29 +122,33 @@ char command;
/**
* Reads new player
*/
void Player::newPlayer() {
cout << "\n\tFull name: "; getline(cin, player->name);
void Player::newPlayer(Player *player)
{
cout << "\n\tFull name: ";
getline(cin, player->name);
player->rating = lesInt("\n\tRating: ", 1, 10);
player->playerID = lesInt("\n\tPlayer ID: ", 1, 1000);
cout << "\n\tClub: "; getline(cin, player->club);
cout << "\n\tClub: ";
getline(cin, player->club);
}
/**
* Writes out all info on player
*
*/
void Player::writePlayer() const {
void Player::writePlayer() const
{
cout << "\n" << name << '\n'
cout << "\n"
<< name << '\n'
<< rating << '\n'
<< playerID << '\n'
<< club << '\n'
<< wins << "\n\n";
}
/**
......@@ -137,11 +157,16 @@ void Player::writePlayer() const {
* @param name
* @return Player*
*/
Player* findPlayer(const string name) {
for(int i = 0; i < gPlayers.size(); i++) {
if(gPlayers[i]->playerName(name) == true) {
Player *findPlayer(const string name)
{
for (int i = 0; i < gPlayers.size(); i++)
{
if (gPlayers[i]->playerName(name) == true)
{
return gPlayers[i];
} else return nullptr;
}
else
return nullptr;
}
}
......@@ -151,29 +176,34 @@ Player* findPlayer(const string name) {
* @param ID
* @return Player*
*/
Player* findPlayerID(const int ID) {
for(int i = 0; i < gPlayers.size(); i++) {
if(gPlayers[i]->playerId(ID) == true) {
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;
}
else
return nullptr;
}
}
/**
* Calculates the score for a single player
*
*/
void Player::calculateScore(){
void Player::calculateScore()
{
for(int i=1; i<wins; i++){
for (int i = 1; i < wins; i++)
{
score += 1;
}
for(int i=1; i<draws; i++){
for (int i = 1; i < draws; i++)
{
score += 0.5;
}
}
/**
......@@ -181,13 +211,15 @@ for(int i=1; i<draws; i++){
*
* @param in - the file being read from
*/
void Player::readFromFilePlayer(ifstream & in){
void Player::readFromFilePlayer(ifstream &in)
{
getline(in, name);
in >> rating; in.ignore();
in >> playerID; in.ignore();
in >> rating;
in.ignore();
in >> playerID;
in.ignore();
getline(in, club);
}
/**
......@@ -195,7 +227,8 @@ getline(in, club);
*
* @param out - the file being written to
*/
void Player::writeToFilePlayer(ofstream & out){
void Player::writeToFilePlayer(ofstream &out)
{
out << setw(STRLEN) << name
<< setw(STRLEN) << rating
......@@ -208,11 +241,13 @@ void Player::writeToFilePlayer(ofstream & out){
* Writes out all data for one tournament round
*
*/
void Tournament::writeTournament(){
void Tournament::writeTournament()
{
cout << setw(STRLEN) << "White player"
<< setw(STRLEN) << "Black player"
<< setw(STRLEN) << "Table number\n";
for(int i=0; i>gTables; i++){
for (int i = 0; i > gTables; i++)
{
cout << setw(STRLEN) << player1[i]
<< setw(STRLEN) << player2[i]
<< setw(STRLEN) << tableNr[i];
......@@ -223,9 +258,11 @@ void Tournament::writeTournament(){
* Fills the tableNr array with numbers [1,2,3,...,gTables]
*
*/
void Tournament::fillTableArray(){
void Tournament::fillTableArray()
{
tableNr[0] = 1;
for(int i=1; i<gTables; i++){
for (int i = 1; i < gTables; i++)
{
tableNr[i] = tableNr[i - 1] + 1;
}
}
......@@ -235,9 +272,11 @@ void Tournament::fillTableArray(){
*
* @param out - the file being written to
*/
void Tournament::writeToFileTournament(ofstream & out){
void Tournament::writeToFileTournament(ofstream &out)
{
for(int i=0; i<gTables; i++){
for (int i = 0; i < gTables; i++)
{
out << setw(STRLEN) << player1[i]
<< setw(STRLEN) << player2[i]
<< setw(STRLEN) << tableNr[i]
......@@ -250,14 +289,18 @@ for(int i=0; i<gTables; i++){
*
* @param in - file being read from
*/
void Tournament::readFromFileTournament(ifstream & in){
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
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
}
}
/**
......@@ -270,7 +313,8 @@ for(int i=0; i<gTables; i++){
* Enter results for a specific round
*
*/
void enterResults() {
void enterResults()
{
int round;
int table;
......@@ -286,28 +330,27 @@ table = lesInt("Table:",0,(gTables+1));
cout << "\nEnter result for " << /*name of player*/ "(W(in), L(ose), D(raw)):";
result = lesChar("Result:");
if(result == 'W'){
if (result == 'W')
{
//+1 to wins int of player ]
}
else if(result == 'D'){
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(){
void viewResults()
{
for(int i=0; i < gPlayers.size();i++){
for (int i = 0; i < gPlayers.size(); i++)
{
gPlayers[i]->calculateScore();
}
}
/**
......@@ -315,7 +358,8 @@ for(int i=0; i < gPlayers.size();i++){
* @see writeTournament()
* @see fillTableArray()
*/
void newTournament() {
void newTournament()
{
gRounds++;
}
......@@ -325,11 +369,12 @@ void newTournament() {
*
* @see - Player::newPlayer()
*/
void addPlayer(){
void addPlayer()
{
Player *newPlayer;
newPlayer = new Player;
newPlayer->newPlayer();
newPlayer->newPlayer(newPlayer);
gPlayers.push_back(newPlayer);
cout << "\n\nPlayer added!\n\n";
......@@ -340,52 +385,69 @@ void addPlayer(){
*
* @see - Player::updatePlayer
*/
void editPlayer(){
int choise;
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";
if(choise == 1){
choise = 0;
// 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){
cout<< "\n\tName: "; getline(cin,search);
if(findPlayer(search) == nullptr) {
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{
}
else
{ // if player found, write out info
cout << "\n\tPlayer found!\n";
findPlayer(search)->writePlayer();
}
}
else if(choise == 2){
findPlayer(search)->ID = lesInt("\n\tPlayerID: ",1, 1000);
if(findPlayerID(ID) == nullptr) {
} // if choose playerID
else if (choise == 2)
{
ID = lesInt("\n\tPlayerID: ", 1, 1000);
if (findPlayerID(ID) == nullptr)
{
cout << "\n\tPlayerID not recognized!";
} else {
}
else
{
cout << "\n\tPlayer found!\n";
findPlayerID(ID)->writePlayer();
}
}
cout << "\n\n";
}
else if(choise == 2) {
else if (choise == 2)
{
viewPlayers();
}
}
/**
*
*
*/
void viewPlayers() {
void viewPlayers()
{
for(int i = 0; i < gPlayers.size(); i++) {
for (int i = 0; i < gPlayers.size(); i++)
{
cout << gPlayers[i] << "\n\n";
}
}
/**
......@@ -394,14 +456,15 @@ void viewPlayers() {
* @return true
* @return false
*/
bool checkPlayers() {
bool checkPlayers()
{
}
/**
* writes the programs menu options
*/
void skrivMeny() {
void skrivMeny()
{
cout << "\nMenu: \n"
<< "\t1 - Generate tournament set-up\n"
......@@ -411,17 +474,18 @@ cout << "\nMenu: \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(){
void writeToFile()
{
ofstream outfile("PlayerList.dta");
if(outfile){
if (outfile)
{
for (int i = 0; i < gPlayers.size(); i++)
gPlayers[i]->writeToFilePlayer(outfile);
......@@ -429,19 +493,21 @@ if(outfile){
}
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(){
void readFromFile()
{
Player *playerNew;
ifstream infile("PlayerList.dta");
if(infile){
while(!infile.eof()){
if (infile)
{
while (!infile.eof())
{
playerNew = new Player();
playerNew->readFromFilePlayer(infile);
gPlayers.push_back(playerNew);
......@@ -451,17 +517,18 @@ if(infile){
}
else
cout << "\nCouldn't find PlayerList.dta!\n";
}
/**
* Writes all data about the tournament rounds to file
*/
void writeToFileTournament(){
void writeToFileTournament()
{
ofstream outfile("tournamentData.dta");
if(outfile){
if (outfile)
{
for (int i = 0; i < gTournaments.size(); i++)
gTournaments[i]->writeToFileTournament(outfile);
......@@ -469,19 +536,21 @@ if(outfile){
}
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(){
void readFromFileTournament()
{
Tournament *tournamentNew;
ifstream infile("tournamentData.dta");
if(infile){
while(!infile.eof()){
if (infile)
{
while (!infile.eof())
{
tournamentNew = new Tournament();
tournamentNew->readFromFileTournament(infile);
gTournaments.push_back(tournamentNew);
......@@ -491,5 +560,4 @@ if(infile){
}
else
cout << "\nCouldn't find tournamentData.dta!\n";
}
\ 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