<gh_stars>1-10
import board, pieces, ai

# Returns a move object based on the users input. Does not check if the move is valid.
def get_user_move():
    #print("Example Move: A2 A4")
    move_str = input("Your Move: ")
    move_str = move_str.replace(" ", "")

    try:
        xfrom = letter_to_xpos(move_str[0:1])
        yfrom = 8 - int(move_str[1:2]) # The board is drawn "upside down", so flip the y coordinate.
        xto = letter_to_xpos(move_str[2:3])
        yto = 8 - int(move_str[3:4]) # The board is drawn "upside down", so flip the y coordinate.
        mv = ai.Move(xfrom, yfrom, xto, yto)
        return mv 
    except ValueError:
        print("Invalid format. Example: A2 A4")
        return get_user_move()

# Returns a valid move based on the users input.
def get_valid_user_move(board):
    while True:
        move = get_user_move()
        valid = False
        possible_moves = board.get_possible_moves(pieces.Piece.WHITE)
        #print(possible_moves)
        # No possible moves
        if (not possible_moves):
            return 0

        for possible_move in possible_moves:
            #print("pos: "+possible_move.to_string())
            if (move.equals(possible_move)):
                valid = True
                break
        if (valid):
            break
        else:
            print("Invalid move.")
    return move

# Converts a letter (A-D) to the x position on the chess board.
def letter_to_xpos(letter):
    letter = letter.upper()
    if letter == 'A':
        return 0
    if letter == 'B':
        return 1
    if letter == 'C':
        return 2
    if letter == 'D':
        return 3

    raise ValueError("Invalid letter.")

#
# Entry point.
#
board = board.Board.new()
print(board.to_string())

while True:
    move = get_valid_user_move(board)
    if (move == 0):
        if (board.is_check(pieces.Piece.WHITE)):
            print("Checkmate. Black Wins.")
            break
        else:
            print("Stalemate.")
            break

    board.perform_move(move)

    print("User move: " + move.to_string())
    print(board.to_string())

    ai_move = ai.AI.get_ai_move(board, [])
    if (ai_move == 0):
        if (board.is_check(pieces.Piece.BLACK)):
            print("Checkmate. White wins.")
            break
        else:
            print("Stalemate.")
            break

    board.perform_move(ai_move)
    print("AI move: " + ai_move.to_string())
    print(board.to_string())

import board, pieces, ai

# Returns a move object based on the users input. Does not check if the move is valid.
def get_user_move():
    #print("Example Move: A2 A4")
    move_str = input("Your Move: ")
    move_str = move_str.replace(" ", "")

    try:
        xfrom = letter_to_xpos(move_str[0:1])
        yfrom = 8 - int(move_str[1:2]) # The board is drawn "upside down", so flip the y coordinate.
        xto = letter_to_xpos(move_str[2:3])
        yto = 8 - int(move_str[3:4]) # The board is drawn "upside down", so flip the y coordinate.
        mv = ai.Move(xfrom, yfrom, xto, yto)
        return mv 
    except ValueError:
        print("Invalid format. Example: A2 A4")
        return get_user_move()

# Returns a valid move based on the users input.
def get_valid_user_move(board):
    while True:
        move = get_user_move()
        valid = False
        possible_moves = board.get_possible_moves(pieces.Piece.WHITE)
        #print(possible_moves)
        # No possible moves
        if (not possible_moves):
            return 0

        for possible_move in possible_moves:
            #print("pos: "+possible_move.to_string())
            if (move.equals(possible_move)):
                valid = True
                break
        if (valid):
            break
        else:
            print("Invalid move.")
    return move

# Converts a letter (A-D) to the x position on the chess board.
def letter_to_xpos(letter):
    letter = letter.upper()
    if letter == 'A':
        return 0
    if letter == 'B':
        return 1
    if letter == 'C':
        return 2
    if letter == 'D':
        return 3

    raise ValueError("Invalid letter.")

#
# Entry point.
#
board = board.Board.new()
print(board.to_string())

while True:
    move = get_valid_user_move(board)
    if (move == 0):
        if (board.is_check(pieces.Piece.WHITE)):
            print("Checkmate. Black Wins.")
            break
        else:
            print("Stalemate.")
            break

    board.perform_move(move)

    print("User move: " + move.to_string())
    print(board.to_string())

    ai_move = ai.AI.get_ai_move(board, [])
    if (ai_move == 0):
        if (board.is_check(pieces.Piece.BLACK)):
            print("Checkmate. White wins.")
            break
        else:
            print("Stalemate.")
            break

    board.perform_move(ai_move)
    print("AI move: " + ai_move.to_string())
    print(board.to_string())
# Returns a move object based on the users input. Does not check if the move is valid. #print("Example Move: A2 A4") # The board is drawn "upside down", so flip the y coordinate. # The board is drawn "upside down", so flip the y coordinate. # Returns a valid move based on the users input. #print(possible_moves) # No possible moves #print("pos: "+possible_move.to_string()) # Converts a letter (A-D) to the x position on the chess board. # # Entry point. #