def connect_four_consoleUI(): ''' This is the main user interface where the program is executed and the Connect Four game runs with all its respective functions ''' WelcomeBanner() # prints welcome banner print() PlayerRed = InputRed() # creates player 1 - red PlayerYellow = InputYellow() # creates player 2 - yellow print() GameState = connectfour.new_game() # starts a new game and prints an empty board Project2commonUI.printBoard(GameState.board) while True: PlayerTurn(GameState, PlayerRed, PlayerYellow) # function call to PlayerTurn() which determines whose turn it is message = input() message = message.upper() # prompts for input to drop/pop a piece try: if len(message) == 0: # if there is nothing, this error is printed. print('\nSorry invalid input. Try again. :p\n') else: Responses = message.split() # otherwise the move is placed on the board and printed through InputMove() GameState = InputMove(Responses, GameState) except connectfour.InvalidMoveError: print('Invalid move! Try again. :p\n[ You can only POP your own piece or DROP a piece in an empty column! ]') # error for invalid move except ValueError: print('Invalid column number! Try again. :p\n[ Number must be 1, 2, 3, 4, 5, 6, or 7! ]\n') # error for value error - wrong col num. finally: Finale = WinningBanner(GameState, PlayerRed, PlayerYellow) # prints the banner and returns the exit string if Finale == 'You may exit the program!': # if exit string is correct then the program breaks and ends. break
def connect_four_consoleUI(): ''' This is the main user interface where the program is executed and the Connect Four game runs with all its respective functions ''' WelcomeBanner() # prints welcome banner print() PlayerRed = InputRed() # creates player 1 - red PlayerYellow = InputYellow() # creates player 2 - yellow print() GameState = connectfour.new_game( ) # starts a new game and prints an empty board Project2commonUI.printBoard(GameState.board) while True: PlayerTurn( GameState, PlayerRed, PlayerYellow ) # function call to PlayerTurn() which determines whose turn it is message = input() message = message.upper() # prompts for input to drop/pop a piece try: if len(message ) == 0: # if there is nothing, this error is printed. print('\nSorry invalid input. Try again. :p\n') else: Responses = message.split( ) # otherwise the move is placed on the board and printed through InputMove() GameState = InputMove(Responses, GameState) except connectfour.InvalidMoveError: print( 'Invalid move! Try again. :p\n[ You can only POP your own piece or DROP a piece in an empty column! ]' ) # error for invalid move except ValueError: print( 'Invalid column number! Try again. :p\n[ Number must be 1, 2, 3, 4, 5, 6, or 7! ]\n' ) # error for value error - wrong col num. finally: Finale = WinningBanner( GameState, PlayerRed, PlayerYellow) # prints the banner and returns the exit string if Finale == 'You may exit the program!': # if exit string is correct then the program breaks and ends. break
def ClientInputMove(L: list, GameState) -> None: ''' Given a list of the input and the current GameState this function moves a Client's piece and updates the GameState of Connect Four ''' if L[0] == 'DROP' or L[0] == 'POP': # The following is an 'if' statement that takes a Client input and calls user_move to do the print() # appropriate action to make a move and returns the updated GameState regardless of the input GameState = Project2commonUI.user_move(L, GameState) Board = Project2commonUI.printBoard(GameState.board) return GameState print() else: return GameState
def InputMove(L: list, GameState) -> None: ''' Given a list of the input and the current GameState this function moves a piece and updates the GameState of Connect Four ''' if len(L) == 2: # the following drops a piece with the function call user_move() if L[0] == 'DROP' or L[0] == 'POP': # it is only done if 'DROP' or 'POP' are in the input print() GameState = Project2commonUI.user_move(L, GameState) Board = Project2commonUI.printBoard(GameState.board) # Once drops it prints the board with the new piece return GameState print() else: print('\nSorry invalid input. Try again. :p\n') # regardless of the outcomem GameState is always returned return GameState else: print('\nSorry invalid input. Try again. :p\n') # error message is printed if the input is wrong return GameState
def InputMove(L: list, GameState) -> None: ''' Given a list of the input and the current GameState this function moves a piece and updates the GameState of Connect Four ''' if len( L ) == 2: # the following drops a piece with the function call user_move() if L[0] == 'DROP' or L[ 0] == 'POP': # it is only done if 'DROP' or 'POP' are in the input print() GameState = Project2commonUI.user_move(L, GameState) Board = Project2commonUI.printBoard( GameState.board ) # Once drops it prints the board with the new piece return GameState print() else: print('\nSorry invalid input. Try again. :p\n' ) # regardless of the outcomem GameState is always returned return GameState else: print('\nSorry invalid input. Try again. :p\n' ) # error message is printed if the input is wrong return GameState
def connect_four_networkUI() -> None: ''' Acts as a user interface which runs the entire Connect Four game and its respective functions ''' WelcomeBanner() # prints welcome banner! host = read_host() # prompts for host port = read_port() # promps for port print('\nConnecting to {} (port {})...'.format(host, port)) # prints that its attempting to connect to the server time.sleep(2) connection = Project2sockethandling.connect(host, port) # function used to connect to server with given host & port print('Connected!') # prints 'Connected' if successful time.sleep(1) name = InitiateGame(connection) # calls to InitiateGame() which prompts user to enter AI_GAME to initiate system GameState = connectfour.new_game() Project2commonUI.printBoard(GameState.board) # Once done, this initializes new game and prints the empty board while True: while True: if PlayerTurn(GameState, name) == True: # Calls to function PlayerTurn() which checks if it is User's Turn message = read_message() # If so, the following prompts the user to drop or pop a piece message = message.upper() response = message.split() if len(response)== 2: # if the response given is a list of 2 elements, the following executes... if response[0] == 'DROP' or response[0] == 'POP': # if drop or pop is in the input then the client sends a message to the server of the placement if response[1] in response: if response[1] in ['1','2','3','4','5','6','7']: Project2sockethandling.send_message(connection, message) else: print('\nInvalid column number; Try again.\nNumber must be 1, 2, 3, 4, 5, 6, or 7!\n') # if its invalid its prints this message break # breaks out of this while loop else: print('\nSorry invalid input; try again\n') # if its empty its prints this error message break # breaks out of this while loop if GameState.turn == 2: # if it is the computer's turn the function call ServerTurn() is made to print that the server is going to make a move response = ServerTurn(connection) try: GameState = ClientInputMove(response, GameState) # This function ClientInputMove() updates the GameState with the server's move response = ServerResponse(response, connection) # This function ServerResponse() prints the server's response after the move is made if type(response) == list: # If type of response is list, this executes... if response[0] == 'DROP' or response[0] == 'POP': # The following executes if 'DROP' or 'POP' is in input GameState = Project2commonUI.user_move(response, GameState) # Updates the server with the new move answer = Project2sockethandling.receive_response(connection) response = answer.split() time.sleep(1) ServerReady(response) # Prints that the server is ready after move is placed time.sleep(1) Project2commonUI.printBoard(GameState.board) # prints the board with the server's move placed print() ### for invalid move except connectfour.InvalidMoveError: answer = Project2sockethandling.receive_response(connection) # Once the response is recieved, the function call InvalidMovePrint() is made... response = answer.split() Result = InvalidMovePrint(response, connection) # ... if the move made previously brings up this type of exception if type(Result) == list: ServerReady(Result) # it then prints that the server is ready for the next action except ValueError: ### for column numbers answer = Project2sockethandling.receive_response(connection) # Once the response is recieved, the function call InvalidValuePrint() is made... response = answer.split() Result = InvalidValuePrint(response, connection) # ... if the move made previously brings up this type of exception if type(Result) == list: ServerReady(Result) # it then prints that the server is ready for the next action finally: if connectfour.winner(GameState) == 1: answer = Project2sockethandling.receive_response(connection) # Once everything is done, a winner is determined... response = answer.split() if response[0] == 'WINNER_RED': # if the winner is red (the user) it prints out the banner that they won! time.sleep(1.5) print('\n*****************************************\n') print('** Player RED -- {} has won! :D **\n'.format(name)) print('*****************************************\n') time.sleep(2) return False # return false breaks out of the while loops and closes the connection and program if connectfour.winner(GameState) == 2: print() if response[0] == 'WINNER_YELLOW': time.sleep(1.5) print('\n*************************************\n') print('** Player YELLOW -- AI has won! :( **\n') # if the winner is yellow (the server) it prints out the banner that they won! print('*************************************\n') time.sleep(2) return False # return false breaks out of the while loops and closes the connection and program pass