mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 02:07:35 +00:00
2048: Do not allow the creation of games with invalid board sizes
The logic only works with nxn grids, so no need to take separate row_size/column_size arguments.
This commit is contained in:
parent
16e86a3dda
commit
40c16b3ce5
3 changed files with 11 additions and 13 deletions
|
@ -27,14 +27,13 @@
|
||||||
#include "Game.h"
|
#include "Game.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
Game::Game(size_t rows, size_t columns)
|
Game::Game(size_t grid_size)
|
||||||
: m_rows(rows)
|
: m_grid_size(grid_size)
|
||||||
, m_columns(columns)
|
|
||||||
{
|
{
|
||||||
m_board.resize(rows);
|
m_board.resize(grid_size);
|
||||||
for (auto& row : m_board) {
|
for (auto& row : m_board) {
|
||||||
row.ensure_capacity(columns);
|
row.ensure_capacity(grid_size);
|
||||||
for (size_t i = 0; i < columns; i++)
|
for (size_t i = 0; i < grid_size; i++)
|
||||||
row.append(0);
|
row.append(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,8 +46,8 @@ void Game::add_random_tile()
|
||||||
int row;
|
int row;
|
||||||
int column;
|
int column;
|
||||||
do {
|
do {
|
||||||
row = rand() % m_rows;
|
row = rand() % m_grid_size;
|
||||||
column = rand() % m_columns;
|
column = rand() % m_grid_size;
|
||||||
} while (m_board[row][column] != 0);
|
} while (m_board[row][column] != 0);
|
||||||
|
|
||||||
size_t value = rand() < RAND_MAX * 0.9 ? 2 : 4;
|
size_t value = rand() < RAND_MAX * 0.9 ? 2 : 4;
|
||||||
|
|
|
@ -30,7 +30,7 @@
|
||||||
|
|
||||||
class Game final {
|
class Game final {
|
||||||
public:
|
public:
|
||||||
Game(size_t rows, size_t columns);
|
Game(size_t);
|
||||||
Game(const Game&) = default;
|
Game(const Game&) = default;
|
||||||
|
|
||||||
enum class MoveOutcome {
|
enum class MoveOutcome {
|
||||||
|
@ -59,8 +59,7 @@ public:
|
||||||
private:
|
private:
|
||||||
void add_random_tile();
|
void add_random_tile();
|
||||||
|
|
||||||
size_t m_rows { 0 };
|
size_t m_grid_size { 0 };
|
||||||
size_t m_columns { 0 };
|
|
||||||
|
|
||||||
Board m_board;
|
Board m_board;
|
||||||
size_t m_score { 0 };
|
size_t m_score { 0 };
|
||||||
|
|
|
@ -75,7 +75,7 @@ int main(int argc, char** argv)
|
||||||
main_widget.set_layout<GUI::VerticalBoxLayout>();
|
main_widget.set_layout<GUI::VerticalBoxLayout>();
|
||||||
main_widget.set_fill_with_background_color(true);
|
main_widget.set_fill_with_background_color(true);
|
||||||
|
|
||||||
Game game { 4, 4 };
|
Game game { 4 };
|
||||||
|
|
||||||
auto& board_view = main_widget.add<BoardView>(&game.board());
|
auto& board_view = main_widget.add<BoardView>(&game.board());
|
||||||
board_view.set_focus(true);
|
board_view.set_focus(true);
|
||||||
|
@ -90,7 +90,7 @@ int main(int argc, char** argv)
|
||||||
update();
|
update();
|
||||||
|
|
||||||
auto start_a_new_game = [&]() {
|
auto start_a_new_game = [&]() {
|
||||||
game = Game(4, 4);
|
game = Game(4);
|
||||||
update();
|
update();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue