lab_tic_tac_toe/nim/game_rev.py

52 lines
1.6 KiB
Python

class NimGameRev:
"""This is the NimGameRev. game_stub.py was used as the beginning
shell code in reference to correct methods, inputs, and outputs.
I entered the code for the game to be functional.
"""
def get_initial_state(self):
return {
"board": [1, 3, 5, 7],
"first_player": True
}
def get_next_state(self, state, action):
next_state = {
"board": state["board"].copy(),
"first_player": not state["first_player"],
}
row,num_lines=action
next_state["board"][row]-=num_lines
return next_state
"""def get_actions(self, state):
"initalize with actions"
actions = [
(0, 0),
(1, 0), (1, 1),
(2, 0), (2, 1), (2, 2),
(3, 0), (3, 1), (3, 2), (3, 3),
]
return actions"""
def get_actions(self, state):
actions = []
for row_index, lines_in_row in enumerate(state["board"]):
for num_lines_to_remove in range(1, lines_in_row + 1):
actions.append((row_index, num_lines_to_remove))
return actions
def get_reward(self, state):
if self.is_over(state):
return -1 if state["first_player"] else 1
return 0
def is_over(self, state):
"""Determines if the game is over.
Checks a condition, whether all rows are empty.
If all rows are empty game is over.
"""
return all(lines == 0 for lines in state["board"])
def get_objective(self, state):
return max if state["first_player"] else min