Repository navigation
Expand file tree
/
Copy path__main__.py
More file actions
100 lines (81 loc) · 3.11 KB
/
Copy path__main__.py
File metadata and controls
100 lines (81 loc) · 3.11 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import json
import sys
from src.games.reversi.reversi_game import ReversiGame
from src.games.reversi.reversi_player import ReversiRLPlayer
from src.config import default_args
def short_time_mode():
"""
短时运行模式
"""
def init_board():
full_input = json.loads(input())
requests = full_input["requests"]
responses = full_input["responses"]
color = 1
if requests[0]['x'] >= 0:
color = -1
action_tmp = requests[0]['x'] * game.n + requests[0]['y']
game.get_next_state(-color, action_tmp, game.get_current_state())
turn = len(responses)
for i in range(turn):
action_tmp = responses[i]['x'] * game.n + responses[i]['y']
game.get_next_state(color, action_tmp, game.get_current_state())
action_tmp = requests[i + 1]['x'] * game.n + requests[i + 1]['y']
game.get_next_state(-color, action_tmp, game.get_current_state())
return color
game = ReversiGame(8)
my_color = init_board()
player = ReversiRLPlayer(game, choice_mode=0, check_point=['data', default_args.best_folder_file])
action = player.play(game.get_relative_state(player=my_color, board=game.get_current_state()))[0]
if 0 <= action < game.n ** 2:
x, y = int(action // game.n), int(action % game.n)
else:
x, y = -1, -1
print(json.dumps({"response": {"x": x, "y": y}}))
def long_time_mode():
"""
长时运行模式
"""
def get_requests_action(requests):
action_tmp = -1
if requests['x'] >= 0:
action_tmp = requests['x'] * game.n + requests['y']
return action_tmp
def put_response(action_tmp):
if 0 <= action_tmp < game.n ** 2:
x, y = int(action_tmp // game.n), int(action_tmp % game.n)
else:
x, y = -1, -1
print(json.dumps({"response": {"x": x, "y": y}}))
print('>>>BOTZONE_REQUEST_KEEP_RUNNING<<<')
sys.stdout.flush()
game = ReversiGame(8)
board = game.get_current_state()
# 第一回合确定己方颜色
my_color = 1
action = get_requests_action(json.loads(input())["requests"][0])
if action != -1:
my_color = -1
board, _ = game.get_next_state(-my_color, action, board)
# 加载 RL 玩家
player = ReversiRLPlayer(game, choice_mode=0, check_point=['data', default_args.best_folder_file])
# 初始化颜色
player.init(my_color)
# 执行一次操作
action = player.play(game.get_relative_state(player=my_color, board=board))[0]
board, _ = game.get_next_state(my_color, action, board)
put_response(action)
try:
while True:
# 对方
action = get_requests_action(json.loads(input()))
board, _ = game.get_next_state(-my_color, action, board)
# 己方
action = player.play(game.get_relative_state(player=my_color, board=board))[0]
board, _ = game.get_next_state(my_color, action, board)
put_response(action)
except EOFError:
pass
if __name__ == '__main__':
long_time_mode()
# short_time_mode()