def _move_selected(state, button, move): """Interface callback for proceeding through game play with user""" # Guard clause for when already-filled place is attempted if button.get_label() in [X, O]: return interface.set_invalid_move_state(state) # Update board state based on move human_id = board.not_id(state.agent_state.identifier) state.board_state = board.place(state.board_state, human_id, move) # Update UI to reflect model interface.update_board(state) # Check for game finished if board.is_finished(state.board_state): # Display game results interface.game_finished(state) else: # Process agent's decision (move, agent_state) = agent.move(state.agent_state, state.board_state) # Update board and agent state state.board_state = board.place(state.board_state, state.agent_state.identifier, move) state.agent_state = agent_state # Update UI to reflect model interface.update_board(state) # Another check for game finish to prevent inadvertent user events if board.is_finished(state.board_state): # Display game results interface.game_finished(state)
def _reward_for_move(state, board_state, move): """Helper function for determining reward from suggested move""" return Pipe( # Create new board for looking up reward board.place(board_state, state.identifier, move), # Lookup reward (or default) Then2(rewards.lookupR, state.rewards_state, state.identifier))
def _play_train_move(player, board_state): """Helper for handling single agent's move""" # Get move and updated player state (move, player) = agent.move(player, board_state) # Update board state board_state = board.place(board_state, player.identifier, move) return (board_state, player)
def reward_matrix(state, board_state): """Matrix of the reward value of all available moves""" return reduce( # Accumulate board with reward for each move lambda acc, m: board.place( acc, "{0:.3f}".format(_reward_for_move(state, board_state, m)), m), # Get available moves board_move.available(board_state), # Use board as initial state board_state).value
def place_pieces(self, board: board): #gets dictionary of worker(key) and where it is being placed(value) worker_psns = self.strategy.place_init_workers(board) for worker, psn in worker_psns: board.place(worker, psn) return board
def _move_and_agent(state, board_state, move): """Helper function for returning move and updated agent state""" last_board_state = board.place(board_state, state.identifier, move) return (move, state.set(last_board_state=last_board_state))