diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-03-21 17:55:49 +0100 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-03-21 17:55:49 +0100 |
| commit | 9401116d59066a2bb6de9c12d9642afee1069314 (patch) | |
| tree | f886c23c9b4e2a300a1e2b1e18a068a2b39cd425 /src/game.cpp | |
| parent | caeb26e90ef09fdad85af72f460644e09d3b2c7c (diff) | |
| download | mario_sokoban-master.tar.gz mario_sokoban-master.tar.bz2 mario_sokoban-master.zip | |
Diffstat (limited to 'src/game.cpp')
| -rw-r--r-- | src/game.cpp | 23 |
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 |
