Monopoly — Portage moderne 0.1.0
Portage moderne C/C++23 du Monopoly de 1999
Loading...
Searching...
No Matches
PhaseStack.cpp
Go to the documentation of this file.
1#include "PhaseStack.hpp"
2
3#include <algorithm>
4#include <tuple>
5#include <utility>
6
8{
9 namespace
10 {
12 {
14 0,
15 0,
16 0
17 };
18 }
19
20 bool push(
22 GamePhase phase,
23 PlayerNumber fromPlayer,
24 PlayerNumber toPlayer,
25 std::int64_t amount)
26 {
27 // PushPhase() original.
29 {
30 return false;
31 }
32
33 // L'original décale toute la pile vers le bas afin que
34 // l'élément 0 reste toujours la phase courante.
35 for (std::size_t i = state.numberOfPendingPhases; i >= 1; --i)
36 {
38 state.phaseUndo[i] = std::move(state.phaseUndo[i - 1]);
39 }
40
42
43 state.phaseStack[0] =
44 {
45 phase,
46 fromPlayer,
47 toPlayer,
48 amount
49 };
50
51 state.phaseUndo[0].reset();
52
53 return true;
54 }
55
57 {
58 // PopPhase() original.
60 {
62
63 push(
64 state,
66 0,
67 0,
68 0
69 );
70
71 return false;
72 }
73
75
76 for (std::size_t i = 0;
78 ++i)
79 {
81 state.phaseStack[i + 1];
82 state.phaseUndo[i] = std::move(state.phaseUndo[i + 1]);
83 }
84
86
87 return true;
88 }
89
92 GamePhase phase,
93 PlayerNumber fromPlayer,
94 PlayerNumber toPlayer,
95 std::int64_t amount)
96 {
97 // SwitchPhase() original.
99 {
101 state.phaseUndo[0].reset();
102 }
103
104 state.phaseStack[0] =
105 {
106 phase,
107 fromPlayer,
108 toPlayer,
109 amount
110 };
111 }
112
114 {
115 if (state.numberOfPendingPhases == 0) return;
116 auto snapshot = std::make_shared<GameState>(state);
117 snapshot->phaseUndo = {};
118 state.phaseUndo[0] = std::move(snapshot);
119 }
120
122 {
123 return state.numberOfPendingPhases > 0 && state.phaseUndo[0] != nullptr;
124 }
125
127 {
128 return std::any_of(state.phaseUndo.begin(),
130 [](const auto& snapshot) { return snapshot != nullptr; });
131 }
132
134 {
135 if (!hasCurrentSnapshot(state)) return false;
136 const auto& saved = *state.phaseUndo[0];
137 // Compare values, not strings' storage addresses, padding or transient
138 // snapshot ownership. Legacy normalizes duration only for comparison.
139 const auto values = [](const GameState& value)
140 {
141 return std::tie(value.options, value.players, value.squares,
142 value.dice, value.nextDice, value.utilityDice,
143 value.numberOfDoublesRolled, value.justRolledOutOfJail,
144 value.pendingCard, value.freeParkingJackpotAmount,
145 value.tradeInProgress, value.countHits, value.auction,
146 value.configurationProposer, value.currentPlayer,
147 value.numberOfPlayers, value.phaseStack, value.numberOfPendingPhases);
148 };
149 if (values(state) != values(saved)) return true;
150 return !std::equal(state.cards.begin(), state.cards.end(), saved.cards.begin(),
151 [](const CardDeck& a, const CardDeck& b)
152 {
153 return std::tie(a.cardCount, a.cardPile, a.jailOwner, a.jailOfferedInTradeTo) ==
154 std::tie(b.cardCount, b.cardPile, b.jailOwner, b.jailOfferedInTradeTo);
155 });
156 }
157
159 {
160 if (!hasCurrentSnapshot(state)) return false;
161 auto undo = std::move(state.phaseUndo);
162 state = *undo[0];
163 undo[0].reset();
164 state.phaseUndo = std::move(undo);
165 return true;
166 }
167
169 {
170 // Équivalent moderne du macro :
171 //
172 // #define CurrentPhaseM
173 // (CurrentRulesState.phaseStack[0].phase)
174
176 {
177 return fallbackPhase;
178 }
179
180 return state.phaseStack[0];
181 }
182}
fonts::Settings saved
unsigned index
std::uint16_t value
Definition Timers.cpp:16
bool pop(GameState &state)
const PendingPhase & current(const GameState &state)
void saveCurrent(GameState &state)
bool hasSnapshots(const GameState &state)
bool push(GameState &state, GamePhase phase, PlayerNumber fromPlayer, PlayerNumber toPlayer, std::int64_t amount)
bool currentStateChanged(const GameState &state)
bool hasCurrentSnapshot(const GameState &state)
bool restoreCurrent(GameState &state)
void switchTo(GameState &state, GamePhase phase, PlayerNumber fromPlayer, PlayerNumber toPlayer, std::int64_t amount)
std::uint8_t PlayerNumber
Definition RuleTypes.hpp:12
const GameState & state()
constexpr std::size_t MaxPendingPhases
std::array< std::shared_ptr< const GameState >, MaxPendingPhases > phaseUndo
std::array< CardDeck, static_cast< std::size_t >(DeckType::Count) > cards
std::array< PendingPhase, MaxPendingPhases > phaseStack
std::uint8_t numberOfPendingPhases