class TestHand(unittest.TestCase): hand = Hand() def test_handAppend(self): self.hand.append("HA") self.assertEqual("HA", self.hand.playACard()) def test_handSize(self): self.hand.clear() self.hand.append("HA") self.assertEqual(1,self.hand.size()) def test_handCopySameCard(self): self.hand.clear() self.hand.append("HA") newHand = self.hand.copy() self.assertEqual("HA", newHand.playACard()) def test_handCopyDiff(self): newHand = self.hand.copy() self.assertNotEqual(self.hand,newHand) def test_handCopyDiffHand(self): newHand = self.hand.copy() newHand.append("H3") self.assertNotEqual(self.hand.hand,newHand.hand) def test_handCopyDiffHand(self): self.hand.clear() newHand = self.hand.copy() newHand.append("H3") self.assertEqual(0,self.hand.size())
def dealCards(self,noOfCards,noOfHands): hands=[] allCards = False if noOfCards == 0: noOfCards = math.floor(self.deck.size()/noOfHands) allCards = True for handIndex in range(0,noOfHands): hand = Hand() for counter in range(noOfCards): hand.append(self.deck.dealACard()) hands.append(hand.copy()) if allCards: counter = 0 while self.deck.size() > 0: hands[counter].append(self.deck.dealACard()) counter = (counter +1) % noOfHands return hands
def test_hasTwoGroupsOfSuitsSecond(self): handOfCards = ["C2", "D3", "C6", "C9", "D4", "D6", "D8"] hand = Hand() hand.hand = handOfCards groupBySuit = self.winningRummy.getGroupsOfSuits(hand) compareHandOne = Hand() compareHandOne = ["C2", "C6", "C9"] compareHandTwo = Hand() compareHandTwo.hand = ["D3", "D4", "D6", "D8"] self.assertEqual(["D3", "D4", "D6", "D8"], groupBySuit[1].getHand())
def test_hasFourOfSameSuit(self): handOfCards = ["S2", "D3", "C6", "S9", "D9", "D6", "D8"] hand = Hand() hand.hand = handOfCards groupBySuit = self.winningRummy.getGroupsOfSuits(hand) compareHand = Hand() compareHand.hand = ["D3", "D4", "D6", "D8"] self.assertEqual(["D3", "D6", "D8", "D9"], groupBySuit[1].getHand())
def test_hasRunofThree(self): handOfCards = ["C2", "D6", "CA", "H9", "D4", "D5", "SK"] hand = Hand() hand.hand = handOfCards handsOfRun = self.winningRummy.getRuns(hand) compareHand = Hand() compareHand.hand = ["D4", "D5", "D6"] self.assertEqual(["D4", "D5", "D6"], handsOfRun[1].getHand())
class Deck(): suits = {"H": "Hearts", "D": "Diamonds", "S": "Spades", "C": "Clubs"} faces = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"] deckOfCards = Hand() userHand = 0 # Function: generateDeck # Description: This function generates a 52 pack of cards, with four suites and 13 playing cards Ace to King. # The cards are returned in an ordered deck def generateDeck(self): for suit in self.suits.keys(): for face in self.faces: self.deckOfCards.append(suit + face) # Function: shuffleDeck # Description: A set of cards is supplied and shuffled, randomly ordered. def shuffleCards(self): random.shuffle(self.deckOfCards.getHand()) # Method: trentineSmall # Description: An Italian set of cards Tertine can be only 40 cards or 52. The small set has no "8","9" or "10". It # still has ace to seven and jack, queen and king. def trentineSmall(self): faces.remove("8") faces.remove("9") faces.remove("10") # Function: dealACard # Description: Remove a card from a deck or hand of cards and return the card. def dealACard(self): if self.deckOfCards.size() == 0: self.generateDeck() self.shuffleCards() return self.deckOfCards.playACard() def append(self, card): self.deckOfCards.append(card) def show(self): return self.deckOfCards.getHand() def size(self): return self.deckOfCards.size()
def getGroupsOfSuits(self, hand): allGroupsOfSuites = [] groupOfSameSuite = Hand() hand.sort() previousSuit = "" for card in hand.getHand(): if card[0] == previousSuit: groupOfSameSuite.append(card) else: if groupOfSameSuite.size() > 0: allGroupsOfSuites.append(groupOfSameSuite.copy()) groupOfSameSuite.clear() groupOfSameSuite.append(card) previousSuit = card[0] allGroupsOfSuites.append(groupOfSameSuite.copy()) return allGroupsOfSuites
def getRuns(self, hand): allRuns = [] currentRun = Hand() previousSuit = "" previousFace = 0 hand.sort() hand.convertFacesToNumbers() for card in hand.getHand(): if card[0] == previousSuit and int(card[1:3]) == previousFace + 1: currentRun.append(card) else: if currentRun.size() > 1: currentRun.convertNumbersToFaces() allRuns.append(currentRun.copy()) currentRun.clear() currentRun.append(card) previousSuit = card[0] previousFace = int(card[1:3]) hand.convertNumbersToFaces() return allRuns
def test_getRunsFirst(self): handOfCards = ["C2", "D6", "CA", "H9", "D4", "D5", "SK"] hand = Hand() hand.hand = handOfCards self.assertEqual(["CA", "C2"], self.winningRummy.getRuns(hand)[0].getHand())
def test_getGroupsOfSuitsFirst(self): handOfCards = ["S2", "C5", "C6", "S9", "D4", "D6", "D8"] hand = Hand() hand.hand = handOfCards self.assertEqual(["C5", "C6"], self.winningRummy.getGroupsOfSuits(hand)[0].getHand())
def test_hasNoGroupsOfSuits(self): handOfCards = ["C2", "H3", "C6", "H9", "D4", "D6", "S8"] hand = Hand() hand.hand = handOfCards score = self.winningRummy.hasRummyScore(hand) self.assertEqual(0, score)
def test_hasNoRummy(self): handOfCards = ["C2", "D6", "CA", "H9", "D4", "D5", "D3"] hand = Hand() hand.hand = handOfCards self.assertFalse(self.winningRummy.hasRummy(hand, 0))
def test_hasRummyScore(self): handOfCards = ["C2", "D6", "CA", "C9", "D4", "D5", "D3"] hand = Hand() hand.hand = handOfCards self.assertEqual(7, self.winningRummy.hasRummyScore(hand))
def test_hasRunofFour(self): handOfCards = ["C2", "D6", "CA", "H9", "D4", "D5", "D3"] hand = Hand() hand.hand = handOfCards handsOfRun = self.winningRummy.getRuns(hand) self.assertEqual(["D3", "D4", "D5", "D6"], handsOfRun[1].getHand())