1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-20 05:25:08 +00:00
serenity/Userland/Games/GameOfLife/Board.h
Linus Groh f89eb0e4ce 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.
2021-05-16 23:00:21 +02:00

57 lines
1.3 KiB
C++

/*
* Copyright (c) 2021, Andres Crucitti <dasc495@gmail.com>
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Vector.h>
#include <LibGfx/Point.h>
#include <stdio.h>
class Board {
public:
Board(size_t rows, size_t columns);
~Board();
size_t columns() const { return m_columns; }
size_t rows() const { return m_rows; }
void toggle_cell(size_t row, size_t column);
void set_cell(size_t row, size_t column, bool on);
bool cell(size_t row, size_t column) const;
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 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_rows { 0 };
size_t m_columns { 0 };
bool m_stalled { false };
Vector<Vector<bool>> m_cells;
};