Beispiel #1
0
class HeartsPlayer:
	"""The player in a game of hearts."""
	def __init__(self, name):
		self.name = name
		self.scores = []
		self.hand = Deck()
		self.points_this_round = 0
		self.tricks_this_round = 0
		self.active_card = 0
	def play_active_card(self, game):
		if not self.hand:
			return False
		return self.play_card(self.hand[self.active_card], game)
	def play_card(self, card, game):
		if can_play_card(game, self.hand, card) and game.player() == self:
			self.hand.play(card)
			game.trick.push(card)
			if self.active_card >= len(self.hand):
				self.active_card = len(self.hand) - 1
			return True
		return False
	def get_trick(self, cards):
		self.points_this_round += sum(hearts_points(card) for card in cards)
		self.tricks_this_round += 1
	def score(self):
		return sum(self.scores)
	def render(self, where, visible, win):
		text = "%s: %d" % (self.name, self.points_this_round)
		h, w = win.getmaxyx()
		method = [self.hand.draw_h, self.hand.draw_v][where % 2]
		coord = [h - 9, 1, 2, w - 10][where]
		method(win, coord, self.active_card, visible)
		handlength = (7 + 2*len(self.hand)) if len(self.hand) != 0 else 0
		textloc = [
			(coord - 1, (w - len(text))/2),
			((h - handlength)/2 - 1, coord + (0 if len(text) > 9 else (9 - len(text))/2)),
			(coord - 1, (w - len(text))/2),
			((h - handlength)/2 - 1, coord + 9 - len(text) - (0 if len(text) > 9 else (9 - len(text))/2))
		][where]
		win.addstr(textloc[0], textloc[1], text, curses.A_BOLD)