summaryrefslogtreecommitdiff
path: root/Userland/Games
diff options
context:
space:
mode:
Diffstat (limited to 'Userland/Games')
-rw-r--r--Userland/Games/2048/GameSizeDialog.cpp6
-rw-r--r--Userland/Games/2048/GameSizeDialog.h10
-rw-r--r--Userland/Games/2048/main.cpp9
3 files changed, 17 insertions, 8 deletions
diff --git a/Userland/Games/2048/GameSizeDialog.cpp b/Userland/Games/2048/GameSizeDialog.cpp
index 3053bd4cff..fee71d8d9f 100644
--- a/Userland/Games/2048/GameSizeDialog.cpp
+++ b/Userland/Games/2048/GameSizeDialog.cpp
@@ -11,9 +11,13 @@
#include <LibGUI/CheckBox.h>
#include <LibGUI/Label.h>
#include <LibGUI/SpinBox.h>
+#include <math.h>
-GameSizeDialog::GameSizeDialog(GUI::Window* parent)
+GameSizeDialog::GameSizeDialog(GUI::Window* parent, size_t board_size, size_t target, bool evil_ai)
: GUI::Dialog(parent)
+ , m_board_size(board_size)
+ , m_target_tile_power(log2(target))
+ , m_evil_ai(evil_ai)
{
set_rect({ 0, 0, 200, 150 });
set_title("New Game");
diff --git a/Userland/Games/2048/GameSizeDialog.h b/Userland/Games/2048/GameSizeDialog.h
index 6a3d982634..a3a604bde5 100644
--- a/Userland/Games/2048/GameSizeDialog.h
+++ b/Userland/Games/2048/GameSizeDialog.h
@@ -12,16 +12,16 @@
class GameSizeDialog : public GUI::Dialog {
C_OBJECT(GameSizeDialog)
public:
- GameSizeDialog(GUI::Window* parent);
-
size_t board_size() const { return m_board_size; }
u32 target_tile() const { return 1u << m_target_tile_power; }
bool evil_ai() const { return m_evil_ai; }
bool temporary() const { return m_temporary; }
private:
- size_t m_board_size { 4 };
- size_t m_target_tile_power { 11 };
- bool m_evil_ai { false };
+ GameSizeDialog(GUI::Window* parent, size_t board_size, size_t target_tile, bool evil_ai);
+
+ size_t m_board_size;
+ size_t m_target_tile_power;
+ bool m_evil_ai;
bool m_temporary { true };
};
diff --git a/Userland/Games/2048/main.cpp b/Userland/Games/2048/main.cpp
index db75e0aee5..adcf0db70c 100644
--- a/Userland/Games/2048/main.cpp
+++ b/Userland/Games/2048/main.cpp
@@ -39,9 +39,14 @@ int main(int argc, char** argv)
auto config = Core::ConfigFile::get_for_app("2048");
size_t board_size = config->read_num_entry("", "board_size", 4);
- u32 target_tile = config->read_num_entry("", "target_tile", 0);
+ u32 target_tile = config->read_num_entry("", "target_tile", 2048);
bool evil_ai = config->read_bool_entry("", "evil_ai", false);
+ if ((target_tile & (target_tile - 1)) != 0) {
+ // If the target tile is not a power of 2, reset to its default value.
+ target_tile = 2048;
+ }
+
config->write_num_entry("", "board_size", board_size);
config->write_num_entry("", "target_tile", target_tile);
config->write_bool_entry("", "evil_ai", evil_ai);
@@ -94,7 +99,7 @@ int main(int argc, char** argv)
Vector<Game> redo_stack;
auto change_settings = [&] {
- auto size_dialog = GameSizeDialog::construct(window);
+ auto size_dialog = GameSizeDialog::construct(window, board_size, target_tile, evil_ai);
if (size_dialog->exec() || size_dialog->result() != GUI::Dialog::ExecOK)
return;