1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 02:37:35 +00:00

2048: Refactor tile sliding and score logic out of attempt_move()

This commit is contained in:
Dmitrii Ubskii 2021-05-15 17:28:52 +03:00 committed by Linus Groh
parent 24c5b0e81c
commit 3e8220dec2
2 changed files with 13 additions and 2 deletions

View file

@ -161,7 +161,7 @@ static bool is_stalled(const Game::Board& board)
return true; return true;
} }
Game::MoveOutcome Game::attempt_move(Direction direction) bool Game::slide_tiles(Direction direction)
{ {
size_t successful_merge_score = 0; size_t successful_merge_score = 0;
Board new_board; Board new_board;
@ -184,9 +184,18 @@ Game::MoveOutcome Game::attempt_move(Direction direction)
bool moved = new_board != m_board; bool moved = new_board != m_board;
if (moved) { if (moved) {
m_board = new_board; 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++; m_turns++;
add_random_tile(); add_random_tile();
m_score += successful_merge_score;
} }
if (is_complete(m_board, m_target_tile)) if (is_complete(m_board, m_target_tile))

View file

@ -47,6 +47,8 @@ public:
} }
private: private:
bool slide_tiles(Direction);
void add_random_tile(); void add_random_tile();
size_t m_grid_size { 0 }; size_t m_grid_size { 0 };