class TestSort(unittest.TestCase): def setUp(self): self.sorter = Sort() def test_bubble(self): a =[9,1,7,6,5,4,3,2] self.assertListEqual(self.sorter.bubble(a), [1,2,3,4,5,6,7,9]) def test_qsort(self): a=[6,5,7,9,8] self.sorter.qsort(0, len(a) - 1, a) self.assertListEqual(a, [5,6,7,8,9])
def performActions(player, cardsToBeat, cardsToPlay): # reset cardsToPlay from previous player for card in cardsToPlay: player.addCardToHand(card) cardsToPlay[:] = [] Sort.qsort(player.hand, compareCard) print "To start putting cards down, type 'choose' and 'complete' to finish your turn" print "Or if you can't beat the current cards, just enter 'pass'\n" printAllRelevantHands(player, cardsToPlay, cardsToBeat) action = raw_input("Enter what you want to do (type 'options' for list of actions available): ") # let user decide all the things to do this turn while doAction(player, action, cardsToPlay, cardsToBeat): action = raw_input("Enter what you want to do (type 'options' for list of actions available): ") printAllRelevantHands(player, cardsToPlay, cardsToBeat) validHand = isAValidHand(cardsToPlay) # no cards currently on the table if len(cardsToBeat) == 0: # note that the player is participating in the round player.passed = False if len(cardsToPlay) == 0 or not validHand: printPause("You are the current leader!") printPause("You must play something valid!\n") return performActions(player, cardsToBeat, cardsToPlay) else: # set current hand on table return list(cardsToPlay) else: # have to pass or beat the leader typeInPlay = typeOfPlay(cardsToBeat) if len(cardsToPlay) == 0: printPause("You are choosing to pass\n") player.passed = True return cardsToBeat elif not validHand or not handCanBePlayed(cardsToPlay, typeInPlay, cardsToBeat): printPause("The hand you want to play is not valid!") print "You need to have a straight of", typeInPlay["straight"] print "and a \"of a kind\" of", typeInPlay["ofAKind"] print "to match the leader" printPause("") return performActions(player, cardsToBeat, cardsToPlay) else: # set current hand on table player.passed = False return list(cardsToPlay)
def sort(self, cmpfn): Sort.qsort(self.cards, cmpfn)
def sortHand(self, cmpfn): Sort.qsort(self.hand, compareCard)
def typeOfPlay(cardsPlayed): Sort.qsort(cardsPlayed, compareCard) return {"straight" : numOfStraight(cardsPlayed), "ofAKind" : numOfAKind(cardsPlayed)}