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
Notes: sideshowbarker 2024-07-18 17:52:43 +09:00
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;
}
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))

View file

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