本文整理汇总了Python中ai.AI.init方法的典型用法代码示例。如果您正苦于以下问题:Python AI.init方法的具体用法?Python AI.init怎么用?Python AI.init使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ai.AI
的用法示例。
在下文中一共展示了AI.init方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from ai import AI [as 别名]
# 或者: from ai.AI import init [as 别名]
class Game:
def __init__(self, conn, addr, port, name):
self.serv_conn = conn
self.serv_addr = addr
self.serv_port = port
self.game_name = name
self.ai = AI()
self.ai.connection = self.serv_conn
#Attempt to connect to the server
def connect(self):
while True:
try:
#Attempting to connect
self.serv_conn.connect((self.serv_addr, self.serv_port))
except socket.error:
#Failed to connect
time.sleep(1)
else:
#Client connected
return True
def receive(self):
data = utility.receive_string(self.serv_conn)
message = json.loads(data)
if message['type'] == 'changes':
self.update_game(message)
elif message['type'] == 'player_id':
self.ai.my_player_id = message['args']['id']
elif message['type'] == 'game_over':
raise GameOverException(message["args"]["winner"], message["args"]["reason"])
return message
def wait_for(self, *types):
while True:
message = self.receive()
if message['type'] in types:
return message
#Attempt to login to the server
def login(self):
login_json = client_json.login.copy()
login_json['args']['username'] = self.ai.username
login_json['args']['password'] = self.ai.password
utility.send_string(self.serv_conn, json.dumps(login_json))
message = self.wait_for('success', 'failure')
if message['type'] == 'success':
#Login success
return True
else:
#Login failed
return False
#Attempt to create a game on the server
def create_game(self):
create_game_json = client_json.create_game.copy()
if self.game_name is not None:
create_game_json['args']['game_name'] = self.game_name
utility.send_string(self.serv_conn, json.dumps(create_game_json))
message = self.wait_for('success', 'failure')
if message['type'] == "success":
self.game_name = message['args']['name']
print("Game created: {}".format(self.game_name))
return True
else:
#Game creation failed
return False
#Receive Player ID from server
def recv_player_id(self):
self.wait_for('player_id')
return True
#Runs before main_loop has began.
def init_main(self):
self.wait_for('start_game')
self.ai.init()
return True
#Runs after main_loop has finished.
def end_main(self):
self.ai.end()
return True
#Main connection loop until end of game.
def main_loop(self):
while True:
message = self.wait_for('start_turn', 'game_over')
if message['type'] == 'game_over':
return True
if self.ai.my_player_id == self.ai.player_id:
utility.v_print("Turn Number: {}".format(self.ai.turn_number))
#.........这里部分代码省略.........
示例2: __init__
# 需要导入模块: from ai import AI [as 别名]
# 或者: from ai.AI import init [as 别名]
class Game:
def __init__(self, conn, addr, port, name):
self.serv_conn = conn
self.serv_addr = addr
self.serv_port = port
self.game_name = name
self.ai = AI()
self.ai.connection = self.serv_conn
#Attempt to connect to the server
def connect(self):
while True:
try:
#Attempting to connect
self.serv_conn.connect((self.serv_addr, self.serv_port))
except socket.error:
#Failed to connect
time.sleep(1)
else:
#Client connected
return True
def receive(self):
data = utility.receive_string(self.serv_conn)
message = json.loads(data)
if message['type'] == 'changes':
self.update_game(message)
elif message['type'] == 'player_id':
self.ai.my_player_id = message['args']['id']
elif message['type'] == 'game_over':
raise GameOverException(message["args"]["winner"], message["args"]["reason"])
return message
def wait_for(self, *types):
while True:
message = self.receive()
if message['type'] in types:
return message
#Attempt to login to the server
def login(self):
login_json = client_json.login.copy()
login_json['args']['username'] = self.ai.username
utility.send_string(self.serv_conn, json.dumps(login_json))
message = self.wait_for('success', 'failure')
if message['type'] == 'success':
#Login success
return True
else:
#Login failed
return False
#Attempt to create a game on the server
def create_game(self):
create_game_json = client_json.create_game.copy()
if self.game_name is not None:
create_game_json['args']['game_name'] = self.game_name
utility.send_string(self.serv_conn, json.dumps(create_game_json))
message = self.wait_for('success', 'failure')
if message['type'] == "success":
self.game_name = message['args']['name']
print("Game created: {}".format(self.game_name))
return True
else:
#Game creation failed
return False
#Receive Player ID from server
def recv_player_id(self):
self.wait_for('player_id')
return True
#Runs before main_loop has began.
def init_main(self):
self.wait_for('start_game')
self.ai.init()
return True
#Runs after main_loop has finished.
def end_main(self):
self.ai.end()
return True
#Main connection loop until end of game.
def main_loop(self):
while True:
message = self.wait_for('start_turn', 'game_over')
if message['type'] == 'game_over':
return True
if self.ai.my_player_id == self.ai.player_id:
utility.v_print("Turn Number: {}".format(self.ai.turn_number))
self.ai.run()
#.........这里部分代码省略.........