-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtalia.py
68 lines (45 loc) · 1.24 KB
/
talia.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import time
from dataclasses import dataclass
from typing import Any
class EndGameException(Exception):
pass
@dataclass
class Tile:
id: int
@dataclass
class Player:
tile: Tile
class Game:
player: Player
tiles: list[Tile]
env: dict[str, Any]
def __init__(self, player: Player, tiles: list[Tile]) -> None:
self.player = player
self.tiles = tiles
self.env = {}
def end_game(self) -> None:
raise EndGameException
def parse_logic(self, filename: str) -> None:
with open(filename) as f:
source: str = f.read()
self.env["player"] = self.player
self.env["tiles"] = self.tiles
self.env["sleep"] = time.sleep
self.env["write"] = print
self.env["end"] = self.end_game
exec(source, self.env)
def run(self) -> None:
self.env["on_start"]()
self.running = True
while True:
try:
self.env["on_turn"]()
except EndGameException:
break
def main() -> None:
tiles = [Tile(0), Tile(1), Tile(2), Tile(3), Tile(4)]
game = Game(Player(tiles[0]), tiles)
game.parse_logic("basic.talia")
game.run()
if __name__ == '__main__':
main()