diff options
author | Linus Groh <mail@linusgroh.de> | 2021-05-16 21:32:39 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-05-16 23:00:21 +0200 |
commit | f89eb0e4ce10a9377145609591193bd8c70a0691 (patch) | |
tree | 16c5184e5c2367b51741801d67ee0a3ba23fb241 /Userland/Games | |
parent | 00bfcef5befbd2cbceac860a42e8dd76163231cb (diff) | |
download | serenity-f89eb0e4ce10a9377145609591193bd8c70a0691.zip |
GameOfLife: Switch from single indexed vector to rows+columns
This is not only easier to comprehend code-wise and avoids some function
overloads, but also makes resizing the board while preserving game state
*a lot* easier. We now no longer have to allocate a new board on every
resize, we just grow/shrink the individual row vectors.
Also fixes a crash when clicking the board widget outside of the drawn
board area.
Diffstat (limited to 'Userland/Games')
-rw-r--r-- | Userland/Games/GameOfLife/Board.cpp | 91 | ||||
-rw-r--r-- | Userland/Games/GameOfLife/Board.h | 37 | ||||
-rw-r--r-- | Userland/Games/GameOfLife/BoardWidget.cpp | 66 | ||||
-rw-r--r-- | Userland/Games/GameOfLife/BoardWidget.h | 24 | ||||
-rw-r--r-- | Userland/Games/GameOfLife/main.cpp | 4 |
5 files changed, 110 insertions, 112 deletions
diff --git a/Userland/Games/GameOfLife/Board.cpp b/Userland/Games/GameOfLife/Board.cpp index 452c803776..897f30c340 100644 --- a/Userland/Games/GameOfLife/Board.cpp +++ b/Userland/Games/GameOfLife/Board.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2021, Andres Crucitti <dasc495@gmail.com> + * Copyright (c) 2021, Linus Groh <linusg@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -9,13 +10,8 @@ #include <time.h> Board::Board(size_t rows, size_t columns) - : m_columns(columns) - , m_rows(rows) { - m_cells.resize(total_size()); - for (size_t i = 0; i < total_size(); ++i) { - m_cells[i] = false; - } + resize(rows, columns); } Board::~Board() @@ -25,28 +21,27 @@ Board::~Board() void Board::run_generation() { m_stalled = true; - Vector<bool> new_cells; - new_cells.resize(total_size()); - - for (size_t i = 0; i < total_size(); ++i) { - bool old_val = m_cells[i]; - new_cells[i] = calculate_next_value(i); - if (old_val != new_cells[i]) { + Vector<Vector<bool>> new_cells; + new_cells.resize(m_rows); + for (size_t row = 0; row < m_rows; ++row) + new_cells[row].resize(m_columns); + + for_each_cell([&](auto row, auto column) { + bool old_value = m_cells[row][column]; + bool new_value = calculate_next_value(row, column); + new_cells[row][column] = new_value; + if (old_value != new_value) m_stalled = false; - } - } + }); if (m_stalled) return; - m_cells = new_cells; + m_cells = move(new_cells); } -bool Board::calculate_next_value(size_t index) const +bool Board::calculate_next_value(size_t row, size_t column) const { - size_t row = index / columns(); - size_t column = index % columns(); - int top_left = cell(row - 1, column - 1); int top_mid = cell(row - 1, column); int top_right = cell(row - 1, column + 1); @@ -58,10 +53,10 @@ bool Board::calculate_next_value(size_t index) const int sum = top_left + top_mid + top_right + left + right + bottom_left + bottom_mid + bottom_right; - bool current = m_cells[index]; - bool new_value = current; + bool old_value = m_cells[row][column]; + bool new_value = old_value; - if (current) { + if (old_value) { if (sum < 2 || sum > 3) new_value = false; } else { @@ -74,59 +69,47 @@ bool Board::calculate_next_value(size_t index) const void Board::clear() { - for (size_t i = 0; i < total_size(); ++i) - set_cell(i, false); + for_each_cell([this](auto row, auto column) { + set_cell(row, column, false); + }); } void Board::randomize() { - for (size_t i = 0; i < total_size(); ++i) - set_cell(i, get_random<u32>() % 2); + for_each_cell([this](auto row, auto column) { + set_cell(row, column, get_random<u32>() % 2); + }); } -void Board::toggle_cell(size_t index) +void Board::resize(size_t rows, size_t columns) { - VERIFY(index < total_size()); + m_rows = rows; + m_columns = columns; - m_cells[index] = !m_cells[index]; + // Vector values get default-initialized, we don't need to set them to false explicitly. + m_cells.resize(rows); + for (size_t row = 0; row < rows; ++row) + m_cells[row].resize(columns); } void Board::toggle_cell(size_t row, size_t column) { - VERIFY(column < total_size() && row < total_size()); + VERIFY(row < m_rows && column < m_columns); - size_t index = calculate_index(row, column); - set_cell(index, !m_cells[index]); -} - -void Board::set_cell(size_t index, bool on) -{ - VERIFY(index < total_size()); - - m_cells[index] = on; + m_cells[row][column] = !m_cells[row][column]; } void Board::set_cell(size_t row, size_t column, bool on) { - VERIFY(column < total_size() && row < total_size()); - - size_t index = calculate_index(row, column); - set_cell(index, on); -} - -bool Board::cell(size_t index) const -{ - if (index > total_size() - 1) - return false; + VERIFY(row < m_rows && column < m_columns); - return m_cells[index]; + m_cells[row][column] = on; } bool Board::cell(size_t row, size_t column) const { - if (column > total_size() - 1 || row > total_size() - 1) + if (row >= m_rows || column >= m_columns) return false; - size_t index = calculate_index(row, column); - return cell(index); + return m_cells[row][column]; } diff --git a/Userland/Games/GameOfLife/Board.h b/Userland/Games/GameOfLife/Board.h index 86fe40c807..bcd3425e99 100644 --- a/Userland/Games/GameOfLife/Board.h +++ b/Userland/Games/GameOfLife/Board.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2021, Andres Crucitti <dasc495@gmail.com> + * Copyright (c) 2021, Linus Groh <linusg@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -12,39 +13,45 @@ class Board { public: - Board(size_t rows, size_t column); + Board(size_t rows, size_t columns); ~Board(); - size_t total_size() const { return m_columns * m_rows; } size_t columns() const { return m_columns; } size_t rows() const { return m_rows; } - size_t calculate_index(size_t row, size_t column) const { return row * m_columns + column; }; - - void toggle_cell(size_t index); void toggle_cell(size_t row, size_t column); - void set_cell(size_t row, size_t column, bool on); - void set_cell(size_t index, bool on); - bool cell(size_t row, size_t column) const; - bool cell(size_t index) const; - - const Vector<bool>& cells() const { return m_cells; } + const Vector<Vector<bool>>& cells() const { return m_cells; } void run_generation(); bool is_stalled() const { return m_stalled; } void clear(); void randomize(); + void resize(size_t rows, size_t columns); + + struct RowAndColumn { + size_t row { 0 }; + size_t column { 0 }; + }; private: - bool calculate_next_value(size_t index) const; + bool calculate_next_value(size_t row, size_t column) const; + + template<typename Callback> + void for_each_cell(Callback callback) + { + for (size_t row = 0; row < m_rows; ++row) { + for (size_t column = 0; column < m_columns; ++column) + callback(row, column); + } + } - size_t m_columns { 1 }; - size_t m_rows { 1 }; + size_t m_rows { 0 }; + size_t m_columns { 0 }; bool m_stalled { false }; - Vector<bool> m_cells; + Vector<Vector<bool>> m_cells; }; diff --git a/Userland/Games/GameOfLife/BoardWidget.cpp b/Userland/Games/GameOfLife/BoardWidget.cpp index eb7051bcd4..a8a7c1fb45 100644 --- a/Userland/Games/GameOfLife/BoardWidget.cpp +++ b/Userland/Games/GameOfLife/BoardWidget.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2021, Andres Crucitti <dasc495@gmail.com> + * Copyright (c) 2021, Linus Groh <linusg@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -8,6 +9,7 @@ #include <LibGUI/Painter.h> BoardWidget::BoardWidget(size_t rows, size_t columns) + : m_board(make<Board>(rows, columns)) { m_timer = add<Core::Timer>(); m_timer->stop(); @@ -15,8 +17,6 @@ BoardWidget::BoardWidget(size_t rows, size_t columns) run_generation(); }; m_timer->set_interval(m_running_timer_interval); - - update_board(rows, columns); } void BoardWidget::run_generation() @@ -30,19 +30,12 @@ void BoardWidget::run_generation() }; } -void BoardWidget::update_board(size_t rows, size_t columns) +void BoardWidget::resize_board(size_t rows, size_t columns) { - set_running(false); - - m_last_cell_toggled = columns * rows; - - if (m_board) { - if (columns == m_board->columns() && rows == m_board->rows()) { - return; - } - } - - m_board = make<Board>(rows, columns); + if (columns == m_board->columns() && rows == m_board->rows()) + return; + m_board->resize(rows, columns); + m_last_cell_toggled = { rows, columns }; } void BoardWidget::set_running_timer_interval(int interval) @@ -76,16 +69,16 @@ void BoardWidget::set_running(bool running) update(); } -void BoardWidget::toggle_cell(size_t index) +void BoardWidget::toggle_cell(size_t row, size_t column) { - if (m_running || !m_toggling_cells || m_last_cell_toggled == index) + if (m_running || !m_toggling_cells || (m_last_cell_toggled.row == row && m_last_cell_toggled.column == column)) return; - m_last_cell_toggled = index; - m_board->toggle_cell(index); + m_last_cell_toggled = { row, column }; + m_board->toggle_cell(row, column); if (on_cell_toggled) - on_cell_toggled(m_board, index); + on_cell_toggled(m_board, row, column); update(); } @@ -145,17 +138,23 @@ void BoardWidget::paint_event(GUI::PaintEvent& event) void BoardWidget::mousedown_event(GUI::MouseEvent& event) { - size_t index = get_index_for_point(event.x(), event.y()); set_toggling_cells(true); - toggle_cell(index); + auto row_and_column = get_row_and_column_for_point(event.x(), event.y()); + if (!row_and_column.has_value()) + return; + auto [row, column] = row_and_column.value(); + toggle_cell(row, column); } void BoardWidget::mousemove_event(GUI::MouseEvent& event) { - size_t index = get_index_for_point(event.x(), event.y()); - if (is_toggling()) { - if (last_toggled() != index) - toggle_cell(index); + auto row_and_column = get_row_and_column_for_point(event.x(), event.y()); + if (!row_and_column.has_value()) + return; + auto [row, column] = row_and_column.value(); + if (m_toggling_cells) { + if (m_last_cell_toggled.row != row || m_last_cell_toggled.column != column) + toggle_cell(row, column); } } @@ -164,9 +163,18 @@ void BoardWidget::mouseup_event(GUI::MouseEvent&) set_toggling_cells(false); } -size_t BoardWidget::get_index_for_point(int x, int y) const +Optional<Board::RowAndColumn> BoardWidget::get_row_and_column_for_point(int x, int y) const { - int cell_size = get_cell_size(); - Gfx::IntSize board_offset = get_board_offset(); - return m_board->columns() * ((y - board_offset.height()) / cell_size) + (x - board_offset.width()) / cell_size; + auto board_offset = get_board_offset(); + auto cell_size = get_cell_size(); + auto board_width = m_board->columns() * cell_size; + auto board_height = m_board->rows() * cell_size; + if (x <= board_offset.width() || static_cast<size_t>(x) >= board_offset.width() + board_width) + return {}; + if (y <= board_offset.height() || static_cast<size_t>(y) >= board_offset.height() + board_height) + return {}; + return { { + .row = static_cast<size_t>((y - board_offset.height()) / cell_size), + .column = static_cast<size_t>((x - board_offset.width()) / cell_size), + } }; } diff --git a/Userland/Games/GameOfLife/BoardWidget.h b/Userland/Games/GameOfLife/BoardWidget.h index 0d10e2daa2..7933c96e8f 100644 --- a/Userland/Games/GameOfLife/BoardWidget.h +++ b/Userland/Games/GameOfLife/BoardWidget.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2021, Andres Crucitti <dasc495@gmail.com> + * Copyright (c) 2021, Linus Groh <linusg@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -7,6 +8,10 @@ #pragma once #include "Board.h" +#include <AK/Function.h> +#include <AK/NonnullOwnPtr.h> +#include <AK/Optional.h> +#include <AK/RefPtr.h> #include <LibCore/Timer.h> #include <LibGUI/Widget.h> @@ -23,29 +28,24 @@ public: { m_toggling_cells = toggling; if (!toggling) - m_last_cell_toggled = m_board->total_size(); + m_last_cell_toggled = { m_board->rows(), m_board->columns() }; } - size_t last_toggled() const { return m_last_cell_toggled; } - bool is_toggling() const { return m_toggling_cells; } - - void toggle_cell(size_t index); + void toggle_cell(size_t row, size_t column); void clear_cells() { m_board->clear(); } void randomize_cells() { m_board->randomize(); } int get_cell_size() const; Gfx::IntSize get_board_offset() const; - size_t get_index_for_point(int x, int y) const; + Optional<Board::RowAndColumn> get_row_and_column_for_point(int x, int y) const; - void update_board(size_t rows, size_t columns); + void resize_board(size_t rows, size_t columns); const Board* board() const { return m_board.ptr(); } bool is_running() const { return m_running; } void set_running(bool r); - void set_toolbar_enabled(bool); - void run_generation(); int running_timer_interval() const { return m_running_timer_interval; } @@ -53,15 +53,15 @@ public: Function<void()> on_running_state_change; Function<void()> on_stall; - Function<void(Board*, size_t)> on_cell_toggled; + Function<void(Board*, size_t row, size_t column)> on_cell_toggled; private: BoardWidget(size_t rows, size_t columns); bool m_toggling_cells { false }; - size_t m_last_cell_toggled { 0 }; + Board::RowAndColumn m_last_cell_toggled {}; - OwnPtr<Board> m_board { nullptr }; + NonnullOwnPtr<Board> m_board; bool m_running { false }; diff --git a/Userland/Games/GameOfLife/main.cpp b/Userland/Games/GameOfLife/main.cpp index 9f0eb2ae74..040fae53b8 100644 --- a/Userland/Games/GameOfLife/main.cpp +++ b/Userland/Games/GameOfLife/main.cpp @@ -60,7 +60,7 @@ int main(int argc, char** argv) auto size_changed_function = [&] { statusbar.set_text(click_tip); - board_widget.update_board(rows_spinbox.value(), columns_spinbox.value()); + board_widget.resize_board(rows_spinbox.value(), columns_spinbox.value()); board_widget.randomize_cells(); board_widget.update(); }; @@ -159,7 +159,7 @@ int main(int argc, char** argv) statusbar.set_text("Stalled..."); }; - board_widget.on_cell_toggled = [&](auto, auto) { + board_widget.on_cell_toggled = [&](auto, auto, auto) { statusbar.set_text(click_tip); }; |