#include "curses_io_V12.h"
#include <cstdlib>
#include <windows.h>


////////////////////////////
//Fun at the Races - Written By Brad Towle
//2-1-09
//Revision 1.1
//This game allows the user to bet one which
//car he/she thinks will win.
//
////////////////////////////
const int carxStart = 1;    //initial x starting for all cars
const int car1yStart = 6;   //initial y starintg for car 1
//const int carxEnd = 42;     //final point for all cars (actually unused)
const int car2yStart = 13;  //Initial y position of car 2  
const int car3yStart = 19;  //Initial y position of car 3
using namespace std;


//Ignore the class right now 
//You will learn about this in cs202
class car1
{//ignore the private thing right now
 private:
         int x;       //the x location of the car1 object
         int y;       //The y position of the care1 object

public:      //ignore the public for right now
car1()       //default constructor - Ignore
      {
      x = carxStart;   //sets x location of object
      y = car1yStart;  //Sets y lcoation of object
      }
void draw()            //Draws car based on x,y
{
 setColor(COLOR_BLUE,COLOR_RED,true);  //This code uses  curses to print a 
 printStringAt(x+4,y-4," #","LEFT");   //red monster Truck!
 printStringAt(x,y-3,"         ","LEFT");
 printStringAt(x,y-2,"         ","LEFT");
 setColor(COLOR_YELLOW,COLOR_WHITE,false);
 printStringAt(x+1,y-1,"   ","LEFT");
 printStringAt(x+5,y-1,"   ","LEFT");
 printStringVertical(x+2,y-2," O ","LEFT");
 printStringVertical(x+6,y-2," O ","LEFT");
 //printIntAt(x+4,y-6,x,"CENTER");
 }
void Race(int m)
     {
     x += m;    //This moves the object car1 forward
     }
void Reset()    //resets the Car 1 variable
{
  x = carxStart;       //This resets the car
  y = car1yStart;
}

bool winner()    //This will return true or false
{                //Based on if the car object is past 71       
if(x >=72)
 {
  return true;
 }
 else
     { 
     return false;
     } 
 }//
};//end of Object


//THis car 2 object creates a Blue Race car
class car2
{
private:
int x;    //X position
int y;    //Yposition

public:
car2()
{
x = carxStart;       //Sets initial position
y = car2yStart;
}
void draw()
{
setColor(COLOR_RED,COLOR_BLUE,true);
printStringAt(x,y-3,"  ","LEFT");
printStringAt(x+1,y-2,"    ","LEFT");
printStringAt(x+2,y-1,"       ","LEFT");
setColor(COLOR_YELLOW,COLOR_WHITE,true);
printStringAt(x+5,y-2,"  ","LEFT");
setColor(COLOR_WHITE,COLOR_BLACK,true);
printStringAt(x+3,y,"o","LEFT");
printStringAt(x+7,y,"o","LEFT");
//printIntAt(x+4,y-6,x,"CENTER");
}
void Race(int m)
{
x += m;       //Moves the car
}
void Reset()
{
x = carxStart;        //resets the car
y = car2yStart;
}

bool winner()
{
if(x >=72)     //Checks if the car is a winner!
 {
 return true;
 }
else 
     return false;
 }
};


//Same sort of thing as the other 2 car objects
//This one creates a yellow bug.
class car3
{
private:
        int x;
        int y;

public:
car3()
{
 x = carxStart;
 y = car3yStart;
}
void draw()
{
 char Dummy[4] = {219,219,219,0}; //using extended ASCII so I can
                                  //Get Brighter Yellow!
 char Dummy1[7] = {219,219,219,219,219,219,0};
 setColor(COLOR_YELLOW,COLOR_BLUE,true);
 printStringAt(x+3,y-3,Dummy,"LEFT");
 printStringAt(x+2,y-2,Dummy,"LEFT");
 printStringAt(x+1,y-1,Dummy1,"LEFT");
 setColor(COLOR_BLACK,COLOR_BLUE,true);
 printStringAt(x+5,y-2," ","LEFT");
 setColor(COLOR_BLACK,COLOR_WHITE,false);
 printStringAt(x+2,y,"o","LEFT");
 printStringAt(x+5,y,"o","LEFT");
 //printIntAt(x+4,y-6,x,"CENTER");
}
void Race(int m)
{
 x += m;
}
void Reset()
{
 x = carxStart;
 y = car3yStart;
}

bool winner()
{
 if(x >=72)
 {
 return true;
 }
 else 
     return false;
}
};
/////////////////////////////////////////
//Draw Background
//Draws the background of the race course
//This is called first, then 
//inidividual Racers are drawn
//Written By Brad Towle
//2-2-09
/////////////////////////////////////
void drawBackground()
{
 setColor(COLOR_YELLOW,COLOR_GREEN,true);
 clearScreen(0,0,90,25);//Draw the Grass
 setColor(COLOR_BLACK,COLOR_MAGENTA,true);
 for(int i = 1;i<72;i+=5)
 {
 printStringAt(i,car1yStart +1," ","CENTER");    //Using for loops
 printStringAt(i,car2yStart +1," ","CENTER");    //Draw the road
 printStringAt(i,car3yStart +1," ","CENTER");    //For each racer
 }
 setColor(COLOR_BLACK,COLOR_WHITE,true);
 for(int i = 0; i< 8;i++)               //This for loop will
 {                                      //Draw a white rectangle
  printStringVertical(72+i,0,"                         ","CENTER");
 }
 setColor(COLOR_WHITE,COLOR_BLACK,true);
 for(int i =0; i<8;i++)                 //These two for loops will use math
 {                                      //To draw the black Checkered parts of 
  for(int j =i%2;j<25;j+=2)             //The finish Pay attention 
         {
         printStringAt(72+i,j," ","LEFT");
         }
 }
}

