-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathsimulator.py
63 lines (55 loc) · 2.13 KB
/
simulator.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
import os
import subprocess
import tempfile
from typing import List, Optional
from mario_gpt.utils import load_level, save_level
pt = os.path.dirname(os.path.realpath(__file__))
IMAGE_PATH = os.path.join(pt, "img/")
INTERACTIVE_JAR_PATH = os.path.join(pt, "PlayLevel.jar")
ASTAR_JAR_PATH = os.path.join(pt, "PlayAstar.jar")
class Simulator:
def __init__(
self,
level_filename: Optional[str] = None,
level: Optional[List[str]] = None,
interactive_jar_path: Optional[str] = None,
astar_jar_path: Optional[str] = None,
):
if level_filename is None and level is None:
raise ValueError("level_filename OR level_txt must be provided!")
elif level is None:
level = load_level(level_filename)
if interactive_jar_path is None:
interactive_jar_path = INTERACTIVE_JAR_PATH
if astar_jar_path is None:
astar_jar_path = ASTAR_JAR_PATH
self.level_filename = level_filename
self.level = level
self.interactive_jar_path = interactive_jar_path
self.astar_jar_path = astar_jar_path
def interactive(self):
t = tempfile.NamedTemporaryFile(suffix=".txt", delete=False)
save_level(self.level, t.name)
print(f"Playing level interactively -- {t.name}!")
_ = subprocess.run(
["java", "-jar", self.interactive_jar_path, t.name, IMAGE_PATH],
stdout=subprocess.PIPE,
)
t.close()
os.unlink(t.name)
def astar(self, render: bool = True):
t = tempfile.NamedTemporaryFile(suffix=".txt", delete=False)
save_level(self.level, t.name)
print(f"Running Astar agent on level! -- {t.name}")
render_str = "human" if render else "norender"
_ = subprocess.run(
["java", "-jar", self.astar_jar_path, t.name, render_str, IMAGE_PATH],
stdout=subprocess.PIPE,
)
t.close()
os.unlink(t.name)
def __call__(self, simulate_mode: str = "interactive", render: bool = True):
if simulate_mode == "interactive":
self.interactive()
else:
self.astar(render)