From 6af96454bd0537d40034109df6256f36b29bec80 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Wed, 3 Jan 2024 17:15:23 +0200 Subject: [PATCH] feat(game): add `Game2048` class --- src/game/__init__.py | 3 +++ src/game/game.py | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 src/game/__init__.py create mode 100644 src/game/game.py diff --git a/src/game/__init__.py b/src/game/__init__.py new file mode 100644 index 0000000..fea3aa8 --- /dev/null +++ b/src/game/__init__.py @@ -0,0 +1,3 @@ +from .game import Game2048 + +__all__ = ["Game2048"] diff --git a/src/game/game.py b/src/game/game.py new file mode 100644 index 0000000..988fe36 --- /dev/null +++ b/src/game/game.py @@ -0,0 +1,23 @@ +import random + +import numpy as np +from utils import Config + + +class Game2048: + def __init__(self, size: int = 4): + self.size = size + self.board = np.zeros((size, size), dtype=np.int) + self.score = 0 + self.game_over = False + self.add_random_tile() + self.add_random_tile() + + def add_random_tile(self) -> None: + """Add a random tile to the board.""" + empty_cells: np.ndarray = np.argwhere(self.board == 0) + if empty_cells.shape(0) > 0: + row, col = random.choice(empty_cells) + self.board[row, col] = random.choices( + [2, 4], weights=Config.tile.probability + )[0]