Question: 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
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 o string name: the name of the horse o int distance: how far the horse has traveled o int offset: used to track penalties and bonuses from events o 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------------- //----------------------------------
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
