-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
52 lines (35 loc) · 1.08 KB
/
cli.py
File metadata and controls
52 lines (35 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import asyncio
import os
import sys
import time
from game import SnakeGame
sys.path.append(os.path.dirname(os.path.abspath("submission/agent.py")))
from submission.agent import Agent
CLEAR_COMMAND = "cls" if os.name == "nt" else "clear"
class SnakeCLI:
def __init__(self, game: SnakeGame) -> None:
self.game = game
self.icons = {
0: " . ",
1: " ■ ",
2: " □ ",
3: " & ",
}
async def render(self):
self.game.initialise()
size = self.game.board.size
async for _ in self.game.run():
# Render snake on the board
os.system(CLEAR_COMMAND)
for i in range(size):
print(" ".join(self.icons[c] for c in self.game.board.board[i]))
time.sleep(0.25)
async def main():
# Instantiate the agent
agent = Agent()
# Start playing the game remotely
game = SnakeGame(agent)
ui = SnakeCLI(game)
await ui.render()
if __name__ == "__main__":
asyncio.run(main())