本文整理汇总了Python中state.State.setSlot方法的典型用法代码示例。如果您正苦于以下问题:Python State.setSlot方法的具体用法?Python State.setSlot怎么用?Python State.setSlot使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类state.State
的用法示例。
在下文中一共展示了State.setSlot方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from state import State [as 别名]
# 或者: from state.State import setSlot [as 别名]
class Game:
def __init__(self, host, port):
self.gameId = 0
self.state = None
self.host = host
self.port = port
def init(self, nickname, level):
response = self.get_post_request(self.host, self.port, '/init/' + str(level), {'nickname': nickname, 'scaffold': 'python'})
json_root = json.loads(response)
self.gameId = json_root['gameId']
self.update_board(json_root['board'])
def move(self, col):
response = self.get_post_request(self.host, self.port, '/game/move/' + str(self.gameId), {'move': col})
json_root = json.loads(response)
self.update_board(json_root['board'])
def update_board(self, board):
cols = len(board)
rows = len(board[0])
self.state = State(rows, cols)
for col_i, col in enumerate(board):
for row_i, slot in enumerate(col):
self.state.setSlot(row_i, col_i, slot)
def get_post_request(self, host, port, url, params):
params = urllib.urlencode(params)
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
conn = httplib.HTTPConnection(host, port)
conn.request('POST', url, params, headers)
return conn.getresponse().read()
示例2: __init__
# 需要导入模块: from state import State [as 别名]
# 或者: from state.State import setSlot [as 别名]
class Game:
ROWS = 6
COLS = 7
def __init__(self, host, port):
self.gameId = 0
self.state = None
self.host = host
self.port = port
self.state = State(Game.ROWS, Game.COLS)
def init(self, nickname, level):
response = self.getPostRequest(
self.host, self.port,
'/game/init/%s' % level,
{'nickname': nickname, 'scaffold': 'python'}
)
stateJson = json.loads(response)
self.updateState(stateJson)
self.gameId = stateJson['id']
return self.state
def move(self, col):
response = self.getPostRequest(
self.host, self.port,
'/game/move/%s' % self.gameId,
{'move': col}
)
stateJson = json.loads(response)
self.updateState(stateJson)
return self.state
def updateState(self, stateJson):
if 'error' in stateJson:
self.state.setError(stateJson['error'])
return
self.updateBoard(stateJson['board'])
self.state.setGameOver(stateJson['gameOver'])
self.state.setMoveList(stateJson['moveList'])
self.state.setMoves(stateJson['moves'])
self.state.setPlayerNumber(stateJson['challengerPlayer'])
def updateBoard(self, board):
for col_i, col in enumerate(board):
for row_i, slot in enumerate(col):
self.state.setSlot(row_i, col_i, slot)
def getPostRequest(self, host, port, url, params):
params = urllib.urlencode(params)
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
conn = httplib.HTTPConnection(host, port)
conn.request('POST', url, params, headers)
return conn.getresponse().read()