-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathleftright.py
69 lines (53 loc) · 1.69 KB
/
leftright.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
from dataclasses import dataclass
import environment
@dataclass(frozen=True)
class State:
board_length: int
player_position: int
whose_move: int = 0
@dataclass
class Environment(environment.Environment):
board_length: int
def __post_init__(self):
super().__post_init__()
assert self.board_length % 2 == 1, "Must be odd"
def add_agent(self, agent):
super().add_agent(agent)
def get_name(self):
return "leftright"
def initial_state(self):
return State(
board_length=self.board_length,
player_position=self.board_length // 2,
)
def transition_state(self, state, move):
new_position = state.player_position - 1
if move == "r":
new_position = state.player_position + 1
return State(
board_length=state.board_length,
player_position=new_position,
)
def is_terminal(self, state):
if state.player_position in (0, state.board_length - 1):
return True
return False
def translate_human_input(self, human_input):
return human_input
def enumerate_actions(self, state):
return ("l", "r")
def rewards(self, state):
# :agents unused for this game cuz only 1
if state.player_position == 0:
return [-1]
if state.player_position == (state.board_length - 1):
return [1]
return [0]
def text_display(self, state):
board = ["—"] * state.board_length
board[0] = "L"
board[state.board_length - 1] = "W"
board[state.player_position] = "X"
return "".join(board)
def run(self):
super().run()