def minimax(curr_node, player): score_list = [] score = utils.calculate_score(curr_node.get_board(), player) if score in [math.inf, -math.inf]: curr_node.set_score(score) print(curr_node.get_board()) return score if curr_node.get_depth() == 2: print(curr_node.get_board()) curr_node.set_score(score) return score moves = utils.get_available_moves(curr_node.get_board()) if moves == []: print(curr_node.get_board()) curr_node.set_score(score) return score for move in moves: new_board = utils.perform_move(copy.deepcopy(curr_node.get_board()), move, player) new_node = node.Node(curr_node, new_board, curr_node.get_depth() + 1, move) curr_node.add_lower(new_node) if player == 1: move_score = minimax(new_node, 2) else: move_score = minimax(new_node, 1) score_list.append(move_score) if (player == 1): score = max(score_list) curr_node.set_score(score) else: score = min(score_list) curr_node.set_score(score) return score
def minimax_alphabeta(curr_node, player, alpha, beta): # end = datetime.datetime.now() # diff = end - start # print(diff) # print(curr_node.get_board()) global identifier global forbidden_moves score_list = [] identifier += 1 if(curr_node.get_depth() >= c.max_depth): # if we hit max depth, recursion is stopped score = utils.calc_differential_score_distinct_weights(curr_node.get_board(), player) curr_node.set_score(score) return score elif(curr_node.is_final()): # if current state is final, recursion is stopped score = utils.calc_differential_score_distinct_weights(curr_node.get_board(), player) curr_node.set_score(score) return score moves = utils.get_available_moves(curr_node.get_board()) # If there are forbidden moves, they must be removed from available moves list if(len(forbidden_moves) > 0): # print("REMOVE FORBIDDEN MOVES") # print(str(forbidden_moves)) # print() moves = utils.remove_forbidden_moves(moves, forbidden_moves) if(player == 1): value = -9999999999 for move in moves: new_board = utils.perform_move(copy.deepcopy(curr_node.get_board()), move, player) new_node = node.Node(curr_node, new_board, curr_node.get_depth() + 1, player, move) new_node.set_identifier(identifier) # print("New move " + str(move[0]) + ',' + str(move[1])) # print(new_node.get_board()) # print() # print curr_node.add_lower(new_node) move_score = minimax_alphabeta(new_node, 2, alpha, beta) value = max(move_score, value) alpha = max(alpha, value) if(alpha >= beta): break curr_node.set_score(value) return value else: value = 9999999999 for move in moves: new_board = utils.perform_move(copy.deepcopy(curr_node.get_board()), move, player) new_node = node.Node(curr_node, new_board, curr_node.get_depth() + 1, player, move) new_node.set_identifier(identifier) # print("New move " + str(move[0]) + ',' + str(move[1])) # print(new_node.get_board()) # print() # print curr_node.add_lower(new_node) move_score = minimax_alphabeta(new_node, 1, alpha, beta) value = min(move_score, value) beta = min(beta, value) if(alpha >= beta): break curr_node.set_score(value) return value
def alpha_beta_pruning(curr_node, player, abp_class, forbidden_moves): alpha = abp_class.get_alpha() beta = abp_class.get_beta() score = utils.calculate_score(curr_node.get_board(), player) if score in [math.inf, -math.inf]: curr_node.set_score(score) return score if curr_node.get_depth() == 2: curr_node.set_score(score) return score moves = utils.get_available_moves(curr_node.get_board()) for move in moves: if [move[0] + 1, move[1] + 1] in forbidden_moves: moves.remove(move) if moves == []: curr_node.set_score(score) return score if len(moves) in [80, 79]: if [5, 4] in moves: curr_node.set_score(score) curr_node.set_move([5, 4]) return score elif [5, 5] in moves: curr_node.set_score(score) curr_node.set_move([5, 5]) return score elif [4, 3] in moves: curr_node.set_score(score) curr_node.set_move([4, 3]) return score elif [6, 3] in moves: curr_node.set_score(score) curr_node.set_move([6, 3]) return score if player == 1: best_score = -math.inf for move in moves: new_board = utils.perform_move( copy.deepcopy(curr_node.get_board()), move, player) new_node = node.Node(curr_node, new_board, curr_node.get_depth() + 1, move) curr_node.add_lower(new_node) move_score = alpha_beta_pruning(new_node, 2, abp_class, forbidden_moves) best_score = max(best_score, move_score) abp_class.set_alpha(max(alpha, best_score)) if abp_class.get_alpha() >= beta: break curr_node.set_score(best_score) return best_score else: best_score = math.inf for move in moves: new_board = utils.perform_move( copy.deepcopy(curr_node.get_board()), move, player) new_node = node.Node(curr_node, new_board, curr_node.get_depth() + 1, move) curr_node.add_lower(new_node) move_score = alpha_beta_pruning(new_node, 1, abp_class, forbidden_moves) best_score = min(best_score, move_score) abp_class.set_beta(min(beta, best_score)) if alpha >= abp_class.get_beta(): break curr_node.set_score(best_score) return best_score
def available_moves(self): return get_available_moves(self.board, self.position, can_use_enemy=True, maximum_steps=2)
def available_moves(self): return get_available_moves(self.board, self.position, can_use_body=True)
def available_moves(self): return get_available_moves(self.board, self.position)
def minimax_alphabeta(curr_node, player, ab): global identifier global forbidden_moves score_list = [] identifier += 1 alpha = ab[0] beta = ab[1] if (curr_node.get_depth() >= c.max_depth): # if we hit max depth, recursion is stopped score = utils.calc_differential_score(curr_node.get_board(), player) curr_node.set_score(score) return score elif (curr_node.is_final() ): # if current state is final, recursion is stopped score = utils.calc_differential_score(curr_node.get_board(), player) curr_node.set_score(score) return score moves = utils.get_available_moves(curr_node.get_board()) # If there are forbidden moves, they must be removed from available moves list if (len(forbidden_moves) > 0): moves = utils.remove_forbidden_moves(moves, forbidden_moves) if (player == 1): value = -9999999999 for move in moves: new_board = utils.perform_move( copy.deepcopy(curr_node.get_board()), move, player) new_node = node.Node(curr_node, new_board, curr_node.get_depth() + 1, player, move) new_node.set_identifier(identifier) curr_node.add_lower(new_node) move_score = minimax_alphabeta(new_node, 2, ab) value = max(move_score, value) ab[0] = max(alpha, value) if (ab[0] >= beta): break curr_node.set_score(value) return value else: value = 9999999999 for move in moves: new_board = utils.perform_move( copy.deepcopy(curr_node.get_board()), move, player) new_node = node.Node(curr_node, new_board, curr_node.get_depth() + 1, player, move) new_node.set_identifier(identifier) curr_node.add_lower(new_node) move_score = minimax_alphabeta(new_node, 1, ab) value = min(move_score, value) ab[1] = min(beta, value) if (alpha >= ab[1]): break curr_node.set_score(value) return value