def test_get_best_action_success3(self): # Tests minimax search on an incipient state and then on the state after a move has # been made # Get best move for player id 9 self.assertEqual(Strategy.get_best_action(self.__state5, 2), ((1, 0), (3, 0))) self.__state5.move_avatar(Position(1, 0), Position(3, 0)) # Get best move for player id 10 self.assertEqual(Strategy.get_best_action(self.__state5, 2), ((0, 1), (2, 2)))
def xstrategy(): # Initialize objects to read to input_obj = "" # Read from STDIN indefinitely until stream is closed for k in sys.stdin: input_obj += k # Load from read string json_list = json.loads(input_obj) # Get desired depth and state from json_list depth = json_list[0] state = json_list[1] # Find the first player in the json state first_player_color = _str_to_color(state['players'][0]['color']) # Load the json state into a State object initialized_state = initialize_state(state) # Check if the first player in the array is stuck and print false to STDOUT if so if first_player_color in initialized_state.stuck_players: print(json.dumps(False)) else: # Compute the best action the first player can take given the current state and depth best_action = Strategy.get_best_action(initialized_state, depth) # Print the best action to STDOUT print(_action_to_json(best_action))
def get_action(self, state: State) -> Action: """ Implements PlayerInterface.get_action(State). Throws GameNotRunningException() if game is still in placement mode. """ # Validate params if not isinstance(state, State): raise TypeError('Expected State for state!') # Update internal state self.__state = state return Strategy.get_best_action(state, self.__search_depth)
def __handle_take_turn(self, args): """ Handle the take-turn message (request for turn movement) from the remote player proxy. :param args: [State] representing the current state of the game and [Action, ... , Action] representing either an empty array or an array of Actions represents the penguin moves since the last time the take-turn method was called. It is empty if this is the first call or a player was eliminated since the last call. :return: the JSON-encoded Action message to send back to the RPP """ state = args[0] if Client.DEBUG: print(f'[{self.name}] is calculating turn...') action = Strategy.get_best_action(state, self.__lookahead_depth) if Client.DEBUG: print( f'[{self.name}] [{self.color}] [SEND -> RPP] take-turn ~ {action[0]} -> {action[1]}' ) return self.__json_serializer.encode_action(action)
def test_get_best_action_success2(self): # Tests successful get_best_action with a max depth of 5 (3 players) self.assertEqual(Strategy.get_best_action(self.__state2, 5), ((1, 1), (2, 2)))
def test_get_best_action_success1(self): # Tests successful get_best_action with a max depth of 2 (3 players) self.assertEqual(Strategy.get_best_action(self.__state2, 1), ((1, 0), (0, 0)))
def test_get_best_action_fail3(self): # Fails due to invalid depth with self.assertRaises(TypeError): Strategy.get_best_action(self.__state2, 0)
def test_get_best_action_fail2(self): # Fails due to invalid state (still placing) with self.assertRaises(GameNotRunningException): Strategy.get_best_action(self.__state1, 10)
def test_get_best_action_fail1(self): # Fails due to invalid state (type-wise) with self.assertRaises(TypeError): Strategy.get_best_action('', 10)