mirror of
https://github.com/kristoferssolo/2048.git
synced 2025-10-21 15:20:35 +00:00
feat(game): add play function
This commit is contained in:
parent
8a88e5ee62
commit
f5d990b1fd
6
main.py
6
main.py
@ -2,7 +2,7 @@
|
||||
|
||||
import argparse
|
||||
|
||||
from game import Game2048
|
||||
from game import play
|
||||
|
||||
# from ai import train
|
||||
from loguru import logger
|
||||
@ -85,10 +85,10 @@ def main(args: argparse.ArgumentParser) -> None:
|
||||
logger.debug("Train")
|
||||
elif args.noui:
|
||||
logger.debug("Run game in CLI")
|
||||
play()
|
||||
else:
|
||||
logger.debug("Run app")
|
||||
game = Game2048()
|
||||
game.display()
|
||||
# play()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
from .game import Game2048
|
||||
from .game import Game2048, play
|
||||
|
||||
__all__ = ["Game2048"]
|
||||
__all__ = ["Game2048", "play"]
|
||||
|
||||
@ -5,6 +5,30 @@ from loguru import logger
|
||||
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:
|
||||
def __init__(self, size: int = 4):
|
||||
self.size = size
|
||||
|
||||
Loading…
Reference in New Issue
Block a user