1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 02:57:44 +00:00

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.
This commit is contained in:
Linus Groh 2021-05-16 21:32:39 +01:00 committed by Andreas Kling
parent 00bfcef5be
commit f89eb0e4ce
5 changed files with 109 additions and 111 deletions

View file

@ -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());
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 (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]) {
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());
VERIFY(row < m_rows && column < m_columns);
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;
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];
}