Advertisement
akosiraff

Download Horse C++ Answer

Mar 7th, 2018
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1.  
  2. Download: https://solutionzip.com/downloads/horse/
  3. Overview: 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:
  4. private name: A string holding the horses name.
  5. private rider: A string holding the rider’s name.
  6. private maxRunningDistPerSecond: An int that holds the maximum distance the horse could run in one second.
  7. private distanceTraveled: How far the horse has gone already.
  8. private racesWon: An int that determines how much races this horse and rider have won
  9. In addition, the class should have the following constructors and member functions:
  10. 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.
  11. Accessors and mutators: Appropriate accessor and mutator functions should be used as required by the following methods:
  12. runASecond: A method that adds to distanceTraveled an amount from 0-maxRunningDistPerSecond. This is the method that moves this horse along.
  13. sendToGate: Reset the horse to the start of a race by setting distanceTraveled to 0.
  14. 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.
  15. Some examples:
  16. |—————> | Pharaoh, ridden by Mark
  17. |——————–> | Secretariat, ridden by George
  18. |————> | Calamity Jane, ridden by Mary
  19. 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:
  20. |——————————–|>John
  21. indicating the horse has finished the race.
  22. 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.
  23. 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.
  24. 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.
  25. You can include extra methods or attributes if you want, but make sure to only make members public if it is absolutely necessary.
  26. This project will require 3 files.
  27. main.cpp, the main driver program.
  28. Horse.h: The header file for the Horse class
  29. Horse.cpp: The file defining the Horse methods
  30. 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.
  31. Your output should look very similar to the following:
  32. How many horses are in the race: 3
  33. Please give me the name of horse 1: Pharaoh
  34. Please give me the rider of rider 1: Mark
  35. Please give me the name of horse 2: Secretariat
  36. Please give me the name of rider 2: George
  37. Please give me the name of horse 3: Calamity Jane
  38. Please give me the name of rider 3: Mary
  39. Please enter the distance of the race: 500
  40. The start!
  41. |> | Pharaoh, ridden by Mark
  42. |> | Secretariat, ridden by George
  43. |> | Calamity Jane, ridden by Mary
  44. Are you ready for the next second(y/n)?:y
  45. | > | Pharaoh, ridden by Mark
  46. | > | Secretariat, ridden by George
  47. | > | Calamity Jane, ridden by Mary
  48. Are you ready for the next second(y/n)?:y
  49. | > | Pharaoh, ridden by Mark
  50. | > | Secretariat, ridden by George
  51. | > | Calamity Jane, ridden by Mary
  52. and so on, until a winner is found, which would end with some output like this:
  53. | |> Pharaoh, ridden by Mark
  54. | > | Secretariat, ridden by George
  55. | > | Calamity Jane, ridden by Mary
  56. Pharaoh has won 1/1 races.
  57. Secretariat has won 0/1 races.
  58. Calamity Jane has won 0/1 races.
  59. Do you wish to continue(y/n)?:
  60. 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.
  61.  
  62. Download: https://solutionzip.com/downloads/horse/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement