#include <iostream>
#include <cstdlib>
#include <windows.h>
#include <ctime>

using namespace std;

int main()
{
    int hBound = 23;
    int wBound = 45;
    //Ball's X and Y
    int bX, bY;
    int dX, dY;
    //Player's X and Y
    int pY;
    int tmp;
    //Stats
    int life, score;

    //Init start
    bX = 21; bY = rand() % (23 - 19 + 1) + 19;
    dX = -1 ; dY = -1;
    pY = 22;
    life = 3; score = 0;
    srand(time(0));



    //Set window size
    system ("MODE 55,30");

    while (life != -1)
    {
        string s = "";
        system("cls");
        if (life > 0)
        {
            cout << "Score: " << score << "\n";
            s += "Life: ";
            for (int i = 1; i <= life; i++)
            {
                s = s + (char)3 + " ";
            }
            s += "\n";

            for (int i = 0; i < hBound; i++)
            {
                for (int j = 0; j < wBound; j++)
                {
                    if (i == 0 || i == hBound-1)
                        s += "=";
                    else if ((i != 0 || i != hBound-1) && (j == 0 || j == wBound-1))
                        s += "|";
                    else if (i == 3 && (j == 3 || j == 5 || j == 7 || j == 9))
                        s += (char)178;

                    //Ball Printout
                    else if (i == bX && j == bY)
                        s += (char)2;

                    //Player's Bumper
                    else if (i == hBound-2 && (j == pY || j == pY-1 || j == pY-2 || j == pY+1 || j == pY+2))
                        s += "X";
                    else if (i == hBound-2 && j == pY-3)
                        s += "[";
                    else if (i == hBound-2 && j == pY+3)
                        s += "]";

                    else
                        s += " ";
                }
                //cout << s << "\n";
                //s = "";
                s += "\n";
            }
            cout << s;


            //Check user input
            if (GetAsyncKeyState(VK_LEFT) && pY >= 5)
                pY -= 1;
            else if (GetAsyncKeyState(VK_RIGHT) && pY <=  wBound-6)
                pY += 1;




            //Decrease Life
            if (bX + dX == hBound)
            {
                life -= 1;

                bX = 21;
                bY = rand() % (23 - 19 + 1) + 19;

                pY = 22;
                dX *= -1;
                dY *= -1;

                Sleep(1000);
            }

            //Check bounce
            tmp = hBound-1;
            if (bX + dX == 0)
                dX *= -1;
            else if (bX + dX == hBound-2 && (bY == pY || bY == pY-1 || bY == pY-2 || bY == pY-3 ||
                                             bY == pY-4 || bY == pY+1 || bY == pY+2 || bY == pY+3 || bY == pY+4))
                dX *= -1;

            if (bY + dY == 0 || (bY + dY == wBound-1) )
            {
                dY *= -1;
            }



            bX += dX;
            bY += dY;

            Sleep(20);
        }
        else
        {
            life = -1;

        }
    }

    for (int i = 1; i <= 3; i++)
    {
        system("cls");
        Sleep(500);
        if (life <= 0)
            cout << "\n\n                     YOU LOSE!!!\n";
        Sleep(500);
    }
    cout << "                   Your Score: " << score << "\n\n";
    return 0;
}
