This project is for testing the introduction of classes and dynamic allocation o
ID: 3726865 • Letter: T
Question
This project is for testing the introduction of classes and dynamic allocation of arrays of classes. In this assignment, you will be performing a horse racing simulation. To start with, you will write a Horse class. The Horse class should have the following member variables:
In addition, the class should have the following constructors and member functions:
Constructor: This constructor should accept the horse’s name and rider as arguments. It should initialize each horse to a random maxRunningDistPerSecond(1-100). DistanceTraveled should be set to 0.
Accessors and mutators: Appropriate accessor and mutator functions should be used as required by the following methods:
runASecond: A method that adds to distanceTraveled an amount from 0-maxRunningDistPerSecond. This is the method that moves this horse along.
sendToGate: Reset the horse to the start of a race by setting distanceTraveled to 0.
displayHorse(int goalLength): This method should attempt to draw where the horse is along its race. Should try to graphically display in ascii how far the horse has gone on its way to goalLength. This should be scaled so it doesn’t overrun the screen.
Some examples:
So if a horse has “distanceTraveled” of 50 and the goalLength passed is 100, then the > for the horse should be halfway towards the “goal”. If it is called where the distanceTraveled is greater than the goalLength (indicating it has finished), this output would be:
indicating the horse has finished the race.
Demonstrate this class in a program by first prompting the user for the number of horses in the race. Then create an array of pointers to Horse objects, filling it up as appropriate (an array of pointers, so you can use the new operator to call the constructors of each one). Then prompt the user for a distance to race. Since horses can travel in any time interval a max of 100, a good distance of 1000 could be appropriate to test.
In a loop, start the race. In this loop, for each interval of the loop, execute runASecond for each horse. Then output all the horses toString to show the user the current race status. Then prompt the user if they want to continue the race. Keep doing this until one of the horses has “won” the race. If there is a tie, break the tie by distanceTraveled. If there is a tie in distanceTraveled, randomly pick a winner.
Your program should allow multiple races with the same horses. At the end of each race, display the winner and statistics on how many each horse has won or lost.
You can include extra methods or attributes if you want, but make sure to only make members public if it is absolutely necessary.
This project will require 3 files.
Note: If you are using an online compiler like www.cpp.sh (Links to an external site.)Links to an external site., then you may put the contents of Horse.h at the beginning of the program, followed by the contents of Horse.cpp, followed by the contents of main.cpp. This way, all code will be in one place.
Your output should look very similar to the following:
and so on, until a winner is found, which would end with some output like this:
Error conditions: You must check for any invalid entries. For example, when asking for the number of horses, do not accept a negative answer and re-prompt until an appropriate result is given. Only accept race distances of 100 or greater, again re-prompting if necessary.
Note: Make sure to follow proper programming standards. This should be a good time to review the supplement, as we are now talking about classes and member functions.
Explanation / Answer
//main.cpp
#include <iostream>
#include "Horse.h"
#define MIN_NUM_HORSES 2
#define MIN_RACE_DIST 100
// driver program
int main() {
int i;
// input # of horses and validate is # and >= MIN_NUM_HORSES
int numHorses;
cout << "How many horses are in the race: ";
while (!(cin >> numHorses && numHorses >= MIN_NUM_HORSES)) {
cin.clear();
cin.ignore(512, ' ');
// re-prompt user
cout << "Need at least " << MIN_NUM_HORSES << " horses to begin a race: ";
}
// allocate horse objects
Horse *horses[numHorses];
// input horse name and rider name for all horses
string name;
string rider;
for (i = 0; i < numHorses; i++) {
cout << "Please give me the name of horse " << (i + 1) << ": ";
cin >> name;
cout << "Please give me the name of rider " << (i + 1) << ": ";
cin >> rider;
horses[i] = new Horse(name, rider);
}
// input race distance and validate is # and >= MIN_RACE_DIST
int raceDist;
cout << "Please enter the distance of the race: ";
while (!(cin >> raceDist && raceDist >= MIN_RACE_DIST)) {
cin.clear();
cin.ignore(512, ' ');
// re-prompt user
cout << "Race distance needs to be a minimum of " << MIN_RACE_DIST << ": ";
}
// current # of races
int numRaces = 0;
// races loop
do {
numRaces++;
// reset all horses
for (i = 0; i < numHorses; i++) {
horses[i]->sendToGate();
}
// display all horses starting position
cout << " The start!" << endl;
for (i = 0; i < numHorses; i++) {
horses[i]->displayHorse(raceDist);
}
cout << " Are you ready for the next second(y/n)?: ";
cin.ignore();
// check if user wants to start race
if (cin.get() == 'y') {
bool someHorseWon = false;
// seconds loop
do {
// run a second, check if any horse won, display horse, ask user to update race by 1 second
Horse *h;
for (i = 0; i < numHorses; i++) {
h = horses[i];
// run second for horse
h->runASecond();
cout << h->getDistanceTraveled() << endl;
// check if any horse won
if (h->getDistanceTraveled() >= raceDist) {
someHorseWon = true;
}
// display horse on track and horse info.
h->displayHorse(raceDist);
}
if (someHorseWon) {
someHorseWon = false;
break;
}
// ask user to update another second
cout << " Are you ready for the next second(y/n)?: ";
cin.ignore();
} while (cin.get() == 'y');
}
// get horse that actually won
int horseWinnerIndex = 0;
int maxDistance = 0;
Horse *h;
for (i = 0; i < numHorses; i++) {
h = horses[i];
if (h->getDistanceTraveled() > maxDistance) {
maxDistance = h->getDistanceTraveled();
horseWinnerIndex = i;
}
}
horses[horseWinnerIndex]->win();
// display race info.
for (i = 0; i < numHorses; i++) {
cout << horses[i]->getName() << " has won " << horses[i]->getRacesWon() << "/" << numRaces << " races." << endl;
}
// ask user for another race
cout << "Do you wish to continue(y/n)?: ";
cin.ignore();
} while (cin.get() == 'y');
// deallocate all horse objects
for (i = 0; i < numHorses; i++) {
delete horses[i];
}
return 0;
}
----------------------------------------------------------------------------------------------
//Horse.cpp
#include "Horse.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
Horse::Horse(string name, string rider) {
this->name = name;
this->rider = rider;
srand((unsigned) time(NULL));
sendToGate();
}
Horse::~Horse() {
}
void Horse::runASecond() {
distanceTraveled += maxRunningDistPerSecond;
}
void Horse::sendToGate() {
maxRunningDistPerSecond = 1 + rand() % 101;
distanceTraveled = 0;
}
void Horse::displayHorse(int goalLength) {
cout << "|";
for (int i = 0; i < DISPLAY_RACE_DIST; i++) {
if (i == (distanceTraveled * DISPLAY_RACE_DIST) / goalLength) {
cout << ">";
} else {
cout << " ";
}
}
cout << "|";
if (distanceTraveled >= goalLength) {
cout << ">";
} else {
cout << " ";
}
cout << " " << name << ", " << "ridden by " << rider << endl;
}
void Horse::win() {
racesWon++;
}
---------------------------------------------------------------------------------------------------------
//Horse.h
#include <string>
using namespace std;
#ifndef HORSE_H
#define HORSE_H
class Horse {
private:
static const int DISPLAY_RACE_DIST = 50;
string name;
string rider;
int maxRunningDistPerSecond;
int distanceTraveled;
int racesWon = 0;
public:
Horse(string, string);
~Horse();
void runASecond();
void sendToGate();
void displayHorse(int);
void win();
string getName() const {
return name;
}
int getDistanceTraveled() const {
return distanceTraveled;
}
int getRacesWon() const {
return racesWon;
}
};
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.