def get_move(self,move): self.communicator.send(str(move).encode()) ai_move,err = self.communicator.recv(return_stderr=True) if len(err) > 1: print("exception") raise Exception(err.decode()) ai_move = ai_move.decode().split("\n")[-1].rstrip() return Move.from_str(ai_move)
def get_move(self,move): """ get_move function for NetworkAI called from the gameloop in the main module. @param move: A Move object describing the move. @return res_move: A Move object describing the move manualAI wants to make. This move is a random move from the set of valid moves. @raise : """ sleep(0.3) #TODO: Combine two branches if self.mode == 'host': if move.seq: print('SENT:', str(move)) sentence = str(move).encode() self.connectionSocket.send(sentence) response = self.connectionSocket.recv(1024).decode().rstrip() try: res_move = Move.from_str(response) if not res_move.seq: raise Exception except: print("You win. Your peer crashed.") raise Exception print('GET:', res_move) return res_move else: sleep(0.1) if move.seq: print('SENT:', str(move)) sentence = str(move).encode() self.topSocket.send(sentence) response = self.topSocket.recv(1024).decode().rstrip() try: res_move = Move.from_str(response) print(res_move.seq) if not res_move.seq: raise Exception except: print("You win. Your peer crashed.") raise Exception print('GET:', res_move) return res_move