Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C++ note: please create separate functions outside the main function. Build a

ID: 3746110 • Letter: I

Question

In C++

note: please create separate functions outside the main function.

Build a program that simulates a horse race. You will have a track of 15 units long, and five horses. On each turn, each horse will flip a coin' (Right, I don't know how horses flip coins either, but go with me on this.) Half the time, the horse will move forward one unit, and half the time it will stay in the same place. On each turn, you will see the track with the positions of the five horses, and dots for all the other positions. The race ends when one of the horses passes the finish line.

Explanation / Answer

/*
* File: main.cpp
* Author:
*
* Created on 12 September, 2018
*/


#include<string>
#include<iostream>
#include<ctime>
using namespace std;

//global variables which represents position of a horse in a race
char horse0[15]={'0','.','.','.','.','.','.','.','.','.','.','.','.','.','.'};
char horse1[15]={'1','.','.','.','.','.','.','.','.','.','.','.','.','.','.'};
char horse2[15]={'2','.','.','.','.','.','.','.','.','.','.','.','.','.','.'};
char horse3[15]={'3','.','.','.','.','.','.','.','.','.','.','.','.','.','.'};
char horse4[15]={'4','.','.','.','.','.','.','.','.','.','.','.','.','.','.'};

/**
* Print the horses' position on screen
*/
void print(){
cout<<" ";
for(int i=0;i<5;i++)
switch(i+1) {
case 1:
for(int i=0;i<15;i++)
cout<<horse0[i]<<" ";
cout<<" ";
break;
case 2:
for(int i=0;i<15;i++)
cout<<horse1[i]<<" ";
cout<<" ";
break;
case 3:
for(int i=0;i<15;i++)
cout<<horse2[i]<<" ";
cout<<" ";
break;
case 4:
for(int i=0;i<15;i++)
cout<<horse3[i]<<" ";
cout<<" ";
break;
case 5:
for(int i=0;i<15;i++)
cout<<horse4[i]<<" ";
cout<<" ";
break;
}
  
}
/**
* Flips the coin for horse0 and move the horse if random number is 1.
* It doesn't move the horse if random number is 0.
* @return true if horse wins, else false
*/
bool flipAndMoveHorse0()
{
srand ( time(0) );//seeds the random number generator with current time.This ensures different number every time rand() is called   
if(rand()%2==1)//generates a random number and divide by 2.If even gives 0 else 1.Horse moves forward we get 1
for(int i=0;i<15;i++)
{
if(horse0[i]=='0')//horse at position i
{
horse0[i]='.';//change current position
horse0[i+1]='0';//move to next position
if((i+1)==14)//if it reaches last position, means win
{
cout<<" Horse0 WINS";
return true;
}
return false;//if not last position, horse has not won yet
}
}
}
/**
* Flips the coin for horse1 and move the horse if random number is 1.
* It doesn't move the horse if random number is 0.
* @return true if horse wins, else false
*/
bool flipAndMoveHorse1()
{
if(rand()%2==1)
for(int i=0;i<15;i++)
{
if(horse1[i]=='1')
{
horse1[i]='.';
horse1[i+1]='1';
if((i+1)==14)
{
cout<<" Horse1 WINS";
return true;
}
return false;
}
}
}
/**
* Flips the coin for horse2 and move the horse if random number is 1.
* It doesn't move the horse if random number is 0.
* @return true if horse wins, else false
*/
bool flipAndMoveHorse2()
{
if(rand()%2==1)
for(int i=0;i<15;i++)
{
if(horse2[i]=='2')
{
horse2[i]='.';
horse2[i+1]='2';
if((i+1)==14)
{
cout<<" Horse2 WINS";
return true;
}
return false;
}
}
}
/**
* Flips the coin for horse3 and move the horse if random number is 1.
* It doesn't move the horse if random number is 0.
* @return true if horse wins, else false
*/
bool flipAndMoveHorse3()
{
if(rand()%2==1)
for(int i=0;i<15;i++)
{
if(horse3[i]=='3')
{
horse3[i]='.';
horse3[i+1]='3';
if((i+1)==14)
{
cout<<" Horse3 WINS";
return true;
}
return false;
}
}
}
/**
* Flips the coin for horse4 and move the horse if random number is 1.
* It doesn't move the horse if random number is 0.
* @return true if horse wins, else false
*/
bool flipAndMoveHorse4()
{
if(rand()%2==1)
for(int i=0;i<15;i++)
{
if(horse4[i]=='4')
{
horse4[i]='.';
horse4[i+1]='4';
if((i+1)==14)
{
cout<<" Horse4 WINS";
return true;
}
return false;
}
}
}
/**
* Simulates coin flip for all horses
* @return true if any of the horse wins, else false
*/
bool flipCoin(){
bool win0=flipAndMoveHorse0();
bool win1=flipAndMoveHorse1();
bool win2=flipAndMoveHorse2();
bool win3=flipAndMoveHorse3();
bool win4=flipAndMoveHorse4();
if(win0||win1||win2||win3||win4)   
return true;
else
return false;
}

/*
* This program simulates a horse race
*
*/
int main(int argc, char** argv) {
cout<<"RACE BEGIN ";
print();
char ch;
while(true){
cout << " Press enter for another turn:";
if(cin.get()== ' ')
{
bool win=flipCoin();
print();
if(win)//race continues till one horse wins
break;
}
else
break;
}
return (EXIT_SUCCESS);
}

Sample Output

RACE BEGIN

0 . . . . . . . . . . . . . .
1 . . . . . . . . . . . . . .
2 . . . . . . . . . . . . . .
3 . . . . . . . . . . . . . .
4 . . . . . . . . . . . . . .

Press enter for another turn:

. 0 . . . . . . . . . . . . .
1 . . . . . . . . . . . . . .
. 2 . . . . . . . . . . . . .
3 . . . . . . . . . . . . . .
4 . . . . . . . . . . . . . .

Press enter for another turn:

. . 0 . . . . . . . . . . . .
1 . . . . . . . . . . . . . .
. . 2 . . . . . . . . . . . .
3 . . . . . . . . . . . . . .
4 . . . . . . . . . . . . . .

Press enter for another turn:

. . . 0 . . . . . . . . . . .
1 . . . . . . . . . . . . . .
. . . 2 . . . . . . . . . . .
3 . . . . . . . . . . . . . .
4 . . . . . . . . . . . . . .

Press enter for another turn:

. . . 0 . . . . . . . . . . .
1 . . . . . . . . . . . . . .
. . . . 2 . . . . . . . . . .
. 3 . . . . . . . . . . . . .
. 4 . . . . . . . . . . . . .

Press enter for another turn:

. . . 0 . . . . . . . . . . .
1 . . . . . . . . . . . . . .
. . . . . 2 . . . . . . . . .
. . 3 . . . . . . . . . . . .
. . 4 . . . . . . . . . . . .

Press enter for another turn:

. . . 0 . . . . . . . . . . .
1 . . . . . . . . . . . . . .
. . . . . . 2 . . . . . . . .
. . . 3 . . . . . . . . . . .
. . . 4 . . . . . . . . . . .

Press enter for another turn:

. . . 0 . . . . . . . . . . .
1 . . . . . . . . . . . . . .
. . . . . . . 2 . . . . . . .
. . . . 3 . . . . . . . . . .
. . . . 4 . . . . . . . . . .

Press enter for another turn:

. . . 0 . . . . . . . . . . .
. 1 . . . . . . . . . . . . .
. . . . . . . 2 . . . . . . .
. . . . 3 . . . . . . . . . .
. . . . 4 . . . . . . . . . .

Press enter for another turn:

. . . 0 . . . . . . . . . . .
. . 1 . . . . . . . . . . . .
. . . . . . . 2 . . . . . . .
. . . . 3 . . . . . . . . . .
. . . . 4 . . . . . . . . . .

Press enter for another turn:

