def get_all_moves(self): moves = [] for i in range(0, self.size): for j in range(0, self.size): point = Point(i, j) move = Move(point) moves.append(move) pass_move = Move() moves.append(pass_move) return moves
def get_legal_moves(self): moves = [] for i in range(0, self.board.board_size): for j in range(0, self.board.board_size): point = Point(i, j) move = Move(self.board, point) if move.is_valid_move(): moves.append(move) pass_move = Move(self.board, None) moves.append(pass_move) return moves
def __init__(self, n, path="input.txt"): self.size = n with open(path, 'r') as f: lines = f.readlines() self.our_player = int(lines[0]) self.other_player = 3 - self.our_player self.cur_step_num = None # self.good_moves = [Move(Point(1, 1)), # Move(Point(1, 2)), # Move(Point(1, 3)), # Move(Point(2, 1)), # Move(Point(2, 3)), # Move(Point(3, 1)), # Move(Point(3, 2)), # Move(Point(3, 3))] self.good_moves = [ Move(Point(1, 1)), Move(Point(1, 3)), Move(Point(3, 1)), Move(Point(3, 3)) ] previous_board_arr = [] current_board_arr = [] black_stones = 0 white_stones = 0 for line in lines[1:self.size + 1]: row = [int(x) for x in line.rstrip('\n')] previous_board_arr.append(row) for line in lines[self.size + 1:2 * self.size + 1]: row = [] for x in line.rstrip('\n'): row.append(int(x)) if int(x) == BLACK: black_stones += 1 elif int(x) == WHITE: white_stones += 1 current_board_arr.append(row) previous_board = Board(self.size, previous_board_arr) current_board = Board(self.size, current_board_arr) self.board = current_board self.previous_state = State(previous_board, None, self.other_player, None) self.active_state = State(current_board, self.previous_state, self.our_player, None) self.set_step_num(black_stones, white_stones)
def select_move(self): go = Go(self.size) self.all_moves = go.get_all_moves() self.our_player = go.our_player moves = deepcopy(self.all_moves) shuffle(moves) best_moves_list = [] best_recorded_score = MIN_SCORE if self.depth < 1: for move in moves: new_state = go.active_state.apply_move(move) if new_state is not None: if not new_state.board.fills_own_eye(move): best_moves_list.append(move) else: for move in moves: new_state = go.active_state.apply_move(move) if new_state is not None: if not new_state.board.fills_own_eye(move): cur_move_score = self.min_value(new_state, self.depth-1) if cur_move_score == best_recorded_score: best_moves_list.append(move) if cur_move_score > best_recorded_score: best_moves_list = [move] best_recorded_score = cur_move_score if not best_moves_list: best_moves_list.append(Move()) return random.choice(best_moves_list)
def __init__(self, size): self.size = size self.type = 'AlphaBeta' self.all_moves = None self.our_player = None self.depth = None self.step_num = None self.remaining_steps = None self.center_moves_set = (Move(Point(1, 1)), Move(Point(1, 2)), Move(Point(1, 3)), Move(Point(2, 1)), Move(Point(2, 2)), Move(Point(2, 3)), Move(Point(3, 1)), Move(Point(3, 2)), Move(Point(3, 3)))
def select_move(self): go = Go(self.size) self.all_moves = go.get_all_moves() self.our_player = go.our_player self.step_num = go.cur_step_num if self.step_num < 5: move = go.valid_good_move() if isinstance(move, Move): return move elif self.step_num < 9: self.depth = 2 elif self.step_num < 15: self.depth = 3 else: self.depth = 4 self.remaining_steps = 24 - self.step_num moves = deepcopy(self.all_moves) shuffle(moves) best_moves_set = set() alpha = MIN_SCORE beta = MAX_SCORE best_recorded_score = MIN_SCORE for move in moves: new_state = go.active_state.apply_move(move) if new_state is not None: cur_move_score = self.min_value(new_state, 0, alpha, beta) if cur_move_score == best_recorded_score: best_moves_set.add(move) if cur_move_score > best_recorded_score: best_moves_set = set() best_moves_set.add(move) best_recorded_score = cur_move_score priority_moves_set = best_moves_set.intersection(self.center_moves_set) if priority_moves_set: return random.choice(list(priority_moves_set)) elif best_moves_set: return random.choice(list(best_moves_set)) else: return Move()
def select_move(self): go = Go(self.size) self.all_moves = go.get_all_moves() self.our_player = go.our_player self.step_num = go.cur_step_num self.depth = min(self.depth, 24 - self.step_num) if self.step_num < 7: return go.valid_good_move() moves = deepcopy(self.all_moves) shuffle(moves) best_moves_list = [] alpha = MIN_SCORE beta = MAX_SCORE best_recorded_score = MIN_SCORE if self.depth < 0: for move in moves: new_state = go.active_state.apply_move(move) if new_state is not None: if not new_state.board.fills_own_eye(move): best_moves_list.append(move) else: for move in moves: new_state = go.active_state.apply_move(move) if new_state is not None: if not new_state.board.fills_own_eye(move): cur_move_score = self.min_value( new_state, self.depth, alpha, beta) if cur_move_score == best_recorded_score: best_moves_list.append(move) if cur_move_score > best_recorded_score: best_moves_list = [move] best_recorded_score = cur_move_score if not best_moves_list: best_moves_list.append(Move()) return random.choice(best_moves_list)