summaryrefslogtreecommitdiff
path: root/Userland/Games/2048
diff options
context:
space:
mode:
authorDmitrii Ubskii <ubskydm@gmail.com>2021-05-15 17:28:52 +0300
committerLinus Groh <mail@linusgroh.de>2021-05-18 08:51:56 +0100
commit3e8220dec261f43cfcf71809238d5ab8ee2482cd (patch)
treeac71c46678b966e2d7eb902eaf25d6bdcdb92c93 /Userland/Games/2048
parent24c5b0e81c6902907d708f862e54d40f5248973e (diff)
downloadserenity-3e8220dec261f43cfcf71809238d5ab8ee2482cd.zip
2048: Refactor tile sliding and score logic out of attempt_move()
Diffstat (limited to 'Userland/Games/2048')
-rw-r--r--Userland/Games/2048/Game.cpp13
-rw-r--r--Userland/Games/2048/Game.h2
2 files changed, 13 insertions, 2 deletions
diff --git a/Userland/Games/2048/Game.cpp b/Userland/Games/2048/Game.cpp
index 11a8bdd896..c196dc9d8c 100644
--- a/Userland/Games/2048/Game.cpp
+++ b/Userland/Games/2048/Game.cpp
@@ -161,7 +161,7 @@ static bool is_stalled(const Game::Board& board)
return true;
}
-Game::MoveOutcome Game::attempt_move(Direction direction)
+bool Game::slide_tiles(Direction direction)
{
size_t successful_merge_score = 0;
Board new_board;
@@ -184,9 +184,18 @@ Game::MoveOutcome Game::attempt_move(Direction direction)
bool moved = new_board != m_board;
if (moved) {
m_board = new_board;
+ m_score += successful_merge_score;
+ }
+
+ return moved;
+}
+
+Game::MoveOutcome Game::attempt_move(Direction direction)
+{
+ bool moved = slide_tiles(direction);
+ if (moved) {
m_turns++;
add_random_tile();
- m_score += successful_merge_score;
}
if (is_complete(m_board, m_target_tile))
diff --git a/Userland/Games/2048/Game.h b/Userland/Games/2048/Game.h
index b4801bbd59..1e36afbccb 100644
--- a/Userland/Games/2048/Game.h
+++ b/Userland/Games/2048/Game.h
@@ -47,6 +47,8 @@ public:
}
private:
+ bool slide_tiles(Direction);
+
void add_random_tile();
size_t m_grid_size { 0 };