2048: Separate game logic from the view :^)

Look Ali, it's simple:

* The *model* (in many cases, an instance of GUI::Model, but it doesn't have to
  be) should implement the "business logic" (in this case, game logic) and
  should not concern itself with how the data/state is displayed to the user.

* The *view*, conversely, should interact with the user (display data/state,
  accept input) and should not concern itself with the logic. As an example, a
  GUI::Button can display some text and accept clicks -- it doesn't know or care
  what that text *means*, or how that click affects the app state. All it does
  is it gets its text from *somebody* and notifies *somebody* of clicks.

* The *controller* connects the model to the view, and acts as "glue" between
  them.

You could connect *several different* views to one model (see FileManager), or
use identical views with different models (e.g. a table view can display pretty
much anything, depending on what model you connect to it).

In this case, the model is the Game class, which maintains a board and
implements the rules of 2048, including tracking the score. It does not display
anything, and it does not concern itself with undo management. The view is the
BoardView class, which displays a board and accepts keyboard input, but doesn't
know how exactly the tiles move or merge -- all it gets is a board state, ready
to be displayed. The controller is our main(), which connects the two classes
and bridges between their APIs. It also implements undo management, by basically
making straight-up copies of the game.

Isn't this lovely?
This commit is contained in:
Sergey Bugaev 2020-08-18 16:01:25 +03:00 committed by Andreas Kling
parent 99efc01b2e
commit 05ea144961
Notes: sideshowbarker 2024-07-19 03:26:42 +09:00
7 changed files with 583 additions and 431 deletions

View File