. . . 0 . . . . . . . . . . .
. . . 1 . . . . . . . . . . .
. . . . . . . 2 . . . . . . .
. . . . 3 . . . . . . . . . .
. . . . 4 . . . . . . . . . .

Press enter for another turn:

. . . 0 . . . . . . . . . . .
. . . . 1 . . . . . . . . . .
. . . . . . . 2 . . . . . . .
. . . . 3 . . . . . . . . . .
. . . . 4 . . . . . . . . . .

Press enter for another turn:

. . . 0 . . . . . . . . . . .
. . . . . 1 . . . . . . . . .
. . . . . . . 2 . . . . . . .
. . . . 3 . . . . . . . . . .
. . . . 4 . . . . . . . . . .

Press enter for another turn:

. . . 0 . . . . . . . . . . .
. . . . . . 1 . . . . . . . .
. . . . . . . 2 . . . . . . .
. . . . 3 . . . . . . . . . .
. . . . 4 . . . . . . . . . .

Press enter for another turn:

. . . 0 . . . . . . . . . . .
. . . . . . . 1 . . . . . . .
. . . . . . . 2 . . . . . . .
. . . . 3 . . . . . . . . . .
. . . . 4 . . . . . . . . . .

Press enter for another turn:

. . . . 0 . . . . . . . . . .
. . . . . . . . 1 . . . . . .
. . . . . . . 2 . . . . . . .
. . . . 3 . . . . . . . . . .
. . . . 4 . . . . . . . . . .

Press enter for another turn:

. . . . . 0 . . . . . . . . .
. . . . . . . . . 1 . . . . .
. . . . . . . 2 . . . . . . .
. . . . 3 . . . . . . . . . .
. . . . 4 . . . . . . . . . .

Press enter for another turn:

. . . . . . 0 . . . . . . . .
. . . . . . . . . . 1 . . . .
. . . . . . . 2 . . . . . . .
. . . . 3 . . . . . . . . . .
. . . . 4 . . . . . . . . . .

Press enter for another turn:

. . . . . . . 0 . . . . . . .
. . . . . . . . . . . 1 . . .
. . . . . . . 2 . . . . . . .
. . . . 3 . . . . . . . . . .
. . . . 4 . . . . . . . . . .

Press enter for another turn:

. . . . . . . . 0 . . . . . .
. . . . . . . . . . . . 1 . .
. . . . . . . 2 . . . . . . .
. . . . 3 . . . . . . . . . .
. . . . 4 . . . . . . . . . .

Press enter for another turn:

. . . . . . . . . 0 . . . . .
. . . . . . . . . . . . . 1 .
. . . . . . . 2 . . . . . . .
. . . . 3 . . . . . . . . . .
. . . . 4 . . . . . . . . . .

Press enter for another turn:

. . . . . . . . . 0 . . . . .
. . . . . . . . . . . . . 1 .
. . . . . . . 2 . . . . . . .
. . . . . 3 . . . . . . . . .
. . . . . 4 . . . . . . . . .

Press enter for another turn:

. . . . . . . . . 0 . . . . .
. . . . . . . . . . . . . 1 .
. . . . . . . 2 . . . . . . .
. . . . . . 3 . . . . . . . .
. . . . . . 4 . . . . . . . .

Press enter for another turn:

. . . . . . . . . . 0 . . . .
. . . . . . . . . . . . . 1 .
. . . . . . . 2 . . . . . . .
. . . . . . 3 . . . . . . . .
. . . . . . . 4 . . . . . . .

Press enter for another turn:

. . . . . . . . . . . 0 . . .
. . . . . . . . . . . . . 1 .
. . . . . . . 2 . . . . . . .
. . . . . . 3 . . . . . . . .
. . . . . . . . 4 . . . . . .

Press enter for another turn:

Horse1 WINS
. . . . . . . . . . . . 0 . .
. . . . . . . . . . . . . . 1
. . . . . . . 2 . . . . . . .
. . . . . . 3 . . . . . . . .
. . . . . . . . 4 . . . . . .


RUN SUCCESSFUL (total time: 20s)