No Description

main.cpp 4.3KB

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