Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
24 kB
13
Indexable
Never
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <vector>
#include <sstream>
#include <iomanip>
#include <algorithm>

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;
    string teamName, shortName, coach;
    float teamPerf, DPO, OSPO, FPO;
    int defencePlayers, midPlayers, forPlayers;
    string teamLineUp;
    int wins = 0;
    int draws = 0;
    int goalScored = 0;
    int loses = 0;
    int point = 0;
    int goalConcede = 0;
    int average = 0;

    Team(vector<Player> teamPlayers, string teamName, string coach, string teamLineUp)
    {
        this->teamPlayers = teamPlayers;
        this->teamName = teamName;
        this->shortName = createShortName();
        this->teamLineUp = teamLineUp;

        this->coach = coach;

        this->teamPerf = calculateTeamPerf(teamPlayers);

        this->defencePlayers = getDefencePlayerCount();

        this->midPlayers = getMidPlayerCount();

        this->forPlayers = getForwPlayerCount();

        this->DPO = calculateDefencePerf();

        this->OSPO = calculateMiddlePerf();

        this->FPO = calculateForwPerf();
    }
    void setWins(int wins)
    {
        this->wins += wins;
    }
    int getWins()
    {
        return this->wins;
    }
    void setLoses(int loses)
    {
        this->loses += loses;
    }
    int getLoses()
    {
        return this->loses;
    }
    void setDraws(int draws)
    {
        this->draws += draws;
    }
    int getDraws()
    {
        return this->draws;
    }
    void setAverage(int average)
    {
        this->average += average;
    }
    int getAverage()
    {
        return this->average;
    }
    void setPoint(int point)
    {
        this->point += point;
    }
    int getPoint()
    {
        return this->point;
    }
    void setGoalScored(int goalScored)
    {
        this->goalScored += goalScored;
    }
    int getGoalScored()
    {
        return this->goalScored;
    }
    void setGoalConcede(int goalConcede)
    {
        this->goalConcede += goalConcede;
    }
    int getGoalConcede()
    {
        return this->goalConcede;
    }

    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;
    }
    float getTeamDPO()
    {
        return this->DPO;
    }
    float getTeamOSPO()
    {
        return this->OSPO;
    }
    float getTeamFPO()
    {
        return this->FPO;
    }
    string createShortName()
    {
        return teamName.substr(0, 2);
    }
    int 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 calculateDefencePerf()
    {
        int defencePerfSum = 0;
        for (int i = 0; i < this->teamPlayers.size(); i++)
        {
            if (teamPlayers[i].getPosition() == "D")
            {
                defencePerfSum += teamPlayers[i].getPerformance();
            }
        }

        return defencePerfSum / defencePlayers;
    }
    float calculateMiddlePerf()
    {
        int midPerfSum = 0;
        for (int i = 0; i < this->teamPlayers.size(); i++)
        {
            if (teamPlayers[i].getPosition() == "O")
            {
                midPerfSum += teamPlayers[i].getPerformance();
            }
        }

        return midPerfSum / midPlayers;
    }
    float calculateForwPerf()
    {
        int forwPerfSum = 0;
        for (int i = 0; i < this->teamPlayers.size(); i++)
        {
            if (teamPlayers[i].getPosition() == "F")
            {
                forwPerfSum += teamPlayers[i].getPerformance();
            }
        }

        return forwPerfSum / forPlayers;
    }

    int getDefencePlayerCount()
    {
        int defPlayerNumber = 0;
        string position;
        int pos = 1;
        position = teamLineUp.substr(0, pos);
        stringstream posCount(position);
        posCount >> defPlayerNumber;
        return defPlayerNumber;
    }
    int getMidPlayerCount()
    {

        int midPlayerNumber = 0;
        string position;
        int pos = 1;
        teamLineUp.erase(0, pos + 1);
        position = teamLineUp.substr(0, pos);
        stringstream posCount(position);
        posCount >> midPlayerNumber;
        return midPlayerNumber;
    }
    int getForwPlayerCount()
    {
        int forwPlayerNumber = 0;
        string position;
        int pos = 1;
        teamLineUp.erase(0, pos + 1);

        position = teamLineUp.substr(0, pos);
        stringstream posCount(position);
        teamLineUp.erase(0, pos + 1);

        posCount >> forwPlayerNumber;
        return forwPlayerNumber;
    }
};

