mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 03:37:45 +00:00
Flood: Return a Color from Board::cell
This allows us to get rid of many needless release_value() calls.
This commit is contained in:
parent
5f7099cff6
commit
39caaae90a
4 changed files with 11 additions and 14 deletions
|
@ -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<Color> 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<int>(m_rows) || candidate_point.x() >= static_cast<int>(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;
|
||||
|
|
|
@ -23,7 +23,7 @@ public:
|
|||
|
||||
bool is_flooded() const;
|
||||
void set_cell(size_t row, size_t column, Color color);
|
||||
ErrorOr<Color> 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();
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<int> 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();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue