aboutsummaryrefslogtreecommitdiff
path: root/src/game.cpp
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-03-21 17:55:49 +0100
committerCharles <sircharlesaze@gmail.com>2020-03-21 17:55:49 +0100
commit9401116d59066a2bb6de9c12d9642afee1069314 (patch)
treef886c23c9b4e2a300a1e2b1e18a068a2b39cd425 /src/game.cpp
parentcaeb26e90ef09fdad85af72f460644e09d3b2c7c (diff)
downloadmario_sokoban-master.tar.gz
mario_sokoban-master.tar.bz2
mario_sokoban-master.zip
Added history and some map from sokonlineHEADmaster
Diffstat (limited to 'src/game.cpp')
-rw-r--r--src/game.cpp23
1 files changed, 20 insertions, 3 deletions
diff --git a/src/game.cpp b/src/game.cpp
index cdc8937..67e4a28 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -45,6 +45,7 @@ void Game::move(Direction direction)
{
m_playerDirection = direction;
Position saved = m_playerPos;
+ std::vector<Position> cratePosSaved = m_cratePos;
switch (direction)
{
case DirectionUp:
@@ -64,14 +65,30 @@ void Game::move(Direction direction)
m_playerPos = saved;
return;
}
-
if (get(m_playerPos) == CellCrate || get(m_playerPos) == CellCrateSolved)
- {
if (!tryMoveCrate(*std::find(
m_cratePos.begin(), m_cratePos.end(), m_playerPos),
direction))
+ {
m_playerPos = saved;
- }
+ return;
+ }
+ m_history.push(std::make_pair(saved, cratePosSaved));
+}
+
+void Game::undo()
+{
+ if (m_history.size() < 1)
+ return;
+ m_playerPos = m_history.top().first;
+ m_cratePos = m_history.top().second;
+ m_history.pop();
+}
+
+void Game::reset()
+{
+ while (m_history.size() > 0)
+ undo();
}
Game::Cell Game::get(int y, int x) const