def testPlayCardDoesNotAcceptCardIfCardHasBeenPlayed(self): self._set_player_position() self._trick_panel.setCards(self._cards) position = random.choice(POSITION_TAGS) card_played = _generate_random_card() while card_played == self._internal_cards[asPosition(position)]: card_played = _generate_random_card() self._trick_panel.playCard(position, card_played) self.assertNotEqual( card_played, self._trick_panel.cards()[asPosition(position)])
def testPlayCardDoesNotAcceptCardIfCardHasBeenPlayed(self): self._set_player_position() self._trick_panel.setCards(self._cards) position = random.choice(POSITION_TAGS) card_played = _generate_random_card() while card_played == self._internal_cards[asPosition(position)]: card_played = _generate_random_card() self._trick_panel.playCard(position, card_played) self.assertNotEqual(card_played, self._trick_panel.cards()[asPosition(position)])
def setPositionInTurn(self, position): if position is not None: position = positions.asPosition(position) if self._position_in_turn is not None: _mark_turn(self._hand_map[self._position_in_turn][1], False) if position in self._hand_map: _mark_turn(self._hand_map[position][1], True) self._position_in_turn = position
def testPlayCardClearsPreviousTrick(self): self._set_player_position() self._trick_panel.setCards(self._cards) self._trick_panel.setCards({}) card_played = (random.choice(POSITION_TAGS), _generate_random_card()) self._trick_panel.playCard(*card_played) self.assertEqual(self._trick_panel.cards(), [(asPosition(card_played[0]), card_played[1])])
def testPlayCardClearsPreviousTrick(self): self._set_player_position() self._trick_panel.setCards(self._cards) self._trick_panel.setCards({}) card_played = (random.choice(POSITION_TAGS), _generate_random_card()) self._trick_panel.playCard(*card_played) self.assertEqual( self._trick_panel.cards(), [(asPosition(card_played[0]), card_played[1])])
def setUp(self): self._app = QApplication(sys.argv) self._trick_panel = cards.TrickPanel() self._cards = [ dict(position=position, card=card) for (position, card) in zip(POSITION_TAGS, _generate_random_cards())] self._internal_cards = [ (asPosition(pair[cards.POSITION_TAG]), pair[cards.CARD_TAG]) for pair in self._cards]
def setUp(self): self._app = QApplication(sys.argv) self._trick_panel = cards.TrickPanel() self._cards = [ dict(position=position, card=card) for (position, card) in zip(POSITION_TAGS, _generate_random_cards()) ] self._internal_cards = [(asPosition(pair[cards.POSITION_TAG]), pair[cards.CARD_TAG]) for pair in self._cards]
def setCards(self, cards): """Set cards for all players This method accepts as its argument a mapping between positions and lists of cards held by the players in those positions. The effect of the method is to set the cards for all players based on the mapping. """ if not isinstance(cards, dict): raise messaging.ProtocolError("Invalid cards format: %r" % cards) for position, cards_for_position in cards.items(): if _is_position(position): position = positions.asPosition(position) self._hand_map[position][0].setCards(cards_for_position)
def playCard(self, position, card): """Confirm that a card has been played by the player in the position The effect of this method is to call playCard() for the correct HandPanel (as determined by position). Keyword Arguments: position -- the position of the player card -- the card played by the player """ position = positions.asPosition(position) card = asCard(card) if position in self._hand_map: self._hand_map[position][0].playCard(card) self._trick_panel.playCard(position, card)
def setPlayerPosition(self, position): """Set position of the current player The position must be set before setting cards. Cards for the current player are laid at the bottom of the area. """ self._position = positions.asPosition(position) hand_map = {} for position, hand, label in zip( positions.rotate(self._position), self._hand_panels, self._position_labels): hand_map[position] = (hand, label) label.setText(positions.positionLabel(position)) label.resize(hand.width(), label.height()) self._hand_map = hand_map self._trick_panel.setPlayerPosition(self._position)
def addCall(self, position, call): """Add call to the table Add new call coming from the specified position to the table. Both arguments can be either in serialized or internal representation. If the call is not in correct order (that is, positions do not follow each other clockwise), the call is ignored. Error is raised if the position or call does not have correct format (see bridge protocol specification). Keyword Arguments: position -- the position of the player to make the call call -- the call to be added """ self._add_call_helper((positions.asPosition(position), asCall(call)))
def setPlayerPosition(self, position): """Set position of the current player The position must be set before setting cards. Cards for the current player are laid at the bottom of the area. """ self._position = positions.asPosition(position) hand_map = {} for position, hand, label in zip(positions.rotate(self._position), self._hand_panels, self._position_labels): hand_map[position] = (hand, label) label.setText(positions.positionLabel(position)) label.resize(hand.width(), label.height()) self._hand_map = hand_map self._trick_panel.setPlayerPosition(self._position)
def setCards(self, cards): """Set cards for all players This method accepts as its argument a mapping between positions and lists of cards held by the players in those positions. The effect of the method is to set the cards for all players based on the mapping. """ if not isinstance(cards, dict): raise messaging.ProtocolError("Invalid cards format: %r" % cards) positions_to_clear = set(positions.Position) for position, cards_for_position in cards.items(): if _is_position(position): position = positions.asPosition(position) self._hand_map[position][0].setCards(cards_for_position) positions_to_clear.remove(position) for position in positions_to_clear: self._hand_map[position][0].setCards([])
def playCard(self, position, card): """Play single card to the trick This method puts card to given position, given that the position is empty or the timer period for clearing cards is ongoing. The arguments can be in internal or serialized representation. Keyword Arguments: position -- the position of the player who plays the card card -- the card played """ if self._timer.isActive(): del self._cards[:] position = positions.asPosition(position) if position not in (p for (p, _) in self._cards): card = asCard(card) self._cards.append((position, card)) self._timer.stop() self.repaint()
def playCard(self, position, card): """Play single card to the trick This method puts card to given position, given that the position is empty or the timer period for clearing cards is ongoing. The arguments can be in internal or serialized representation. Keyword Arguments: position -- the position of the player who plays the card card -- the card played """ if self._timer.isActive(): del self._cards[:] position = positions.asPosition(position) if position not in (p for (p, _) in self._cards): card = asCard(card) self._cards.append((position, card)) self._timer.stop() self.repaint() if len(self._cards) == len(positions.Position): self._timer.start()
def _generate_position_card_pair(pair): return (positions.asPosition(pair[POSITION_TAG]), asCard(pair[CARD_TAG]))
def _generate_position_card_pair(pair): return ( positions.asPosition(pair[POSITION_TAG]), asCard(pair[CARD_TAG]))
def _generate_position_call_pair(pair): return (positions.asPosition(pair[POSITION_TAG]), asCall(pair[CALL_TAG]))