#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <ctime>

using namespace std;

struct data
{
    int score;
    int hiscore;
    int pX,pY;
    int fX,fY;
    int eX,eY;
    string hiname;
}game;

int random(int nMin, int nMax)
{
    return ((rand() % (nMax - nMin)) + 1) + nMin;
}

bool checkBlock(int pX, int pY)
{
    if (pX % 2 != 0  &&  pY % 2 != 0)
        return true;
    else
        return false;
}

int main()
{
    //bool repeat = false;
    bool dead   = false;
    bool f = false;
    bool first = true;
    string s;
    string name;
    string in = "";
    game.hiscore = 0;

    srand(time(0));

    while (1)
    {
        system("cls");
        if (dead)
        {
            dead = false;
            cout << "You Lose!\n";
            cout << "Wanna play again? (y/n) ";
            do
            {
                cin >> in;
            }
            while (!(in == "y" || in == "n"));
            if (in == "n")
                return 0;
            first = true;
        }


        system("cls");
        cout << "Highscore: " << game.hiname << " (" << game.hiscore << ")" << "\n\n";
        cout << "Masukkan nama player: ";
        cin  >> name;
        game.score = 0;
        s = "";
        do
        {
            game.pX = random(2,9);
            game.pY = random(2,9);
        } while ((checkBlock(game.pX,game.pY)));

        do
        {
            game.eX = random(2,9);
            game.eY = random(2,9);
            if (!(game.eX == game.pX && game.eY == game.pY) && !checkBlock(game.eX,game.eY))
                f = true;
        } while (f != true);
        f = false;


        do
        {
            game.fX = random(2,9);
            game.fY = random(2,9);
            if (!(game.fX == game.pX && game.fY == game.pY) && !checkBlock(game.fX,game.fY))
                f = true;
        } while (f != true);
        f = false;

        Sleep(100);
        while (dead != true)
        {
            system("cls");
            cout << "Player: " << name << "\n";
            cout << "Score:  " << game.score << "\n";

            if ((game.pX > game.eX) && game.eY % 2 == 0)
            {
                game.eX++;
                if ( game.pY > game.eY)
                {

                }
            }
            else if (game.pX < game.eX && game.eY % 2 == 0)
            {
                game.eX--;
            }



            if ((game.pY > game.eY)  && game.eX % 2 == 0)
            {
                game.eY++;
            }
            else if (game.pY < game.eY  && game.eX % 2 == 0)
            {
                game.eY--;
            }

            if (game.pX == game.fX && game.pY == game.fY)
            {
                do
                {
                    game.fX = random(2,9);
                    game.fY = random(2,9);
                    if (!(game.fX == game.pX && game.fY == game.pY) && !checkBlock(game.fX,game.fY))
                        f = true;
                } while (f != true);
                f = false;
                game.score += 5;
            }

            if (game.eX == game.pX && game.eY == game.pY)
            {
                game.pX = -1;
                game.pY = -1;
                dead = true;
            }

            for (int i = 1; i <= 11; i++)
            {
                for (int j = 1; j <= 11; j++)
                {
                    if (i == 1 || i == 11)
                        s += "=";
                    else if (j == 1 || j == 11)
                        s += "|";
                    else if ( (i % 2 != 0 && (i != 1 || i != 11)) && (j % 2 != 0 && (j != 1 || j != 11)) )
                        s += "X";
                    else if (i == game.pX && j == game.pY)
                        s += name[0];
                    else if (i == game.fX && j == game.fY)
                        s += "*";
                    else if (i == game.eX && j == game.eY)
                        s += "#";
                    else
                        s += " ";
                }
                s += "\n";
            }
            cout << s;
            s = "";

            if (first)
            {
                first = false;
                Sleep(2000);
            }

            if (GetAsyncKeyState(VK_UP))
            {
                game.pX--;
                if ( checkBlock(game.pX,game.pY) || game.pX == 1)
                    game.pX++;
                //delay = true;
            }
            else if (GetAsyncKeyState(VK_DOWN))
            {
                game.pX++;
                if ( checkBlock(game.pX,game.pY) || game.pX == 11)
                    game.pX--;
                //delay = true;
            }
            else if (GetAsyncKeyState(VK_LEFT))
            {
                game.pY--;
                if ( checkBlock(game.pX,game.pY) || game.pY == 1)
                    game.pY++;
                //delay = true;
            }
            else if (GetAsyncKeyState(VK_RIGHT))
            {
                game.pY++;
                if ( checkBlock(game.pX,game.pY) || game.pY == 11)
                    game.pY--;
            }

            cout << "\n\n" << "Location: " << game.eX << "," << game.eY;
            Sleep(150);

        }
        if (game.score > game.hiscore)
        {
            game.hiscore = game.score;
            game.hiname = name;
        }
    }
    return 0;
}
