summaryrefslogtreecommitdiff
path: root/Games/2048
diff options
context:
space:
mode:
authorSergey Bugaev <bugaevc@serenityos.org>2020-08-18 16:30:25 +0300
committerAndreas Kling <kling@serenityos.org>2020-08-18 17:19:52 +0200
commit7861ebaad726ef125243158185b593236cf31c60 (patch)
treecb41c8aa451a1281b501cd721f04b7b2e2e19018 /Games/2048
parent8dd5b0af4e41ce1536feb58145386dbd47cda66a (diff)
downloadserenity-7861ebaad726ef125243158185b593236cf31c60.zip
2048: Generate more "2" tiles
This is how the original game does it.
Diffstat (limited to 'Games/2048')
-rw-r--r--Games/2048/Game.cpp14
-rw-r--r--Games/2048/Game.h2
2 files changed, 8 insertions, 8 deletions
diff --git a/Games/2048/Game.cpp b/Games/2048/Game.cpp
index f5412b0555..244548db8e 100644
--- a/Games/2048/Game.cpp
+++ b/Games/2048/Game.cpp
@@ -25,6 +25,7 @@
*/
#include "Game.h"
+#include <stdlib.h>
Game::Game(size_t rows, size_t columns)
: m_rows(rows)
@@ -37,11 +38,11 @@ Game::Game(size_t rows, size_t columns)
row.append(0);
}
- add_tile(2);
- add_tile(2);
+ add_random_tile();
+ add_random_tile();
}
-void Game::add_tile(u32 max_tile_value)
+void Game::add_random_tile()
{
int row;
int column;
@@ -50,9 +51,8 @@ void Game::add_tile(u32 max_tile_value)
column = rand() % m_columns;
} while (m_board[row][column] != 0);
- int value = rand() % max_tile_value;
- value = round_up_to_power_of_two(value, max_tile_value);
- m_board[row][column] = max(2, value);
+ size_t value = rand() < RAND_MAX * 0.9 ? 2 : 4;
+ m_board[row][column] = value;
}
static Game::Board transpose(const Game::Board& board)
@@ -198,7 +198,7 @@ Game::MoveOutcome Game::attempt_move(Direction direction)
if (moved) {
m_board = new_board;
m_turns++;
- add_tile(4);
+ add_random_tile();
m_score += successful_merge_score;
}
diff --git a/Games/2048/Game.h b/Games/2048/Game.h
index 9df56fa0a9..dfaad90a9c 100644
--- a/Games/2048/Game.h
+++ b/Games/2048/Game.h
@@ -57,7 +57,7 @@ public:
const Board& board() const { return m_board; }
private:
- void add_tile(u32 max_tile_value);
+ void add_random_tile();
size_t m_rows { 0 };
size_t m_columns { 0 };