def closing_display(connection: 'ConnectFourConnection') -> None: "Shows closing message after game ends." print() print("Thanks for playing") print("This program will close after 10 seconds") print() time.sleep(10) connectfour_socket.close(connection) return
def checkServerReady(connection: 'connection'): response = cfs.receive_response(connection) if (response.startswith('WINNER_')): #Should only receive WINNER_YELLOW cfs.close(connection) #Connection Cut: Game Over return 'GameOver' elif (response != 'READY'): #Bad Response cfs.close(connection) #Connection Cut return 'BadConnection' else: return 'Continue'
def checkServerReady(connection: 'connection'): response = cfs.receive_response(connection) if (response.startswith('WINNER_')): ## AI_Winner = response[6::]#Not Used cfs.close(connection) ##Connection Cut: Game Over return 'GameOver' elif (response != 'READY'): print('Server Response: ' + response + '\nConnection Cut') cfs.close(connection) ##Connection Cut: Bad Response return 'BadConnection' else: return 'Continue'
def user_inter() -> None: ''' Connecting to a server and ask for game. If it's True, the program will start the game. It will send user's command to the server and receive server's command back. The all command will record and print current gameboard. The program will run until there is a winner come out. Whatever if there a exception or not, the connection will close finally ''' Connection,username = run_connect() try: if ask_game(Connection,username): GameBoard = connectfour.new_game() connectfour_gameboard.print_broad(GameBoard) while connectfour_gameboard.check_game_over(GameBoard): command,col_num = send_command(Connection) response1 = response(Connection) if response1 == 'OKAY': response2 = response(Connection) response3 = response(Connection) if response3 == 'READY' and response2 != None: print('Play Yellow: '+ response2) GameBoard = client_step(command,col_num,GameBoard) GameBoard = server_step(response2,GameBoard) connectfour_gameboard.print_broad(GameBoard) elif response3.startswith('WINNER') and response2 != None: GameBoard = client_step(command,col_num,GameBoard) GameBoard = server_step(response2,GameBoard) connectfour_gameboard.print_broad(GameBoard) else: break elif response1 == 'INVAILD': response2 = response(Connection) print('Invalid') pass elif response1.startswith('WINNER'): break else: break finally: print('Closing connection now') connectfour_socket.close(Connection) print('Finishing close')
def _user_interface() -> None: '''Run the main user interface''' print('Welcome to Connect Four!') try: connection = _connect_to_server() connectfour_socket.hello(connection, _get_username()) connectfour_socket.request_ai_game(connection) print('You will be RED and the computer will be YELLOW...') game_state = connectfour.new_game() while True: connectfour_shared_ui.print_board(game_state) user_move = connectfour_shared_ui.get_move(game_state) connectfour_socket.send_move(connection, user_move.move) game_state = user_move.gamestate response = connectfour_socket.handle_responses(connection) winner = response[0] move = response[1] if winner == 'WINNER_RED' and move == '': connectfour_shared_ui.print_board(game_state) print('RED is the winner!') break elif winner == 'WINNER_YELLOW' and move == '': connectfour_shared_ui.print_board(game_state) print('YELLOW is the winner!') break elif winner == '' and move != '': game_state = _execute_server_move(game_state, move) print('YELLOW chose to {}.'.format(move)) elif winner != '' and move != '': game_state = _execute_server_move(game_state, move) print('YELLOW chose to {}.'.format(move)) connectfour_shared_ui.print_board(game_state) if winner == 'WINNER_YELLOW': print('YELLOW is the winner!') break elif winner == 'WINNER_RED': print('RED is the winner!') break except connectfour_socket.ProtocolError: print('There was an issue with the server. Goodbye!') finally: connectfour_socket.close(connection)
def AITurn(connection: 'connection', currentBoard: cf.GameState) -> cf.GameState: while True: cff.printInfo(currentBoard) response = cfs.receive_response(connection) print(response) if response.upper().startswith("POP "): newBoard, action, col = cff.processTurn(currentBoard, "P", int(response[3::]) - 1) return newBoard elif response.upper().startswith("DROP "): newBoard, action, col = cff.processTurn(currentBoard, "D", int(response[4::]) - 1) return newBoard else: print('Response:' + response + '\nCut') cfs.close(connection) ##CUT
def AITurn(connection: 'connection', currentBoard: cf.GameState) -> cf.GameState: while True: cff.printInfo(currentBoard) response = cfs.receive_response(connection) #Listen for Server print(response) #Print Server Move #Run/Process Server Input if response.upper().startswith("POP "): newBoard, action, col = cff.processTurn(currentBoard, "P", int(response[3::]) - 1) return newBoard elif response.upper().startswith("DROP "): newBoard, action, col = cff.processTurn(currentBoard, "D", int(response[4::]) - 1) return newBoard else: #Bad Response cfs.close(connection) #Cut Connection
def playerTurn(connection: 'connection', currentBoard: cf.GameState) -> cf.GameState: while True: cff.printInfo(currentBoard) while True: newBoard, action, col = cff.userInput(currentBoard) if (action == "INVALID_INPUT"): ## print("Please Enter Valid Input") cff.printBoard(currentBoard) continue elif (action == "NO_COLUMN"): ## print("Please Enter Valid Column") cff.printBoard(currentBoard) continue elif (action == "INVALID_COLUMN"): ## print("Please Enter Valid Column") cff.printBoard(currentBoard) continue elif (action == "INVALID_MOVE"): ## print("PLease Enter Valid Move") cff.printBoard(currentBoard) continue else: break response = cfs.send_receive(connection, action + str(col + 1)) if (response.startswith('WINNER_') ): #Since player turn, received WINNER_RED ## AI_Winner = response[6::]#Not Used ## print('GameOverPlayer') ## cfs.close(connection)#Cut, Game Over return newBoard elif (response == 'ERROR'): print("Invalid Input") continue elif (response != 'OKAY'): print('Response1:' + response + '\nCut') cfs.close(connection) ##CUT return newBoard
def playerTurn(connection: 'connection', currentBoard: cf.GameState) -> cf.GameState: while True: cff.printInfo(currentBoard) while True: newBoard, action, col = cff.userInput( currentBoard) #Run/Process User Input #Check For Errors if (action == "INVALID_INPUT"): cff.printBoard(currentBoard) continue elif (action == "NO_COLUMN"): cff.printBoard(currentBoard) continue elif (action == "INVALID_COLUMN"): cff.printBoard(currentBoard) continue elif (action == "INVALID_MOVE"): cff.printBoard(currentBoard) continue else: break response = cfs.send_receive(connection, action + str(col + 1)) #Send Move to Server if (response.startswith('WINNER_') ): #Since player turn, received WINNER_RED return newBoard elif (response == 'ERROR'): #Should Never Run print("Invalid Input") continue elif (response == "INVALID"): #Should Never Run print("Invalid Input") elif (response != 'OKAY'): #Bad Response cfs.close(connection) #Cut Connection return newBoard
elif (response != 'READY'): print('Server Response: ' + response + '\nConnection Cut') cfs.close(connection) ##Connection Cut: Bad Response return 'BadConnection' else: return 'Continue' connection = cfs.connect(host, port) #Connect to Server response = cfs.send_receive(connection, 'I32CFSP_HELLO ' + username) if (response != 'WELCOME ' + username): print('Response:' + response + '\nCut') cfs.close(connection) ##CUT #Reqeust AI Game response = cfs.send_receive(connection, 'AI_GAME') if (response != 'READY'): print('Response:' + response + '\nCut') cfs.close(connection) ##CUT currentBoard = cf.new_game() #Begin Turns while True: try: if cf.winner(currentBoard) != cf.NONE: print(cff.colorPrint(cf.winner(currentBoard))) ## break
continue connection = cfs.connect(host, port) #Caught by cfs.socket.gaierror if unable to connect #Send/Receive Username while True: username = input('Input Username: '******' ' in username) != True): #Repeat until username has no whitespaces break else: print("Username cannot contain whitespace") response = cfs.send_receive(connection, 'I32CFSP_HELLO ' + username) if (response != 'WELCOME ' + username): #Bad Response cfs.close(connection) #Cut Connection #Reqeust AI Game response = cfs.send_receive(connection, 'AI_GAME') if (response != 'READY'): #Bad Response cfs.close(connection) #Cut Connection #Create Blank Game currentBoard = cf.new_game() #Begin Turns while True: try: #Test for Winner following AI turn(Should Not Run) if cf.winner(currentBoard) != cf.NONE: cff.printBoard(currentBoard)
def run_user_interface() -> None: connection = _connect_server() _user_login(connection) _ask_startcommand(connection) _start_game(connection) connectfour_socket.close(connection)