No Description

main.cpp 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. //
  2. // 1 on 1 basketball simulator.
  3. // Rafael Arce Nazario - 2014
  4. //
  5. // This program simulates a 1 on 1 game between two players
  6. // based only on their shooting percentages, rebounds per game.
  7. //
  8. #include <iostream>
  9. #include <cassert>
  10. #include <cmath>
  11. using namespace std;
  12. typedef unsigned int uint;
  13. class BBPlayer {
  14. private:
  15. string _name;
  16. uint _shotsTaken;
  17. uint _shotsMade;
  18. uint _gamesPlayed;
  19. uint _rebounds;
  20. uint _score;
  21. public:
  22. BBPlayer();
  23. BBPlayer(string name, int shotsTaken, int shotsMade, int gamesPlayed, int rebounds);
  24. float shotPercentage() const;
  25. float reboundsPerGame() const;
  26. void shotMissed();
  27. void shotMade();
  28. void reboundMade();
  29. void addGame();
  30. void setAll(string name, int shotsTaken, int shotsMade, int gamesPlayed, int rebounds);
  31. uint score() const;
  32. string name() const;
  33. void printStats() const;
  34. };
  35. BBPlayer::BBPlayer() {
  36. _shotsTaken = _shotsMade = _gamesPlayed = _rebounds = _score = 0;
  37. }
  38. BBPlayer::BBPlayer(string name, int shotsTaken, int shotsMade, int gamesPlayed, int rebounds) {
  39. _name = name;
  40. _shotsTaken = shotsTaken >= 0 ? shotsTaken : 0;
  41. _shotsMade = shotsMade >= 0 ? shotsMade : 0;
  42. _shotsMade = shotsMade <= shotsTaken ? shotsMade : shotsTaken;
  43. _gamesPlayed = gamesPlayed >= 0 ? gamesPlayed : 0;
  44. _rebounds = rebounds >= 0 ? rebounds : 0;
  45. _score = 0;
  46. if (_gamesPlayed == 0 && (_shotsTaken | _rebounds) ) {
  47. cout << "You've just declared a magical player. No games played. "
  48. << "Yet he has rebounds or shots taken."
  49. << "Please verify object definition" << endl;
  50. }
  51. }
  52. void BBPlayer::setAll(string name, int shotsTaken, int shotsMade, int gamesPlayed, int rebounds) {
  53. _name = name;
  54. _shotsTaken = shotsTaken >= 0 ? shotsTaken : 0;
  55. _shotsMade = shotsMade >= 0 ? shotsMade : 0;
  56. _shotsMade = shotsMade <= shotsTaken ? shotsMade : shotsTaken;
  57. _gamesPlayed = gamesPlayed >= 0 ? gamesPlayed : 0;
  58. _rebounds = rebounds >= 0 ? rebounds : 0;
  59. _score = 0;
  60. if (_gamesPlayed == 0 && (_shotsTaken | _rebounds) ) {
  61. cout << "You've just declared a magical player. No games played. "
  62. << "Yet s/he has rebounds or shots taken."
  63. << "Please verify object definition" << endl;
  64. }
  65. }
  66. string BBPlayer::name() const{
  67. return _name;
  68. }
  69. float BBPlayer::shotPercentage() const {
  70. return _shotsTaken == 0 ? .5 : static_cast<float>(_shotsMade) / _shotsTaken;
  71. }
  72. float BBPlayer::reboundsPerGame() const {
  73. return _gamesPlayed == 0 ? 3 : static_cast<float>(_rebounds) / _gamesPlayed;
  74. }
  75. void BBPlayer::shotMissed() {
  76. _shotsTaken++;
  77. }
  78. void BBPlayer::shotMade() {
  79. _shotsTaken++;
  80. _shotsMade++;
  81. _score += 2;
  82. }
  83. void BBPlayer::reboundMade() {
  84. _rebounds++;
  85. }
  86. void BBPlayer::addGame() {
  87. _gamesPlayed++;
  88. }
  89. uint BBPlayer::score() const {
  90. return _score;
  91. }
  92. void BBPlayer::printStats() const {
  93. cout << "Stats for " << _name << endl;
  94. cout << "Games played: " << _gamesPlayed << endl;
  95. cout << "Rebounds: " << _rebounds << endl;
  96. cout << "Shots taken: " << _shotsTaken << endl;
  97. cout << "Shots made: " << _shotsMade << endl;
  98. }
  99. void test_BBPlayer() {
  100. BBPlayer T("Chemba", 100, 65, 10, 35);
  101. // Test the name() method
  102. assert( T.name() == "Chemba" );
  103. // Test the initial shot pct
  104. assert( abs( T.shotPercentage() - 0.65) < 0.00001 );
  105. // Test after a shot is made
  106. T.shotMade();
  107. assert( abs( T.shotPercentage() - (1.0 * 66 / 101) ) < 0.00001) ;
  108. // Test after a shot is missed
  109. T.shotMissed();
  110. assert( abs( T.shotPercentage() - (1.0 * 66 / 102) ) < 0.00001 );
  111. // Test the initial rebounds per game
  112. assert( abs( T.reboundsPerGame() - 3.5 ) < 0.00001 );
  113. // Test the score up to this point
  114. assert( T.score() == 2);
  115. // Make two shots, then test score and pct
  116. T.shotMade();
  117. T.shotMade();
  118. assert( T.score() == 6);
  119. assert( abs( T.shotPercentage() - (1.0 * 68 / 104) ) < 0.00001 );
  120. // Make two rebounds then test
  121. T.reboundMade();
  122. T.reboundMade();
  123. assert( abs( T.reboundsPerGame() - 3.7 ) < 0.00001 );
  124. // Tests if the number of game has increased.
  125. T.addGame();
  126. assert( abs( T.reboundsPerGame() - (1.0 * 37 / 11) ) < 0.00001 );
  127. cout << "All unit tests passed!!!" << endl;
  128. }
  129. int main()
  130. {
  131. test_BBPlayer();
  132. const uint GOAL_SCORE = 32;
  133. // create the two players
  134. BBPlayer P[2];
  135. P[0].setAll("Chemba", 100, 65, 10, 35);
  136. P[1].setAll("Wes", 100, 65, 10, 35);
  137. srand(time(NULL));
  138. // the index of the player in offense, randomly determined
  139. int playerIdx = rand() % 2;
  140. // the player who won, -1 if no one has won
  141. int playerWon = -1;
  142. // the result of a dice throw, a number between [0.0,1.0]
  143. float diceThrow;
  144. cout << "The game starts...." << endl;
  145. P[0].addGame();
  146. P[1].addGame();
  147. while (playerWon == -1) {
  148. // the offensive player takes a shot
  149. cout << P[playerIdx].name() << " shoots .... ";
  150. diceThrow = static_cast<float>( rand() ) / RAND_MAX;
  151. // determine if shot is made based on shooting percentage
  152. if ( diceThrow < P[playerIdx].shotPercentage() ) {
  153. cout << "and scores!!!" << endl;
  154. P[playerIdx].shotMade();
  155. cout << "The score is " << P[0].name() << ":"<< P[0].score()
  156. << " to " << P[1].name() << ":"<< P[1].score() << endl;
  157. if (P[playerIdx].score() >= GOAL_SCORE) playerWon = playerIdx;
  158. else playerIdx = (playerIdx + 1) % 2;
  159. }
  160. else {
  161. cout << "and misses :-(" << endl;
  162. P[playerIdx].shotMissed();
  163. // determine who gets the rebound
  164. float chanceOfense = P[playerIdx].reboundsPerGame();
  165. float chanceDefense = P[(playerIdx + 1) % 2].reboundsPerGame() * 2;
  166. diceThrow = static_cast<float>( rand() ) / RAND_MAX;
  167. diceThrow = diceThrow * ( chanceOfense + chanceDefense);
  168. playerIdx = diceThrow < chanceOfense ? playerIdx : (playerIdx + 1) % 2;
  169. cout << P[playerIdx].name() << " gets the rebound .... " << endl;
  170. }
  171. }
  172. cout << P[playerWon].name() << " wins the game!" << endl << endl;
  173. cout << "Final statistics . . . " << endl;
  174. cout << "---------------------------" << endl;
  175. P[0].printStats();
  176. cout << "---------------------------" << endl;
  177. P[1].printStats();
  178. cout << "---------------------------" << endl;
  179. return 0;
  180. }