summaryrefslogtreecommitdiff
path: root/Userland/Games/2048/Game.cpp
diff options
context:
space:
mode:
authorDmitrii Ubskii <ubskydm@gmail.com>2021-06-11 22:50:54 +0300
committerAndreas Kling <kling@serenityos.org>2021-06-11 22:45:14 +0200
commit242742b6c208ae65a55ad714c2b7242636f056a0 (patch)
treed2870a14883f3346815b2256999e858f870b5fc8 /Userland/Games/2048/Game.cpp
parent8a8c2572b1b456cdb2e55256fa3ddabe72a0085a (diff)
downloadserenity-242742b6c208ae65a55ad714c2b7242636f056a0.zip
2048: Animate sliding tiles
Diffstat (limited to 'Userland/Games/2048/Game.cpp')
-rw-r--r--Userland/Games/2048/Game.cpp16
1 files changed, 16 insertions, 0 deletions
diff --git a/Userland/Games/2048/Game.cpp b/Userland/Games/2048/Game.cpp
index 70da92d1e5..ce827d29ff 100644
--- a/Userland/Games/2048/Game.cpp
+++ b/Userland/Games/2048/Game.cpp
@@ -52,6 +52,10 @@ void Game::Board::transpose()
for (size_t j = 0; j < i; j++)
swap(m_tiles[i][j], m_tiles[j][i]);
}
+ for (auto& t : m_sliding_tiles) {
+ swap(t.row_from, t.column_from);
+ swap(t.row_to, t.column_to);
+ }
}
void Game::Board::reverse()
@@ -60,6 +64,12 @@ void Game::Board::reverse()
for (size_t i = 0; i < row.size() / 2; ++i)
swap(row[i], row[row.size() - i - 1]);
}
+
+ auto const row_size = m_tiles[0].size();
+ for (auto& t : m_sliding_tiles) {
+ t.column_from = row_size - t.column_from - 1;
+ t.column_to = row_size - t.column_to - 1;
+ }
}
size_t Game::Board::slide_row(size_t row_index)
@@ -85,12 +95,16 @@ size_t Game::Board::slide_row(size_t row_index)
while (first < row.size()) {
auto second = next_nonempty(first + 1);
if (second == row.size() || row[first] != row[second]) {
+ m_sliding_tiles.append({ row_index, first, row[first], row_index, current_index, row[first] });
+
row[current_index] = row[first];
current_index++;
first = second;
} else {
VERIFY(row[first] == row[second]);
+ m_sliding_tiles.append({ row_index, first, row[first], row_index, current_index, 2 * row[first] });
+ m_sliding_tiles.append({ row_index, second, row[second], row_index, current_index, 2 * row[first] });
row[current_index] = 2 * row[first];
current_index++;
@@ -107,6 +121,8 @@ size_t Game::Board::slide_row(size_t row_index)
size_t Game::Board::slide_left()
{
+ m_sliding_tiles.clear();
+
size_t successful_merge_score = 0;
for (size_t row_index = 0; row_index < m_tiles.size(); row_index++)