//method signatures
void createPlayers();
void playerCreator(vector<string> nameList);
int setRandomAge();
int setRandomNumber();
int setRandomPerformance();
string createCoach(int lineNumber);
//functions for team class
vector<string> createTeamNames();
void teamCreator(vector<string> teamList);
vector<Player> createTeamPlayers(string teamLineUp);
string setRandomPos();
bool controlTeamPlayers(vector<Player> playerList, string teamLineUp);
bool formationSumController(string teamLineUp);
//functions for league class
void playMatches(vector<Team>& teamList);
void showLeagueStats();
void showTeamStats(vector<Team> teamList);
void calculateWinPos(vector<Team> teamList);
vector<Team> sortTeams();
void setAverage();

vector<Player> playerList;
vector<Team> teamList;

//main function
int main()
{
    int secim;

    vector<string> teamNameList;
    srand(time(0));
    createPlayers();

    cout << "Sanal Lig Uygulamasina Hos Geldiniz! Seciminizi Yanlarinda Bulunan Sayi Ile Giriniz." << endl;
    cout << endl;
    while (true)
    {
        cout << "1 --- Takim Dizislerini Gir" << endl
            << "2 --- Takma Ad Ile Takim Ozellikleri Sorgulama (Takim Dizilisleri Girilmeli!)" << endl
            << "3 --- 2 Takim Arasinda Kazanma Ihtimali Hesapla (Takim Dizilisleri Girilmeli!) " << endl
            << "4 --- Lig Sonuclarini Goster (Takim Dizilisleri Girilmeli!)" << endl
            << "5 --- Uygulamadan Cikis Yap" << endl;
        cout << "Secim:";
        cin >> secim;
        cout << endl;

        switch (secim)
        {
        case 1:
            teamNameList = createTeamNames();
            teamCreator(teamNameList);
            break;

        case 2:
            showTeamStats(teamList);
            break;

        case 3:
            calculateWinPos(teamList);
            break;

        case 4:
            playMatches(teamList);
            setAverage();
            showLeagueStats();
            break;

        case 5:
            goto exit_loop;

        default:
            cout << "Yanlis Secim Yaptiniz!" << endl;
            cout << endl;
            continue;
            break;
        }
    }
exit_loop:;

    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 < 500; 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++)
    {
        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(Formasyon toplami 10 olmali!)]: " << 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(i), 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();
            if (i == 0)
            {
                Player goalKeeper = playerList[randIndex];
                goalKeeper.setPosition('G');
                teamPlayers.push_back(goalKeeper);
                indexHolder.push_back(randIndex);
            }
            else
            {

                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(); 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 defPlayetNumber = 0;
    int totalPlayer = 0;

    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;
        }
    }
    totalPlayer = defPlayetNumber + midPlayerNumber + forwPlayerNumber;

    if (totalPlayer == 10)
    {
        return true;
    }
    else
    {
        return false;
    }
}
string createCoach(int lineNum)
{
    string coach, line;

    int numOfLines = 0;

    ifstream File("teknik.txt");

    while (getline(File, line))
    {

        if (numOfLines == lineNum)
        {
            coach = line;
        }

        ++numOfLines;
    }
    return coach;
}

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;
            int random = rand() % 2;

            if (teamList[i].getTeamFPO() > teamList[j].getTeamFPO())
            {
                team1StartGoal++;
            }

            else if (teamList[i].getTeamFPO() < teamList[j].getTeamFPO())
            {
                team2StartGoal++;
            }

            if (teamList[i].getTeamOSPO() > teamList[j].getTeamOSPO())
            {

                team1StartGoal++;
                team2StartGoal--;
                if (team2StartGoal < 0)
                {

                    team2StartGoal = 0;
                }
            }

            else if (teamList[i].getTeamOSPO() < teamList[j].getTeamOSPO())
            {

                team1StartGoal--;
                team2StartGoal++;
                if (team1StartGoal < 0)
                {

                    team1StartGoal = 0;
                }
            }

            if (teamList[i].getTeamDPO() > teamList[j].getTeamDPO())
            {

                switch (team2StartGoal)
                {
                case 0:
                    team2StartGoal = 0;
                    break;

                default:
                    team2StartGoal--;
                    break;
                }
            }

            else
            {

                switch (team1StartGoal)
                {
                case 0:
                    team1StartGoal = 0;
                    break;

                default:
                    team1StartGoal--;
                    break;
                }
            }

            if (teamList[i].getTeamPerformance() > teamList[i].getTeamPerformance())
            {

                switch (team1StartGoal, team2StartGoal)

                    switch (random)
                    {
                    case 1:
                        team1StartGoal++;
                        break;

                    case 0:
                        team2StartGoal--;
                        break;
                    }
                if (team2StartGoal < 0)
                {
                    team2StartGoal = 0;
                }
            }

            else if (teamList[i].getTeamPerformance() < teamList[i].getTeamPerformance())
            {

                switch (random)
                {
                case 1:
                    team1StartGoal--;
                    break;

                case 0:
                    team2StartGoal++;
                    break;
                }
                if (team1StartGoal < 0)
                    team1StartGoal = 0;
            }

            teamList[i].setGoalScored(team1StartGoal);
            teamList[i].setGoalConcede(team2StartGoal);

            teamList[j].setGoalScored(team2StartGoal);
            teamList[j].setGoalConcede(team1StartGoal);

            if (team1StartGoal > team2StartGoal)
            {
                teamList[i].setWins(1);
                teamList[j].setLoses(1);

                teamList[i].setPoint(3);
            }
            else if (team1StartGoal < team2StartGoal)
            {
                teamList[j].setWins(1);
                teamList[i].setLoses(1);

                teamList[j].setPoint(3);
            }
            else
            {
                teamList[i].setDraws(1);
                teamList[j].setDraws(1);

                teamList[i].setPoint(1);
                teamList[j].setPoint(1);
            }
        }
    }
}
void setAverage()
{
    int average;
    for (int i = 0; i < teamList.size(); i++)
    {
        average = teamList[i].getGoalScored() - teamList[i].getGoalConcede();
        teamList[i].setAverage(average);
    }
}


