def main():
    """
    Main client function.  All subsequent calls are made from this function 
    or from functions called by this function.
    
    Params: None
    
    Returns: This function never returns.  Either sys.exit is called directly
    by this function or by one of its subfunctions.
    """
    
    print "\n\nWelcome to the CS218 Spring 2015 Team #2 Chess Playing Program"
    print "\nTeam Members: David Smith, Geetika Garg, and Zayd Hammoudeh\n\n"
    print "Loading the board file...\n\n"
        
    # Error check the inputs.  Ensure a board file was specified.
    if(len(sys.argv) == 1):
        print "No board file was specified.  Exiting."
        sys.exit(0)
        
    # Error check the inputs.  Ensure a board file was specified.
    elif(len(sys.argv) > 2):
        print "Invalid input arguments.  Only a single board file can be specified. Exiting."
        sys.exit(0)
    # Error check the board file exists.
    elif(not os.path.isfile(sys.argv[1])):
        print "The specified board file: \"" + sys.argv[1] + "\" does not appear to exist.  Please check your system and try again."
        sys.exit(0)
    
    # Load the board.
    file_name = sys.argv[1]
    lines = open(file_name).readlines()
    current_player, board =  parse_board(lines)
    print "Board file successfully loaded.\n"
    print "The starting board is:"
    ChessUtils.print_board(board)
    print "\n\n"
    
    # Let the human select whether they are playing black or white.
    human_player = ""
    while(human_player != "0" and human_player != "1"):
        print "Select whether you are playing white or black."
        print "Enter \"0\" for white or \"1\" for black:  ",
        human_player = sys.stdin.readline().strip().replace(" ","") # Take trailing characters and new line off.
        
        if(human_player != "0" and human_player != "1"):
            print "\nInvalid input: \"" + human_player + "\""
            print "A valid input for player color is required before continuing.\n"

    # Convert the string version of player to the integer version. 
    human_player = int(human_player)
    if(human_player == PlayerType.WHITE):
        print "\nYou selected to play as white."
    else:
        print "\nYou selected to play as black."
    print "Starting the game...\n"
    
    # Run the game.
    play_game(current_player, human_player, board)
def make_player_move(check, valid_moves, board):
    """
    Handles the error checking and printing associated with a player
    making a move.
    
    Params:
    current_player : Integer - 0 for white and 1 for black.
    board - List of list of integers of the pieces on the board.
    
    Returns: Tuple of two integers.  Index 0 in the Tuple is the 
    current (i.e. source) location of the piece to be moved
    while index 1 is the new (i.e. destination) location of the piece.
    """
    
    # Keep looping until a valid move is entered.
    next_move = (-1, -1)
    while(next_move not in valid_moves):
        
        next_move = (-1, -1) # Reset next move to a dummy value
        
        if check:
            print "You are in check.  You must enter a move to exit check.\n"
        else:
            print "Its your turn.\n"
        print "Your move should be in the format of two integers separated by a comma."
        print "The first number is the current location of the piece you want to move,"
        print "and the second number is the new location of the piece after the move.\n"
        print "Enter your move: ",
        
        # Get the user text.
        move_text = sys.stdin.readline().strip()
        
        # Split move on comma.
        temp_move = move_text.replace(" ","").split(",")

        # Parse the move.
        if(len(temp_move) == 2):
            try:
                source_loc = int(temp_move[0])
                dest_loc = int(temp_move[1])
                next_move = (source_loc, dest_loc)
                valid_move_text = True
            except:
                # Unable to parse to integer
                valid_move_text = False
        else:
            valid_move_text = False 
        
        # Print messages if the moves are not valid
        if(not valid_move_text):
            print "\nIt does not appear the entered move \"" + move_text + "\" is in the correct format.  Please try again...\n\n"
            ChessUtils.print_board(board)
        # Verify the move is in the move array.
        elif(next_move not in valid_moves):
            print "\nThe specified is not allowed given the current board.  Check your move and try again.\n"
            ChessUtils.print_board(board)   

    # Return the move.
    return next_move 
def play_game(current_player, human_player, board):
    '''
    
    Params:
    current_player: Integer - 0 or 1 to indicate whether it is 
    white's or black's turn to start the game.
    
    human_player: Integer - 0 or 1 indicates whether the human player
    is white or black.  This value should not change.
    
    board: List of lists of integers.  It contains the current board.
    It will be updated at the end of each turn.
    
    Returns: 
    This function runs and never returns.  The program exits directly 
    from this function.
    ''' 
    while(True):
        
        check, valid_moves = ChessUtils.get_valid_moves(current_player, board)
        # Check if the game is over.
        if check and len(valid_moves) == 0:
            ChessUtils.print_board(board)
            print "\nCheckmate!"
            if current_player == human_player:
                print "The computer won!  You lose!"
            else:
                print "You win!"
            sys.exit(0)

        # Check if the game is over due to stalemate.
        elif(not check and len(valid_moves) == 0):
            print "\nStalemate!\n"
            print "No moves possible so its a draw!"
            sys.exit(0)
        
        # Print the current board
        print "\n\nCurrent Board:"
        ChessUtils.print_board(board)
        
        # Human's turn.
        if(current_player == human_player):
            # Keep loop until the user enters a valid move.
            next_move = make_player_move(check, valid_moves, board)

        # Computer's turn.
        else:
            if check:
                print "The computer is in check.  It must enter a move to exit check:\n"
            else:
                print "Its the computer's turn.  It is deciding on its move.\n"
            
            next_move = query_server(current_player, board)
            
            # Print the computer's move.
            print "The computer's move is:  (" + str(next_move[0]) + \
                                              ", " + str(next_move[1]) + ")\n"
        
        board = ChessUtils.make_move(next_move, board) # Apply the move.
        current_player = 1 - current_player # update the player
        
    pass