//////////////////////////////////
//titleScreen
//Written By Brad Towle
//2-2-09
//This will draw the obnoxious 
//Title, and wait for a user to
//Enter a character
////////////////////////////////

void titleScreen()
{
char junk;
setColor(COLOR_BLACK,COLOR_WHITE,true);  //This prints white on squares that
for(int i =0; i<25; i++)                 //are intervals - thus creating
{                                        //A checkered Pattern      
   for(int j = i %2;j<90;j +=2)
   {
   printStringAt(j,i," ","LEFT");
   }
}

//This prints the title, the author, and prompts for a character!
setColor(COLOR_WHITE,COLOR_YELLOW,true);
printStringAt(15,10,"FUN AT THE RACES!","LEFT");
setColor(COLOR_CYAN,COLOR_RED,true);
printStringAt(17,12,"Written by -> B-rad!","LEFT");
setColor(COLOR_CYAN,COLOR_GREEN,true);
junk = promptForCharAt(15,15,"Enter a character to continue");

}



//////////////////////////////
//Main - Written By Brad Towle
//2-1-09
//This function will create three cars
//Call the title screen then ask the user to 
//Bet on a car
//Then using random numbers the 
//cars will race!
//////////////////////////
int main()
{
car1 RedTruck;            //The RedTruck Object
car2 BlueRacer;           //The BlueRacer object
car3 YellowBug;           //The YellowBug object
bool valid = false;       //Exit input loop
char choice = 'n';        //Default choice

srand(time(NULL));        //Seed the random Number generator
startCurses();            //Starts curses
titleScreen();            //Calls title Screen
while(choice !='q')       //While user does not enter q execute game
{valid = false;           //setting valid to false

//The following while loop shoudl be in it's own function.
while(!valid)             //While the user does not enter
{                         //A valid input continue prompting
setColor(COLOR_WHITE,COLOR_BLACK,false);
choice = promptForCharAt(0,20,"Bet on R,B,Y for Red Truck,Blue Racer, or Yellow Bug! q to quit");
switch(choice)              //Switch on r,b,y,q OR R,B,Y,Q       
      {
      case 'q':
           return 0;
           break;
      case 'r':
           valid = true;
           break;
      case 'b':
           valid = true;
      case 'y':
           valid = true;
           break;
      case 'Q':
           return 0;
           break;
      case 'R':
           valid = true;
           break;
      case 'B':
           valid = true;
      case 'Y':
           valid = true;
           break;        
      }//end of switch
}//end of while not valid

while(!RedTruck.winner()&&!BlueRacer.winner()&&!YellowBug.winner())//While no one has won!
{
//Draw Background 
 drawBackground();
 RedTruck.draw(); //Draw the red truck
 BlueRacer.draw();//Draw the blue racer
 YellowBug.draw();//Draw the yellow bug

Sleep(200+rand()%(rand()%200));     //Sleep the process (adds delay)
 RedTruck.Race(rand()%3+1);         //Move the red truck randomly
 BlueRacer.Race(rand()%3+1);        //Move the Blue Racer randomly
 YellowBug.Race(rand()%3+1);        //Move the Yellow Bug randomly

}//end of game loop
 drawBackground();                  //This was necesary to draw the final step
 RedTruck.draw();                   // " "
 BlueRacer.draw();                  //" "
 YellowBug.draw();                  //" "
setColor(COLOR_WHITE,COLOR_BLACK,true);

//These if statement should be in there own function
if(RedTruck.winner() )                 
{
 if(choice == 'r' || choice == 'R')
           printStringAt(20,10,"Red Truck WON!  CONGRADS","CENTER");
 else
           printStringAt(20,10,"Red Truck WON!  Better LUCK NEX TIME","CENTER");
}

else if(BlueRacer.winner() )
{
 if(choice == 'b' || choice == 'B')
           printStringAt(20,10,"Blue Racer WON!  CONGRADS","CENTER");
 else
           printStringAt(20,10,"Blue Racer WON!  Better LUCK NEX TIME","CENTER");
}

else if(YellowBug.winner() )
{
 if(choice == 'y' || choice == 'Y')
           printStringAt(20,10,"Yellow Bug WON!  CONGRADS","CENTER");
 else
           printStringAt(20,10,"Yello Bug WON!  Better LUCK NEX TIME","CENTER");
}

//Reset the vehicles
BlueRacer.Reset();
RedTruck.Reset();
YellowBug.Reset();
}//while not q

endCurses(); //end curses
return 0;    //exit the game
}
