C++ I need to add a function for the following code which determines and display
ID: 3817911 • Letter: C
Question
C++
I need to add a function for the following code which determines and displays the winner of the race.
//Program Description: Horse Racing Game
//Date: April 12, 2017
#include
#include//for rand() code
#include//For time() code
#include//For sleep() code
using namespace std;
int speed(int horseSpeed)
{
horseSpeed = horseSpeed + rand() % 10 + 1;
//This code will be randomly generated between 1 and 10.
return horseSpeed;
}
void horsePosition(int a)
{
int i;
a = a / 10; //Distance horse travel on screen.
//Larger values slow the movement down.95
for (i = 0; i <= a; i++)
{
//loop will place horse on screen in proper position.
if (i == a)
cout << "~n-n^" << endl;
else
cout << " "; //will draw space until the current position is reached.
}
}
int main()
{
int a, b, c, d, e, f, g, h;
a = b = c = d = e = f = g = h = 0; //Set all horses to same tarting position
int race, winner = 0;
srand(time(NULL)); //Null previous random number.
//This ensures the program will generate new random numbers.
for (race = 0; race <= 50; race++)//Race continues for 50 iterations
{
a = speed(a);//Call function to get speed for each horse returns a cumulative value that contines to build into the end of the race.
b = speed(b);
c = speed(c);
d = speed(d);
e = speed(e);
f = speed(f);
g = speed(g);
h = speed(h);
cout << "Speedy: ";
horsePosition(a);//Call function for the horse position
cout << "Rider: ";
horsePosition(b);
cout << "Blaze: ";
horsePosition(c);
cout << "Jolt: ";
horsePosition(d);
cout << "Knight: ";
horsePosition(e);
cout << "Boulder: ";
horsePosition(f);
cout << "Torrent: ";
horsePosition(g);
cout << "Wave: ";
horsePosition(h);
cout << " Final Count Values" << endl;
cout << "Shadow: " << a << endl;
cout << "Luster: " << b << endl;
cout << "Blaze: " << c << endl;
cout << "Jolt: " << d << endl;
cout << "Knight: " << e << endl;
cout << "Boulder: " << f << endl;
cout << "Torrent: " << g << endl;
cout << "Wave: " << h << endl;
Sleep(25);//Slows down the horses. CHange the value to add drama.
if (race < 50)
system("CLS"); //Keep screen clear until end of race.
}
system("pause");
return 0;
}
Explanation / Answer
// Please note am just adding code to print winner on top of your code and is not changing anything else. For example am not adding what specific header files are requried.
//Program Description: Horse Racing Game
//Date: April 12, 2017
#include
#include//for rand() code
#include//For time() code
#include//For sleep() code
using namespace std;
int max(a, b)
{
if (a > b)
return a;
else
return b;
}
void printWinner(int a, int b, int c, int d, int e, int f, int g, int h)
{
int winner = max((max(max(max(max(max(max(a, b),)c), d), e), f), h);
cout << "Winner is: ";
switch(winner)
{
case a:
cout << "Speedy";
break;
case b:
cout << "Rider";
break;
case c:
cout << "Blaze";
break;
case d:
cout << "Jolt";
break;
case e:
cout << "Knight";
break;
case f:
cout << "Boulder";
break;
case g:
cout << "Torrent";
break;
case h:
cout << "Wave";
break;
}
cout << endl;
}
int speed(int horseSpeed)
{
horseSpeed = horseSpeed + rand() % 10 + 1;
//This code will be randomly generated between 1 and 10.
return horseSpeed;
}
void horsePosition(int a)
{
int i;
a = a / 10; //Distance horse travel on screen.
//Larger values slow the movement down.95
for (i = 0; i <= a; i++)
{
//loop will place horse on screen in proper position.
if (i == a)
cout << "~n-n^" << endl;
else
cout << " "; //will draw space until the current position is reached.
}
}
int main()
{
int a, b, c, d, e, f, g, h;
a = b = c = d = e = f = g = h = 0; //Set all horses to same tarting position
int race, winner = 0;
srand(time(NULL)); //Null previous random number.
//This ensures the program will generate new random numbers.
for (race = 0; race <= 50; race++)//Race continues for 50 iterations
{
a = speed(a);//Call function to get speed for each horse returns a cumulative value that contines to build into the end of the race.
b = speed(b);
c = speed(c);
d = speed(d);
e = speed(e);
f = speed(f);
g = speed(g);
h = speed(h);
cout << "Speedy: ";
horsePosition(a);//Call function for the horse position
cout << "Rider: ";
horsePosition(b);
cout << "Blaze: ";
horsePosition(c);
cout << "Jolt: ";
horsePosition(d);
cout << "Knight: ";
horsePosition(e);
cout << "Boulder: ";
horsePosition(f);
cout << "Torrent: ";
horsePosition(g);
cout << "Wave: ";
horsePosition(h);
cout << " Final Count Values" << endl;
cout << "Shadow: " << a << endl;
cout << "Luster: " << b << endl;
cout << "Blaze: " << c << endl;
cout << "Jolt: " << d << endl;
cout << "Knight: " << e << endl;
cout << "Boulder: " << f << endl;
cout << "Torrent: " << g << endl;
cout << "Wave: " << h << endl;
Sleep(25);//Slows down the horses. CHange the value to add drama.
if (race < 50)
system("CLS"); //Keep screen clear until end of race.
}
printWinner(a,b,c,d,e,f,g,h);
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.