#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <vector>
#include <sstream>
using namespace std;
//functions for player class
class Player
{
public:
string name;
int age, perf, number;
string position;
Player(string name, int age, int perf, int number, string position)
{
this->name = name;
this->age = age;
this->perf = perf;
this->number = number;
this->position = position;
}
void setName(string name)
{
this->name = name;
}
string getName()
{
return this->name;
}
void setNumber(int number)
{
this->number = number;
}
int getNumber()
{
return this->number;
}
void setPosition(char position)
{
this->position = position;
}
string getPosition()
{
return this->position;
}
void setAge(int age)
{
this->age = age;
}
int getAge()
{
return this->age;
}
void setPerformance(int perf)
{
this->perf = perf;
}
int getPerformance()
{
return this->perf;
}
};
class Team
{
public:
vector<Player> teamPlayers;
vector<Team> teamList;
string teamName, shortName, coach;
float teamPerf;
string teamLineUp;
float DPO , OSPO , FPO;
Team(vector<Player> teamPlayers, string teamName, string coach, string teamLineUp)
{
this->teamPlayers = teamPlayers;
this->teamLineUp = teamLineUp;
this->teamName = teamName;
// this->shortName = shortName;
this->coach = coach;
this->teamLineUp = teamLineUp;
this->teamPerf = calculateTeamPerf(teamPlayers);
this->DPO = calculateTeamDPO(teamList,teamLineUp);
}
void setTeamPlayers(vector<Player> teamPlayers)
{
this->teamPlayers = teamPlayers;
}
vector<Player> getTeamPlayers()
{
return this->teamPlayers;
}
void setLineUp(string teamLineUp)
{
this->teamLineUp = teamLineUp;
}
string getTeamLineUp()
{
return this->teamLineUp;
}
void setTeamPerformance(float teamPerf)
{
this->teamPerf = teamPerf;
}
float getTeamPerformance()
{
return this->teamPerf;
}
void setTeamName(string teamName)
{
this->teamName = teamName;
}
string getTeamName()
{
return this->teamName;
}
void setShortName(string shortName)
{
this->shortName = shortName;
}
string getShortName()
{
return this->shortName;
}
void setCoach(string coach)
{
this->coach = coach;
}
string getCoach()
{
return this->coach;
}
void setTeamDPO(float DPO)
{
this->DPO = DPO;
}
float getTeamDPO()
{
return this->DPO;
}
float calculateTeamPerf(vector<Player> teamPlayers)
{
float sumPerformance = 0;
for (int i = 0; i < this->teamPlayers.size(); i++)
{
sumPerformance += teamPlayers[i].getPerformance();
}
return sumPerformance / teamPlayers.size();
}
float calculateTeamDPO(vector<Team>teamList,string teamLineUp)
{
string position;
size_t pos = 1;
float DPO = 0;
int midPlayerNumber = 0;
int forwPlayerNumber = 0;
int defPlayerNumber = 0;
for (int i = 0; i < 3; i++)
{
position = teamLineUp.substr(0, pos);
stringstream posCount(position);
if (i == 0)
{
posCount >> defPlayerNumber;
}
else if (i == 1)
{
posCount >> midPlayerNumber;
}
teamLineUp.erase(0, pos + 1);
if (teamLineUp.size() < 1)
{
posCount >> forwPlayerNumber;
}
}
for(int i = 0; i <defPlayerNumber ; i++)
{
DPO += teamList[i].getTeamPlayers()[i].getPerformance();
}
DPO = DPO / defPlayerNumber;
return DPO;
}
private:
};
//method signatures
void createPlayers();
void playerCreator(vector<string> nameList);
int setRandomAge();
int setRandomNumber();
int setRandomPerformance();
//functions for team class
vector<string> createTeamNames();
void teamCreator(vector<string> teamList);
vector<Player> createTeamPlayers(string teamLineUp);
string setRandomPos();
string createCoach();
bool controlTeamPlayers(vector<Player> playerList, string teamLineUp);
bool formationSumController(string teamLineUp);
//functions for league class
void playMatches(vector<Team> teamList);
vector<Player> playerList;
vector<Team> teamList;
int main()
{
vector<string> teamNameList;
srand(time(0));
createPlayers();
teamNameList = createTeamNames();
teamCreator(teamNameList);
playMatches(teamList);
return 0;
}
//returns the string list of players
void createPlayers()
{
vector<string> nameList;
string nameFirst;
string sum1, sum2;
string line1, line2;
int i;
int random1 = 0;
int random2 = 0;
for (i = 0; i < 198; i++)
{
random1 = rand() % 2000;
random2 = rand() % 2000;
int numOfLines1 = 0;
int numOfLines2 = 0;
ifstream File1("isim.txt");
ifstream File2("soyisim.txt");
while (getline(File1, line1))
{
++numOfLines1;
if (numOfLines1 == random1)
{
sum1 = line1;
}
}
while (getline(File2, line2))
{
++numOfLines2;
if (numOfLines2 == random2)
{
sum2 = line2;
}
}
nameFirst = sum1 + " " + sum2;
nameList.push_back(nameFirst);
}
playerCreator(nameList);
// return nameList;
}
//creating player objects for each name in nameList
void playerCreator(vector<string> nameList)
{
for (int i = 0; i < nameList.size(); i++)
{
Player player(nameList[i], setRandomAge(), setRandomPerformance(), setRandomNumber(), setRandomPos());
playerList.push_back(player);
}
}
//creating random ages
int setRandomAge()
{
int age = rand() % 18 + 19;
return age;
}
int setRandomNumber()
{
int num = rand() % 99 + 1;
return num;
}
int setRandomPerformance()
{
int perf = rand() % 71 + 30;
return perf;
}
string setRandomPos()
{
int num;
vector<string> positions = {"D", "O", "F"};
int random = rand() % 3;
return positions[random];
}
//creating team objects for each team name in teamList
vector<string> createTeamNames()
{
vector<string> teamList;
string teamName, line;
// string sum;
int i;
int random = 0;
for (int i = 1; i < 19; i++)
{
random = rand() % 18 + 1;
int numOfLines = 0;
ifstream File("takimlar.txt");
while (getline(File, line))
{
numOfLines++;
if (numOfLines == i)
{
teamName = line;
}
}
teamName.erase(teamName.find_last_not_of(" \n\r\t") + 1);
teamList.push_back(teamName);
}
return teamList;
}
void teamCreator(vector<string> teamNameList)
{
vector<Player> teamPlayerList;
string lineUp;
string defLineUp = "4-4-2";
bool inputController = true;
for (int i = 0; i < teamNameList.size(); i++)
{
while (inputController)
{
cout << teamNameList[i] << " takimi formasyonu giriniz (Aralarinda '-' olacak sekilde): " << endl;
cout << "d yazarak 4-4-2 formasyonu atanabilir!"<<endl;
cout << endl;
cin >> lineUp;
if (lineUp == "d")
{
lineUp = defLineUp;
inputController = false;
}
else if (lineUp.size() == 5)
{
if (formationSumController(lineUp))
{
inputController = false;
}
else
{
cout << "Formasyon toplami 10 olmalidir! " << endl;
}
}
else
{
cout << "Gecerli formasyon giriniz." << endl;
}
}
inputController = true;
cout << endl;
Team team(createTeamPlayers(lineUp), teamNameList[i], createCoach(), lineUp);
teamList.push_back(team);
}
}
//takes the global variable -> playerList
vector<Player> createTeamPlayers(string teamLineUp)
{
vector<Player> teamPlayers;
int randIndex;
while (true)
{
vector<int> indexHolder;
for (int i = 0; i < 11; i++)
{
randIndex = rand() % playerList.size();
teamPlayers.push_back(playerList[randIndex]);
indexHolder.push_back(randIndex);
}
if (controlTeamPlayers(teamPlayers, teamLineUp))
{
for (int j = 0; j < indexHolder.size(); j++)
{
playerList.erase(playerList.begin() + indexHolder[j]);
}
return teamPlayers;
}
else
{
teamPlayers.clear();
indexHolder.clear();
}
}
}
bool controlTeamPlayers(vector<Player> teamPlayers, string teamLineUp)
{
int averageAge = 0;
float teamPerf = 0;
int defPlayetNumber = 0;
int midPlayerNumber = 0;
int forwPlayerNumber = 0;
bool ageControl = true;
bool perfControl = true;
bool lineUpControl = true;
string position;
size_t pos = 0;
while (ageControl)
{
for (int i = 0; i < teamPlayers.size(); i++)
{
averageAge += teamPlayers[i].getAge();
}
averageAge = averageAge / teamPlayers.size();
if (averageAge >= 25 && averageAge <= 32)
{
ageControl = false;
}
else
{
return false;
}
}
while (perfControl)
{
for (int i = 0; i < teamPlayers.size(); i++)
{
teamPerf += teamPlayers[i].getPerformance();
}
teamPerf = teamPerf / teamPlayers.size();
if (teamPerf >= 60)
{
perfControl = false;
}
else
{
return false;
}
}
int defCounter = 0;
int midCounter = 0;
int forCounter = 0;
pos = 1;
int counter = 0;
while (lineUpControl)
{
for (int i = 0; i < 3; i++)
{
position = teamLineUp.substr(0, pos);
stringstream posCount(position);
if (i == 0)
{
posCount >> defPlayetNumber;
}
else if (i == 1)
{
posCount >> midPlayerNumber;
}
teamLineUp.erase(0, pos + 1);
if (teamLineUp.size() < 1)
{
posCount >> forwPlayerNumber;
}
}
for (int j = 0; j < teamPlayers.size() - 1; j++)
{
if (teamPlayers[j].getPosition() == "D")
{
defCounter += 1;
}
else if (teamPlayers[j].getPosition() == "O")
{
midCounter += 1;
}
else if (teamPlayers[j].getPosition() == "F")
{
forCounter += 1;
}
}
if (defPlayetNumber + midPlayerNumber + forwPlayerNumber == 10)
{
lineUpControl = false;
}
else
{
return false;
}
if (defCounter == defPlayetNumber && midCounter == midPlayerNumber && forCounter == forwPlayerNumber)
{
lineUpControl = false;
}
else
{
return false;
}
}
return true;
}
bool formationSumController(string teamlineUp)
{
string position;
size_t pos = 1;
int midPlayerNumber = 0;
int forwPlayerNumber = 0;
int defPlayerNumber = 0;
int totalPlayer = 0;
for (int i = 0; i < 3; i++)
{
position = teamlineUp.substr(0, pos);
stringstream posCount(position);
if (i == 0)
{
posCount >> defPlayerNumber;
}
else if (i == 1)
{
posCount >> midPlayerNumber;
}
teamlineUp.erase(0, pos + 1);
if (teamlineUp.size() < 1)
{
posCount >> forwPlayerNumber;
}
}
totalPlayer = defPlayerNumber + midPlayerNumber + forwPlayerNumber;
cout << endl;
if (totalPlayer == 10)
{
return true;
}
else
{
return false;
}
}
string createCoach()
{
string coach, line;
int i;
int random = 0;
random = rand() % 18 + 1;
int numOfLines = 0;
ifstream File("teknik.txt");
while (getline(File, line))
{
numOfLines++;
if (numOfLines == i)
{
coach = line;
}
}
return coach;
}
class League
{
private:
int matchID;
void setmatchID(int matchID)
{
this->matchID = matchID;
}
int getmatchID()
{
return this->matchID;
}
};
void playMatches(vector<Team> teamList)
{
for (int i = 0; i < teamList.size(); i++)
{
for (int j = i+1; j < teamList.size(); j++)
{
int team1StartGoal = rand() % 7;
int team2StartGoal = rand() % 7;
cout<<teamList[i].getTeamName()<<"Takiminin kazanma olasiligi "
<<teamList[i].getTeamPerformance()*100 / (teamList[i].getTeamPerformance() + teamList[j].getTeamPerformance())<<" , ";
cout<<teamList[j].getTeamName()<<"Takiminin kazanma olasiligi "
<<teamList[j].getTeamPerformance()*100 / (teamList[i].getTeamPerformance() + teamList[j].getTeamPerformance())<<endl;
cout<<endl;
cout << teamList[i].getTeamName()<<" vs "<<teamList[j].getTeamName()<<"----";
if (teamList[i].getTeamPerformance() > teamList[j].getTeamPerformance()){
cout << teamList[i].getTeamName() << " Maci Kazandi!!" << endl;
}
else{
cout << teamList[j].getTeamName() << " Maci Kazandi!!" << endl;
}
cout<<teamList[i].getTeamDPO()<<endl;
}
}
}