No Description

main.cpp 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. // DEFINE AQUI LA CLASE BBPlayer
  13. BBPlayer::BBPlayer() {
  14. _shotsTaken = _shotsMade = _gamesPlayed = _rebounds = _score = 0;
  15. }
  16. float BBPlayer::shotPercentage() const {
  17. return _shotsTaken == 0 ? .5 : static_cast<float>(_shotsMade) / _shotsTaken;
  18. }
  19. float BBPlayer::reboundsPerGame() const {
  20. return _gamesPlayed == 0 ? 3 : static_cast<float>(_rebounds) / _gamesPlayed;
  21. }
  22. void BBPlayer::printStats() const {
  23. cout << "Stats for " << _name << endl;
  24. cout << "Games played: " << _gamesPlayed << endl;
  25. cout << "Rebounds: " << _rebounds << endl;
  26. cout << "Shots taken: " << _shotsTaken << endl;
  27. cout << "Shots made: " << _shotsMade << endl;
  28. }
  29. void test_BBPlayer() {
  30. BBPlayer T;
  31. // T.setAll("Chemba", 100, 65, 10, 35);
  32. // Test the name() method
  33. // assert( T.name() == "Chemba" );
  34. // Test the initial shot pct
  35. // assert( abs( T.shotPercentage() - 0.65) < 0.00001 );
  36. // Test after a shot is made
  37. // T.shotMade();
  38. // assert( abs( T.shotPercentage() - (1.0 * 66 / 101) ) < 0.00001) ;
  39. // Test after a shot is missed
  40. // T.shotMissed();
  41. // assert( abs( T.shotPercentage() - (1.0 * 66 / 102) ) < 0.00001 );
  42. // Test the initial rebounds per game
  43. // assert( abs( T.reboundsPerGame() - 3.5 ) < 0.00001 );
  44. // Test the score up to this point
  45. // assert( T.score() == 2);
  46. // Make two shots, then test score and pct
  47. // T.shotMade();
  48. // T.shotMade();
  49. // assert( T.score() == 6);
  50. // assert( abs( T.shotPercentage() - (1.0 * 68 / 104) ) < 0.00001 );
  51. // Make two rebounds then test
  52. // T.reboundMade();
  53. // T.reboundMade();
  54. // assert( abs( T.reboundsPerGame() - 3.7 ) < 0.00001 );
  55. // Tests if the number of game has increased.
  56. // T.addGame();
  57. // assert( abs( T.reboundsPerGame() - (1.0 * 37 / 11) ) < 0.00001 );
  58. cout << "All unit tests passed!!!" << endl;
  59. }
  60. int main()
  61. {
  62. test_BBPlayer();
  63. // const uint GOAL_SCORE = 32;
  64. // create the two players
  65. // BBPlayer P[2];
  66. // P[0].setAll("Chemba", 100, 65, 10, 35);
  67. // P[1].setAll("Wes", 100, 56, 10, 1);
  68. // srand(time(NULL));
  69. // the index of the player in offense, randomly determined
  70. // int playerIdx = rand() % 2;
  71. // the player who won, -1 if no one has won
  72. // int playerWon = -1;
  73. // the result of a dice throw, a number between [0.0,1.0]
  74. // float diceThrow;
  75. // cout << "The game starts...." << endl;
  76. // P[0].addGame();
  77. // P[1].addGame();
  78. // while (playerWon == -1) {
  79. // the offensive player takes a shot
  80. // cout << P[playerIdx].name() << " shoots .... ";
  81. // diceThrow = static_cast<float>( rand() ) / RAND_MAX;
  82. // determine if shot is made based on shooting percentage
  83. // if ( diceThrow < P[playerIdx].shotPercentage() ) {
  84. // cout << "and scores!!!" << endl;
  85. // P[playerIdx].shotMade();
  86. // cout << "The score is " << P[0].name() << ":"<< P[0].score()
  87. // << " to " << P[1].name() << ":"<< P[1].score() << endl;
  88. // if (P[playerIdx].score() >= GOAL_SCORE) playerWon = playerIdx;
  89. // else playerIdx = (playerIdx + 1) % 2;
  90. // }
  91. // else {
  92. // cout << "and misses :-(" << endl;
  93. // P[playerIdx].shotMissed();
  94. // determine who gets the rebound
  95. // float chanceOfense = P[playerIdx].reboundsPerGame();
  96. // float chanceDefense = P[(playerIdx + 1) % 2].reboundsPerGame() * 2;
  97. // diceThrow = static_cast<float>( rand() ) / RAND_MAX;
  98. // diceThrow = diceThrow * ( chanceOfense + chanceDefense);
  99. // playerIdx = diceThrow < chanceOfense ? playerIdx : (playerIdx + 1) % 2;
  100. // cout << P[playerIdx].name() << " gets the rebound .... " << endl;
  101. // }
  102. // }
  103. // cout << P[playerWon].name() << " wins the game!" << endl << endl;
  104. // cout << "Final statistics . . . " << endl;
  105. // cout << "---------------------------" << endl;
  106. // P[0].printStats();
  107. // cout << "---------------------------" << endl;
  108. // P[1].printStats();
  109. // cout << "---------------------------" << endl;
  110. return 0;
  111. }