banaandrew

connectfour

Nov 5th, 2017
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.01 KB | None | 0 0
  1. import socket_handler
  2. import connectfour
  3. import shared_functions
  4.  
  5. DROP = shared_functions.DROP
  6. POP = shared_functions.POP
  7.  
  8. def network_game_director() -> None:
  9.     '''
  10.    Calls the necessary functions to provide a coherent gmae experience for the client
  11.    '''
  12.    
  13.     host = socket_handler.read_host()
  14.     port = socket_handler.read_port()
  15.    
  16.     username = socket_handler.read_username()
  17.    
  18.     connection = socket_handler.connect(host, port)
  19.    
  20.     socket_handler.send_message(connection, 'I32CFSP_HELLO ' + username)
  21.     socket_handler.receive_response(connection)
  22.     socket_handler.send_message(connection, 'AI_GAME')
  23.     socket_handler.receive_response(connection)
  24.    
  25.     gamestate = connectfour.new_game()
  26.    
  27.     while True:
  28.         if gamestate.turn == 1:
  29.             shared_functions.print_current_player(gamestate.turn)
  30.             shared_functions.print_board(gamestate.board)
  31.            
  32.             gamestate = action_parser_client(gamestate) ###################################
  33.                
  34.         elif gamestate.turn == 2:
  35.             gamestate = action_parser_server(gamestate)
  36.                
  37.         if shared_functions.determine_winner(gamestate):
  38.             shared_functions.print_board(gamestate.board)
  39.             break
  40.        
  41. def action_parser_client(gamestate) -> gamestate:###################################
  42.     '''
  43.    Updates board according to player's action
  44.    '''
  45.     (col, action) = shared_functions.prompt_user(len(gamestate.board), len(gamestate.board[0]))
  46.     print()
  47.    
  48.     try:
  49.         if action == DROP:
  50.             gamestate = connectfour.drop(gamestate, col-1)
  51.             socket_handler.send_message(connection, 'DROP ' + str(col))
  52.         else:
  53.             gamestate = connectfour.pop(gamestate, col-1)
  54.             socket_handler.send_message(connection, 'POP ' + str(col))
  55.            
  56.     except connectfour.InvalidMoveError():
  57.         print('Invalid move; drop can only be used on open columns')
  58.     except connectfour.GameOverError():
  59.         print('Game is over')
  60.     except ValueError:
  61.         print('Value error: column out of range ')    
  62.        
  63.     return gamestate
  64.    
  65. def action_parser_server(gamestate) -> gamestate:
  66.     '''
  67.    Updates board according to server's action
  68.    '''
  69.     while True:
  70.         response = socket_handler.receive_response(connection)
  71.        
  72.         if 'DROP' in response or 'POP' in response:
  73.             (col, action) = (response.split()[0], int(response.split()[1]))
  74.            
  75.             if action == 'DROP':
  76.                 connectfour.drop(gamestate, col-1)
  77.             elif action == 'POP':
  78.                 connectfour.pop(gamestate, col-1)
  79.             break
  80.                
  81.         elif response == 'INVALID':
  82.             print('Invalid input; format as "[col]" to drop or "p [col]" to drop')
  83.             break
  84.            
  85.         gamestate = connectfour.drop(gamestate, col-1)
  86.        
  87.         return gamestate
  88.    
  89. if __name__ == '__main__':
  90.     network_game_director()
Add Comment
Please, Sign In to add comment