vector<Team> sortTeams()
{
    vector<int> perfVector;

    sort(teamList.rbegin(), teamList.rend(), [](const Team& lhs, const Team& rhs)
        { return tie(lhs.point, lhs.average) < tie(rhs.point, rhs.average); });

    return teamList;
}

void showLeagueStats()
{
    vector<Team> teamListSorted = sortTeams();

    cout << setw(15) << " Takim Adi " << setw(10) << " Oynadigi Mac " << setw(10) << " Galibiyet "
        << " Beraberlik " << setw(10)
        << "Maglubiyet " << setw(10) << "Atilan Gol" << setw(14) << "Yenilen Gol "
        << setw(2) << "Averaj " << setw(6) << " Puan" << endl;

    for (int i = 0; i < teamListSorted.size(); i++)
    {
        cout << setw(15) << teamListSorted[i].getTeamName() << setw(10) << "17"
            << setw(10) << teamListSorted[i].getWins() << setw(10)
            << teamListSorted[i].getDraws() << setw(12) << teamListSorted[i].getLoses() << setw(10)
            << teamListSorted[i].getGoalScored() << setw(13) << teamListSorted[i].getGoalConcede() << setw(12) << teamListSorted[i].getAverage() << setw(7)
            << teamListSorted[i].getPoint() << endl;
    }

    cout << endl;
}

void showTeamStats(vector<Team> teamList)
{
    string takmaisim;
    cout << "Takma Isim Giriniz:";
    cin >> takmaisim;
    cout << endl;

    for (int i = 0; i < teamList.size(); i++)
    {

        if (takmaisim == teamList[i].getShortName())
        {
            for (int k = 0; k < 11; k++)
            {
                cout << "Isim " << teamList[i].getTeamPlayers()[k].getName() << setw(30 - teamList[i].getTeamPlayers()[k].getName().size()) << " Yas "
                    << teamList[i].getTeamPlayers()[k].getAge() << "       Performans " << teamList[i].getTeamPlayers()[k].getPerformance()
                    << "       Forma Numarasi " << teamList[i].getTeamPlayers()[k].getNumber() << endl;
            }
            cout << endl;
            cout << "Takim Adi: " << teamList[i].getTeamName() << "    "
                << " Teknik Direktor: "
                << teamList[i].getCoach() << "    "
                << " Takim Performans: " << teamList[i].getTeamPerformance() << endl;

        }
    }
    cout << endl;
}
void calculateWinPos(vector<Team> teamList)
{
    string team1, team2;
    cout << "Ilk Takimi Giriniz:";
    cin >> team1;
    cout << "Ikinci Takimi Giriniz:";
    cin >> team2;
    cout << endl;

    for (int i = 0; i < teamList.size(); i++)
    {

        if (team1 == teamList[i].getTeamName())
        {

            for (int j = 1; j < teamList.size(); j++)
            {

                if (team2 == teamList[j].getTeamName())
                {

                    cout << teamList[i].getTeamName() << " Takiminin kazanma olasiligi %"
                        << teamList[i].getTeamPerformance() * 100 / (teamList[i].getTeamPerformance() + teamList[j].getTeamPerformance()) << endl;
                    cout << endl;
                    cout << teamList[j].getTeamName() << " Takiminin kazanma olasiligi %"
                        << teamList[j].getTeamPerformance() * 100 / (teamList[i].getTeamPerformance() + teamList[j].getTeamPerformance()) << endl;
                }
            }
        }
    }

    cout << endl;
}