diff options
-rw-r--r-- | Userland/Games/Flood/Board.cpp | 34 | ||||
-rw-r--r-- | Userland/Games/Flood/Board.h | 18 | ||||
-rw-r--r-- | Userland/Games/Flood/BoardWidget.cpp | 2 | ||||
-rw-r--r-- | Userland/Games/Flood/main.cpp | 32 |
4 files changed, 42 insertions, 44 deletions
diff --git a/Userland/Games/Flood/Board.cpp b/Userland/Games/Flood/Board.cpp index d5c41aee48..dd204b2416 100644 --- a/Userland/Games/Flood/Board.cpp +++ b/Userland/Games/Flood/Board.cpp @@ -17,17 +17,17 @@ void Board::clear() { for (size_t row = 0; row < m_rows; ++row) { for (size_t column = 0; column < m_columns; ++column) { - set_cell(row, column, Color::Transparent); + set_cell(row, column, 0); } } } bool Board::is_flooded() const { - auto first_cell_color = cell(0, 0); + auto first_cell_value = 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)) + if (first_cell_value == cell(row, column)) continue; return false; } @@ -39,11 +39,9 @@ void Board::randomize() { for (size_t row = 0; row < m_rows; ++row) { for (size_t column = 0; column < m_columns; ++column) { - auto const& color = m_colors[get_random_uniform(m_colors.size())]; - set_cell(row, column, color); + set_cell(row, column, get_random_uniform(m_colors.size())); } } - set_current_color(cell(0, 0)); } void Board::resize(size_t rows, size_t columns) @@ -57,22 +55,22 @@ void Board::resize(size_t rows, size_t columns) m_cells[row].resize(columns); } -void Board::set_cell(size_t row, size_t column, Color color) +void Board::set_cell(size_t row, size_t column, int value) { VERIFY(row < m_rows && column < m_columns); - m_cells[row][column] = color; + m_cells[row][column] = value; } -Color Board::cell(size_t row, size_t column) const +int Board::cell(size_t row, size_t column) const { return m_cells[row][column]; } -void Board::set_current_color(Color new_color) +void Board::set_current_value(int new_value) { - m_previous_color = m_current_color; - m_current_color = new_color; + m_previous_value = m_current_value; + m_current_value = new_value; } void Board::set_color_scheme(Vector<Color> colors) @@ -84,17 +82,17 @@ void Board::set_color_scheme(Vector<Color> colors) void Board::reset() { clear(); - set_current_color(Color::Transparent); - m_previous_color = Color::Transparent; + m_current_value = 0; + m_previous_value = 0; } // Adapted from Userland/PixelPaint/Tools/BucketTool.cpp::flood_fill. -u32 Board::update_colors(bool only_calculate_flooded_area) +u32 Board::update_values(bool only_calculate_flooded_area) { Queue<Gfx::IntPoint> points_to_visit; points_to_visit.enqueue({ 0, 0 }); - set_cell(0, 0, get_current_color()); + set_cell(0, 0, get_current_value()); Vector<Vector<bool>> visited_board; visited_board.resize(cells().size()); @@ -115,12 +113,12 @@ u32 Board::update_colors(bool only_calculate_flooded_area) for (auto candidate_point : candidate_points) { 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()) == (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_value() : get_previous_value())) { ++painted; points_to_visit.enqueue(candidate_point); visited_board[candidate_point.y()][candidate_point.x()] = true; if (!only_calculate_flooded_area) - set_cell(candidate_point.y(), candidate_point.x(), get_current_color()); + set_cell(candidate_point.y(), candidate_point.x(), get_current_value()); } } } diff --git a/Userland/Games/Flood/Board.h b/Userland/Games/Flood/Board.h index 90dd726dae..08526aa81f 100644 --- a/Userland/Games/Flood/Board.h +++ b/Userland/Games/Flood/Board.h @@ -22,21 +22,21 @@ public: size_t rows() const { return m_rows; } bool is_flooded() const; - void set_cell(size_t row, size_t column, Color color); - Color cell(size_t row, size_t column) const; + void set_cell(size_t row, size_t column, int value); + int cell(size_t row, size_t column) const; auto const& cells() const { return m_cells; } void clear(); void randomize(); void reset(); void resize(size_t rows, size_t columns); - u32 update_colors(bool only_calculate_flooded_area = false); + u32 update_values(bool only_calculate_flooded_area = false); - Color get_current_color() { return m_current_color; } - Color get_previous_color() { return m_previous_color; } + int get_current_value() { return m_current_value; } + int get_previous_value() { return m_previous_value; } Vector<Color> get_color_scheme() { return m_colors; } - void set_current_color(Color new_color); + void set_current_value(int new_value); void set_color_scheme(Vector<Color> colors); struct RowAndColumn { @@ -48,9 +48,9 @@ private: size_t m_rows { 0 }; size_t m_columns { 0 }; - Color m_current_color; - Color m_previous_color; + int m_current_value; + int m_previous_value; Vector<Color> m_colors; - Vector<Vector<Color>> m_cells; + Vector<Vector<int>> m_cells; }; diff --git a/Userland/Games/Flood/BoardWidget.cpp b/Userland/Games/Flood/BoardWidget.cpp index db6ec154f0..b8c0e2dd02 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); + Color fill_color = m_board->get_color_scheme()[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 0c2ff979b0..4fa0ed2782 100644 --- a/Userland/Games/Flood/main.cpp +++ b/Userland/Games/Flood/main.cpp @@ -72,27 +72,27 @@ static ErrorOr<Vector<Color>> get_color_scheme_from_string(StringView name) // A fairly simple way to improve this would be to test deeper moves and then choose the most efficient sequence. 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)); + Board ai_board { board }; + auto const color_scheme = ai_board.get_color_scheme(); + ai_board.set_current_value(ai_board.cell(0, 0)); int moves { 0 }; - while (!optimal_board.is_flooded()) { + while (!ai_board.is_flooded()) { ++moves; int most_painted = 0; - Color optimal_color = optimal_board.cell(0, 0); + int best_value = ai_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]); - // The first update applies the current color, and the second update is done to obtain the new area. - test_board.update_colors(); - int new_area = test_board.update_colors(true); + Board test_board { ai_board }; + test_board.set_current_value(i); + // The first update applies the current value, and the second update is done to obtain the new area. + test_board.update_values(); + int new_area = test_board.update_values(true); if (new_area > most_painted) { most_painted = new_area; - optimal_color = color_scheme[i]; + best_value = i; } } - optimal_board.set_current_color(optimal_color); - optimal_board.update_colors(); + ai_board.set_current_value(best_value); + ai_board.update_values(); } return moves; } @@ -199,10 +199,10 @@ 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)); - if (board_widget->board()->get_previous_color() != board_widget->board()->get_current_color()) { + board_widget->board()->set_current_value(board_widget->board()->cell(row, column)); + if (board_widget->board()->get_previous_value() != board_widget->board()->get_current_value()) { ++moves_made; - board_widget->board()->update_colors(); + board_widget->board()->update_values(); update(); if (board_widget->board()->is_flooded()) { String dialog_text("You have tied with the AI."sv); |