def take(self, card, players): if int(self) >= 17: return False if True in map_to_list(lambda x: bool(x), players): for q, n in map_to_list(lambda x: (bool(x), int(x)), players): if q and n >= int(self): if card is not None: return super().take(card) return True return False
def test_participants(): """ verifies the following: -participant can obtain cards -participant has a string representation -participant has an integer representation -participant loses if their score is above 21 -participant can be forced to lose -participants can be forced to obtain a card """ print("Testing implementation of participants...") from pkg.roles import Participant from pkg.cards import Card p = Participant("Dealer") if p.name != "Dealer": print("Error: participant name cannot be specified") return cards = map_to_list(lambda x: p.take(Card(x)), ['H3', 'H4', 'SQ']) if len(p) != 3: print("Error: participant can not obtain cards") return True s = repr(p) if s != "Dealer: H3, H4, SQ": print("Error: participant can not be represented as string") return True n = int(p) if n != 17: print("Error: participant can not be represented as integer") return True p.take(Card('DQ')) if int(p) > 21 and bool(p): print("Error: participant does not lose if score is above 21") return True p = Participant("Player") p.defeat() if bool(p): print("Error: participant can not be forced to lose") return True p.give(Card('H6')) if len(p) != 1: print("Error: participant can not be forcefully given a card") return True print("OK")
def __repr__(self): return self.name + ": " + ", ".join( map_to_list(lambda x: repr(x), self.cards))
def test_decks(deck_path, bogus_path): """ verifies the following: -random deck can be made -deck is reduced when taking cards from it -custom deck can be loaded from file, preserving desired order -importing custom decks with exotic cards raises an exception -importing custom decks with duplicate cards raises an exception -trying to obtain a card from an empty deck will yield none """ print("Testing implementation of decks...") from pkg.cards import Deck, Card, DeckImportError d1 = Deck(test=True) if len(d1) != 52: print("Error: new deck has wrong amount of cards") return True d1.export(bogus_path) c1 = d1.pick() if len(d1) != 51: print("Error: taking a card from a deck does not reduce it's size") return True c2 = d1.pick() if c1 == c2: print("Error: can take the same card out of a deck twice") return True d2 = Deck(deck_path, test=True) test_cards = d2.pick(52) if d2.pick() != None: print( "Error: trying to take a card from an empty deck does not return None" ) return True cards = [] with open(deck_path, 'r') as f: cards = sanitize(f.readline()).split(',') cards = map_to_list(lambda x: Card(x), cards) for a, b in zip(cards, test_cards): if str(a) != str(b): print( "Error: order is not preserved when importing a custom deck: " + str(a) + " vs. " + str(b)) return True with open(bogus_path, 'r') as f: cards = sanitize(f.readline()).split(',') cards.pop() cards.append("X13") s = ",".join(cards) with open(bogus_path, 'w+') as f: f.write(s) q = True try: Deck(bogus_path, test=True) except DeckImportError: q = False if q: print( "Error: a deck containing exotic cards can be imported without raising an exception" ) return True cards.pop() s = ",".join(cards) with open(bogus_path, 'w+') as f: f.write(s) q = True try: Deck(bogus_path, test=True) except DeckImportError: q = False if q: print( "Error: a deck with wrong number of cards can be imported without raising an exception" ) return True cards.append(cards[0]) s = ",".join(cards) with open(bogus_path, 'w+') as f: f.write(s) q = True try: Deck(bogus_path, test=True) except DeckImportError: q = False if q: print( "Error: a deck containing duplicate cards can be imported without raising an exception" ) return True print("OK")
def test_dealer(): """ verifies the following: -dealer can not obtain cards if current score is above 17 -dealer does not obtain cards if dealer's score is higher than that of other players -dealer will not try to beat the score of a player that has lost -dealer will try to beat remaining players """ print("Testing implementation of dealers...") from pkg.roles import Player, Dealer from pkg.cards import Card p1 = Player('Edward') d = Dealer() cards = map_to_list(lambda x: Card(x), ['H3', 'H4', 'SQ']) for c in cards: p1.take(c) d.take(c, [p1]) if d.take(Card('H7'), [p1]): print("Error: dealer can obtain cards if current score is 17 or above") return True c1 = [Card('HQ'), Card('H7')] c2 = [Card('SQ'), Card('H6')] c3 = [Card('DQ'), Card('H5')] p1 = Player("alice") p2 = Player("bob") d = Dealer() for c in c2: p1.take(c) for c in c3: p2.take(c) for c in c1: d.take(c, [p1, p2]) if d.take(Card('S2'), [p1, p2]): print( "Error: dealer takes another card when current score exceeds players' scores" ) return True p1 = Player("alice") d = Dealer() for c in c2: p1.take(c) for c in c3: d.take(c, [p1]) p1.take(Card('SQ')) if d.take(Card('S2'), [p1]): print( "Error: dealer tries to beat the score of a player who has already lost" ) return True p1 = Player("alice") p2 = Player("bob") d = Dealer() for c in c1: p1.take(c) for c in c2: p2.take(c) for c in c3: d.take(c, [p1, p2]) p2.take(Card('SQ')) if not d.take(Card('S2'), [p1, p2]): print("Error: dealer does not try to beat remaining players") return True print("OK")