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


Python Map.regions方法代码示例

本文整理汇总了Python中Map.regions方法的典型用法代码示例。如果您正苦于以下问题:Python Map.regions方法的具体用法?Python Map.regions怎么用?Python Map.regions使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Map的用法示例。


在下文中一共展示了Map.regions方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Player

# 需要导入模块: import Map [as 别名]
# 或者: from Map import regions [as 别名]
class Player(object):

	# The init functions set all member of this class. It takes two
	# optional arguments: 'ki' is set to True if this is the computer
	# player, 'level' to define it's strength.
	def __init__(self, ki=False, level=50):

		# Asking for 'human' I prefer before asking for dump, deadly
		# fast calculating machines...so, I called the member 'human'.
		# I called the "level" now 'ki_level' to make clear it is only
		# valid for computer players, not for human ones.
		self.human		= not ki
		self.ki_level	= level

		# The Player needs some counters for counting his ships
		# 'ship_count' which not sunk so far and a list of ships his foe
		# has already. This is 'foeships'.
		self.ship_count	= 0
		self.foeships	= []

		# Now the maps. Because the handling of maps may be difficult,
		# this is encapsulated in another class called 'Map'. I called
		# them 'ships' for the secret map the Player hold his own ships.
		# And 'hits' which is his open map to track the bombardments.
		self.ships	= Map()
		self.hits	= Map()

		# The player (especially the KI) needs to remember the last
		# turn's result. So here we hold space to save is...
		self.last_result = None


		## public methods


	# To play "battleships" you have to set up the game first. Some of
	# this is done automatically while initialize the object (like
	# create space and initializing counters). The placement of the
	# ships should not be done automatically (but it is done yet in
	# __main__).
	# The following function places _one_ ship onto the secret map.
	# Therefor it gets itself (the Player object) and a ship definition,
	# containing the type and size of the ship.
	#
	def place_ship(self, shipdef):
		"""
		Randomly set a ship onto ship map and returns the ship region or
		None if no space is left.
		"""
		assert isinstance(shipdef, dict), "'shipdef' must be of type 'dict'"

		# Get the type and the size of the ship from the ship
		# definition.
		what, size = shipdef['name'], shipdef['size']

		# Just get the map in a shorthand form.
		mymap = self.ships

		# I calculate a list with all free regions with a minimum size
		# of the ship's size, this is done with 'mymap.regions(size)'.
		# Then I choose a region randomly from this list an return None
		# to the caller if there is no space left.
		region = RAND.choice(mymap.regions(size))
		if len(region) == 0: return None

		# The returned region has a _minimum_ size and can be greater
		# then the ship's size. I choose now the part of the region we
		# use for the ship. I get the starting point first.
		# This can be one of 'size - lenght-of-region' fields.
		first = RAND.randint(0,len(region)-size)

		# Now I set 'size' fields (beginning with the first field,
		# choosen above) to the 'ship' status.
		mymap.set_fields(region[first:first+size], 'ship')

		# Thats important!
		# Because the ships are not allowed to be connected, I set all
		# surrounding fields to 'water'. These fields not empty anymore,
		# so they will not be choosen to place a ship again.
		mymap.set_fields(
			mymap.neighbours(set(region[first:first+size])),
			'water'
		)

		# count this ship and return the region used
		self.ship_count += 1
		return region[first:first+size]


	def cleanup_ships_map(self):
		"""
		Cleanup the ships map of all the helpful water fields.
		"""
		mymap = self.ships
		mymap.set_fields(mymap.get_fields('water'), None)


	def save_foes_ships(self, shipdef):
		# I have to copy this dictionary deeply, not copying the
		# reference to it only.
#.........这里部分代码省略.........
开发者ID:OBdA,项目名称:battleships,代码行数:103,代码来源:battleships.py


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