Skip to content
Snippets Groups Projects
Commit 0b9975ff authored by TheHresvelgian's avatar TheHresvelgian
Browse files

Organizing code

parent 94cc3750
No related branches found
No related tags found
No related merge requests found
......@@ -8,8 +8,8 @@
using namespace std;
void newPlayer(int id);
void searchByID(int id);
void searchByName(string name);
int searchByID(int id, int list);
int searchByName(string name);
void sortRanking();
void printRanking();
......
......@@ -16,12 +16,17 @@ extern vector<Player*>playerList;
void newPlayer(int id)
{
if (searchByID(id,1)!=-1)
{
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, 10);
player->rating = lesInt("\n\tRating: ", 1, 3000);
player->playerID = lesInt("\n\tPlayer ID: ", 1, 1000);
player->playerID = lesInt("\n\tPlayer ID: ", 1, 10000);
cout << "\n\tClub: ";
getline(cin, player->club);
......
......@@ -8,10 +8,12 @@
#include "player.h"
#include "LesData2.h"
#include "functions.h"
using namespace std;
extern vector<Player*>playerList;
extern vector<Player*>sortedRankings;
Player::Player()
{
......@@ -96,6 +98,25 @@ void Player::readFromFilePlayer(ifstream &in)
int Player::currentRank(int id)
{
int index;
sortRanking();
index = searchByID(id,2);
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;
}
\ No newline at end of file
......@@ -32,11 +32,11 @@ class Player
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 calculateScore();
virtual void writeToFilePlayer(ofstream &out);
virtual void readFromFilePlayer(ifstream &in);
int currentRank(int id);
void setPlayer(string nm, string , string clbNm, int rtng, int plyrID, float score); //+
virtual void printPlayer(); //+
virtual void calculateScore(); //+
virtual void writeToFilePlayer(ofstream &out);//-
virtual void readFromFilePlayer(ifstream &in);//-
int currentRank(int id);//+
};
#endif //CHECKMATE_PLAYER_H
#include <iostream> // cout, cin
#include <string> // STRING-KLASSEN
#include <iostream>
#include "player.h"
#include "LesData2.h"
#include "functions.h"
void searchByID(int id)
{
extern vector<Player*> playerList;
extern vector<Player*> sortedRankings;
int searchByID(int id, int list)
{
vector<Player*> listToUse;
if (list == 1)
{
listToUse = playerList;
}
else if (list == 2)
{
listToUse = sortedRankings;
}
else
{
cout << endl << "Unexpected error, please contact publisher!" << endl;
return -1;
}
for (int i = 0; i < listToUse.size(); i++)
{
if (listToUse[i]->playerID == id)
{
return i;
}
}
return -1;
}
\ No newline at end of file
......@@ -2,10 +2,62 @@
#include <string> // STRING-KLASSEN
#include "player.h"
#include "LesData2.h"
#include "functions.h"
void searchByName(string name)
extern vector<Player*> playerList;
int searchByName(string name)
{
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 + f;
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment