aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/game.cpp23
-rw-r--r--src/graphics.cpp9
2 files changed, 29 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
diff --git a/src/graphics.cpp b/src/graphics.cpp
index 298fc43..2c8e525 100644
--- a/src/graphics.cpp
+++ b/src/graphics.cpp
@@ -134,6 +134,9 @@ void Graphics::handleEvent()
case SDL_KEYDOWN:
switch (e.key.keysym.sym)
{
+ case SDLK_ESCAPE:
+ m_running = false;
+ break;
case SDLK_UP:
m_game.move(Game::DirectionUp);
break;
@@ -146,6 +149,12 @@ void Graphics::handleEvent()
case SDLK_RIGHT:
m_game.move(Game::DirectionRight);
break;
+ case SDLK_BACKSPACE:
+ m_game.undo();
+ break;
+ case SDLK_r:
+ m_game.reset();
+ break;
}
if (m_game.won())
m_running = false;