def gameplay():
    '''
    This function processes the entire game with the AI as well as the setting
    up of the game.
    '''
    try:
        connection_and_username = begin_game()
        connection = connection_and_username[0]
        username = connection_and_username[1]
    except InvalidServerError:
        return
    
    ai_gamestate = connectfour.new_game()
    while(connectfour.winner(ai_gamestate) == connectfour.NONE):
        try:
            ai_gamestate = _user_turn(ai_gamestate, connection, username)
            print()
        except connectfour_protocol.ConnectFourProtocolError:
            print('Invalid Input. Please Try Again.')
        except connectfour_protocol.InvalidServerMoveError:
            print('Invalid Input. Please Try Again.')
        except InvalidServerError:
            return

    connectfour_shared.print_board(ai_gamestate)
    print()
    
    if(connectfour.winner(ai_gamestate) == connectfour.RED):
        print('Congratulations! You have won the game!')
    else:
        print('Sorry. The AI has won the game.')

    connectfour_protocol.disconnect(connection)
def console():
    '''
    The methods in the appropriate order to play a console game of connect four.
    Asks users to make moves until someone gets four of their pieces in a row.
    '''
    game_state = connectfour_shared.initiate_game()#initiates game.
    gameover = False#Boolean to determine whether game is over.
    while (gameover == False): 
        game_state = make_move(game_state) #Makes the move of the player
        connectfour_shared.print_board(game_state) #Prints the board
        gameover = check_win(game_state) #Checks if there is a winner
def _full_game(state: connectfour.GameState) -> None:
    '''
    This function controls the flow of the game. It allows for each player to
    continue to play until there is a winner, in which the function prints the
    winning board before announcing the winner and end the function.
    '''
    while (connectfour.winner(state) == connectfour.NONE):
        state = connectfour_shared._player_turn(state, False)[0]
        print()

    connectfour_shared.print_board(state)
    print()
    print('Player ' + connectfour_shared.connectfour_players[state.turn] +
          ' has won the game!')
Exemplo n.º 4
0
def userinterface() -> None:
    '''
    This method runs the connection between this program and the server program
    to play a game of connect four, using functions from this and other classes imported
    from above.
    '''
    try:
        _welcome_screen()#Welcomes the user.
        connection = '' #Used here to avoid error when user doesn't enter a host and port
        connection = _connect_ui() #connects the program to the server
        username = _request_username() #Gets a username for the user

        connectfour_protocol.interact(connection, username, 'I32CFSP_HELLO ', 'WELCOME ' + username) #Sends the initial client message specifying protocol
        connectfour_protocol.interact(connection, 'AI_GAME', '', 'READY') #Sends the client protocol and gets the server protocol to start the AI
        
        game_state = connectfour_shared.initiate_game() #Starts new game and prints the board
        
        while True:
            try:
                move = connectfour_shared.request_move() #Gets users move
                game_state = connectfour_shared.update_board(game_state, move)#updates the board
                connectfour_shared.print_board(game_state)#prints the new game_state

                response = connectfour_protocol.interact(connection, move, '', 'READY')#Response from the server

                if response == None:#Gets out of loop if server doesn't respond
                    break
                
                game_state = connectfour_shared.update_board(game_state, response)#Updates the board with server's move
                connectfour_shared.print_board(game_state)#Prints the board.
                
            except:
                print('Invalid move, please try again')#Exception when user enters invalid move
                
    except ConnectionRefusedError:#If socket fais to connect
        print('Connection failed')
    except OSError:#If invalid IP adress
        print('Not a valid IP Address')
    except:#In other cases of errors
        print("Sorry we weren't able to connect to that server")
    finally:#Closes the connectino if one was made.
        if(connection!=''):
            connectfour_protocol.close(connection)
        print('Game over')