def run_game(connection: connectfour_network.Connection) -> None: ''' Runs the main loop for the game, sending the user turn to the server and reading the AI move. Breaking when someone wins. ''' game_state = connectfour.new_game_state() connectfour_game.display_board(game_state) while True: game_state = user_turn(connection, game_state) connectfour_game.display_board(game_state) server_message = connectfour_network.read_message(connection) if server_message == "OKAY": new_game_state = server_turn(connection, game_state) if game_state != new_game_state: game_state = new_game_state else: print( "The server has not made a valid move, the connection will close." ) break connectfour_game.display_board(game_state) elif server_message == "WINNER_RED": connectfour_game.game_over(connectfour.RED) break elif server_message == "WINNER_YELLOW": connectfour_game.game_over(connectfour.YELLOW) break else: print(server_message) server_message = connectfour_network.read_message(connection) if server_message == "READY": print("It is your turn again!") print() elif server_message == "WINNER_RED": connectfour_game.game_over(connectfour.RED) break elif server_message == "WINNER_YELLOW": connectfour_game.game_over(connectfour.YELLOW) break else: print(server_message)
def game(connection: 'connection') -> None: '''goes through server version of connect four''' intromessage(connection) print('Welcome to Connect 4!') print('You are the RED player!') gamestate = connectfour.new_game() connectfour_common.print_game_board(gamestate.board) connectfour_network.send_message(connection, ('AI_GAME')) while True: a = connectfour_network.read_message(connection) if (a) == 'READY': print('\nIt is the', connectfour_common.getturn(gamestate), 'players turn!') answer = input( 'Choose if you want to DROP or POP and a column between 1 and ' + str(connectfour.BOARD_COLUMNS) + '. (Ex. DROP 4)"\n') answer = isvalid(answer) anslist = answer.split() connectfour_network.send_message(connection, answer) try: c = (int(anslist[1]) - 1) gamestate = connectfour_common.doingmove( anslist[0], c, gamestate) connectfour_common.print_game_board(gamestate.board) except: pass elif a == 'OKAY': print('\nIt is the', connectfour_common.getturn(gamestate), 'players turn!\n') output = connectfour_network.read_message(connection) move = printturn(output, gamestate, connection) col = (int(move[1]) - 1) gamestate = connectfour_common.doingmove(move[0], col, gamestate) connectfour_common.print_game_board(gamestate.board) elif a == 'INVALID': print('Invalid choice choose again.') elif a == "WINNER_RED": print('GAME OVER! You are the winner!') connectfour_network.close(connection) break elif a == "WINNER_YELLOW": print('GAME OVER! YOU LOST! Yellow player is the winner!') connectfour_network.close(connection) break elif a == ('ERROR'): print('Invalid choice choose again') pass
def user_interface() -> None: ''' Displays a user interface which asks for a username and starts a new game.''' connection = connectfour_network.make_connection(HOST, PORT) username = get_username() try: connectfour_network.login(connection, username) print(connectfour_network.read_message(connection)) connectfour_network.start_game(connection) print(connectfour_network.read_message(connection)) run_game(connection) finally: connectfour_network.close_connection(connection)
def server_turn(connection: connectfour_network.Connection, game_state: connectfour.ConnectFourGameState) -> None: ''' Reads the turn from the server AI and returns the game state. ''' print() print("The AI is taking it's turn..") AI_move = connectfour_network.read_message(connection) AI_moves = AI_move.split() AI_move_type = AI_moves[0].lower() try: AI_column_number = int(AI_moves[1]) except ValueError: return game_state if AI_move_type in [ 'drop', 'pop' ] and AI_column_number > 0 and AI_column_number < connectfour.BOARD_COLUMNS: game_state = connectfour_game.move(game_state, AI_column_number, AI_move_type) return game_state else: return game_state
def intromessage(connection: 'connection') -> None: '''sends intro message to server''' connectfour_network.send_message(connection, ('I32CFSP_HELLO ' + getusername())) print(connectfour_network.read_message(connection))