Monopoly — Portage moderne 0.1.0
Portage moderne C/C++23 du Monopoly de 1999
Loading...
Searching...
No Matches
RuleInitialDeal.cpp
Go to the documentation of this file.
1#include "RuleInitialDeal.hpp"
2
3#include "BoardRules.hpp"
4#include "PhaseStack.hpp"
5#include "RuleEconomy.hpp"
6#include "RuleRandom.hpp"
7
8#include <array>
9#include <cstddef>
10#include <cstdint>
11
13{
16 {
17 // ====================================================
18 // DealNProperties() original.
19 //
20 // Plusieurs rounds.
21 // Chaque round doit fournir exactement une propriété
22 // à chaque joueur ou le round entier est abandonné.
23 // ====================================================
24
25 if (
28 {
29 return;
30 }
31
32
33 std::array<
37
38
39 std::array<
40 std::int64_t,
42 > cash{};
43
44
45 std::array<
46 std::int64_t,
48 > cashOwed{};
49
50
51 std::array<
55
56
57 std::array<
61
62
63 for (PlayerNumber player = 0;
64 player < state.numberOfPlayers;
65 ++player)
66 {
67 cash[player] =
68 state.players[player].cash;
69
70 cashOwed[player] = 0;
71
72 propertiesBought[player] = 0;
73 }
74
75
76 int numberOfRounds = 0;
77
78
79 while (
82 {
84 true;
85
86
87 for (PlayerNumber player = 0;
88 player < state.numberOfPlayers;
89 ++player)
90 {
91 hypotheticalPurchase[player] =
92 static_cast<board::SquareType>(
94 );
95 }
96
97
98 for (PlayerNumber player = 0;
99 player < state.numberOfPlayers;
100 ++player)
101 {
102 std::size_t affordableCount = 0;
103
104
105 for (std::size_t squareNo = 0;
107 ++squareNo)
108 {
109 if (
110 state.squares[squareNo].owner !=
112 {
113 continue;
114 }
115
116
117 const auto square =
118 static_cast<board::SquareType>(
120 );
121
122
123 if (!board::isOwnable(square))
124 {
125 continue;
126 }
127
128
129 bool alreadyAllocated =
130 false;
131
132
133 for (PlayerNumber other = 0;
134 other <
136 ++other)
137 {
138 if (
139 static_cast<std::size_t>(
141 ) ==
142 squareNo)
143 {
145 true;
146
147 break;
148 }
149 }
150
151
153 {
154 continue;
155 }
156
157
158 const auto&
159 predefined =
160 board::definition(square);
161
162
163 if (
166 predefined.purchaseCost >
167 cash[player])
168 {
169 continue;
170 }
171
172
175 ] = square;
176 }
177
178
179 if (affordableCount == 0)
180 {
182 false;
183
184 break;
185 }
186
187
188 const std::size_t selected =
189 static_cast<std::size_t>(
191 ) %
193
194
195 hypotheticalPurchase[player] =
196 affordableProperties[selected];
197 }
198
199
201 {
202 break;
203 }
204
205
206 // -----------------------------------------------
207 // Le round entier est valide :
208 // maintenant seulement on modifie GameState.
209 // -----------------------------------------------
210
211 for (PlayerNumber player = 0;
212 player < state.numberOfPlayers;
213 ++player)
214 {
215 const auto square =
216 hypotheticalPurchase[player];
217
218
219 const std::uint8_t squareNo =
220 static_cast<std::uint8_t>(
221 square
222 );
223
224
227 state,
228 squareNo,
229 player
230 );
231
232
233 if (
236 {
237 const std::int64_t price =
239 square
240 ).purchaseCost;
241
242
243 cashOwed[player] +=
244 price;
245
246
247 cash[player] -=
248 price;
249 }
250
251
252 propertiesBought[player] |=
254 square
255 );
256 }
257
258
260 }
261
262
263 // ====================================================
264 // Comptabilité différée.
265 //
266 // Ordre conservé :
267 //
268 // player 0:
269 // PUSH TRANSFER
270 // PUSH DEBT
271 //
272 // player 1:
273 // PUSH TRANSFER
274 // PUSH DEBT
275 //
276 // etc.
277 //
278 // phaseStack étant LIFO, le dernier joueur sera
279 // traité en premier, comme en 1999.
280 // ====================================================
281
282 for (PlayerNumber player = 0;
283 player < state.numberOfPlayers;
284 ++player)
285 {
286 if (
287 propertiesBought[player] != 0)
288 {
290 state,
291 GamePhase::
293 0,
294 player,
295 propertiesBought[player]
296 );
297 }
298
299
300 if (cashOwed[player] != 0)
301 {
303 state,
304 player,
306 cashOwed[player]
307 );
308 }
309 }
310 }
311}
unsigned index
CardTag other
const SquareDefinition & definition(SquareType square)
bool isOwnable(SquareType square)
std::uint32_t PropertySet
PropertySet propertyBit(SquareType square)
void stackDebt(GameState &state, PlayerNumber fromPlayer, PlayerNumber toPlayer, std::int64_t amount)
void transferPropertyForSettlement(GameState &state, std::uint8_t squareNo, PlayerNumber toPlayer)
void dealNProperties(GameState &state)
bool push(GameState &state, GamePhase phase, PlayerNumber fromPlayer, PlayerNumber toPlayer, std::int64_t amount)
std::mt19937 & generator()
constexpr PlayerNumber BankPlayer
Definition RuleTypes.hpp:15
std::uint8_t PlayerNumber
Definition RuleTypes.hpp:12
constexpr std::size_t SquareCount
Definition RuleTypes.hpp:24
const GameState & state()
constexpr PlayerNumber MaxPlayers
Definition RuleTypes.hpp:14
constexpr PlayerNumber NobodyPlayer
Definition RuleTypes.hpp:16
std::array< SquareState, SquareCount > squares
std::array< PlayerState, MaxPlayers > players