mirror of
https://github.com/RGBCube/serenity
synced 2025-07-29 05:07:45 +00:00
Games: Move to Userland/Games/
This commit is contained in:
parent
b8d6a56fa3
commit
aa939c4b4b
49 changed files with 1 additions and 1 deletions
215
Userland/Games/2048/BoardView.cpp
Normal file
215
Userland/Games/2048/BoardView.cpp
Normal file
|
@ -0,0 +1,215 @@
|
|||
/*
|
||||
* Copyright (c) 2020, the SerenityOS developers.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "BoardView.h"
|
||||
#include <LibGUI/Painter.h>
|
||||
#include <LibGfx/Font.h>
|
||||
#include <LibGfx/FontDatabase.h>
|
||||
#include <LibGfx/Palette.h>
|
||||
|
||||
BoardView::BoardView(const Game::Board* board)
|
||||
: m_board(board)
|
||||
{
|
||||
}
|
||||
|
||||
BoardView::~BoardView()
|
||||
{
|
||||
}
|
||||
|
||||
void BoardView::set_board(const Game::Board* board)
|
||||
{
|
||||
if (m_board == board)
|
||||
return;
|
||||
|
||||
if (!board) {
|
||||
m_board = nullptr;
|
||||
return;
|
||||
}
|
||||
|
||||
bool must_resize = !m_board || m_board->size() != board->size();
|
||||
|
||||
m_board = board;
|
||||
|
||||
if (must_resize)
|
||||
resize();
|
||||
|
||||
update();
|
||||
}
|
||||
|
||||
void BoardView::pick_font()
|
||||
{
|
||||
String best_font_name;
|
||||
int best_font_size = -1;
|
||||
auto& font_database = Gfx::FontDatabase::the();
|
||||
font_database.for_each_font([&](const Gfx::Font& font) {
|
||||
if (font.family() != "Liza" || font.weight() != 700)
|
||||
return;
|
||||
auto size = font.glyph_height();
|
||||
if (size * 2 <= m_cell_size && size > best_font_size) {
|
||||
best_font_name = font.qualified_name();
|
||||
best_font_size = size;
|
||||
}
|
||||
});
|
||||
|
||||
auto font = font_database.get_by_name(best_font_name);
|
||||
set_font(font);
|
||||
}
|
||||
|
||||
size_t BoardView::rows() const
|
||||
{
|
||||
if (!m_board)
|
||||
return 0;
|
||||
return m_board->size();
|
||||
}
|
||||
|
||||
size_t BoardView::columns() const
|
||||
{
|
||||
if (!m_board)
|
||||
return 0;
|
||||
if (m_board->is_empty())
|
||||
return 0;
|
||||
return (*m_board)[0].size();
|
||||
}
|
||||
|
||||
void BoardView::resize_event(GUI::ResizeEvent&)
|
||||
{
|
||||
resize();
|
||||
}
|
||||
|
||||
void BoardView::resize()
|
||||
{
|
||||
constexpr float padding_ratio = 7;
|
||||
m_padding = min(
|
||||
width() / (columns() * (padding_ratio + 1) + 1),
|
||||
height() / (rows() * (padding_ratio + 1) + 1));
|
||||
m_cell_size = m_padding * padding_ratio;
|
||||
|
||||
pick_font();
|
||||
}
|
||||
|
||||
void BoardView::keydown_event(GUI::KeyEvent& event)
|
||||
{
|
||||
if (!on_move)
|
||||
return;
|
||||
|
||||
switch (event.key()) {
|
||||
case KeyCode::Key_A:
|
||||
case KeyCode::Key_Left:
|
||||
on_move(Game::Direction::Left);
|
||||
break;
|
||||
case KeyCode::Key_D:
|
||||
case KeyCode::Key_Right:
|
||||
on_move(Game::Direction::Right);
|
||||
break;
|
||||
case KeyCode::Key_W:
|
||||
case KeyCode::Key_Up:
|
||||
on_move(Game::Direction::Up);
|
||||
break;
|
||||
case KeyCode::Key_S:
|
||||
case KeyCode::Key_Down:
|
||||
on_move(Game::Direction::Down);
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Gfx::Color BoardView::background_color_for_cell(u32 value)
|
||||
{
|
||||
switch (value) {
|
||||
case 0:
|
||||
return Color::from_rgb(0xcdc1b4);
|
||||
case 2:
|
||||
return Color::from_rgb(0xeee4da);
|
||||
case 4:
|
||||
return Color::from_rgb(0xede0c8);
|
||||
case 8:
|
||||
return Color::from_rgb(0xf2b179);
|
||||
case 16:
|
||||
return Color::from_rgb(0xf59563);
|
||||
case 32:
|
||||
return Color::from_rgb(0xf67c5f);
|
||||
case 64:
|
||||
return Color::from_rgb(0xf65e3b);
|
||||
case 128:
|
||||
return Color::from_rgb(0xedcf72);
|
||||
case 256:
|
||||
return Color::from_rgb(0xedcc61);
|
||||
case 512:
|
||||
return Color::from_rgb(0xedc850);
|
||||
case 1024:
|
||||
return Color::from_rgb(0xedc53f);
|
||||
case 2048:
|
||||
return Color::from_rgb(0xedc22e);
|
||||
default:
|
||||
ASSERT(value > 2048);
|
||||
return Color::from_rgb(0x3c3a32);
|
||||
}
|
||||
}
|
||||
|
||||
Gfx::Color BoardView::text_color_for_cell(u32 value)
|
||||
{
|
||||
if (value <= 4)
|
||||
return Color::from_rgb(0x776e65);
|
||||
return Color::from_rgb(0xf9f6f2);
|
||||
}
|
||||
|
||||
void BoardView::paint_event(GUI::PaintEvent&)
|
||||
{
|
||||
Color background_color = Color::from_rgb(0xbbada0);
|
||||
|
||||
GUI::Painter painter(*this);
|
||||
|
||||
if (!m_board) {
|
||||
painter.fill_rect(rect(), background_color);
|
||||
return;
|
||||
}
|
||||
auto& board = *m_board;
|
||||
|
||||
Gfx::IntRect field_rect {
|
||||
0,
|
||||
0,
|
||||
static_cast<int>(m_padding + (m_cell_size + m_padding) * columns()),
|
||||
static_cast<int>(m_padding + (m_cell_size + m_padding) * rows())
|
||||
};
|
||||
field_rect.center_within(rect());
|
||||
painter.fill_rect(field_rect, background_color);
|
||||
|
||||
for (size_t column = 0; column < columns(); ++column) {
|
||||
for (size_t row = 0; row < rows(); ++row) {
|
||||
auto rect = Gfx::IntRect {
|
||||
field_rect.x() + m_padding + (m_cell_size + m_padding) * column,
|
||||
field_rect.y() + m_padding + (m_cell_size + m_padding) * row,
|
||||
m_cell_size,
|
||||
m_cell_size,
|
||||
};
|
||||
auto entry = board[row][column];
|
||||
painter.fill_rect(rect, background_color_for_cell(entry));
|
||||
if (entry > 0)
|
||||
painter.draw_text(rect, String::number(entry), font(), Gfx::TextAlignment::Center, text_color_for_cell(entry));
|
||||
}
|
||||
}
|
||||
}
|
61
Userland/Games/2048/BoardView.h
Normal file
61
Userland/Games/2048/BoardView.h
Normal file
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
* Copyright (c) 2020, the SerenityOS developers.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Game.h"
|
||||
#include <LibGUI/Widget.h>
|
||||
|
||||
class BoardView final : public GUI::Widget {
|
||||
C_OBJECT(BoardView)
|
||||
|
||||
public:
|
||||
BoardView(const Game::Board*);
|
||||
virtual ~BoardView() override;
|
||||
|
||||
void set_board(const Game::Board* board);
|
||||
|
||||
Function<void(Game::Direction)> on_move;
|
||||
|
||||
private:
|
||||
virtual void resize_event(GUI::ResizeEvent&) override;
|
||||
virtual void paint_event(GUI::PaintEvent&) override;
|
||||
virtual void keydown_event(GUI::KeyEvent&) override;
|
||||
|
||||
size_t rows() const;
|
||||
size_t columns() const;
|
||||
|
||||
void pick_font();
|
||||
void resize();
|
||||
|
||||
Color background_color_for_cell(u32 value);
|
||||
Color text_color_for_cell(u32 value);
|
||||
|
||||
float m_padding { 0 };
|
||||
float m_cell_size { 0 };
|
||||
|
||||
const Game::Board* m_board { nullptr };
|
||||
};
|
9
Userland/Games/2048/CMakeLists.txt
Normal file
9
Userland/Games/2048/CMakeLists.txt
Normal file
|
@ -0,0 +1,9 @@
|
|||
set(SOURCES
|
||||
BoardView.cpp
|
||||
Game.cpp
|
||||
GameSizeDialog.cpp
|
||||
main.cpp
|
||||
)
|
||||
|
||||
serenity_app(2048 ICON app-2048)
|
||||
target_link_libraries(2048 LibGUI)
|
229
Userland/Games/2048/Game.cpp
Normal file
229
Userland/Games/2048/Game.cpp
Normal file
|
@ -0,0 +1,229 @@
|
|||
/*
|
||||
* Copyright (c) 2020, the SerenityOS developers.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "Game.h"
|
||||
#include <AK/String.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
Game::Game(size_t grid_size, size_t target_tile)
|
||||
: m_grid_size(grid_size)
|
||||
{
|
||||
if (target_tile == 0)
|
||||
m_target_tile = 2048;
|
||||
else if ((target_tile & (target_tile - 1)) != 0)
|
||||
m_target_tile = 1 << max_power_for_board(grid_size);
|
||||
else
|
||||
m_target_tile = target_tile;
|
||||
|
||||
m_board.resize(grid_size);
|
||||
for (auto& row : m_board) {
|
||||
row.ensure_capacity(grid_size);
|
||||
for (size_t i = 0; i < grid_size; i++)
|
||||
row.append(0);
|
||||
}
|
||||
|
||||
add_random_tile();
|
||||
add_random_tile();
|
||||
}
|
||||
|
||||
void Game::add_random_tile()
|
||||
{
|
||||
int row;
|
||||
int column;
|
||||
do {
|
||||
row = rand() % m_grid_size;
|
||||
column = rand() % m_grid_size;
|
||||
} while (m_board[row][column] != 0);
|
||||
|
||||
size_t value = rand() < RAND_MAX * 0.9 ? 2 : 4;
|
||||
m_board[row][column] = value;
|
||||
}
|
||||
|
||||
static Game::Board transpose(const Game::Board& board)
|
||||
{
|
||||
Vector<Vector<u32>> new_board;
|
||||
auto result_row_count = board[0].size();
|
||||
auto result_column_count = board.size();
|
||||
|
||||
new_board.resize(result_row_count);
|
||||
|
||||
for (size_t i = 0; i < board.size(); ++i) {
|
||||
auto& row = new_board[i];
|
||||
row.clear_with_capacity();
|
||||
row.ensure_capacity(result_column_count);
|
||||
for (auto& entry : board) {
|
||||
row.append(entry[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return new_board;
|
||||
}
|
||||
|
||||
static Game::Board reverse(const Game::Board& board)
|
||||
{
|
||||
auto new_board = board;
|
||||
for (auto& row : new_board) {
|
||||
for (size_t i = 0; i < row.size() / 2; ++i)
|
||||
swap(row[i], row[row.size() - i - 1]);
|
||||
}
|
||||
|
||||
return new_board;
|
||||
}
|
||||
|
||||
static Vector<u32> slide_row(const Vector<u32>& row, size_t& successful_merge_score)
|
||||
{
|
||||
if (row.size() < 2)
|
||||
return row;
|
||||
|
||||
auto x = row[0];
|
||||
auto y = row[1];
|
||||
|
||||
auto result = row;
|
||||
result.take_first();
|
||||
|
||||
if (x == 0) {
|
||||
result = slide_row(result, successful_merge_score);
|
||||
result.append(0);
|
||||
return result;
|
||||
}
|
||||
|
||||
if (y == 0) {
|
||||
result[0] = x;
|
||||
result = slide_row(result, successful_merge_score);
|
||||
result.append(0);
|
||||
return result;
|
||||
}
|
||||
|
||||
if (x == y) {
|
||||
result.take_first();
|
||||
result = slide_row(result, successful_merge_score);
|
||||
result.append(0);
|
||||
result.prepend(x + x);
|
||||
successful_merge_score += x * 2;
|
||||
return result;
|
||||
}
|
||||
|
||||
result = slide_row(result, successful_merge_score);
|
||||
result.prepend(x);
|
||||
return result;
|
||||
}
|
||||
|
||||
static Game::Board slide_left(const Game::Board& board, size_t& successful_merge_score)
|
||||
{
|
||||
Vector<Vector<u32>> new_board;
|
||||
for (auto& row : board)
|
||||
new_board.append(slide_row(row, successful_merge_score));
|
||||
|
||||
return new_board;
|
||||
}
|
||||
|
||||
static bool is_complete(const Game::Board& board, size_t target)
|
||||
{
|
||||
for (auto& row : board) {
|
||||
if (row.contains_slow(target))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool has_no_neighbors(const Span<const u32>& row)
|
||||
{
|
||||
if (row.size() < 2)
|
||||
return true;
|
||||
|
||||
auto x = row[0];
|
||||
auto y = row[1];
|
||||
|
||||
if (x == y)
|
||||
return false;
|
||||
|
||||
return has_no_neighbors(row.slice(1, row.size() - 1));
|
||||
};
|
||||
|
||||
static bool is_stalled(const Game::Board& board)
|
||||
{
|
||||
static auto stalled = [](auto& row) {
|
||||
return !row.contains_slow(0) && has_no_neighbors(row.span());
|
||||
};
|
||||
|
||||
for (auto& row : board)
|
||||
if (!stalled(row))
|
||||
return false;
|
||||
|
||||
for (auto& row : transpose(board))
|
||||
if (!stalled(row))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Game::MoveOutcome Game::attempt_move(Direction direction)
|
||||
{
|
||||
size_t successful_merge_score = 0;
|
||||
Board new_board;
|
||||
|
||||
switch (direction) {
|
||||
case Direction::Left:
|
||||
new_board = slide_left(m_board, successful_merge_score);
|
||||
break;
|
||||
case Direction::Right:
|
||||
new_board = reverse(slide_left(reverse(m_board), successful_merge_score));
|
||||
break;
|
||||
case Direction::Up:
|
||||
new_board = transpose(slide_left(transpose(m_board), successful_merge_score));
|
||||
break;
|
||||
case Direction::Down:
|
||||
new_board = transpose(reverse(slide_left(reverse(transpose(m_board)), successful_merge_score)));
|
||||
break;
|
||||
}
|
||||
|
||||
bool moved = new_board != m_board;
|
||||
if (moved) {
|
||||
m_board = new_board;
|
||||
m_turns++;
|
||||
add_random_tile();
|
||||
m_score += successful_merge_score;
|
||||
}
|
||||
|
||||
if (is_complete(m_board, m_target_tile))
|
||||
return MoveOutcome::Won;
|
||||
if (is_stalled(m_board))
|
||||
return MoveOutcome::GameOver;
|
||||
if (moved)
|
||||
return MoveOutcome::OK;
|
||||
return MoveOutcome::InvalidMove;
|
||||
}
|
||||
|
||||
u32 Game::largest_tile() const
|
||||
{
|
||||
u32 tile = 0;
|
||||
for (auto& row : board()) {
|
||||
for (auto& cell : row)
|
||||
tile = max(tile, cell);
|
||||
}
|
||||
return tile;
|
||||
}
|
78
Userland/Games/2048/Game.h
Normal file
78
Userland/Games/2048/Game.h
Normal file
|
@ -0,0 +1,78 @@
|
|||
/*
|
||||
* Copyright (c) 2020, the SerenityOS developers.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Vector.h>
|
||||
|
||||
class Game final {
|
||||
public:
|
||||
Game(size_t board_size, size_t target_tile = 0);
|
||||
Game(const Game&) = default;
|
||||
|
||||
enum class MoveOutcome {
|
||||
OK,
|
||||
InvalidMove,
|
||||
GameOver,
|
||||
Won,
|
||||
};
|
||||
|
||||
enum class Direction {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right,
|
||||
};
|
||||
|
||||
MoveOutcome attempt_move(Direction);
|
||||
|
||||
size_t score() const { return m_score; }
|
||||
size_t turns() const { return m_turns; }
|
||||
u32 target_tile() const { return m_target_tile; }
|
||||
u32 largest_tile() const;
|
||||
|
||||
using Board = Vector<Vector<u32>>;
|
||||
|
||||
const Board& board() const { return m_board; }
|
||||
|
||||
static size_t max_power_for_board(size_t size)
|
||||
{
|
||||
if (size >= 6)
|
||||
return 31;
|
||||
|
||||
return size * size + 1;
|
||||
}
|
||||
|
||||
private:
|
||||
void add_random_tile();
|
||||
|
||||
size_t m_grid_size { 0 };
|
||||
u32 m_target_tile { 0 };
|
||||
|
||||
Board m_board;
|
||||
size_t m_score { 0 };
|
||||
size_t m_turns { 0 };
|
||||
};
|
95
Userland/Games/2048/GameSizeDialog.cpp
Normal file
95
Userland/Games/2048/GameSizeDialog.cpp
Normal file
|
@ -0,0 +1,95 @@
|
|||
/*
|
||||
* Copyright (c) 2020, the SerenityOS developers.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "GameSizeDialog.h"
|
||||
#include "Game.h"
|
||||
#include <LibGUI/BoxLayout.h>
|
||||
#include <LibGUI/Button.h>
|
||||
#include <LibGUI/CheckBox.h>
|
||||
#include <LibGUI/Label.h>
|
||||
#include <LibGUI/SpinBox.h>
|
||||
|
||||
GameSizeDialog::GameSizeDialog(GUI::Window* parent)
|
||||
: GUI::Dialog(parent)
|
||||
{
|
||||
set_rect({ 0, 0, 200, 150 });
|
||||
set_title("New Game");
|
||||
set_icon(parent->icon());
|
||||
set_resizable(false);
|
||||
|
||||
auto& main_widget = set_main_widget<GUI::Widget>();
|
||||
main_widget.set_fill_with_background_color(true);
|
||||
|
||||
auto& layout = main_widget.set_layout<GUI::VerticalBoxLayout>();
|
||||
layout.set_margins({ 4, 4, 4, 4 });
|
||||
|
||||
auto& board_size_box = main_widget.add<GUI::Widget>();
|
||||
auto& input_layout = board_size_box.set_layout<GUI::HorizontalBoxLayout>();
|
||||
input_layout.set_spacing(4);
|
||||
|
||||
board_size_box.add<GUI::Label>("Board size").set_text_alignment(Gfx::TextAlignment::CenterLeft);
|
||||
auto& spinbox = board_size_box.add<GUI::SpinBox>();
|
||||
|
||||
auto& target_box = main_widget.add<GUI::Widget>();
|
||||
auto& target_layout = target_box.set_layout<GUI::HorizontalBoxLayout>();
|
||||
target_layout.set_spacing(4);
|
||||
spinbox.set_min(2);
|
||||
spinbox.set_value(m_board_size);
|
||||
|
||||
target_box.add<GUI::Label>("Target tile").set_text_alignment(Gfx::TextAlignment::CenterLeft);
|
||||
auto& tile_value_label = target_box.add<GUI::Label>(String::number(target_tile()));
|
||||
tile_value_label.set_text_alignment(Gfx::TextAlignment::CenterRight);
|
||||
auto& target_spinbox = target_box.add<GUI::SpinBox>();
|
||||
target_spinbox.set_max(Game::max_power_for_board(m_board_size));
|
||||
target_spinbox.set_min(3);
|
||||
target_spinbox.set_value(m_target_tile_power);
|
||||
|
||||
spinbox.on_change = [&](auto value) {
|
||||
m_board_size = value;
|
||||
target_spinbox.set_max(Game::max_power_for_board(m_board_size));
|
||||
};
|
||||
|
||||
target_spinbox.on_change = [&](auto value) {
|
||||
m_target_tile_power = value;
|
||||
tile_value_label.set_text(String::number(target_tile()));
|
||||
};
|
||||
|
||||
auto& temp_checkbox = main_widget.add<GUI::CheckBox>("Temporary");
|
||||
temp_checkbox.set_checked(m_temporary);
|
||||
temp_checkbox.on_checked = [this](auto checked) { m_temporary = checked; };
|
||||
|
||||
auto& buttonbox = main_widget.add<GUI::Widget>();
|
||||
auto& button_layout = buttonbox.set_layout<GUI::HorizontalBoxLayout>();
|
||||
button_layout.set_spacing(10);
|
||||
|
||||
buttonbox.add<GUI::Button>("Cancel").on_click = [this](auto) {
|
||||
done(Dialog::ExecCancel);
|
||||
};
|
||||
|
||||
buttonbox.add<GUI::Button>("OK").on_click = [this](auto) {
|
||||
done(Dialog::ExecOK);
|
||||
};
|
||||
}
|
45
Userland/Games/2048/GameSizeDialog.h
Normal file
45
Userland/Games/2048/GameSizeDialog.h
Normal file
|
@ -0,0 +1,45 @@
|
|||
/*
|
||||
* Copyright (c) 2020, the SerenityOS developers.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Types.h>
|
||||
#include <LibGUI/Dialog.h>
|
||||
|
||||
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 temporary() const { return m_temporary; }
|
||||
|
||||
private:
|
||||
size_t m_board_size { 4 };
|
||||
size_t m_target_tile_power { 11 };
|
||||
bool m_temporary { true };
|
||||
};
|
215
Userland/Games/2048/main.cpp
Normal file
215
Userland/Games/2048/main.cpp
Normal file
|
@ -0,0 +1,215 @@
|
|||
/*
|
||||
* Copyright (c) 2020, the SerenityOS developers.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "BoardView.h"
|
||||
#include "Game.h"
|
||||
#include "GameSizeDialog.h"
|
||||
#include <LibCore/ConfigFile.h>
|
||||
#include <LibGUI/Action.h>
|
||||
#include <LibGUI/Application.h>
|
||||
#include <LibGUI/BoxLayout.h>
|
||||
#include <LibGUI/Button.h>
|
||||
#include <LibGUI/Icon.h>
|
||||
#include <LibGUI/Menu.h>
|
||||
#include <LibGUI/MenuBar.h>
|
||||
#include <LibGUI/MessageBox.h>
|
||||
#include <LibGUI/StatusBar.h>
|
||||
#include <LibGUI/Window.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (pledge("stdio rpath wpath cpath shared_buffer accept cpath unix fattr", nullptr) < 0) {
|
||||
perror("pledge");
|
||||
return 1;
|
||||
}
|
||||
|
||||
srand(time(nullptr));
|
||||
|
||||
auto app = GUI::Application::construct(argc, argv);
|
||||
auto app_icon = GUI::Icon::default_icon("app-2048");
|
||||
|
||||
auto window = GUI::Window::construct();
|
||||
|
||||
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);
|
||||
|
||||
config->write_num_entry("", "board_size", board_size);
|
||||
config->write_num_entry("", "target_tile", target_tile);
|
||||
|
||||
config->sync();
|
||||
|
||||
if (pledge("stdio rpath shared_buffer wpath cpath accept", nullptr) < 0) {
|
||||
perror("pledge");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (unveil("/res", "r") < 0) {
|
||||
perror("unveil");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (unveil(config->file_name().characters(), "crw") < 0) {
|
||||
perror("unveil");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (unveil(nullptr, nullptr) < 0) {
|
||||
perror("unveil");
|
||||
return 1;
|
||||
}
|
||||
|
||||
window->set_double_buffering_enabled(false);
|
||||
window->set_title("2048");
|
||||
window->resize(315, 336);
|
||||
|
||||
auto& main_widget = window->set_main_widget<GUI::Widget>();
|
||||
main_widget.set_layout<GUI::VerticalBoxLayout>();
|
||||
main_widget.set_fill_with_background_color(true);
|
||||
|
||||
Game game { board_size, target_tile };
|
||||
|
||||
auto& board_view = main_widget.add<BoardView>(&game.board());
|
||||
board_view.set_focus(true);
|
||||
auto& statusbar = main_widget.add<GUI::StatusBar>();
|
||||
|
||||
auto update = [&]() {
|
||||
board_view.set_board(&game.board());
|
||||
board_view.update();
|
||||
statusbar.set_text(String::formatted("Score: {}", game.score()));
|
||||
};
|
||||
|
||||
update();
|
||||
|
||||
Vector<Game> undo_stack;
|
||||
|
||||
auto change_settings = [&] {
|
||||
auto size_dialog = GameSizeDialog::construct(window);
|
||||
if (size_dialog->exec() || size_dialog->result() != GUI::Dialog::ExecOK)
|
||||
return;
|
||||
|
||||
board_size = size_dialog->board_size();
|
||||
target_tile = size_dialog->target_tile();
|
||||
|
||||
if (!size_dialog->temporary()) {
|
||||
|
||||
config->write_num_entry("", "board_size", board_size);
|
||||
config->write_num_entry("", "target_tile", target_tile);
|
||||
|
||||
if (!config->sync()) {
|
||||
GUI::MessageBox::show(window, "Configuration could not be synced", "Error", GUI::MessageBox::Type::Error);
|
||||
return;
|
||||
}
|
||||
GUI::MessageBox::show(window, "New settings have been saved and will be applied on a new game", "Settings Changed Successfully", GUI::MessageBox::Type::Information);
|
||||
return;
|
||||
}
|
||||
|
||||
GUI::MessageBox::show(window, "New settings have been set and will be applied on the next game", "Settings Changed Successfully", GUI::MessageBox::Type::Information);
|
||||
};
|
||||
auto start_a_new_game = [&] {
|
||||
// Do not leak game states between games.
|
||||
undo_stack.clear();
|
||||
|
||||
game = Game(board_size, target_tile);
|
||||
|
||||
// This ensures that the sizes are correct.
|
||||
board_view.set_board(nullptr);
|
||||
board_view.set_board(&game.board());
|
||||
|
||||
update();
|
||||
window->update();
|
||||
};
|
||||
|
||||
board_view.on_move = [&](Game::Direction direction) {
|
||||
undo_stack.append(game);
|
||||
auto outcome = game.attempt_move(direction);
|
||||
switch (outcome) {
|
||||
case Game::MoveOutcome::OK:
|
||||
if (undo_stack.size() >= 16)
|
||||
undo_stack.take_first();
|
||||
update();
|
||||
break;
|
||||
case Game::MoveOutcome::InvalidMove:
|
||||
undo_stack.take_last();
|
||||
break;
|
||||
case Game::MoveOutcome::Won:
|
||||
update();
|
||||
GUI::MessageBox::show(window,
|
||||
String::formatted("You reached {} in {} turns with a score of {}", game.target_tile(), game.turns(), game.score()),
|
||||
"You won!",
|
||||
GUI::MessageBox::Type::Information);
|
||||
start_a_new_game();
|
||||
break;
|
||||
case Game::MoveOutcome::GameOver:
|
||||
update();
|
||||
GUI::MessageBox::show(window,
|
||||
String::formatted("You reached {} in {} turns with a score of {}", game.largest_tile(), game.turns(), game.score()),
|
||||
"You lost!",
|
||||
GUI::MessageBox::Type::Information);
|
||||
start_a_new_game();
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
auto menubar = GUI::MenuBar::construct();
|
||||
|
||||
auto& app_menu = menubar->add_menu("2048");
|
||||
|
||||
app_menu.add_action(GUI::Action::create("New game", { Mod_None, Key_F2 }, [&](auto&) {
|
||||
start_a_new_game();
|
||||
}));
|
||||
|
||||
app_menu.add_action(GUI::CommonActions::make_undo_action([&](auto&) {
|
||||
if (undo_stack.is_empty())
|
||||
return;
|
||||
game = undo_stack.take_last();
|
||||
update();
|
||||
}));
|
||||
|
||||
app_menu.add_separator();
|
||||
|
||||
app_menu.add_action(GUI::Action::create("Settings", [&](auto&) {
|
||||
change_settings();
|
||||
}));
|
||||
|
||||
app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
|
||||
GUI::Application::the()->quit();
|
||||
}));
|
||||
|
||||
auto& help_menu = menubar->add_menu("Help");
|
||||
help_menu.add_action(GUI::CommonActions::make_about_action("2048", app_icon, window));
|
||||
|
||||
app->set_menubar(move(menubar));
|
||||
|
||||
window->show();
|
||||
|
||||
window->set_icon(app_icon.bitmap_for_size(16));
|
||||
|
||||
return app->exec();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue