Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • master
  • revert-b11f7b52
2 results

Target

Select target project
  • magnuswj/prog-1004-2022-group-4
  • alexholt/dcst1008
  • nilstesd/dcst1008
3 results
Select Git revision
  • master
1 result
Show changes
#include <iostream> // cout, cin
#include <string> // string
#include <vector> // vector
#include <iomanip> // setw
#include <fstream> // ifstream, ofstream
#include "LesData3.h"
#include "player.h"
#include "Tournament.h"
#include "functions.h"
using namespace std;
extern vector<Player*> playerList;
/**
* edit info about a player
*/
void editPlayer()
{
if (playerList.size()==0)
{
cout << endl << "No player's exist.";
return;
}
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,playerList);
if (index == -1)
{
cout << endl << "No player with this ID exists, please try again!" << endl;
}
}while(searchByID(id,playerList)==-1);
playerToEdit = playerList[index];
break;
default:
cout << endl << "Invalid input, please try again";
break;
}
}while(playerToEdit == nullptr);
cout << endl << "Player found!";
playerToEdit->printPlayer();
cout << endl;
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();
edit=false;
}
else if(choice==2)
{
edit=false;
}
}
}while(edit);
}
\ No newline at end of file
/**
* Declaration of all functions
*/
#ifndef CHECKMATE_FUNCTIONS_H
#define CHECKMATE_FUNCTIONS_H
using namespace std;
void newPlayer(int id); //+
int searchByID(int id, vector<Player*> list); //+
int searchByName(string name); //+
void sortRanking(); //+
void printRanking(); //+
void viewPlayers(); //+
void editPlayer(); //+
void startNewTournament();//+
void deletePlayer();//-
void readFromFile();
void readFromFileTournament();
void writeToFile();
void writeToFileTournament();
#endif //CHECKMATE_FUNCTIONS_H
/**
* Reads new player
*/
#include <iostream> // cout, cin
#include <string> // STRING-KLASSEN
#include "player.h"
#include "LesData3.h"
#include "functions.h"
using namespace std;
extern vector<Player*>playerList;
void newPlayer(int id)
{
Player* newPlayer;
if (searchByID(id,playerList)!=-1)
{
do
{
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,playerList)!=-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: ";
newPlayer->white=0;
getline(cin, newPlayer->club);
playerList.push_back(newPlayer);
}
\ No newline at end of file
//
// Created by sigru on 25.04.2022.
//
#include <string> // string
#include <vector> // vector
#include <fstream> // ifstream, ofstream
#include <iostream> // cout, cin
#include <iomanip> // setw
#include "player.h"
#include "LesData3.h"
#include "functions.h"
using namespace std;
extern vector<Player*>playerList;
extern vector<Player*>sortedRankings;
Player::Player()
{
firstName = "";
lastName = "";
club = "";
rating = 0;
playerID = 0;
score = 0.0f;
}
Player::Player(string givenName, string surName, string clubName, int rating, int ID, float currentScore)
{
setPlayer(givenName,surName,clubName,rating,ID, currentScore);
}
void Player::setPlayer(string nm, string srNm, string clbNm, int rtng, int plyrID, float scr)
{
firstName = nm;
lastName = srNm;
club = clbNm;
rating = rtng;
playerID = plyrID;
score = scr;
}
/**
* Prints the player
*
* @see currentRank(playerID);
*/
void Player::printPlayer()
{
cout << endl << lastName << ", " << firstName
<< endl << "Club: " << club << endl
<< "Rating: " << rating << endl
<< "ID: " << playerID << endl
<< "Current score: " << score << endl;
if (results.size()>0)
{
cout << "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;
for (int i = 0; i < results.size(); ++i)
{
tempScore += results[i];
}
score = tempScore / 2;
}
/**
* Write one player to file
*
* @param out - the file being written to
*/
void Player::writeToFilePlayer(ofstream &out)
{
out << firstName << '\n'
<< lastName << '\n'
<< rating << '\n'
<< playerID << '\n'
<< club << '\n'
<< white << '\n';
for(int i = 0; i < results.size(); i++)
out << results[i];
out << '\n';
for(int i = 0; i < playedAgainst.size(); i++)
out << playedAgainst[i];
}
/**
* Read one player from file
*
* @param in - the file being read from
*/
void Player::readFromFilePlayer(ifstream &in)
{
int inn;
getline(in, firstName);
getline(in, lastName);
in >> rating; in.ignore();
in >> playerID; in.ignore();
getline(in, club);
in >> white; in.ignore();
while(!'\n') {
in >> inn;
results.push_back(inn);
}
while(!in.eof()) {
in >> inn;
playedAgainst.push_back(inn);
}
}
int Player::currentRank(int id)
{
int index;
sortRanking();
index = searchByID(id,sortedRankings);
if (index = -1)
{
cout << endl << "Error: no player found!" << endl;
return -1;
}
int currentScore = sortedRankings[0]->score;
int currentPlace = 1;
for (int i = 0; i < index; ++i)
{
if (currentScore > sortedRankings[i]->score)
{
currentScore = sortedRankings[i]->score;
currentPlace++;
}
}
return currentPlace;
}
bool Player::hasPlayedAgainst(int id)
{
for (int i = 0; i < playedAgainst.size(); i++)
{
if (playedAgainst[i] == id)
{
return true;
}
}
return false;
}
//
// Created by sigru on 25.04.2022.
//
#ifndef CHECKMATE_PLAYER_H
#define CHECKMATE_PLAYER_H
#include <string> // string
#include <vector> // vector
#include <fstream> // ifstream, ofstream
using namespace std;
class Player
{
public:
string firstName;
string lastName;
string club;
int rating;
int playerID;
int white;
float score;
vector <int> results; //stores the results of each match, 0 for loss, 1 for draw and 2 for win
vector <int> playedAgainst; //has every person they have played against
Player();
Player(string name, string surName, string clubName, int rating, int playerID, float score);
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);//-
bool hasPlayedAgainst(int id);//-
int currentRank(int id);//+
};
#endif //CHECKMATE_PLAYER_H
#include <string> // string
#include <vector> // vector
#include <fstream> // ifstream, ofstream
#include <iostream> // cout, cin
#include "player.h"
#include "LesData3.h"
#include "functions.h"
using namespace std;
extern vector<Player*>playerList;
extern vector<Player*> sortedRankings;
void printRanking()
{
if (playerList.size()==0)
{
cout << endl << "No player's exist.";
return;
}
sortRanking();
float currentScoreTP = sortedRankings[0]->score;
int currentPlace = 1;
for (int i = 0; i < sortedRankings.size(); i++)
{
if (currentScoreTP > sortedRankings[i]->score)
{
currentScoreTP = sortedRankings[i]->score;
currentPlace++;
}
cout << endl << currentPlace
<< "\t" << sortedRankings[i]->score << "pts"
<< "\t" << sortedRankings[i]->lastName
<< ", " << sortedRankings[i]->firstName;
}
}
\ No newline at end of file
#include <iostream> // cout, cin
#include <string> // string
#include <vector> // vector
#include <iomanip> // setw
#include <fstream> // ifstream, ofstream
#include "LesData3.h"
#include "player.h"
#include "Tournament.h"
#include "functions.h"
using namespace std;
extern vector<Tournament *> gTournaments;
extern vector<Player*> playerList;
extern vector<Player*> sortedRankings;
void readFromFile() {
ifstream inFile("PlayerList.dta"); // File object to read from
Player* newPlayer; // Helping pointer to new object
if(inFile) {
cout << "\n\n\t\t.... READING FROM FILE: PLAYERLIST.DTA ....\n\n\n";
while(!inFile.eof()) { // while NOT end of file do this
newPlayer = new Player;
newPlayer->readFromFilePlayer(inFile);
playerList.push_back(newPlayer);
}
inFile.close();
} else {
cout << "\n\n\t\t.... CAN NOT FIND FILE: PLAYERLIST.DTA ....\n\n\n";
}
}
void readFromFileTournament() {
Tournament *tournamentNew; // Helping pointer to new object
ifstream infile("tournamentData.dta"); // File object to read from
if (infile)
{
cout << "\n\n\t\t.... READING FROM FILE: TOURNAMENTDATA.DTA ....\n\n\n";
while (!infile.eof()) // while NOT end of file do this
{
tournamentNew = new Tournament();
tournamentNew->readFromFileTournaments(infile);
gTournaments.push_back(tournamentNew);
}
infile.close();
} else {
cout << "\n\n\t\t.... CAN NOT FIND FILE: TOURNAMENTDATA.DTA ....\n\n\n";
}
infile.close();
}
\ No newline at end of file
#include <iostream>
#include "player.h"
#include "functions.h"
extern vector<Player*> playerList;
extern vector<Player*> sortedRankings;
int searchByID(int id, vector<Player*> list)
{
for (int i = 0; i < list.size(); i++)
{
if (list[i]->playerID == id)
{
return i;
}
}
return -1;
}
\ No newline at end of file
#include <iostream> // cout, cin
#include <string> // STRING-KLASSEN
#include "player.h"
#include "functions.h"
extern vector<Player*> playerList;
int searchByName(string name)
{
vector<Player*> searchlist;
string playerName;
int findSize = 0;
int finds[10];
int answer;
int f;
int l;
for (int i = 0; i < name.length(); i++)
{
name[i] = tolower(name[i]);
}
for (f = 0; f < playerList.size(); f++)
{
playerName = playerList.at(f)->firstName;
for (int j = 0; j < playerList.at(f)->firstName.length(); j++)
{
playerName[j] = tolower(playerName[j]);
}
if (playerName.find(name) == 0)
{
finds[findSize] = f;
findSize += 1;
}
}
for (l = 0; l < playerList.size(); l++)
{
playerName = playerList.at(l)->lastName;
for (int j = 0; j < playerList.at(l)->lastName.length(); j++)
{
playerName[j] = tolower(playerName[j]);
}
if (playerName.find(name) == 0)
{
finds[findSize] = l;
findSize += 1;
}
}
if(findSize > 1)
{
cout << endl << "Choose one of these players " << endl;
for (int i = 0; i < findSize; i++)
{
cout << i+1 << ": " << playerList.at(finds[i])->lastName << ", "
<< playerList.at(finds[i])->firstName << endl;
}
cin >> answer;
return finds[answer - 1];
}
else if(findSize == 1)
{
return finds[0];
}
else
{
return -1;
}
}
\ No newline at end of file
#include <vector> // vector
#include "player.h"
#include "functions.h"
using namespace std;
extern vector<Player*>playerList;
extern vector<Player*> sortedRankings;
void sortRanking()
{
sortedRankings.clear();
float currentHigh;
vector<Player*> listUnsorted = playerList;
vector<Player*> leadingPlayers;
vector<int> listIDs;
do
{
currentHigh = 0;
for (int i = 0; i < listUnsorted.size(); i++)
{
listUnsorted[i]->calculateScore();
if (listUnsorted[i]->score > currentHigh)
{
leadingPlayers.clear();
listIDs.clear();
leadingPlayers.push_back(listUnsorted[i]);
listIDs.push_back(listUnsorted[i]->playerID);
currentHigh = leadingPlayers[0]->score;
}
else if (listUnsorted[i]->score == currentHigh)
{
leadingPlayers.push_back(listUnsorted[i]);
listIDs.push_back(listUnsorted[i]->playerID);
}
}
for (int i = 0; i < leadingPlayers.size(); i++)
{
sortedRankings.push_back(leadingPlayers[i]);
}
leadingPlayers.clear();
for (int i = 0; i < listIDs.size(); i++)
{
listUnsorted[searchByID(listIDs[i],listUnsorted)] = listUnsorted[listUnsorted.size()-1];
listUnsorted.pop_back();
}
listIDs.clear();
} while (listUnsorted.size() > 0);
}
\ No newline at end of file
//
// 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 "LesData3.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;
if (gTournaments.size()!=0)
{
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");
}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;
int maxRounds = newTournament->nrOfTables*2-1;
newTournament->nrOfRounds = lesInt("\nHow many rounds should the tournament have?\n",1,maxRounds);
newTournament->finished=false;
gTournaments.push_back(newTournament);
}
\ No newline at end of file
nr of tables
current Round
nr of rounds
white vector
black vector
finished
\ No newline at end of file
#include <iostream> // cout, cin
#include <vector> // vector
#include "player.h"
#include "Tournament.h"
using namespace std;
extern vector<Player*> playerList;
/**
* Prints a list of all players with identification
*/
void viewPlayers()
{
if (playerList.size()==0)
{
cout << endl << "No player's exist.";
return;
}
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
#include <iostream> // cout, cin
#include <string> // string
#include <vector> // vector
#include <iomanip> // setw
#include <fstream> // ifstream, ofstream
#include "LesData3.h"
#include "player.h"
#include "Tournament.h"
#include "functions.h"
using namespace std;
extern vector<Tournament *> gTournaments;
extern vector<Player*> playerList;
extern vector<Player*> sortedRankings;
void writeToFile() {
ofstream outFile("PlayerList.dta");
if(outFile) {
for (int i = 0; i < playerList.size(); i++) {
playerList[i]->writeToFilePlayer(outFile);
}
outFile.close();
}
}
void writeToFileTournament() {
ofstream outfile("tournamentData.dta");
if (outfile)
{
for (int i = 0; i < gTournaments.size(); i++)
gTournaments[i]->writeToFileTournaments(outfile);
outfile.close();
}
}
File added