def _run_user_interface() -> None: ''' Runs the console-mode user interface from start to finish. Depending on if there is a winner or not, the end state game board will or will not be printed ''' winner = ' ' host=str((input("Enter Host: "))) port=int((input("Enter Port: "))) try: connection = connectfourserver.connect(host,port) username = _ask_for_username() connectfourserver.hello(connection, username) connectfourfunctions.print_board_game(game_state.board) while _handle_command(connection): _handle_ai_command(connection) if connectfourserver._expect_line(connection, 'READY') == False: break connectfourfunctions.print_board_game(game_state.board) except: print("Error when Connecting") connection=None finally: winner=connectfour.winning_player(game_state) if (winner != ' '): connectfourfunctions.print_board_game(game_state.board) if(connection!=None): connectfourserver.close(connection)
def _handle_command(connection: connectfourserver.ConnectFourConnection) -> bool: ''' Handles a single command from the user, by asking the user what command they'd like to execute and then handling it. Returns True if the move is valid and False if the move is invalid or a Winner is declared ''' global game_state while True: try: player = input("Please Enter (Drop and a col number) or (Pop and a col number): ") command,col=player.split() col = int(col) connectfourserver.send(connection,command.upper() + ' ' + str(col)) game_state = connectfourfunctions.give_turn(game_state,command,col) except: print("Invalid Move, Please Go Again") connectfourserver._expect_line(connection,'INVALID') _handle_command(connection) if connectfourserver._expect_line(connection, 'OKAY') == False: return False return True