コード例 #1
0
 def testDiscardInfo(self):
     """Confirm that discard_info accurately reporst discard_pile status"""
     testState = ServerState()
     discardList = [Card(0, None), Card(3, 'Spades'), Card(2, 'Clubs')]
     testState.discardCards(discardList)
     info = testState.getDiscardInfo()
     self.assertEqual((Card(2, 'Clubs'), 3), info)
コード例 #2
0
    def testNewCards(self):
        """Confirm newCards adds cards to hand"""
        test_state = ClientState(ruleset=None)
        wholeDeck = Card.getStandardDeck()
        test_state.newCards(wholeDeck)
        self.assertEqual(wholeDeck, test_state.hand_cards)

        drawn_cards = [Card(0, None), Card(0, None)]
        test_state.newCards(drawn_cards)
        self.assertEqual(Card.getJokerDeck(), test_state.hand_cards)
コード例 #3
0
    def testDiscard(self):
        """Confirm that discarCards adds all cards to discard pile in order"""
        testState = ServerState()
        discardList = [Card(0, None), Card(3, 'Spades'), Card(2, 'Clubs')]
        testState.discardCards(discardList)
        self.assertEqual(len(testState.discard_pile), 3)
        self.assertEqual(testState.discard_pile, discardList)

        discardList = [Card(12, 'Hearts')]
        testState.discardCards(discardList)
        self.assertEqual(len(testState.discard_pile), 4)
        self.assertEqual(testState.discard_pile[-1], discardList[0])
コード例 #4
0
 def testDealtHands(self):
     """Confirm that extraHands are added to the test_state hand_list"""
     test_state = ClientState()
     hand = [
         Card(1, 'Spades'),
         Card(2, 'Clubs'),
         Card(3, 'Diamonds'),
         Card(4, 'Hearts'),
         Card(0, None)
     ]
     test_state.dealtHands([hand, hand])
     self.assertEqual(test_state.hand_list, [hand])
     self.assertEqual(test_state.hand_cards, hand)
コード例 #5
0
 def testDecks(self):
     """Confirm standard deck and joker deck aren't obviously wrong"""
     deck = Card.getStandardDeck()
     #length check
     self.assertEqual(len(deck), 52)
     #joker check
     self.assertFalse(Card(0, None) in deck)
     joker_deck = Card.getJokerDeck()
     #length check
     self.assertEqual(len(joker_deck), 54)
     #joker check
     self.assertTrue(Card(0, None) in joker_deck)
     #containsStandard check
     self.assertTrue(all(card in joker_deck for card in deck))
コード例 #6
0
 def test_Equivalence(self):
     """Confirm two cards are equal based on values not instantiation"""
     test_card_A = Card(11, "Diamonds")
     test_card_B = Card(11, 'Diamonds')
     test_card_C = Card(10, 'Diamonds')
     test_card_D = Card(11, 'Clubs')
     #Confirm self-equivalence
     self.assertEqual(test_card_A, test_card_A)
     #Confirm dif cards with same values are equal
     self.assertEqual(test_card_A, test_card_B)
     #Confirm non-equivalence if only numbers differ
     self.assertNotEqual(test_card_A, test_card_C)
     #Confirm not equal if only suit differs
     self.assertNotEqual(test_card_A, test_card_D)
コード例 #7
0
 def test_DeSerialize(self):
     """Confirm that cards are properly constructed from deserializing a tuple"""
     #deserialize joker
     test_tuple = (0, None)
     test_card = Card.deserialize(test_tuple)
     self.assertEqual(test_card, Card(0, None))
     #deserialize suit card
     test_tuple = (3, 'Clubs')
     test_card = Card.deserialize(test_tuple)
     self.assertEqual(test_card.suit, "Clubs")
     self.assertEqual(test_card.number, 3)
     #deserialize errors on invalid input card
     with self.assertRaises(ValueError):
         test_tuple = (66, 'Hearts')
         tesCard = Card.deserialize(test_tuple)
