Esempio n. 1
0
def minimax(board, turn):
    """
    Depth:
    depth = 1 Max --> return
    depth = 2 Max --> Mini --> return
    depth = 3 Max --> Mini --> Max --> return
    depth = 4 Max --> Mini --> Max --> Mini --> return
    """
    depth = 3
    
    # Set turn
    if turn == 1:
        color = 'white'
        best_score = float('-inf')
    else:
        color = 'black'
        best_score = float('inf')
    
    # Check for checkmate oppenents checkmate
    if board.checkmate(color):
        output_board_file = open('gameResult.txt', 'w')
        
        if color == 'black':
            print('STALEMATE! {} beats {}'.format(color, Board.op_color(color)), file = output_board_file)
            print(board, file = output_board_file)
            print('STALEMATE! {} beats {}'.format(color, Board.op_color(color)))
            print(board)
            sys.exit()
        
        print('CHECKMATE! {} beats {}'.format(color, Board.op_color(color)), file = output_board_file)
        print(board, file = output_board_file)
        print('CHECKMATE! {} beats {}'.format(color, Board.op_color(color)))
        print(board)
        sys.exit()
    
    # Check for checkmate oppenents checkmate
    if board.checkmate(Board.op_color(color)):
        output_board_file = open('gameResult.txt', 'w')

        if color == 'white':
            print('STALEMATE! {1} beats {0}'.format(color, Board.op_color(color)), file = output_board_file)
            print(board, file = output_board_file)
            print('STALEMATE! {1} beats {0}'.format(color, Board.op_color(color)))
            print(board)
            sys.exit()
            
        print('CHECKMATE! {1} beats {0}'.format(color, Board.op_color(color)), file = output_board_file)
        print(board, file = output_board_file)
        print('CHECKMATE! {1} beats {0}'.format(color, Board.op_color(color)))
        print(board)
        sys.exit()
    
    # Get possible legal moves
    moves = board.possible_moves(color)
    best_move = moves[0]
    
    # Initialize variables
    alpha = float('-inf')
    beta = float('inf')
    
    for move in moves:
        clone = deepcopy(board)
        clone.squares = deepcopy(board.squares)
        step = deepcopy(move)
        clone.move_piece(step[0], step[1])

        if color == 'white':
            # score = heuristicY(clone, depth - 1, turn)
            score = heuristicY(clone, depth - 1, turn, alpha, beta)
            if score > best_score:
                best_move = step
                best_score = score
                # print(best_score)
        else:
            # score = heuristicX(clone, depth - 1, turn)
            score = heuristicX(clone, depth - 1, turn, alpha, beta)
            if score < best_score:
                best_move = step
                best_score = score
                # print(best_score)

    return best_move