-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
60 lines (43 loc) · 1.72 KB
/
main.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
import asyncio
import sys
import time
sys.path.append("modules")
import numpy as np # noqa: E402 F401
from gobblet_rl import gobblet_v1 # noqa: E402
PLAYER = 0
DEPTH = 2
RENDER_MODE = "human"
async def main() -> None:
env = gobblet_v1.env(render_mode="human", args=None)
greedy_policy = gobblet_v1.GreedyGobbletPolicy(depth=DEPTH)
# Continue to display games between greedy agents
while True:
env.reset()
env.render() # need to render the environment before pygame can take user input
iter = 0
for agent in env.agent_iter():
observation, reward, termination, truncation, info = env.last()
if termination or truncation:
env.render()
time.sleep(1)
print(f"Agent: ({agent}), Reward: {reward}, info: {info}")
break
if iter < 2:
# Randomize the first action for variety (games can be repeated otherwise)
action_mask = observation["action_mask"]
action = np.random.choice(
np.arange(len(action_mask)), p=action_mask / np.sum(action_mask)
)
# Wait 1 second between moves so the user can follow the sequence of moves
time.sleep(1)
else:
action = greedy_policy.compute_action(
observation["observation"], observation["action_mask"]
)
# Wait 1 second between moves so the user can follow the sequence of moves
time.sleep(1)
env.step(action)
await asyncio.sleep(0) # Very important, and keep it 0
iter += 1
if __name__ == "__main__":
asyncio.run(main())