def introduction(connection: 'connection', input_str: str): # This is used to send client introduction to server # Use username() function s_output = connection[2] s_input = connection[1] user = username() newGameCall = (input_str + ' ' + user) send_to_server(s_output, newGameCall) print('Client: ' + newGameCall) serverReply = receive_from_server(s_input) print('Server: ' + (serverReply)) expectedReply = 'WELCOME ' + user.strip() if serverReply == expectedReply: newGame = sendGameRequest(connection) #print('Server: ' +serverReply) #userInput(newGame, connection) while Console.winner_is_chosen(newGame) == connectfour.NONE: userCommand = [] if newGame.turn == 1: clientInput = '' clientInput = input('Client: ') userCommand = (clientInput).split() if 0 < int(userCommand[1]) <= len(newGame[0]): if userCommand[0].lower() == 'drop': newGame = Console.drop_piece(newGame, int(userCommand[1])-1) Console.print_board(newGame) send_to_server(s_output, userCommand[0].upper()+' ' +str(userCommand[1])) print(receive_from_server(s_input)) elif userCommand[0].lower() == 'pop': newGame = Console.pop_piece(newGame, int(userCommand[1])-1) Console.print_board(newGame) send_to_server(s_output, userCommand[0].upper()+' ' +str(userCommand[1])) print(receive_from_server(s_input)) else: clientInput = receive_from_server(s_input) print(clientInput) elif newGame.turn == 2: serverInput = receive_from_server(s_input) serverInput = serverInput.split('\r\n') serverInput = serverInput[0].split() for i in serverInput: print (i, end = ' ') print() if serverInput[0].lower() == 'drop': newGame = Console.drop_piece(newGame, int(serverInput[1])-1) Console.print_board(newGame) #send_to_server(s_output, serverInput[0].upper()+' ' +str(serverInput[1])) print(receive_from_server(s_input)) elif serverInput[0].lower() == 'pop': newGame = Console.pop_piece(newGame, int(serverInput[1])-1) Console.print_board(newGame) #send_to_server(s_output, serverInput[0].upper()+' ' +str(serverInput[1])) print(receive_from_server(s_input)) elif serverInput[0].lower() == 'invalid': print() #print(Input[:len(Input)]) else: print('UserName: ERROR')
def userInterface(connection: 'connection', input_str: str): ''' Introduces the user to the server and plays the game ''' s_output = connection[2] s_input = connection[1] user = username() newGameCall = (input_str + ' ' + user) send_to_server(s_output, newGameCall) print('Client: ' + newGameCall) serverReply = receive_from_server(s_input) print('Server: ' + (serverReply)) expectedReply = 'WELCOME ' + user.strip() if serverReply == expectedReply: newGame = sendGameRequest(connection) winner = Console.winner_is_chosen(newGame) while winner == 0: try: userCommand = [] if newGame.turn == 1: clientInput = '' clientInput = input('Client: ') userCommand = (clientInput).split() if len(userCommand) == 2: if 0 < int(userCommand[1]) <= len(newGame[0]): if userCommand[0].lower() == 'drop': newGame = Console.drop_piece(newGame, int(userCommand[1])-1) Console.print_board(newGame) send_to_server(s_output, userCommand[0].upper()+' ' +str(userCommand[1])) print('Server: ', end = '') print(receive_from_server(s_input)) winner = Console.winner_is_chosen(newGame) if winner != 0: print('Game is over.') break elif userCommand[0].lower() == 'pop': try: newGame = Console.pop_piece(newGame, int(userCommand[1])-1) Console.print_board(newGame) send_to_server(s_output, userCommand[0].upper()+' ' +str(userCommand[1])) print('Server: ', end = '') print(receive_from_server(s_input)) winner = Console.winner_is_chosen(newGame) if winner != 0: print('Game is over.') break except: print('That\'s not a valid move.') winner = 0 else: print('The column number you specified is not correct.') winner = 0 else: print('The col number you specified is greater.') winner = 0 else: print('The move is invalid. Try again') winner = 0 elif newGame.turn == 2: serverInput = receive_from_server(s_input) serverInput = serverInput.split('\r\n') serverInput = serverInput[0].split() output = str() for i in serverInput: output += i + ' ' print('Server: ' + output) if serverInput[0].lower() == 'drop': newGame = Console.drop_piece(newGame, int(serverInput[1])-1) print('Server: ' + receive_from_server(s_input)) Console.print_board(newGame) winner = Console.winner_is_chosen(newGame) elif serverInput[0].lower() == 'pop': newGame = Console.pop_piece(newGame, int(serverInput[1])-1) print('Server: ' + receive_from_server(s_input)) Console.print_board(newGame) winner = Console.winner_is_chosen(newGame) elif serverInput[0].lower() == 'invalid': print() else: print('UserName: ERROR') except ValueError: print('The number you attempt to put is not correct. Try again.') winner = 0 except TypeError: print('The number you tried to input is not correct format.') winner = 0