コード例 #8
0
 def testHandStatus(self):
     """Confirm that hand status information is ordered correctly"""
     test_state = ClientState(ruleset=None)
     #set turn phase
     test_state.turn_phase = 'TestPhase'
     #setup so that hand has 5 cards and there is a foot left to play
     hand = [
         Card(1, 'Spades'),
         Card(2, 'Clubs'),
         Card(3, 'Diamonds'),
         Card(4, 'Hearts'),
         Card(0, None)
     ]
     test_state.dealtHands([hand, hand])
     self.assertEqual(test_state.getHandStatus(), ['TestPhase', 5, 1])
コード例 #9
0
 def Network_deal(self, data):
     self._state.round = data["round"]
     self._state.reset()
     hand_list = [[Card.deserialize(c) for c in hand] for hand in data["hands"]]
     #TODO: in HandAndFoot we want to allow the player to choose the order of the hands eventually
     self._state.dealtHands(hand_list)
     self.sendPublicInfo() #More cards in hand now, need to update public information
コード例 #10
0
 def Network_buyingResult(self, data):
     buyer = data["buyer"]
     purchase = Card.deserialize(data["top_card"])
     if buyer == 'No one':
         self.note = "The {0} has been abandoned to the pile.".format(purchase)
     else:
         self.note = "{0} has purchased the {1}.".format(buyer, purchase)
コード例 #11
0
def restoreRunAssignment(visible_scards_dictionary, wild_numbers, numsets):
    """ Convert scards to cards and assign values to Wild cards and Aces in runs from server.

    Needed to maintain integrity of Wilds' assigned values in runs.  Server does not know tempnumbers
    (for backwards compatability not changing json between server and client).
    There's no ambiguity except for wilds and Aces at the ends of the run (processRuns handles wilds in middle).
    """

    if len(visible_scards_dictionary) == 0:
        return(visible_scards_dictionary)
    cardgroup_dictionary = {}
    for key, scard_group in visible_scards_dictionary.items():
        card_group = []
        for scard in scard_group:
            card = Card(scard[0], scard[1], scard[2])
            card_group.append(card)
        cardgroup_dictionary[key] = card_group
    for k_group, card_group in cardgroup_dictionary.items():
        if k_group[1] >= numsets and len(card_group) > 1:       # check if this is a run.
            if card_group[-1].number in wild_numbers:    # reset tempnumber for Wilds/Aces if they are at the end.
                card_group[-1].assignWild(card_group[-2].tempnumber + 1)
            elif card_group[-1].number == 1:
                card_group[-1].assignWild(14)
            if card_group[0].number in wild_numbers:
                card_group[0].assignWild(card_group[1].tempnumber - 1)
            elif card_group[0].number == 1:
                card_group[0].assignWild(-1)
    return cardgroup_dictionary
コード例 #12
0
 def Network_pickUpAnnouncement(self, data):
     player_name = data["player_name"]
     top_scard = data["top_card"]
     top_card = Card.deserialize(top_scard)
     if self._state.rules.Pickup_Size == 1:
         self.note =  player_name + ' picked up the ' + str(top_card)
     else:
         self.note = player_name + ' picked up the pile on the  ' + str(top_card)
コード例 #13
0
 def Network_newCards(self, data):
     card_list = [Card.deserialize(c) for c in data["cards"]]
     self._state.newCards(card_list)
     if self._state.turn_phase == Turn_Phases[2]:
         #This is the result of a pickup and we have a forced action
         self.makeForcedPlay(card_list[0])
     if not self._state.rules.Buy_Option:
         # Only in non-Buying games can we assume that your turn begins after receiving any cards.
         self.note = "You can now play cards or discard"
     self.sendPublicInfo() #More cards in hand now, need to update public information
コード例 #14
0
 def testPickupPile(self):
     """Confirm that picking up the pile works as it should"""
     test_state = ServerState()
     discardList = [
         Card(0, None),
         Card(3, 'Spades'),
         Card(2, 'Clubs'),
         Card(4, 'Hearts'),
         Card(4, 'Hearts'),
         Card(4, 'Hearts'),
         Card(4, 'Hearts'),
         Card(8, 'Hearts')
     ]
     test_state.discardCards(discardList)
     pickup = test_state.pickUpPile()
     self.assertEqual(len(test_state.discard_pile), 0)
     self.assertEqual(pickup[0], Card(8, 'Hearts'))
