mirror of
https://github.com/kristoferssolo/2048.git
synced 2025-10-21 15:20:35 +00:00
feat(game): display game in CLI
This commit is contained in:
parent
5513be4eb5
commit
7510598b82
4
main.py
4
main.py
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
|
from game import Game2048
|
||||||
|
|
||||||
# from ai import train
|
# from ai import train
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
from utils import BASE_PATH
|
from utils import BASE_PATH
|
||||||
@ -85,6 +87,8 @@ def main(args: argparse.ArgumentParser) -> None:
|
|||||||
logger.debug("Run game in CLI")
|
logger.debug("Run game in CLI")
|
||||||
else:
|
else:
|
||||||
logger.debug("Run app")
|
logger.debug("Run app")
|
||||||
|
game = Game2048()
|
||||||
|
game.display()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@ -1,13 +1,14 @@
|
|||||||
import random
|
import random
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
from loguru import logger
|
||||||
from utils import Config, Direction
|
from utils import Config, Direction
|
||||||
|
|
||||||
|
|
||||||
class Game2048:
|
class Game2048:
|
||||||
def __init__(self, size: int = 4):
|
def __init__(self, size: int = 4):
|
||||||
self.size = size
|
self.size = size
|
||||||
self.board = np.zeros((size, size), dtype=np.int)
|
self.board = np.zeros((size, size), dtype=int)
|
||||||
self.score = 0
|
self.score = 0
|
||||||
self.game_over = False
|
self.game_over = False
|
||||||
self.add_random_tile()
|
self.add_random_tile()
|
||||||
@ -16,7 +17,7 @@ class Game2048:
|
|||||||
def add_random_tile(self) -> None:
|
def add_random_tile(self) -> None:
|
||||||
"""Add a random tile to the board."""
|
"""Add a random tile to the board."""
|
||||||
empty_cells: np.ndarray = np.argwhere(self.board == 0)
|
empty_cells: np.ndarray = np.argwhere(self.board == 0)
|
||||||
if empty_cells.shape(0) > 0:
|
if empty_cells.shape[0] > 0:
|
||||||
row, col = random.choice(empty_cells)
|
row, col = random.choice(empty_cells)
|
||||||
self.board[row, col] = random.choices(
|
self.board[row, col] = random.choices(
|
||||||
[2, 4], weights=Config.tile.probability
|
[2, 4], weights=Config.tile.probability
|
||||||
@ -29,4 +30,7 @@ class Game2048:
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
def display(self) -> None:
|
def display(self) -> None:
|
||||||
pass
|
for row in self.board:
|
||||||
|
for val in row:
|
||||||
|
print(f"{val:^3}", end="")
|
||||||
|
print()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user