lab_tic_tac_toe/nim/game.py

50 lines
1.4 KiB
Python

class NimGame:
def get_initial_state(self):
return {
"board": [1, 3, 5, 7],
"first_player": True
}
def get_next_state(self, state, action):
board = state["board"].copy()
row = action[0]
number_to_remove = action[1]
board[row] = board[row] - number_to_remove
next_state = {
"board": board,
"first_player": not state["first_player"],
}
return next_state
def get_actions(self, state):
options = []
for num in range(4):
row = state["board"][num]
if row >=3:
options.append((num, 1))
options.append((num, 2))
options.append((num, 3))
elif row == 2:
options.append((num, 1))
options.append((num, 2))
elif row == 1:
options.append((num, 1))
return options
def get_reward(self, state):
if state["first_player"]:
return 1
elif not state["first_player"]:
return -1
else:
return 0
def is_over(self, state):
for row in state["board"]:
if row != 0:
return False
return True
def get_objective(self, state):
return max if state["first_player"] else min