Question
3. Create a C++ Program From Pseudocode This exercise will use pseudocode (in the form of comments) to again write a program to simulate a
3. Create a C++ Program From Pseudocode
This exercise will use pseudocode (in the form of comments) to again write a program to simulate a horse race. This time, we will use variables to clean up the code and make it easier to modify. 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
int die1, die2 for die rolls
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 racing While loop. While it is true, the race continues.
bool playing is a flag used in the game While loop. While it is true, the game continues.
int hNum is the number (ID) of the horse the player bet on.
Create the following functions.
void getName(Horse, &): Gets a horse name from the user. Note: This changes the horses data and does not return anything (pass by reference). You could optionally have it return the name instead and use pass by value or you could pass by reference and return the name, as we do in the next function. (See Challenge 1 below.)
int checkEvents(Horse, &): Simulate the roll of a 16-sided die [0 ->15] using (rand()%16 ). Use a Switch statement to accomplish the following.
Case 0 or 1: Horse breaks stride and loses 1 or 2 movement points (offset = -(rand()%2+1)).
Case 2 or 3: Horse finds its stride and gains 1 or 2 movement points.
Case 4: Horse stumbles and loses 4, 5, or 6 movement points (offset = -(rand()%3+4)).
Case 5: Horse has a burst of energy and gains 4, 5, or 6 movement points.
Return the horses offset. Note that if you pass by referenceas the prototype above impliesyou dont need to return the horses offset. Changing a value internally and returning the value is a common pattern in C libraries. It allows you the flexibility of either just calling the function to change the horses offset or calling the function as part of a line of code that uses the results. See the following example.
myHorse.distance += checkEvents(myHorse); allows you to add the offset using the function call.
checkEvents(myHorse);
myHorse.distance += myHorse.offset; allows you to calculate the offset and then use the value.
int updatePos( Horse, &): Simulate the rolling of two six-sided dice [1 -> 6] using (rand()%6+1). Get the total sum of the two dice. Add the horses offset to the sum. If the sum is negative, make it the new offset; otherwise, clear the offset and add sum to the distance. Return distance. This is a pass by reference function.
void reportPos(array
void clearData(array
Use a range-based For loop to initialize the horses data. Ask player for names or hard code themyour choiceusing the getname() function.
At the start of each race, ask the player to make a bet and pick a horse.
For each turn during the race, using a range-base For loop, you will first call the checkEvents() function to calculate the offset for each horse, then call updatePos() function to get the horses new position. Outside the loop, use the reportPos() function to print the position of each horse. 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. If the player wins, add six (or however many horses there are in the race) times the bet to cash. If the player loses, subtract the bet from cash. The game is over when the player has run out of money or if the player bets $0. If the player chooses to continue, clear the horses data using the clearData() function and restart the race after having the player make another bet and choose another horse.
The function algorithms are documented above, but for clarity, the description of how the key functions work is repeated here.
The checkEvents() function calculates the offset value using a Switch statement. A random number between 0 and 15 is generated and used as the switch control value. 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 its 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.
The updatePos() function will simulate the horse moving forward based on the sum of the roll of two six-sided dice (16). This value is augmented by the offset calculated in checkEvents(). The offset can be negative or positive. The offset is added to the sum of the dice. (Note: If the offset is negative, adding it to the sum reduces the sum 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 clear the offset.
Challenges
The two challenges presented here are optional. If you are feeling confident in your progress and want something more challenging, you might want to attempt these. There is not extra credit for completing a challenge beyond the satisfaction of completion.
Challenge 1: Instead of getting the names from the player in getNames(), get the names from a text file. You could even give the player a choice of creating his or her own names, using default names, or getting names from a file he or she specifies.
Challenge 2: Instead of just listing the horse positions based on where they are in the file, you could list them in the order of where they are on the track, either front horse first or last horse first.
Here is the pseudocode.
// Week 5 Assignment-3
// Description: Horserace - version 2
//----------------------------------
//**begin #include files************
#include
#include
#include
#include
#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 function prototypes*******
// get horse name
void getName(Horse &);
// Racing events
int checkEvents(Horse &);
// Update positions
int updatePos(Horse &);
// Report positions
void reportPos(array
// Clear racing data
void clearData( array
//--end of function prototypes------
//----------------------------------
//**begin main program**************
int main()
{
// seed random number generator
srand(time(NULL));
// CREATE AND INITIALIZE VARIABLES
// create a std::array of horses
// using a range-based for loop, initialize data for each horse
// have user name horses
// clear distance and offset.
// create ID. (you will probably want to create a index variable external to the loop to do this)
// number of winning horse (init to 0)
// distance of leading horse (init to 0)
// bet
// cash on hand (init to 100--or whatever value you chose)
// flag to indicate the race is active in race do-while loop (init to true)
// flag to indicate the game is active in game do-while loop (init to true)
// horse number bet on
// START OF GAME LOOP
// ask for bet amount and get the number of the horse being bet on
// if the bet is negative or $0, quit.
// START OF RACE LOOP
// for each horse--use range-based for
// check for events using a function and return horse's offset
// roll dice and, using offset, adjust horse's distance using a function
// check for race over
// check if this is leading horse and change lead and winner values if it is
// report horse position using function
//continue race loop until race is over;
// END OF RACE LOOP
//report winner and settle bet
// if player wants to bet in another race
// reset horses and start again
// else quit
// END OF GAME LOOP
// Wait for user input to close program when debugging.
cin.get();
return 0;
}
//--end of main program-------------
//----------------------------------
//**begin function definitions******
// Get horse names
void getName(Horse &Nag)
{
// ask for horse's name
// save name
}
// Racing events
int checkEvents(Horse &Nag)
{
// roll one 16 sided die to check on event (see instructions for event)
// use switch statement to handle event
// case 0, 1: horse breaks stride lose 1-2
// case 2, 3: horse find its stride gain 1-2
// case 4: horse stumbles lose 4-6
// case 5: horse has energy burst gain 4-6
}
// Update positions
int updatePos(Horse &Nag)
{
// simulate rolling two dice using rand()%6+1
// sum up the total of the two dice
// add the horse's offset to the sum
// if the sum is negative make it the new offset
// else add the sum to the distance and clear offset
// return the distance
}
// Report positions
void reportPos(array
{
// use range-based for loop
// print horse position
}
// Clear racing data
void clearData( array
{
// use range-base for loop
// clear distance
// clear offset
}
//--end of function definitions------
//----------------------------------
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