Example #1
0
def place_error(s, message):
    if message == ERR402:
        print("Place Error: Position not in valid range 1-9. Try again")
    elif message == ERR403:
        print("Place Error: You aren't in a game")
    elif message == ERR404:
        print("Place Error: Not your turn")
    elif message == ERR405:
        print("Place Error: Illegal move, space occupied. Try again")
    else:
        exit_error(s, message)
Example #2
0
def finish_game(s, message):
    print_gameboard(s, message)
    if message.split()[0] == "END":
        print("The game has ended. " + message.split()[1] + "won!")
    elif message.split()[0] == "TIE":
        print("The game has ended. Tie game.")
    else:
        exit_error(s, message)

    # Send OK to server
    send_msg(s, OK)

    # TODO: handle new game after finishing
    print("")
Example #3
0
def extract_game_state(s, message):
    words = message.split()
    if words[0] in ["GAME", "END"]:
        start_index = 2
    elif words[0] == "TIE":
        start_index = 1
    else:
        exit_error(s, message)

    game_state_list = []
    for i in range(start_index, start_index + 9):
        game_state_list.append(words[i])
    if len(game_state_list) < 9:
        exit_error(s, message)
    return game_state_list
Example #4
0
def game_login(s, name):
    ''' This command takes one argument, your name. A player name is a userid that uniquely
    identifies a player. Your name is entered with this command and is sent to the server.
    Return True if login success, False if login failed.'''
    # Send message to server
    send_msg(s, LOGIN(name))

    # Receive response message from server
    response = s.recv(1024).decode()

    # Check response from server
    if response == OK:
        print("Logged in as " + name)
        return True
    elif response == ERR401:
        print("Login Failed. Try another name.")
        return False
    else:
        # Message from server is not recognized, close connection and exit
        exit_error(s, response)
Example #5
0
def game_exit(s):
    '''The player exits the server. It takes no argument. A player can issue this 
    command at any time. 
    Close the client socket and exit the program.'''
    # Send exit message to server
    send_msg(s, EXIT)

    # Receive confirmation
    responses = get_messages(s)

    for response in responses:
        if response == OK:
            # Close socket and exit client program
            print("Logging out.")
            s.close()
            sys.exit()
        elif response == QUIT:
            send_msg(s, QUIT)
            s.close()
            sys.exit()
        else:
            # Message from server is not recognized, close connection and exit
            exit_error(s, response)
Example #6
0
def game_place(s, n):
    '''This command issues a move. It takes one argument n, which is between 1 and 9 inclusive.
    It identify a cell that the player chooses to occupy at this move. 
    If all is well, the new game state is received from the server and displayed.'''
    # Send place message to server
    send_msg(s, PLACE(n))

    # Receive response message from server
    responses = get_messages(s)
    if quit_received(responses):
        handle_exit(s)
    response = responses[0]

    if response.split()[0] == "GAME":
        display_game_state(s, response)
    elif response.split()[0] in ["END", "TIE"]:
        finish_game(s, response)
        # Search for new game after finishing
        search_for_game(s)
    elif response.split()[1] == "ERROR":
        place_error(s, response)
    else:
        # Message from server is not recognized, close connection and exit
        exit_error(s, response)