def beta_play(self, board_history): # work in progress current_board = board_history[0] opponent_point = MaybeStone.flip_stone(self.stone) all_opponent_connections = [] for point in Board.get_points(current_board, opponent_point): all_opponent_connections.append(get_blob(current_board, point)) all_opponent_liberties = [] for connection in all_opponent_connections: all_opponent_liberties.append( get_blob_liberties(current_board, connection)) connections_with_one_liberty = set() for liberties_set in all_opponent_liberties: if len(liberties_set) == 1: connections_with_one_liberty.union(liberties_set) for row, maybe_stones in enumerate(current_board): for column, maybe_stone in enumerate(current_board): if (column, row) in connections_with_one_liberty: try: RuleChecker.check_move(self.stone, (column, row), board_history) return Point.point_to_string((column, row)) except IllegalMoveError: pass except ImproperHistoryError as e: # raise PlayerCommandError('Invalid history') from e return 'This history makes no sense!' return 'pass'
def play(self, strategy, board_history): if self.strategy == "random": rand_1 = randint(0, MAX_ROW) rand_2 = randint(0, MAX_COLUMN) return Point.point_to_string((rand_1, rand_2)) elif self.strategy == "n1": return self.n1play(board_history) elif self.strategy == "dumb": return self.dumb_play(board_history) else: raise PlayerCommandError("Undefined Strategy")
def dumb_play(self, board_history): if self.stone == None: raise PlayerCommandError( 'You need to set the stone before you can play.') current_board = board_history[0] for column, maybe_stones in enumerate(current_board): for row, maybe_stone in enumerate(current_board): try: RuleChecker.check_move(self.stone, (column, row), board_history) return Point.point_to_string((column, row)) except IllegalMoveError: pass except ImproperHistoryError as e: # raise PlayerCommandError('Invalid history') from e return 'This history makes no sense!' return 'pass'
def run(self): game_over = False # while not game_over: outcome = 1 # Make referee, set stones of player while not game_over: for curr_player in self.player_holder: try: move = curr_player.n1play(self.ref.rr.board_hist) # Random move -- comment this out to use real ai move = Point.point_to_string((randrange(0, MAX_ROW), randrange(0, MAX_COLUMN))) # print(move) # If player disconnects, force ref to end game except (ConnectionError, player.PlayerCommandError) as e: move = "GO has gone crazy!" outcome = self.ref.execute_turn(move) # print(outcome) if isinstance(outcome, list) and isinstance(outcome[0], str): game_over = True break try: ok1 = self.player_holder[0].end_game() # print(ok1) # print("Gets past first") ok2 = self.player_holder[1].end_game() # print(ok2) except (ConnectionError, ConnectionRefusedError, ConnectionAbortedError, ConnectionResetError): pass # try: # # except ConnectionError: # pass return outcome
def n1play(self, board_history): if self.stone == None: raise PlayerCommandError( 'You need to set the stone before you can play.') current_board = copy.deepcopy(board_history[0]) opponent_point = MaybeStone.flip_stone(self.stone) for column_index in range(MAX_COLUMN): for row_index in range(MAX_ROW): if self.check_for_adjacent_stones(opponent_point, (column_index, row_index), current_board): try: # Try putting a stone in the current position new_board = RuleChecker.check_move( self.stone, (column_index, row_index), board_history) try: Board.place(current_board, self.stone, (column_index, row_index)) # See if capture occurred by comparing with just placing if current_board != new_board: return Point.point_to_string( (column_index, row_index)) else: Board.remove(current_board, self.stone, (column_index, row_index)) except BoardStateError as e: pass except IllegalMoveError as e: pass except ImproperHistoryError as e: # raise PlayerCommandError('Invalid history') from e return 'This history makes no sense!' return self.dumb_play(board_history)