In: Computer Science
Project 1 - OO Programming
Create a program that simulates cars racing and allows users to bet on the races ( start users with $100 )
In main, prompt the user to enter details needed to create 2 instances of the Car class you create.
Ask the user to bet on which car will win the race ( use the overridden << method to display car details ), ensure the bet is between 1 and the amount of money the user has currently.
Create a Car class that has attributes for name, make, model, quarter mile time, and max speed.
( You can reference https://www.0-60specs.com/0-60-times/ - or just make values up )
Add a method Race that accepts a reference to another instance of car for the car to race.
When racing, add a random -1.0 - 1.0 second adjustment to the quarter mile time to account for wind and other conditions.
return a string result that says which car wins the race and by how much time.
Display the results of the bet and the users new balance.
Allow the user to keep creating cars to race until they are done or out of money.
Code in C++
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
class Car { //Class for car
public:
string name, make, model; //String for car name, make and
model
double q_time, max_speed; //Double data type for quarter mile time
and max speed
void input() //Function to input car details by user
{
cout<<"Enter car name:";
cin>>name;
cout<<"Enter car make:";
cin>>make;
cout<<"Enter car model:";
cin>>model;
cout<<"Enter quarter mile time:";
cin>>q_time;
cout<<"Enter max speed:";
cin>>max_speed;
}
void display() //Function to diplay car details
{
cout<<"Name:"<<name <<"
Make:"<<make<<" Model:"<<model<<" Quarter
mile time:"<<q_time<<" Max
speed:"<<max_speed<<"\n";
}
int race(Car b) //Function to race the cars
{
srand (time(NULL)); //Seeding the random number generator
//The below two lines of code add or remove some random time
//between -1 to 1 to the quarter mile time of both cars.
double one_red=((double(rand()) / double(RAND_MAX)) * (1 + 1)) +
1;
double two_red=((double(rand()) / double(RAND_MAX)) * (1 + 1)) +
1;
q_time+=one_red;
b.q_time+=two_red;
if(q_time<b.q_time) //If car 1 has lower quarter mile time, it
wins
{
cout<<name<<" won by "<<b.q_time-q_time;
return 1;
}
if(q_time>b.q_time) //If car 2 has lower quarter mile time, it
wins
{
cout<<b.name<<" won by "<<q_time-b.q_time;
return -1;
}
if(q_time==b.q_time) //If both cars win
{
cout<<"No body won";
return 0;
}
}
};
int main() //Main function
{
int bal=100; //Starting balance is 100 for the user
while(bal>0) //While user balance is greater than 0, he can play
the game
{ int car_select;
int result;
int bet=1;
Car c1,c2; //Below lines of codes are for entering car
details
cout<<"Enter Car 1 details\n";
c1.input();
cout<<"Enter Car 2 details\n";
c2.input();
cout<<" The details of the cars are:\n";
//Displaying the details of the car for the user to choose
cout<<"1.";
c1.display();
cout<<"2.";
c2.display();
cout<<"Select which car to bet on(Press 1 or 2):";//Selecting
which car to bet on
cin>>car_select;
cout<<"Select the amount you want to bet:";
while(1)//This would only take bet value which is between 1 and
balance of user
{
cin>>bet;
if(bet>=1 && bet<=bal)
break;
}
if(car_select) //Based on the car selection, race fucntion would be
called
result=c1.race(c2);
else
result=c2.race(c1);
if(result) //If your car wins, result is 1
{
cout<<"You won the bet. Your new balance is:";
bal+=bet;
cout<<bal<<"\n";
}
else if(result==-1) //If your car looses, result is -1
{
cout<<"You lost the bet. Your new balance is:";
bal-=bet;
cout<<bal<<"\n";
}
else //If race is draw
{
cout<<"It was a draw. Your balance
is:"<<bal<<"\n";
}
}
return 0;
}
Output: