Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket_handler
- import connectfour
- import shared_functions
- DROP = shared_functions.DROP
- POP = shared_functions.POP
- def network_game_director() -> None:
- '''
- Calls the necessary functions to provide a coherent gmae experience for the client
- '''
- host = socket_handler.read_host()
- port = socket_handler.read_port()
- username = socket_handler.read_username()
- connection = socket_handler.connect(host, port)
- socket_handler.send_message(connection, 'I32CFSP_HELLO ' + username)
- socket_handler.receive_response(connection)
- socket_handler.send_message(connection, 'AI_GAME')
- socket_handler.receive_response(connection)
- gamestate = connectfour.new_game()
- while True:
- if gamestate.turn == 1:
- shared_functions.print_current_player(gamestate.turn)
- shared_functions.print_board(gamestate.board)
- gamestate = action_parser_client(gamestate) ###################################
- elif gamestate.turn == 2:
- gamestate = action_parser_server(gamestate)
- if shared_functions.determine_winner(gamestate):
- shared_functions.print_board(gamestate.board)
- break
- def action_parser_client(gamestate) -> gamestate:###################################
- '''
- Updates board according to player's action
- '''
- (col, action) = shared_functions.prompt_user(len(gamestate.board), len(gamestate.board[0]))
- print()
- try:
- if action == DROP:
- gamestate = connectfour.drop(gamestate, col-1)
- socket_handler.send_message(connection, 'DROP ' + str(col))
- else:
- gamestate = connectfour.pop(gamestate, col-1)
- socket_handler.send_message(connection, 'POP ' + str(col))
- except connectfour.InvalidMoveError():
- print('Invalid move; drop can only be used on open columns')
- except connectfour.GameOverError():
- print('Game is over')
- except ValueError:
- print('Value error: column out of range ')
- return gamestate
- def action_parser_server(gamestate) -> gamestate:
- '''
- Updates board according to server's action
- '''
- while True:
- response = socket_handler.receive_response(connection)
- if 'DROP' in response or 'POP' in response:
- (col, action) = (response.split()[0], int(response.split()[1]))
- if action == 'DROP':
- connectfour.drop(gamestate, col-1)
- elif action == 'POP':
- connectfour.pop(gamestate, col-1)
- break
- elif response == 'INVALID':
- print('Invalid input; format as "[col]" to drop or "p [col]" to drop')
- break
- gamestate = connectfour.drop(gamestate, col-1)
- return gamestate
- if __name__ == '__main__':
- network_game_director()
Add Comment
Please, Sign In to add comment