@ -1,402 +0,0 @@
/*
* 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 "2048.h"
#include <LibCore/ConfigFile.h>
#include <LibGUI/FontDatabase.h>
#include <LibGUI/MessageBox.h>
#include <LibGUI/Painter.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/Font.h>
#include <LibGfx/Palette.h>
#include <stdlib.h>
#include <time.h>
TwentyFortyEightGame::TwentyFortyEightGame()
{
srand(time(nullptr));
reset();
}
TwentyFortyEightGame::~TwentyFortyEightGame()
{
}
template<typename Board>
void TwentyFortyEightGame::add_tile(Board& board, int max_tile_value)
{
int row;
int column;
do {
row = rand() % m_rows;
column = rand() % m_columns;
} while (board[row][column] != 0);
int value = rand() % max_tile_value;
value = round_up_to_power_of_two(value, max_tile_value);
board[row][column] = max(2, value);
}
void TwentyFortyEightGame::pick_font()
{
constexpr static auto liza_regular = "Liza Regular";
String best_font_name = liza_regular;
int best_font_size = -1;
auto& font_database = GUI::FontDatabase::the();
font_database.for_each_font([&](const StringView& font_name) {
// Only consider variations of Liza Regular.
if (!font_name.starts_with(liza_regular))
return;
auto metadata = font_database.get_metadata_by_name(font_name);
if (!metadata.has_value())
return;
auto size = metadata.value().glyph_height;
if (size * 2 <= m_cell_size && size > best_font_size) {
best_font_name = font_name;
best_font_size = size;
}
});
auto font = font_database.get_by_name(best_font_name);
set_font(font);
}
void TwentyFortyEightGame::reset()
{
auto initial_state = [&]() -> State {
State state;
state.board.resize(m_columns);
auto& board = state.board;
for (auto& row : board) {
row.resize(m_rows);
for (auto& j : row)
j = 0;
}
add_tile(state.board, m_starting_tile);
add_tile(state.board, m_starting_tile);
return state;
};
m_states.clear();
m_states.append(initial_state());
m_current_turn = 0;
m_states.last().score_text = "Score: 0";
update();
}
Gfx::IntRect TwentyFortyEightGame::score_rect() const
{
int score_width = font().width(m_states.last().score_text);
return { 0, 2, score_width, font().glyph_height() };
}
static Vector<Vector<u32>> transpose(const Vector<Vector<u32>>& 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 Vector<Vector<u32>> reverse(const Vector<Vector<u32>>& 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 Vector<Vector<u32>> slide_left(const Vector<Vector<u32>>& 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 TwentyFortyEightGame::State& state)
{
for (auto& row : state.board) {
if (row.contains_slow(2048))
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 TwentyFortyEightGame::State& state)
{
static auto stalled = [](auto& row) {
return !row.contains_slow(0) && has_no_neighbors(row.span());
};
for (auto& row : state.board)
if (!stalled(row))
return false;
for (auto& row : transpose(state.board))
if (!stalled(row))
return false;
return true;
}
void TwentyFortyEightGame::resize_event(GUI::ResizeEvent&)
{
int score_height = font().glyph_height() + 2;
constexpr float padding_ratio = 7;
m_padding = min(
width() / (m_columns * (padding_ratio + 1) + 1),
(height() - score_height) / (m_rows * (padding_ratio + 1) + 1));
m_cell_size = m_padding * padding_ratio;
pick_font();
}
void TwentyFortyEightGame::keydown_event(GUI::KeyEvent& event)
{
auto& previous_state = m_states.last();
State new_state;
size_t successful_merge_score = 0;
switch (event.key()) {
case KeyCode::Key_A:
case KeyCode::Key_Left:
new_state.board = slide_left(previous_state.board, successful_merge_score);
break;
case KeyCode::Key_D:
case KeyCode::Key_Right:
new_state.board = reverse(slide_left(reverse(previous_state.board), successful_merge_score));
break;
case KeyCode::Key_W:
case KeyCode::Key_Up:
new_state.board = transpose(slide_left(transpose(previous_state.board), successful_merge_score));
break;
case KeyCode::Key_S:
case KeyCode::Key_Down:
new_state.board = transpose(reverse(slide_left(reverse(transpose(previous_state.board)), successful_merge_score)));
break;
default:
return;
}
if (new_state.board != previous_state.board) {
++m_current_turn;
add_tile(new_state.board, m_starting_tile * 2);
auto last_score = m_states.last().score;
if (m_states.size() == 16)
m_states.take_first();
m_states.append(move(new_state));
m_states.last().score = last_score + successful_merge_score;
m_states.last().score_text = String::format("Score: %d", score());
update();
}
if (is_complete(m_states.last())) {
// You won!
GUI::MessageBox::show(window(),
String::format("Score = %d in %zu turns", score(), m_current_turn),
"You won!",
GUI::MessageBox::Type::Information);
return game_over();
}
if (is_stalled(m_states.last())) {
// Game over!
GUI::MessageBox::show(window(),
String::format("Score = %d in %zu turns", score(), m_current_turn),
"You lost!",
GUI::MessageBox::Type::Information);
return game_over();
}
}
Gfx::Color TwentyFortyEightGame::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_NOT_REACHED();
}
}
Gfx::Color TwentyFortyEightGame::text_color_for_cell(u32 value)
{
if (value <= 4)
return Color::from_rgb(0x776e65);
return Color::from_rgb(0xf9f6f2);
}
void TwentyFortyEightGame::paint_event(GUI::PaintEvent&)
{
Color background_color = Color::from_rgb(0xbbada0);
GUI::Painter painter(*this);
painter.draw_text(score_rect(), m_states.last().score_text, font(), Gfx::TextAlignment::TopLeft, palette().color(ColorRole::BaseText));
int score_height = font().glyph_height() + 2;
Gfx::IntRect field_rect {
0,
0,
static_cast<int>(m_padding + (m_cell_size + m_padding) * m_columns),
static_cast<int>(m_padding + (m_cell_size + m_padding) * m_rows)
};
field_rect.center_within({ 0, score_height, width(), height() - score_height });
painter.fill_rect(field_rect, background_color);
for (auto column = 0; column < m_columns; ++column) {
for (auto row = 0; row < m_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 = m_states.last().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));
}
}
}
void TwentyFortyEightGame::game_over()
{
reset();
}
int TwentyFortyEightGame::score() const
{
return m_states.last().score;
}
void TwentyFortyEightGame::undo()
{
if (m_states.size() > 1) {
m_states.take_last();
--m_current_turn;
update();
}
}

225
Games/2048/BoardView.cpp Normal file
View File

@ -0,0 +1,225 @@
/*
* 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/FontDatabase.h>
#include <LibGUI/Painter.h>
#include <LibGfx/Font.h>
#include <LibGfx/Palette.h>
BoardView::BoardView(const Game::Board* board)
: m_board(board)
{
}
BoardView::~BoardView()
{
}
void BoardView::set_score(size_t score)
{
if (m_score == score && !m_score_text.is_null())
return;
m_score = score;
m_score_text = String::format("Score: %d", score);
update();
}
void BoardView::set_board(const Game::Board* board)
{
if (m_board == board)
return;
m_board = board;
update();
}
void BoardView::pick_font()
{
constexpr static auto liza_regular = "Liza Regular";
String best_font_name = liza_regular;
int best_font_size = -1;
auto& font_database = GUI::FontDatabase::the();
font_database.for_each_font([&](const StringView& font_name) {
// Only consider variations of Liza Regular.
if (!font_name.starts_with(liza_regular))
return;
auto metadata = font_database.get_metadata_by_name(font_name);
if (!metadata.has_value())
return;
auto size = metadata.value().glyph_height;
if (size * 2 <= m_cell_size && size > best_font_size) {
best_font_name = font_name;
best_font_size = size;
}
});
auto font = font_database.get_by_name(best_font_name);
set_font(font);
}
Gfx::IntRect BoardView::score_rect() const
{
int score_width = font().width(m_score_text);
return { 0, 2, score_width, font().glyph_height() };
}
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&)
{
int score_height = font().glyph_height() + 2;
constexpr float padding_ratio = 7;
m_padding = min(
width() / (columns() * (padding_ratio + 1) + 1),
(height() - score_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_NOT_REACHED();
}
}
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;
painter.draw_text(score_rect(), m_score_text, font(), Gfx::TextAlignment::TopLeft, palette().color(ColorRole::BaseText));
int score_height = font().glyph_height() + 2;
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({ 0, score_height, width(), height() - score_height });
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));
}
}
}

View File

@ -26,47 +26,39 @@
#pragma once
#include <AK/NonnullRefPtrVector.h>
#include "Game.h"
#include <LibGUI/Widget.h>
class TwentyFortyEightGame final : public GUI::Widget {
C_OBJECT(TwentyFortyEightGame)
class BoardView final : public GUI::Widget {
C_OBJECT(BoardView)
public:
virtual ~TwentyFortyEightGame() override;
BoardView(const Game::Board*);
virtual ~BoardView() override;
void reset();
int score() const;
void undo();
void set_score(size_t score);
void set_board(const Game::Board* board);
struct State {
Vector<Vector<u32>> board;
size_t score { 0 };
String score_text;
};
Function<void(Game::Direction)> on_move;
private:
TwentyFortyEightGame();
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 game_over();
Gfx::IntRect score_rect() const;
template<typename Board>
void add_tile(Board& board, int max_tile_value);
int m_rows { 4 };
int m_columns { 4 };
u32 m_starting_tile { 2 };
size_t m_current_turn { 0 };
Color background_color_for_cell(u32 value);
Color text_color_for_cell(u32 value);
float m_padding { 0 };
float m_cell_size { 0 };
Vector<State, 16> m_states;
size_t m_score { 0 };
String m_score_text;
const Game::Board* m_board { nullptr };
};

View File

@ -1,6 +1,7 @@
set(SOURCES
BoardView.cpp
Game.cpp
main.cpp
2048.cpp
)
serenity_bin(2048)

212
Games/2048/Game.cpp Normal file
View File

@ -0,0 +1,212 @@
/*
* 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"
Game::Game(size_t rows, size_t columns)
: m_rows(rows)
, m_columns(columns)
{
m_board.resize(rows);
for (auto& row : m_board) {
row.ensure_capacity(columns);
for (size_t i = 0; i < columns; i++)
row.append(0);
}
add_tile(2);
add_tile(2);
}
void Game::add_tile(u32 max_tile_value)
{
int row;
int column;
do {
row = rand() % m_rows;
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);
}
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)
{
for (auto& row : board) {
if (row.contains_slow(2048))
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_tile(4);
m_score += successful_merge_score;
}
if (is_complete(m_board))
return MoveOutcome::Won;
if (is_stalled(m_board))
return MoveOutcome::GameOver;
if (moved)
return MoveOutcome::OK;
return MoveOutcome::InvalidMove;
}

68
Games/2048/Game.h Normal file
View File

@ -0,0 +1,68 @@
/*
* 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 rows, size_t columns);
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; }
using Board = Vector<Vector<u32>>;
const Board& board() const { return m_board; }
private:
void add_tile(u32 max_tile_value);
size_t m_rows { 0 };
size_t m_columns { 0 };
Board m_board;
size_t m_score { 0 };
size_t m_turns { 0 };
};

View File

@ -24,7 +24,8 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "2048.h"
#include "BoardView.h"
#include "Game.h"
#include <LibGUI/AboutDialog.h>
#include <LibGUI/Action.h>
#include <LibGUI/Application.h>
@ -32,8 +33,10 @@
#include <LibGUI/Button.h>
#include <LibGUI/Menu.h>
#include <LibGUI/MenuBar.h>
#include <LibGUI/MessageBox.h>
#include <LibGUI/Window.h>
#include <stdio.h>
#include <time.h>
int main(int argc, char** argv)
{
@ -42,6 +45,8 @@ int main(int argc, char** argv)
return 1;
}
srand(time(nullptr));
auto app = GUI::Application::construct(argc, argv);
auto window = GUI::Window::construct();
@ -65,18 +70,69 @@ int main(int argc, char** argv)
window->set_title("2048");
window->resize(324, 336);
auto& game = window->set_main_widget<TwentyFortyEightGame>();
game.set_fill_with_background_color(true);
Game game { 4, 4 };
auto& board_view = window->set_main_widget<BoardView>(&game.board());
board_view.set_fill_with_background_color(true);
auto update = [&]() {
board_view.set_board(&game.board());
board_view.set_score(game.score());
board_view.update();
};
update();
auto start_a_new_game = [&]() {
game = Game(4, 4);
update();
};
Vector<Game> undo_stack;
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::format("Score = %d in %zu turns", game.score(), game.turns()),
"You won!",
GUI::MessageBox::Type::Information);
start_a_new_game();
break;
case Game::MoveOutcome::GameOver:
update();
GUI::MessageBox::show(window,
String::format("Score = %d in %zu turns", game.score(), game.turns()),
"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&) {
game.reset();
start_a_new_game();
}));
app_menu.add_action(GUI::CommonActions::make_undo_action([&](auto&) {
game.undo();
if (undo_stack.is_empty())
return;
game = undo_stack.take_last();
update();
}));
app_menu.add_separator();
app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {