mirror of
https://github.com/kristoferssolo/2048.git
synced 2025-10-21 15:20:35 +00:00
feat(game): add Game2048 class
This commit is contained in:
parent
c3ffd215e0
commit
6af96454bd
3
src/game/__init__.py
Normal file
3
src/game/__init__.py
Normal file
@ -0,0 +1,3 @@
|
||||
from .game import Game2048
|
||||
|
||||
__all__ = ["Game2048"]
|
||||
23
src/game/game.py
Normal file
23
src/game/game.py
Normal file
@ -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]
|
||||
Loading…
Reference in New Issue
Block a user