In: Computer Science
Car Race
Outcome:
Program Specifications:
You will design a simulated car race. The race will consist of three cars: car A, car B, and car C.
Your program will show the cars (the letters A, B, and C) moving across the screen toward the finish line. The finish line will be shown on the screen at all times during the race. Your program will randomly move each car (between 1 and 3 spaces) at randomly selected intervals. The race ends when a car moves 80 spaces. After the race ends, you will display the results:
First Place: carX
Second Place: carX
Third Place: carX
After displaying the results, the program will ask the user if he or she wishes to run another race.
Submission Requirements:
You must follow the rules from the first assignment.
YOU MUST:
YOU CANNOT:
#include <iostream>
#include <ctime>
#include <unistd.h>
using namespace std;
int random()
{
int x =0;
while(!x)
{
x = rand() % 4;
}
return x;
}
void printSpace(int posA)
{
int i = 0;
while(i < posA)
{
cout << " ";
i++;
}
}
void stepMove(int step)
{
int x = step-1;
switch (x)
{
case 0:
break;
case 1:
cout << " ";
break;
case 2:
cout << " ";
break;
case 3:
cout << " ";
break;
default:
break;
}
}
int minimum(int posA, int posB, int posC)
{
if(posA <= posB && posA <= posC)
return posA;
else if(posB <= posA && posB <= posC)
return posB;
else
return posC;
}
int raceA(char A,int posA)
{
if(posA <= 80)
{
int step;
step = random();
printSpace(posA);
if(posA < 80)
stepMove(step);
cout << A;
posA = posA + step;
printSpace(80-posA-1);
cout << "|";
}
return posA;
}
int raceB(char B,int posB)
{
if(posB <= 80)
{
int step;
step = random();
printSpace(posB);
if(posB < 80)
stepMove(step);
cout << B;
posB = posB + step;
printSpace(80-posB-1);
cout << "|";
}
return posB;
}
int raceC(char C,int posC)
{
if(posC <= 80)
{
int step;
step = random();
printSpace(posC);
if(posC < 80)
stepMove(step);
cout << C;
posC = posC + step;
printSpace(80-posC-1);
cout << "|";
}
return posC;
}
void race(char A, char B, char C)
{
int loop = 1;
int posA = 0;
int posB = 0;
int posC = 0;
char winner[3] = {'z','z','z'};
int index = 0;
bool passA = false;
bool passB = false;
bool passC = false;
while(loop <= 81 )
{
posA = raceA(A,posA); cout << "\n";
posB = raceB(B,posB); cout << "\n";
posC = raceC(C,posC); cout << "\n";
if(posA >= 80 && !passA)
{
winner[index] = A;
passA = true;
index++;
}
if(posB >= 80 && !passB)
{
winner[index] = B;
passB = true;
index++;
}
if(posC >= 80 && !passC)
{
winner[index] = C;
passC = true;
index++;
}
loop = minimum(posA,posB,posC);//cout << "loop:" << loop;
sleep(1);
cout << "\n";
if(loop >= 80)
loop = loop + 1;
}
cout << "\nShowing the Results in...";
cout << "3 ";sleep(2);
cout << "2 ";sleep(2);
cout << "1 ";sleep(2);
cout << "\nFirst Place: car" << winner[0] << endl;
cout << "Second place: car" << winner[1] << endl;
cout << "Third place: car" << winner[2] << endl;
}
int main()
{
char A = 'A';
char B = 'B';
char C = 'C';
int finish = 80;
srand(time(0));
cout << "Race starts: \n \n" ;
race(A,B,C);
cout << "\n\t\t ------Race Ends------";
return 0;
}