From 3e8220dec261f43cfcf71809238d5ab8ee2482cd Mon Sep 17 00:00:00 2001 From: Dmitrii Ubskii Date: Sat, 15 May 2021 17:28:52 +0300 Subject: 2048: Refactor tile sliding and score logic out of attempt_move() --- Userland/Games/2048/Game.cpp | 13 +++++++++++-- Userland/Games/2048/Game.h | 2 ++ 2 files changed, 13 insertions(+), 2 deletions(-) (limited to 'Userland/Games/2048') 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 }; -- cgit v1.2.3