def play_card(self, playedCards, dummyHand): """Invoked when the bot has an opportunity to play a card""" chosenCard = max(self.cards, key=lambda x: x.value) if(len(playedCards) > 0): ledSuitCards = [x for x in self.cards if x.suit == playedCards[0].suit] trumpCards = [x for x in self.cards if x.suit == self.winningBid.bidSuit] nonTrumpCards = [x for x in self.cards if x.suit != self.winningBid.bidSuit] topCard = Match.winning_card(playedCards, self.winningBid.bidSuit) if len(ledSuitCards) > 0: if len(playedCards) >= 2: if playedCards.index(topCard) == (len(playedCards) - 2): # Don't overthrow our partner chosenCard = min(ledSuitCards, key=lambda x: x.value) else: # If we're not winning try to take it chosenCard = max(ledSuitCards, key=lambda x: x.value) else: if playedCards.index(topCard) == (len(playedCards) - 2): # Don't trump in on our partner if he's winning if len(nonTrumpCards) > 0: chosenCard = min(nonTrumpCards, key=lambda x: x.value) else: # Trump in if he isn't if len(trumpCards) > 0: chosenCard = min(trumpCards, key=lambda x: x.value) self.cards.remove(chosenCard) return chosenCard
def test_winning_cards(self): winning_cards = [(['4s', 'Ks', '3d', 'As'], 's', 'As'), (['4s', 'Ad', '3d', 'As'], 'nt', 'As'), (['4s', '4d', '4h', '4c'], 'nt', '4s'), (['4s', '4d', '4h', '4c'], 'c', '4c'), (['2s', '4d', '4h', '5h'], 'c', '2s')] for played, trump, winner in winning_cards: played_cards = [Card.from_string(c) for c in played] nose.tools.assert_equal( Match.winning_card(played_cards, trump), Card.from_string(winner), 'Error in playing sequence {0} with trump {1}'.format( played, trump))
def test_winning_cards(self): winning_cards = [ (['4s', 'Ks', '3d', 'As'], 's', 'As'), (['4s', 'Ad', '3d', 'As'], 'nt', 'As'), (['4s', '4d', '4h', '4c'], 'nt', '4s'), (['4s', '4d', '4h', '4c'], 'c', '4c'), (['2s', '4d', '4h', '5h'], 'c', '2s') ] for played, trump, winner in winning_cards: played_cards = [Card.from_string(c) for c in played] nose.tools.assert_equal(Match.winning_card(played_cards, trump), Card.from_string(winner), 'Error in playing sequence {0} with trump {1}'.format(played, trump))
def play_card(self, playedCards, dummyHand): """Invoked when the bot has an opportunity to play a card""" chosenCard = max(self.cards, key=lambda x: x.value) if (len(playedCards) > 0): ledSuitCards = [ x for x in self.cards if x.suit == playedCards[0].suit ] trumpCards = [ x for x in self.cards if x.suit == self.winningBid.bidSuit ] nonTrumpCards = [ x for x in self.cards if x.suit != self.winningBid.bidSuit ] topCard = Match.winning_card(playedCards, self.winningBid.bidSuit) if len(ledSuitCards) > 0: if len(playedCards) >= 2: if playedCards.index(topCard) == (len(playedCards) - 2): # Don't overthrow our partner chosenCard = min(ledSuitCards, key=lambda x: x.value) else: # If we're not winning try to take it chosenCard = max(ledSuitCards, key=lambda x: x.value) else: if playedCards.index(topCard) == (len(playedCards) - 2): # Don't trump in on our partner if he's winning if len(nonTrumpCards) > 0: chosenCard = min(nonTrumpCards, key=lambda x: x.value) else: # Trump in if he isn't if len(trumpCards) > 0: chosenCard = min(trumpCards, key=lambda x: x.value) self.cards.remove(chosenCard) return chosenCard