feat(game): add play function

This commit is contained in:
Kristofers Solo 2024-01-03 17:58:40 +02:00
parent 8a88e5ee62
commit f5d990b1fd
3 changed files with 29 additions and 5 deletions

View File

@ -2,7 +2,7 @@
import argparse import argparse
from game import Game2048 from game import play
# from ai import train # from ai import train
from loguru import logger from loguru import logger
@ -85,10 +85,10 @@ def main(args: argparse.ArgumentParser) -> None:
logger.debug("Train") logger.debug("Train")
elif args.noui: elif args.noui:
logger.debug("Run game in CLI") logger.debug("Run game in CLI")
play()
else: else:
logger.debug("Run app") logger.debug("Run app")
game = Game2048() # play()
game.display()
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -1,3 +1,3 @@
from .game import Game2048 from .game import Game2048, play
__all__ = ["Game2048"] __all__ = ["Game2048", "play"]

View File

@ -5,6 +5,30 @@ from loguru import logger
from utils import Config, Direction from utils import Config, Direction
def play() -> None:
game = Game2048()
while True:
game.display()
move = input("Enter direction: ")
moves = {
"w": Direction.UP,
"a": Direction.LEFT,
"s": Direction.DOWN,
"d": Direction.RIGHT,
}
if move == "q":
break
direction = moves.get(move, None)
if direction:
game.move(direction)
game.display()
class Game2048: class Game2048:
def __init__(self, size: int = 4): def __init__(self, size: int = 4):
self.size = size self.size = size