コード例 #15
0
 def test_Construction(self):
     """Confirm that cards are constructed properly and error on invalid values"""
     #Can make jokers (suit None)
     test_card = Card(0, None)
     #Any number given for a joker is set to 0
     test_card = Card(9999, None)
     self.assertEqual(test_card.number, 0)
     #All suits are options, '' or "" works for strings
     test_card = Card(1, 'Spades')
     test_card = Card(2, "Hearts")
     test_card = Card(13, "Diamonds")
     test_card = Card(10, 'Clubs')
     #Non-suit strings and non-plural suitnames are invalid
     with self.assertRaises(ValueError):
         test_card = Card(1, 'fakityFake')
     with self.assertRaises(ValueError):
         test_card = Card(1, 'Spade')
     #0 and numbers over 13 are invalid for non-Joker cards
     with self.assertRaises(ValueError):
         test_card = Card(0, 'Spades')
     with self.assertRaises(ValueError):
         test_card = Card(14, 'Spades')
コード例 #16
0
    def testDiscardCards(self):
        """Confirm discardCards removes cards without playing them"""
        test_state = ClientState(ruleset=None)
        hand = [
            Card(1, 'Spades'),
            Card(2, 'Clubs'),
            Card(3, 'Diamonds'),
            Card(4, 'Hearts'),
            Card(0, None)
        ]
        test_state.newCards(hand)
        test_state.discardCards([Card(1, 'Spades')])
        self.assertEqual(test_state.played_cards, {})
        hand.remove(Card(1, 'Spades'))
        self.assertEqual(test_state.hand_cards, hand)

        #Confirm can only discard cards actually in your hand
        with self.assertRaises(Exception):
            test_state.discardCards([Card(1, 'Spades')])

        #Confirm can only discard correct number of cards
        with self.assertRaises(Exception):
            test_state.discardCards([Card(2, 'Clubs'), Card(3, 'Diamonds')])
コード例 #17
0
    def testPickupRulesCheck(self):
        """Confirm we check the discard pile size for pickups"""
        test_state = ClientState(ruleset=None)
        test_state.played_cards[1] = [
            Card(1, 'Clubs'),
            Card(1, 'Clubs'),
            Card(1, 'Clubs')
        ]
        test_state.played_cards[4] = [
            Card(4, 'Clubs'),
            Card(4, 'Clubs'),
            Card(0, None)
        ]

        prepared_cards = {}
        prepared_cards[5] = [Card(5, 'Clubs'), Card(5, 'Clubs')]

        #confirm too small pile disallowed
        test_state.discard_info = (Card(5, 'Hearts'), 6)
        with self.assertRaises(Exception):
            test_state.pickupPileRuleCheck(prepared_cards)
コード例 #18
0
    def testPlayCards(self):
        """Confirm playCards transfers cards from hand to visible"""
        test_state = ClientState(ruleset=None)
        hand = [
            Card(1, 'Spades'),
            Card(2, 'Clubs'),
            Card(3, 'Diamonds'),
            Card(4, 'Hearts'),
            Card(4, 'Spades'),
            Card(0, None)
        ]
        test_state.newCards(hand)
        self.assertEqual(test_state.hand_cards, hand)

        #Confirm can't play cards we don't have (even if we have some)
        with self.assertRaises(Exception):
            test_state.playCards(
                {1: [Card(1, 'Spades'),
                     Card(1, 'Spades'),
                     Card(0, None)]})
        #Confirm failed play didn't edit hand
        self.assertEqual(test_state.hand_cards, hand)

        #Confirm can't play illegal move
        with self.assertRaises(Exception):
            test_state.playCards({1: [Card(1, 'Spades')]})
        #Confirm failed play didn't edit hand
        self.assertEqual(test_state.hand_cards, hand)

        #Confirm legal play is allowed and edits played cards and hand properly
        test_state.newCards([Card(1, 'Spades'), Card(0, None)])
        test_state.playCards(
            {1: [Card(1, 'Spades'),
                 Card(1, 'Spades'),
                 Card(0, None)]})
        self.assertEqual(
            test_state.played_cards,
            {1: [Card(1, 'Spades'),
                 Card(1, 'Spades'),
                 Card(0, None)]})
        hand.remove(Card(1, 'Spades'))
        self.assertEqual(test_state.hand_cards, hand)

        #Confirm second play adds to the played cards properly
        test_state.playCards(
            {4: [Card(4, 'Hearts'),
                 Card(4, 'Spades'),
                 Card(2, 'Clubs')]})
        self.assertEqual(
            test_state.played_cards, {
                1: [Card(1, 'Spades'),
                    Card(1, 'Spades'),
                    Card(0, None)],
                4: [Card(4, 'Hearts'),
                    Card(4, 'Spades'),
                    Card(2, 'Clubs')]
            })
        hand.remove(Card(4, 'Hearts'))
        hand.remove(Card(4, 'Spades'))
        hand.remove(Card(2, 'Clubs'))
        self.assertEqual(
            test_state.hand_cards,
            [Card(3, 'Diamonds'), Card(0, None)])
