From 39caaae90a8181e6768c5e93df2501600375db47 Mon Sep 17 00:00:00 2001 From: implicitfield <114500360+implicitfield@users.noreply.github.com> Date: Mon, 28 Nov 2022 21:52:58 +0200 Subject: [PATCH] Flood: Return a Color from Board::cell This allows us to get rid of many needless release_value() calls. --- Userland/Games/Flood/Board.cpp | 15 ++++++--------- Userland/Games/Flood/Board.h | 2 +- Userland/Games/Flood/BoardWidget.cpp | 2 +- Userland/Games/Flood/main.cpp | 6 +++--- 4 files changed, 11 insertions(+), 14 deletions(-) diff --git a/Userland/Games/Flood/Board.cpp b/Userland/Games/Flood/Board.cpp index a1be59535c..d5c41aee48 100644 --- a/Userland/Games/Flood/Board.cpp +++ b/Userland/Games/Flood/Board.cpp @@ -24,10 +24,10 @@ void Board::clear() bool Board::is_flooded() const { - auto first_cell_color = cell(0, 0).release_value(); + auto first_cell_color = cell(0, 0); for (size_t row = 0; row < rows(); ++row) { for (size_t column = 0; column < columns(); ++column) { - if (first_cell_color == cell(row, column).release_value()) + if (first_cell_color == cell(row, column)) continue; return false; } @@ -43,7 +43,7 @@ void Board::randomize() set_cell(row, column, color); } } - set_current_color(cell(0, 0).release_value()); + set_current_color(cell(0, 0)); } void Board::resize(size_t rows, size_t columns) @@ -64,11 +64,8 @@ void Board::set_cell(size_t row, size_t column, Color color) m_cells[row][column] = color; } -ErrorOr Board::cell(size_t row, size_t column) const +Color Board::cell(size_t row, size_t column) const { - if (row >= m_rows || column >= m_columns) - return Error::from_string_literal("No such cell."); - return m_cells[row][column]; } @@ -116,9 +113,9 @@ u32 Board::update_colors(bool only_calculate_flooded_area) current_point.moved_down(1) }; for (auto candidate_point : candidate_points) { - if (cell(candidate_point.y(), candidate_point.x()).is_error()) + if (candidate_point.y() >= static_cast(m_rows) || candidate_point.x() >= static_cast(m_columns) || candidate_point.y() < 0 || candidate_point.x() < 0) continue; - if (!visited_board[candidate_point.y()][candidate_point.x()] && cell(candidate_point.y(), candidate_point.x()).release_value() == (only_calculate_flooded_area ? get_current_color() : get_previous_color())) { + if (!visited_board[candidate_point.y()][candidate_point.x()] && cell(candidate_point.y(), candidate_point.x()) == (only_calculate_flooded_area ? get_current_color() : get_previous_color())) { ++painted; points_to_visit.enqueue(candidate_point); visited_board[candidate_point.y()][candidate_point.x()] = true; diff --git a/Userland/Games/Flood/Board.h b/Userland/Games/Flood/Board.h index 18f4b65f6e..90dd726dae 100644 --- a/Userland/Games/Flood/Board.h +++ b/Userland/Games/Flood/Board.h @@ -23,7 +23,7 @@ public: bool is_flooded() const; void set_cell(size_t row, size_t column, Color color); - ErrorOr cell(size_t row, size_t column) const; + Color cell(size_t row, size_t column) const; auto const& cells() const { return m_cells; } void clear(); diff --git a/Userland/Games/Flood/BoardWidget.cpp b/Userland/Games/Flood/BoardWidget.cpp index f43e7d335b..db6ec154f0 100644 --- a/Userland/Games/Flood/BoardWidget.cpp +++ b/Userland/Games/Flood/BoardWidget.cpp @@ -62,7 +62,7 @@ void BoardWidget::paint_event(GUI::PaintEvent& event) int cell_y = row * cell_size + board_offset.height(); Gfx::Rect cell_rect(cell_x, cell_y, cell_size, cell_size); - Color fill_color = m_board->cell(row, column).release_value(); + Color fill_color = m_board->cell(row, column); painter.fill_rect(cell_rect, fill_color); } } diff --git a/Userland/Games/Flood/main.cpp b/Userland/Games/Flood/main.cpp index 9d4c9f1780..0c2ff979b0 100644 --- a/Userland/Games/Flood/main.cpp +++ b/Userland/Games/Flood/main.cpp @@ -74,12 +74,12 @@ static int get_number_of_moves_from_ai(Board const& board) { Board optimal_board { board }; auto const color_scheme = optimal_board.get_color_scheme(); - optimal_board.set_current_color(optimal_board.cell(0, 0).release_value()); + optimal_board.set_current_color(optimal_board.cell(0, 0)); int moves { 0 }; while (!optimal_board.is_flooded()) { ++moves; int most_painted = 0; - Color optimal_color = optimal_board.cell(0, 0).release_value(); + Color optimal_color = optimal_board.cell(0, 0); for (size_t i = 0; i < color_scheme.size(); ++i) { Board test_board { optimal_board }; test_board.set_current_color(color_scheme[i]); @@ -199,7 +199,7 @@ ErrorOr serenity_main(Main::Arguments arguments) board_widget->on_move = [&](Board::RowAndColumn row_and_column) { auto const [row, column] = row_and_column; - board_widget->board()->set_current_color(board_widget->board()->cell(row, column).release_value()); + board_widget->board()->set_current_color(board_widget->board()->cell(row, column)); if (board_widget->board()->get_previous_color() != board_widget->board()->get_current_color()) { ++moves_made; board_widget->board()->update_colors();