当前位置: 首页>>代码示例>>Python>>正文


Python State.setSlot方法代码示例

本文整理汇总了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()
开发者ID:markdrago,项目名称:four-in-a-row,代码行数:34,代码来源:game.py

示例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()
开发者ID:yodle,项目名称:four-in-a-row,代码行数:58,代码来源:game.py


注:本文中的state.State.setSlot方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。