コード例 #19
0
 def Network_discard(self, data):
     card_list = [Card.deserialize(c) for c in data["cards"]]
     self._server.discardCards(card_list)
     self._server.Send_discardInfo()
     self._server.nextTurn()
コード例 #20
0
 def getDiscardInfo(self):
     """Provides the top card and size of the discard pile as a tuple"""
     top_card = Card(0, None, 0)  # If pile is empty we still need a default card since None doesn't serialize
     if len(self.discard_pile) > 0:
         top_card = self.discard_pile[-1]
     return [top_card, len(self.discard_pile)]
コード例 #21
0
outline_width = 8 * scale
no_outline_color = (-1, -1, -1)  # flags there is no outline.
outline_colors = (no_outline_color, Yellow, Green, Bright_Green, Bright_Blue,
                  Bright_Blue, Gray, Gray, Red, Bright_Red)
'''
No_outline_color indicates clickable image (usually a card) not selected or prepared.
Yellow indicates not selected, but mouse is over clickable image.
green elements indicate card is 'selected'
bright green indicates mouse is over card,
blue indicates card is prepared (for play).
Since cannot change prepared cards status with mouse, don't highlight those cards when mouse is over them...
'''
# load image of back of card, and scale it.
Back_Img = pygame.image.load(
    os.path.join('bundle_data', 'cardimages', 'cardBack.png'))
Back_Img = pygame.transform.rotozoom(Back_Img, 0, scale)

# Load images for full deck of cards:
suit_letter = 'N'  # this doesn't distinguish between red & black Jokers
temp_deck = Card.getStandardDeck(0)
card_images = {}
card_images['0N'] = pygame.image.load(
    os.path.join('bundle_data', 'cardimages', 'card0N.png'))
for card in temp_deck:
    suit_letter = card.suit[0]
    image_index = str(card.number) + suit_letter
    card_string = 'card' + image_index
    image_file = os.path.join('bundle_data', 'cardimages',
                              card_string + '.png')
    card_images[image_index] = pygame.image.load(image_file)
コード例 #22
0
    def test_PickupCheck(self):
        """Confirm that canPickup works"""
        played_cards = {}
        prepared_cards = {}

        #confirm with meld and valid cards, works
        played_cards[1] = [
            Card(1, 'Clubs'),
            Card(1, 'Clubs'),
            Card(1, 'Clubs')
        ]
        played_cards[4] = [Card(4, 'Clubs'), Card(4, 'Clubs'), Card(0, None)]
        prepared_cards[5] = [Card(5, 'Clubs'), Card(5, 'Clubs')]
        self.assertTrue(
            Rules.canPickupPile(Card(5, 'Hearts'), prepared_cards,
                                played_cards, 0))

        #confirm without valid cards, fails
        prepared_cards[5] = [Card(5, 'Clubs')]
        with self.assertRaises(Exception):
            Rules.canPickupPile(Card(5, 'Hearts'), prepared_cards,
                                played_cards, 0)

        prepared_cards[5] = [Card(5, 'Clubs'), Card(2, 'Clubs')]
        with self.assertRaises(Exception):
            Rules.canPickupPile(Card(5, 'Hearts'), prepared_cards,
                                played_cards, 0)

        #confirm without meld, need to meet meld requirement
        prepared_cards[5] = [Card(5, 'Clubs'), Card(5, 'Clubs')]
        played_cards = {}
        with self.assertRaises(Exception):
            Rules.canPickupPile(Card(5, 'Hearts'), prepared_cards,
                                played_cards, 0)

        #confirm meld requirement can INCLUDE top card
        prepared_cards[6] = [
            Card(2, 'Clubs'),
            Card(6, 'Diamonds'),
            Card(6, 'Diamonds'),
            Card(6, 'Diamonds')
        ]
        self.assertTrue(
            Rules.canPickupPile(Card(5, 'Hearts'), prepared_cards,
                                played_cards, 0))

        #confirm 3s can't be picked up
        with self.assertRaises(Exception):
            Rules.canPickupPile(Card(3, 'Hearts'), prepared_cards,
                                played_cards, 0)

        #confirm wilds can't be picked up
        with self.assertRaises(Exception):
            Rules.canPickupPile(Card(0, None), prepared_cards, played_cards, 0)
