def generate_uno_deck(): # Generate cards for UNO cards = [] colors = ["Red", "Green", "Yellow", "Blue"] for color in colors: cards.append(Card(len(cards), str(0), color)) for num in range(1, 10): # generate 2 cards each from [1 - 9] cards.append(Card(len(cards), str(num), color)) cards.append(Card(len(cards), str(num), color)) cards.append(Card(len(cards), "draw", color)) cards.append(Card(len(cards), "draw", color)) cards.append(Card(len(cards), "skip", color)) cards.append(Card(len(cards), "skip", color)) cards.append(Card(len(cards), "reverse", color)) cards.append(Card(len(cards), "reverse", color)) cards.append(Card(len(cards), "wild", "wild")) cards.append(Card(len(cards), "wild", "wild")) cards.append(Card(len(cards), "wild", "wild")) cards.append(Card(len(cards), "wild", "wild")) cards.append(Card(len(cards), "wild_draw", "wild")) cards.append(Card(len(cards), "wild_draw", "wild")) cards.append(Card(len(cards), "wild_draw", "wild")) cards.append(Card(len(cards), "wild_draw", "wild")) for card in cards: surface = f"{card.color.upper()}_{card.value.upper()}" animation.game.track_card(animation.assets.CARDS[surface], card.id) # Populate main deck and create discard discard = Deck() deck = Deck(discard=discard, cards=cards) deck.shuffle() sfx_card_shuffle.play() return deck
def test_deck_json_dumps(): discardDeck = Deck(None, [Card(1, "3", "Red"), Card(2, "4", "Blue")]) cards = [Card(3, "5", "Yellow"), Card(4, "6", "Green")] deck = Deck(discardDeck, cards) string = json.dumps(deck.reprJSON(), cls=ComplexEncoder) expected = """{"discardDeck": {"discardDeck": null, "cards": [{"id": 1, "value": "3", "color": "Red"}, {"id": 2, "value": "4", "color": "Blue"}]}, "cards": [{"id": 3, "value": "5", "color": "Yellow"}, {"id": 4, "value": "6", "color": "Green"}]}""" assert string == expected
def test_deck_json_dump_load(): discardDeck = Deck(None, [Card(1, "3", "Red"), Card(2, "4", "Blue")]) cards = [Card(3, "5", "Yellow"), Card(4, "6", "Green")] deck = Deck(discardDeck, cards) string = json.dumps(deck.reprJSON(), cls=ComplexEncoder) deck2 = Deck() deck2.loadJSON(string) assert deck.cards == deck2.cards
def test_deck_setup(): discardDeck = Deck(None, None) cards = [] colors = ["Red", "Green", "Yellow", "Blue"] for color in colors: cards.append(Card(len(cards), str(0), color)) for num in range(1, 10): cards.append(Card(len(cards), str(num), color)) cards.append(Card(len(cards), "+2", color)) cards.append(Card(len(cards), "skip", color)) cards.append(Card(len(cards), "reverse", color)) cards.append(Card(len(cards), "wild", "wild")) cards.append(Card(len(cards), "+4", "wild")) deck = Deck(discardDeck, cards) return deck
def test_deck_loadJSON(): testjson = """ {"discardDeck": {"discardDeck": null, "cards": [{"id": 101, "value": "wild", "color": "wild"}]}, "cards": [{"id": 54, "value": "2", "color": "Yellow"}, {"id": 28, "value": "2", "color": "Green"}, {"id": 15, "value": "8", "color": "Red"}, {"id": 77, "value": "1", "color": "Blue"}, {"id": 99, "value": "reverse", "color": "Blue"}]} """ deck = Deck() deck.loadJSON(testjson) assert len(deck.cards) == 5 assert len(deck.discardDeck.cards) == 1 assert deck.discardDeck.discardDeck is None
def test_deck_draw_excess_no_discard(): cards = [Card(3, "5", "Yellow"), Card(4, "6", "Green")] deck = Deck(None, cards) drawn_cards = deck.draw(3) assert len(drawn_cards) == 2
def test_deck_draw_excess_reshuffle(): discardDeck = Deck(None, [Card(1, "3", "Red"), Card(2, "4", "Blue")]) cards = [Card(3, "5", "Yellow"), Card(4, "6", "Green")] deck = Deck(discardDeck, cards) drawn_cards = deck.draw(3) assert len(drawn_cards) == 3
def test_deck_not_list(): sample_card = Card(1, "4", "Red") with pytest.raises(TypeError): deck = Deck(None, sample_card)
def test_deck(): sample_discard = Deck() sample_card = Card(1, "4", "Red") deck = Deck(sample_discard, [sample_card]) assert deck.cards[0] == sample_card assert deck.discardDeck == sample_discard
def test_deck_no_discard(): sample_card = Card(1, "4", "Red") deck = Deck(None, [sample_card]) assert deck.cards[0] == sample_card
def test_empty_deck(): deck = Deck() assert deck.discardDeck == None assert deck.cards == []
def test_deck_reprJSON(): discardDeck = Deck(None, [Card(1, "3", "Red"), Card(2, "4", "Blue")]) cards = [Card(3, "5", "Yellow"), Card(4, "6", "Green")] deck = Deck(discardDeck, cards) assert deck.reprJSON() == dict(discardDeck=deck.discardDeck, cards=deck.cards)
def test_deck_getDiscard_empty(): discardDeck = Deck() cards = [Card(3, "5", "Yellow"), Card(4, "6", "Green")] deck = Deck(discardDeck, cards) assert deck.getDiscard() == None
def test_deck_getDiscard(): discardDeck = Deck(None, [Card(1, "3", "Red"), Card(2, "4", "Blue")]) cards = [Card(3, "5", "Yellow"), Card(4, "6", "Green")] deck = Deck(discardDeck, cards) assert str(deck.getDiscard()) == "Blue 4"
cards.append(Card(len(cards), "+2", color)) cards.append(Card(len(cards), "skip", color)) cards.append(Card(len(cards), "skip", color)) cards.append(Card(len(cards), "reverse", color)) cards.append(Card(len(cards), "reverse", color)) cards.append(Card(len(cards), "wild", "wild")) cards.append(Card(len(cards), "wild", "wild")) cards.append(Card(len(cards), "wild", "wild")) cards.append(Card(len(cards), "wild", "wild")) cards.append(Card(len(cards), "+4", "wild")) cards.append(Card(len(cards), "+4", "wild")) cards.append(Card(len(cards), "+4", "wild")) cards.append(Card(len(cards), "+4", "wild")) # Populate main deck and create discard discard = Deck() deck = Deck(discard=discard, cards=cards) deck.shuffle() discard.cards = deck.draw(1) # Discard the top card of the deck # Create 3 players player1 = Player("Player 1", deck) player2 = Player("Player 2", deck) player3 = Player("Player 3", deck) players = [player1, player2, player3] for _ in range(7): for player in players: player.draw(1) print(json.dumps(deck.reprJSON(), cls=ComplexEncoder))