Question
So I am trying to complete a program for a horse race. This first portion is the assignment itself and the requirements 3. Create a
So I am trying to complete a program for a horse race.
This first portion is the assignment itself and the requirements
3. Create a Program From Pseudocode
This exercise will be to use pseudocode (in the form of comments) to write a program to simulate a horse race. You will need the following variables.
struct Horse>member variables
string name: the name of the horse
int distance: how far the horse has traveled
int offset: used to track penalties and bonuses from events
int ID: the number of the horse and index for horses
global constant int horseCount = 6: the number of horses in the race
global constant int trackLength = 100: the length of the race track
std::array horses
int die1, die2 for die rolls (These are optionalthe sum can be generated without them.)
int sum is used to store the sum of the die rolls.
int winner is the winning horses ID.
int lead is used to track the distance of the leading horse.
int bet is the amount bet on the race.
int cash is the amount of money the player has.
bool racing is a flag used in the Do-While racing loop. While it is true, the race continues.
bool playing is a flag used in the Do-While game loop. While it is true, the game continues.
int hNum is the number (ID) of the horse the player bet on.
Use a range-based For loop to initialize the horses data. Ask the player for names or hard code themyour choice.
At the start of each race, ask the player to make a bet and pick a horse.
Each turn during the race, each horse will move forward based on the sum of the roll of two six-sided dice (16). This value is augmented by an offset. The offset can be negative or positive. Add the offset to the sum of the dice. (Note: If the offset is negative, adding to the sum will reduce the sums value.) If the sum is negative, store the negative value in offset and leave the horses distance value unchanged. If the sum is positive, add it to the horses distance and set the offset to zero. Once any horse has gone more than the length of the track, the race is over and the horse with the greatest distance value wins. Add six times the bet to cash if the player wins, or subtract the bet from cash if the player loses. The game is over when the player has run out of money. You also quit when the player bets $0 or less.
The offset value is calculated using a Switch statement and a random number between 0 and 15. If the number is 0 or 1, the horse breaks stride and the offset is negative 1 or 2 (randomly selected). If the random number is 2 or 3, the horse finds his stride and the offset is a positive 1 or 2 (randomly selected). If the random number is 4, the horse stumbles and the offset is a negative 46 (randomly selected). If the random number is 5, the horse has a burst of energy and the offset is a positive 46 (randomly selected). If the random number is 615, nothing happens.
Reset the values of the horses distance and offset using a range-based For loop before each new race.
Here is the pseudocode.
// Week 4 Assignment-3
// Description: Horserace
//----------------------------------
//**begin #include files************
#include // provides access to cin and cout
#include // provides access to std::array
#include // provides access to time()
#include // provides access to getline()
#include
//--end of #include files-----------
//----------------------------------
using namespace std;
//----------------------------------
//**begin global constants**********
// struct for horse (name, distance, eventOffset, ID)
// number of horses (6)
// length of track (100)
//--end of global constants---------
//----------------------------------
//**begin main program**************
int main()
{
// seed random number generator
srand(time(NULL));
// create and initialize variables
// create horses
// have user name horses (range-based for loop)
// two 1-6 dice
// sum of dice roll
// number of winning horse
// distance of leading horse
// bet
// cash on hand
// flag for ending race
// flag for ending game
// horse number bet on
// start of game loop
// ask for bet amount
// if bet is less than or equal to $0-- quit
// else get the number of the horse being bet on
// start of race loop
// for each horse--use range-based for
// roll one 16 sided die to check on event
// use switch statement to handle event
//(see instructions for event)
// roll dice and, using offset, adjust horses distance
// check for race over
// report horse position
// check if this is leading horse and change lead and winner values if it is
//continue race loop until race is over
//end race loop
// report winner and settle bet
// if player has cash, get new bet, new horse, and start race
// else quit
// Wait for user input to close program when debugging.
cin.get();
return 0;
}
//--end of main program-------------
//----------------------------------
The following after this point is what I have so far completed.
My question for this is how to make it work correctly because everytime I run it, it becomes an infinite loop.
// Week 4 Assignment-3
// Description: Horserace
//----------------------------------
//**begin #include files************
#include // provides access to cin and cout
#include // provides access to std::array
#include // provides access to time()
#include // provides access to getline()
#include
//--end of #include files-----------
//----------------------------------
using namespace std;
//----------------------------------
//**begin global constants**********
// struct for horse (name, distance, eventOffset, ID)
struct Horse
{
string name;
int distance;
int Offset;
int ID;
};
// number of horses (6)
const int horseCount = 6;
// length of track (100)
const int trackLength = 100;
std::array horses;
//--end of global constants---------
//----------------------------------
//**begin main program**************
int main()
{
horses[0].ID = 1;
horses[1].ID = 2;
horses[2].ID = 3;
horses[3].ID = 4;
horses[4].ID = 5;
horses[5].ID = 6;
//--------------------------------------------
// seed random number generator
srand(time(NULL));
// create and initialize variables
// create horses
for (Horse &h : horses)// have user name horses (range-based for loop)
{
cout << "What's the name of this horse? ";
cin >> h.name;
cout << endl;
}
// two 1-6 dice -----------------------------
int die1 = rand() % 6 + 1;
int die2 = rand() % 6 + 1;
// sum of dice roll
int sum;
sum = (die1 + die2);
// bet
int bet;
// cash on hand
int cash = 100;
bool racing = false;
bool playing = true;
int betNum;
//-------------------------------------------
// start of game loop
while (playing == true)
{// ask for bet amount
cout << "You have $" << endl;
cout << "How much do you want to bet? ";
cin >> bet;
// if bet is less than or equal to $0-- quit
if (bet <= 0)
{
playing = false;
}
// else get the number of the horse being bet on
else
{
cout << "Which horse number (from 1-6) do you want to bet on? ";
cin >> betNum;
cout << endl;
racing = true;
cash = (cash - bet);
}
// start of race loop
while(racing == true)
{
cout << "The horses are off!" << endl;
// for each horse--use range-based for
//closes for loop to start race
//---------------------------------------------------------------
int winner = 0;
if (betNum == winner)
{
cout << "Your horse won!" << endl;
}
else
{
cout << "Your horse didn't win!" << endl;
}
cin.get();
if (cash <= 0)
{
cout << "Uh-oh! You ran out of money!" << endl;
system("pause");
playing = false;
}
}//end race loop
// report winner and settle bet
// if player has cash, get new bet, new horse, and start race
// else quit
// Wait for user input to close program when debugging.
return 0;
}
//--end of main program-------------
//----------------------------------
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started