コード例 #23
0
    def test_PlayCheck(self):
        """Confirm canPlay works"""
        played_cards = {}
        prepared_cards = {}

        #confirm initial meld requires minimum point score
        prepared_cards[1] = [
            Card(1, 'Hearts'),
            Card(1, 'Hearts'),
            Card(1, 'Hearts')
        ]
        with self.assertRaises(Exception):
            Rules.canPlay(prepared_cards, played_cards, 0)

        #confirm that incomplete sets rejected from meld
        prepared_cards[5] = [Card(5, 'Spades'), Card(5, 'Spades')]
        with self.assertRaises(Exception):
            Rules.canPlay(prepared_cards, played_cards, 0)

        #confirm too many wilds are rejected from meld
        prepared_cards[5].append(Card(5, 'Hearts'))
        prepared_cards[10] = [
            Card(10, 'Clubs'),
            Card(10, 'Clubs'),
            Card(0, None),
            Card(2, 'Diamonds')
        ]
        with self.assertRaises(Exception):
            Rules.canPlay(prepared_cards, played_cards, 0)

        prepared_cards[10].remove(Card(0, None))
        self.assertTrue(Rules.canPlay(prepared_cards, played_cards, 0))
        played_cards = Rules.combineCardDicts(played_cards, prepared_cards)
        prepared_cards = {}

        #confirm that incomplete sets rejected from add ons
        prepared_cards[6] = [Card(6, 'Spades'), Card(6, 'Spades')]
        with self.assertRaises(Exception):
            Rules.canPlay(prepared_cards, played_cards, 0)

        prepared_cards[6].append(Card(6, 'Clubs'))
        self.assertTrue(Rules.canPlay(prepared_cards, played_cards, 0))
        played_cards = Rules.combineCardDicts(played_cards, prepared_cards)
        prepared_cards = {}

        #confirm that threes can't be played
        prepared_cards[3] = [
            Card(3, 'Spades'),
            Card(3, 'Diamonds'),
            Card(0, None)
        ]
        with self.assertRaises(Exception):
            Rules.canPlay(prepared_cards, played_cards, 0)
        prepared_cards = {}

        #confirm too many wilds total rejected from add ons
        prepared_cards[10] = [Card(2, 'Clubs')]
        with self.assertRaises(Exception):
            Rules.canPlay(prepared_cards, played_cards, 0)

        prepared_cards[10].append(Card(10, 'Clubs'))
        self.assertTrue(Rules.canPlay(prepared_cards, played_cards, 0))
        played_cards = Rules.combineCardDicts(played_cards, prepared_cards)
        prepared_cards = {}

        #confirm wild only additions work
        prepared_cards[5] = [Card(0, None)]
        self.assertTrue(Rules.canPlay(prepared_cards, played_cards, 0))
コード例 #24
0
    def test_GoneOut(self):
        """Confirm detection of going out works
        
        remember goneOut only called when player has no cards
        """
        played_cards = {}

        #confirm no caniestas doesn't go out
        played_cards[1] = [
            Card(1, 'Spades'),
            Card(1, 'Spades'),
            Card(1, 'Spades'),
            Card(1, 'Spades'),
            Card(1, 'Hearts'),
            Card(1, 'Spades')
        ]
        played_cards[11] = [
            Card(11, 'Clubs'),
            Card(11, 'Clubs'),
            Card(11, 'Clubs'),
            Card(11, 'Clubs'),
            Card(11, 'Clubs'),
            Card(11, 'Clubs')
        ]

        self.assertFalse(Rules.goneOut(played_cards))

        #confirm multiple cleans doesn't go out
        played_cards[1].append(Card(1, 'Diamonds'))
        played_cards[11].append(Card(11, 'Spades'))
        self.assertFalse(Rules.goneOut(played_cards))

        #confirm multiple dirties doesn't go out
        played_cards[1].append(Card(0, None))
        played_cards[11].append(Card(0, None))
        self.assertFalse(Rules.goneOut(played_cards))

        #confirm clean and dirty is out
        played_cards[1].remove(Card(0, None))
        self.assertTrue(Rules.goneOut(played_cards))
コード例 #25
0
    def test_Score(self):
        """Confirm scoring works"""
        played_cards = {}
        hand_cards = []
        went_out = True

        #confirm that a set of played cards that went out is correct
        played_cards[1] = [
            Card(1, 'Spades'),
            Card(1, 'Spades'),
            Card(1, 'Spades'),
            Card(1, 'Spades'),
            Card(1, 'Hearts'),
            Card(1, 'Spades')
        ]
        played_cards[11] = [
            Card(11, 'Clubs'),
            Card(11, 'Clubs'),
            Card(11, 'Clubs'),
            Card(11, 'Clubs'),
            Card(11, 'Clubs'),
            Card(11, 'Clubs')
        ]
        self.assertEqual(Rules.scoreRound(played_cards, hand_cards, went_out),
                         250)

        #confirm that a same set of cards and a hand of just black threes is only different by went_out bonus
        went_out = False
        hand_cards = [Card(3, 'Clubs')]
        self.assertEqual(Rules.scoreRound(played_cards, hand_cards, went_out),
                         150)

        #confirm cards left in hand are counted against properly
        hand_cards = [Card(5, 'Spades'), Card(0, None), Card(10, 'Clubs')]
        self.assertEqual(Rules.scoreRound(played_cards, hand_cards, went_out),
                         85)

        #confirm caniesta bonuses are correct:
        #confirm clean bonus is 500 and still counts cards in caniesta
        played_cards[1].append(Card(1, 'Clubs'))
        self.assertEqual(Rules.scoreRound(played_cards, hand_cards, went_out),
                         600)
        #confirm dirty bonus is 300 and counts cards in caniesta
        played_cards[11].append(Card(2, 'Hearts'))
        self.assertEqual(Rules.scoreRound(played_cards, hand_cards, went_out),
                         920)

        #confirm that red 3 penalty is 500
        hand_cards.append(Card(3, 'Diamonds'))
        self.assertEqual(Rules.scoreRound(played_cards, hand_cards, went_out),
                         420)
コード例 #26
0
 def test_Serialize(self):
     """Confirm that cards serialize to a tuple properly"""
     #Confirm its number than suit
     test_card = Card(3, 'Hearts')
     self.assertEqual(test_card.serialize(), (3, 'Hearts'))
コード例 #27
0
 def test_GetColor(self):
     """Confirm that card class correctly identifies colors"""
     #Jokers have no color
     test_card = Card(0, None)
     self.assertEqual(test_card.getColor(), None)
     #Spades are "Black"
     test_card = Card(2, 'Spades')
     self.assertEqual(test_card.getColor(), "Black")
     #Clubs are "Black"
     test_card = Card(3, 'Clubs')
     self.assertEqual(test_card.getColor(), 'Black')
     #Hearts are "Red"
     test_card = Card(5, 'Hearts')
     self.assertEqual(test_card.getColor(), 'Red')
     #Diamonds are "Red"
     test_card = Card(6, 'Diamonds')
     self.assertEqual(test_card.getColor(), "Red")
コード例 #28
0
 def Network_discardInfo(self, data):
     top_card = Card.deserialize(data["top_card"])
     size = data["size"]
     self._state.updateDiscardInfo(top_card, size)
コード例 #29
0
def singleDeck(n):
    """return a single deck of the correct type, n designates which deck of the numDecks to be used"""
    return Card.getJokerDeck(n)
コード例 #30
0
 def Network_buyingOpportunity(self, data):
     if  self._state.discard_info[1]  > 0:
         self.buying_opportunity = True
         self.note = "The {0} is for sale, Do you want to buy it? [y/n]".format(Card.deserialize(data["top_card"]))