Guest User

Untitled

a guest
Sep 20th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. /********************************************************
  2. * *
  3. * Written by Mikaiyl i3nigma <[email protected]> *
  4. * *
  5. * This program simulates a derby style horse race. *
  6. * *
  7. *******************************************************/
  8.  
  9. #include <iostream>
  10. #include <stdio.h>
  11. #include <ctime>
  12. #include <cstdlib>
  13.  
  14. using namespace std;
  15.  
  16. bool winner = false;
  17.  
  18. /************************
  19. * Create Horse Class *
  20. ************************/
  21.  
  22. class Horse {
  23. private:
  24.  
  25. char name;
  26. int position = 0;
  27.  
  28. public:
  29. Horse(void);
  30. void incriment(void);
  31. void printP(void);
  32. void setName(char n);
  33. void isWinner(void);
  34. int getPos(void);
  35. };
  36.  
  37. /* Horse constructor, set default name to '0' */
  38. Horse::Horse(void){
  39. name = '0';
  40. }
  41.  
  42. /* Method to move horse randomly */
  43. void Horse::incriment(void){
  44. if(rand() % 100 > 50 && position < 20){
  45. position++;
  46. } else {}
  47. }
  48.  
  49. /* Print horse's position */
  50. void Horse::printP(void){
  51. char p[20];
  52. for(int i = 0; i < 22; i++){
  53. if(i == position){
  54. p[i] = name;
  55. } else {
  56. p[i] = '.';
  57. }
  58. }
  59. printf("%s\n", p);
  60. }
  61.  
  62. /* Set horse's name */
  63. void Horse::setName(char n){
  64. name = n;
  65. }
  66.  
  67. /* Get horses position */
  68. int Horse::getPos(void){
  69. return position;
  70. }
  71.  
  72. /* Check for winning horse */
  73. void Horse::isWinner(void){
  74. if( winner == false && position == 20){
  75. printf("Horse %c wins!\n", name);
  76. winner = !winner;
  77. }
  78. }
  79.  
  80. int main(){
  81.  
  82. /* Seed rand() */
  83. srand(time(NULL));
  84.  
  85. /* Create five instances of horse class */
  86. Horse a, b, c, d, e;
  87.  
  88. /* Name the horses */
  89. a.setName('0');
  90. b.setName('1');
  91. c.setName('2');
  92. d.setName('3');
  93. e.setName('4');
  94.  
  95. do {
  96.  
  97. /* Randomly incriment the horses */
  98. a.incriment();
  99. b.incriment();
  100. c.incriment();
  101. d.incriment();
  102. e.incriment();
  103.  
  104. /* Print the horses */
  105. a.printP();
  106. b.printP();
  107. c.printP();
  108. d.printP();
  109. e.printP();
  110.  
  111. /* Check for a winner */
  112. a.isWinner();
  113. b.isWinner();
  114. c.isWinner();
  115. d.isWinner();
  116. e.isWinner();
  117.  
  118. /* Prompt user to continue */
  119. cout << "Press enter for another turn:";
  120. cin.ignore();
  121.  
  122. } while( a.getPos() < 20 || b.getPos() < 20 || c.getPos() < 20 || d.getPos() < 20 || e.getPos() < 20);
  123.  
  124. return 0;
  125.  
  126. }
Advertisement
Add Comment
Please, Sign In to add comment