-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplay_game_cli.py
78 lines (61 loc) · 2.02 KB
/
replay_game_cli.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
69
70
71
72
73
74
75
76
77
78
import sys
import os
import time
import json
"""
Displays a replay in the terminal via ASCII
Sample usage: python3 replay_game_cli.py game_replay.awap25r
"""
# ANSI color codes
COLOR_MAP = {
"GRASS": "\033[42m ", # Green background
"WATER": "\033[46m ", # Cyan background
"MOUNTAIN": "\033[47m ", # White background
"BRIDGE": "\033[45m ", # Brown background
"SAND": "\033[43m ", # Yellow background
"BLUE": "\033[44mB", # Blue background for Blue team
"RED": "\033[41mR", # Red background for Red team
"RESET": "\033[0m",
}
def clear_screen():
os.system("cls" if os.name == "nt" else "clear")
def render_game_state(game_state, map_data):
width, height = map_data["width"], map_data["height"]
tiles = map_data["tiles"]
grid = [
[COLOR_MAP[tiles[y][x]] + " " + COLOR_MAP["RESET"] for x in range(width)]
for y in range(height)
]
# Place buildings
for team, buildings in game_state["buildings"].items():
for building in buildings:
grid[building["y"]][building["x"]] = (
COLOR_MAP[team] + "C" + COLOR_MAP["RESET"]
)
# Place units
for team, units in game_state["units"].items():
for unit in units:
grid[unit["y"]][unit["x"]] = COLOR_MAP[team] + "U" + COLOR_MAP["RESET"]
# Print the grid
for row in grid:
print("".join(row))
print(
f"Turn: {game_state['turn']}, Balance: BLUE {game_state['balance']['BLUE']} - RED {game_state['balance']['RED']}"
)
def main():
if len(sys.argv) < 2:
print("Usage: python3 replay_game_cli.py <replay_file>")
return
replay_file = sys.argv[1]
with open(replay_file, "r") as f:
data = json.load(f)
map_data = data["map"]
replay = data["replay"]
for step in replay:
clear_screen()
print(f"Turn {step['turn_number']}")
render_game_state(step["game_state"], map_data)
time.sleep(1)
print(f"Winner: {data['winner_color']}")
if __name__ == "__main__":
main()