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