-
Notifications
You must be signed in to change notification settings - Fork 0
/
Score.cpp
338 lines (320 loc) · 10 KB
/
Score.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*=============================================================================
* TarotClub - Score.cpp
*=============================================================================
* Helper class that stores various scoring information
*=============================================================================
* TarotClub ( http://www.tarotclub.fr ) - This file is part of TarotClub
* Copyright (C) 2003-2999 - Anthony Rabine
*
* TarotClub is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TarotClub is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with TarotClub. If not, see <http://www.gnu.org/licenses/>.
*
*=============================================================================
*/
#include <cstdlib>
#include "Score.h"
#include "Common.h"
/*****************************************************************************/
Points::Points()
{
Clear();
}
/*****************************************************************************/
void Points::Clear()
{
// Points for one deal
pointsAttack = 0;
oudlers = 0;
handlePoints = 0;
slamDone = false;
littleEndianOwner = Team::NO_TEAM;
}
/*****************************************************************************/
Team Points::Winner() const
{
if (pointsAttack >= Tarot::PointsToDo(oudlers))
{
return Team(Team::ATTACK);
}
else
{
return Team(Team::DEFENSE);
}
}
/*****************************************************************************/
std::int32_t Points::Difference() const
{
return pointsAttack - Tarot::PointsToDo(oudlers);
}
/*****************************************************************************/
std::int32_t Points::GetSlamPoints(const Tarot::Bid &bid) const
{
std::int32_t slamPoints = 0;
if (slamDone)
{
if (Winner() == Team::ATTACK)
{
if (bid.slam == true)
{
slamPoints = 400; // Announced AND realized
}
else
{
slamPoints = 200; // not announced but realized
}
}
else
{
// Defense has won the game and realized a slam!
slamPoints = 200;
}
}
else
{
if (bid.slam == true)
{
if (Winner() == Team::ATTACK)
{
slamPoints = -200; // Announced but not ealized :(
}
else
{
slamPoints = 200; // deducted from the winner
}
}
else
{
slamPoints = 0;
}
}
return slamPoints;
}
/*****************************************************************************/
std::int32_t Points::GetLittleEndianPoints() const
{
std::int32_t littleEndianPoints = 0;
// Little endian bonus:
// Le camp qui réalise la dernière levée, à condition que cette levée
// comprenne le Petit, bénéficie d'une prime de 10 points,
// multipliable selon le contrat, quel que soit le résultat de la donne.
if (littleEndianOwner == Team::ATTACK)
{
// Bonus belong to the attack
if (Winner() == Team::ATTACK)
{
littleEndianPoints = 10; // added to the attack
}
else
{
littleEndianPoints = -10; // deducted from the defense
}
}
else if (littleEndianOwner == Team::DEFENSE)
{
// Bonus belong to the defense
if (Winner() == Team::ATTACK)
{
littleEndianPoints = -10; // deducted to the attack (defense bonus)
}
else
{
littleEndianPoints = 10; // added to the defense
}
}
else
{
// no team, no bonus!
littleEndianPoints = 0;
}
return littleEndianPoints;
}
/*****************************************************************************/
/**
* @brief Calculate the final score of all players
*
* We calculate the points with the following formula:
* points = ((25 + pt + pb) * mu) + pg + ch
*
* pt = difference between the card points the taker actually won and the minimum number of points he needed
* pb = the petit au bout bonus is added or subtracted if applicable
* mu = factor (mu) depending on the bid (1, 2, 4 or 6)
* pg = the handle bonus (20, 30 or 40)
* ch = the slam bonus (200 or 400)
*
*/
std::int32_t Points::GetPoints(const Team team, const Tarot::Bid &bid, uint8_t nbPlayers) const
{
std::int32_t slamPoints = GetSlamPoints(bid);
std::int32_t littleEndianPoints = GetLittleEndianPoints();
// Final scoring
std::int32_t score = (25 + std::abs(Difference()) + littleEndianPoints) * Tarot::GetMultiplier(bid.contract) + handlePoints + slamPoints;
std::int32_t sign = 1;
std::int32_t multiplier = 1;
if (team == Team::ATTACK)
{
if (Winner() == Team::DEFENSE)
{
sign = -1;
}
multiplier = (nbPlayers - 1);
if (bid.HasPartner())
{
multiplier--;
}
}
else
{
if (Winner() == Team::ATTACK)
{
sign = -1;
}
}
return (score * sign * multiplier);
}
/*****************************************************************************/
/*****************************************************************************/
Score::Score()
{
NewGame(TournamentConfig::DEFAULT_NUMBER_OF_TURNS);
}
/*****************************************************************************/
void Score::NewGame(std::uint8_t numberOfTurns)
{
// Reset tournament information
for (std::uint32_t i = 0U; i < TournamentConfig::MAX_NUMBER_OF_TURNS; i++)
{
for (std::uint32_t j = 0U; j < 5U; j++)
{
scores[i][j] = 0;
}
}
mNumberOfTurns = numberOfTurns;
dealCounter = 0U;
mHistory.clear();
}
/*****************************************************************************/
void Score::NewDeal()
{
// Manage rollover
if (dealCounter >= mNumberOfTurns)
{
dealCounter = 0U;
}
}
/*****************************************************************************/
/**
* @brief Add the current score to the tournament
* @param info
* @return true if the tournament must continue, false if it is finished
*/
bool Score::AddPoints(const Points &points, const Tarot::Bid &bid, std::uint8_t numberOfPlayers)
{
Score::Entry entry;
entry.bid = bid;
entry.points = points;
entry.nbPlayers = numberOfPlayers;
mHistory.push_back(entry);
std::int32_t attackPoints = points.GetPoints(Team(Team::ATTACK), bid, numberOfPlayers);
std::int32_t defensePoints = points.GetPoints(Team(Team::DEFENSE), bid, numberOfPlayers);
std::int32_t takerPoints = attackPoints;
std::int32_t parterPoints = 0;
// Si en 5 joueurs le preneur a appeté un partenaire
// alors la répartition est de 2/3 pour le preneur et 1/3 pour le partenaire en attaque
if ((numberOfPlayers == 5) && bid.HasPartner())
{
float deuxTiers = static_cast<float>(attackPoints);
deuxTiers = deuxTiers * 2 / 3;
deuxTiers += 0.4; // arrondi sup
takerPoints = static_cast<int32_t>(deuxTiers);
parterPoints = attackPoints - takerPoints;
}
for (std::uint32_t i = 0U; i < numberOfPlayers; i++)
{
if (numberOfPlayers == 5)
{
if (Place(i) == bid.taker)
{
scores[dealCounter][i] = takerPoints;
}
else if ((Place(i) == bid.partner) && bid.HasPartner())
{
scores[dealCounter][i] = parterPoints;
}
else
{
scores[dealCounter][i] = defensePoints;
}
}
else
{
if (Place(i) == bid.taker)
{
scores[dealCounter][i] = takerPoints;
}
else
{
scores[dealCounter][i] = defensePoints;
}
}
}
dealCounter++;
if (dealCounter < mNumberOfTurns)
{
return true;
}
return false;
}
/*****************************************************************************/
int32_t Score::GetTotalPoints(Place p) const
{
int32_t total;
total = 0;
for (std::uint32_t i = 0U; i < dealCounter; i++)
{
total += scores[i][p.Value()];
}
return (total);
}
/*****************************************************************************/
/**
* @brief Score::GetPodium
*
* Gets the podium of the tournament. The first player has the highest number of points.
*
* @return The sorted list of players, by points
*/
std::map<int, Place> Score::GetPodium()
{
std::map<int, Place> podium;
if (dealCounter > 0U)
{
// init podium
for (std::uint32_t i = 0U; i < 5U; i++)
{
int points = scores[dealCounter - 1U][i];
podium[points] = (Place)i;
}
}
// Since the map naturally sorts the list in the key order (points)
// there is no need to sort anything
return podium;
}
/*****************************************************************************/
Place Score::GetWinner()
{
return GetPodium().rbegin()->second;
}
//=============================================================================
// End of file Score.cpp
//=============================================================================