-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
74 lines (61 loc) · 2.67 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import sys
import json
from ai_to_judger import pacman_to_judger
from ai_to_judger import ghost_to_judger
from ai import *
from core.GymEnvironment import PacmanEnv
from core.gamedata import GameState
from utils.utils import write_to_judger
def pacman_op(env: PacmanEnv,ai):
op = ai(env.game_state()) # 返回一个含1个元素的数组
print(f"pacman send operation {op[0]}", file=sys.stderr)
pacman_to_judger(op[0])
def ghosts_op(env: PacmanEnv,ai):
op = ai(env.game_state()) # 返回一个含3个元素的数组
print(f"ghosts send operation {op[0]} {op[1]} {op[2]}", file=sys.stderr)
ghost_to_judger(op[0],op[1],op[2])
class Controller:
def __init__(self):
self.env = PacmanEnv()
id = int(input())
self.id = id
self.level_change = True
self.eat_all_beans = False
def run(self, ai):
while 1:
if self.level_change == True:
init_info = input()
self.env.ai_reset(json.loads(init_info))
self.level_change = False
if self.id == 0:
#当前为0号玩家
# 0号玩家发送信息
ghosts_op(self.env,ai)
# 等待1号玩家发送信息
get_info = input()
print(f"receive info: {get_info}", file=sys.stderr)
# 接收信息,调用step
get_op = input()
print(f"receive operation info: {get_op}", file=sys.stderr)
get_op_json = json.loads(get_op)
pacman_action = get_op_json["pacman_action"]
ghosts_action = get_op_json["ghosts_action"]
info , pacman_reward , ghosts_reward , self.level_change , self.eat_all_beans = self.env.step(pacman_action,ghosts_action)
else:
#当前为1号玩家
# 等待0号玩家发送信息
get_info = input()
print(f"receive info: {get_info}", file=sys.stderr)
# 1号玩家发送信息
ghosts_op(self.env,ai)
# 接收信息,调用step
get_op = input()
print(f"receive operation info: {get_op}", file=sys.stderr)
get_op_json = json.loads(get_op)
pacman_action = get_op_json["pacman_action"]
ghosts_action = get_op_json["ghosts_action"]
info , pacman_reward , ghosts_reward , self.level_change , self.eat_all_beans = self.env.step(pacman_action,ghosts_action)
if __name__ == "__main__":
print("init done", file=sys.stderr)
controller = Controller()
controller.run(ai_func)