示例#1
0
class GameController(object):
    
    START_MONEY = 100
    START_BET = 1
    GAMESTATES = {
                  1 : "Choose bet and Deal.",
                  2 : "Choose cards you want to hold.",
                  3 : "Round ended.",
                  4 : "Ran out of money. GAME OVER."
                  }
    WINFACTORS = {
                  0 : "Has nothing.",
                  1 : "Has a pair.",
                  2 : "Has two pairs.",
                  3 : "Has three of a kind.",
                  4 : "Has straight.",
                  5 : "Has flush.",
                  6 : "Has full house.",
                  7 : "Has four of a kind.",
                  8 : "Has straight flush."
                  }
    
    def __init__(self, view):
        '''Constructor'''
        self._view = view
        self._winchecker = WinChecker(numberOfJokers = 0)
        self.newGame()
    
    def _drawAFullHand(self):
        '''Draw five cards from the deck to the hand.'''
        self._hand = Hand()
        for x in xrange(5):
            self._hand.add(self._deck.draw())

    def newGame(self):
        '''Set the game to a starting state'''
        self._setGamestate(1)
        self._setBet(self.START_BET)
        self._setMoney(self.START_MONEY)
        
        self._view.enableBetting()
        self._view.setCardsUnactive()
    
    def dealClicked(self):
        '''Things we do when deal -button is clicked. Action depends on the gamestate'''
        
        if self._gamestate == 3:
            self._setGamestate(1)
        
        if self._gamestate == 2:
            self._replaceNotHoldedCards()
            self._setUICards()
            self._view.setCardsUnactive()
            self._setMoney(self._money + self._bet * self._getHandWinFactor(self._hand))
            self._view.enableBetting()
            
            if self._money <= 0:
                #Ran out of money -> GAME OVER
                self._setGamestate(4)
            else:
                self._setGamestate(3)    
            
        elif self._gamestate == 1:
            
            if self._bet > self._money:
                self._setBet(self._money)
            
            self._view.disableBetting()
            self._view.setCardsUnactive()
            
            self._setMoney(self._money - self._bet)
            
            self._deck = Deck(0)
            self._drawAFullHand()
            
            self._setUICards()
            self._setGamestate(2)

    def betUpByOne(self):
        '''Increases the bet by one. Can't be greater than the money you have.'''
        if self._money > self._bet:
            self._setBet(self._bet + 1)
    
    def betDownByOne(self):
        '''Reduces the bet by one. Can't go below one'''
        if self._bet > 1:
            self._setBet(self._bet - 1)

    def _setMoney(self,money):
        '''Updates the money on the user interface and on the controllerobject itself.'''
        self._money = money
        self._view.setMoney(money)
    
    def _setBet(self,bet):
        '''Updates the bet on the user interface and on the controllerobject itself.'''
        self._bet = bet
        self._view.setBet(bet)

    def _replaceNotHoldedCards(self):
        '''Replaces the cards from the hand that are not marked as "held" in the userinterface.'''
        holdedCards = self._view.getHoldedCards()
        notHoldedCards = range(1,6)
        
        for card in holdedCards:
            notHoldedCards.remove(card)
        
        for place in notHoldedCards:
            self._hand.replace(place, self._deck.draw())
        
    def _setGamestate(self,gamestate):
        '''Update the gamestate on the controller and on the statusbar of the userinterface'''
        
        if gamestate > len(self.GAMESTATES) or gamestate < 1:
            raise SyntaxError("No such gamestate")
        
        self._gamestate = gamestate
        
        if gamestate == 1:
            self._view.setStatus(self.GAMESTATES.get(gamestate) )
        else:
            self._view.setStatus(self.GAMESTATES.get(gamestate) + " " +
                                 self.WINFACTORS.get(self._getHandWinFactor(self._hand)) )
    def _setUICards(self):
        '''Update the cards on the userinterface to resemble the cards in the hand.'''
        cardList = []
        
        for card in self._hand:
            cardList.append( str(card.color()) + "\n" + str(card.number()) )
        
        self._view.setCards(cardList)

    def _getHandWinFactor(self,hand):
        '''Check if the hand has winning combination of cards and return a winfactor for that win.'''
        if self._winchecker.hasStraightFlush(hand):
            return 8
        elif self._winchecker.hasFourOfAKind(hand):
            return 7
        elif self._winchecker.hasFullHouse(hand):
            return 6
        elif self._winchecker.hasFlush(hand):
            return 5
        elif self._winchecker.hasStraight(hand):
            return 4
        elif self._winchecker.hasThreeOfAKind(hand):
            return 3
        elif self._winchecker.hasTwoPairs(hand):
            return 2
        elif self._winchecker.hasAPair(hand):
            return 1
        else:
            return 0
示例#2
0
class WinCheckerTest(unittest.TestCase):

    def setUp(self):
        self.hand = Hand()
        self.winchecker = WinChecker(0)  

    def tearDown(self):
        del self.hand
        del self.winchecker


    def test_hasAPair(self):
        self.hand.add( Card("X",1) )
        self.hand.add( Card("Y",1) )
        self.hand.add( Card("Z",5) )
        self.hand.add( Card("X",7) )
        self.hand.add( Card("Y",9) )
        
        self.assertTrue( self.winchecker.hasAPair(self.hand) )

    def test_hasNotAPair(self):
        self.hand.add( Card("X",1) )
        self.hand.add( Card("Y",3) )
        self.hand.add( Card("Z",5) )
        self.hand.add( Card("X",7) )
        self.hand.add( Card("Y",9) )
        
        self.assertFalse( self.winchecker.hasAPair(self.hand) )

    def test_hasTwoPairs(self):
        self.hand.add( Card("X",1) )
        self.hand.add( Card("Y",5) )
        self.hand.add( Card("Z",3) )
        self.hand.add( Card("Z",5) )
        self.hand.add( Card("Z",1) )
        
        self.assertTrue( self.winchecker.hasTwoPairs(self.hand))
    
    def test_hasNotTwoPairs(self):
        self.hand.add( Card("X",1) )
        self.hand.add( Card("Y",1) )
        self.hand.add( Card("Z",5) )
        self.hand.add( Card("X",7) )
        self.hand.add( Card("Y",9) )
        
        self.assertFalse( self.winchecker.hasTwoPairs(self.hand) )
    
    def test_hasThreeOfAKind(self):
        self.hand.add( Card("X",13) )
        self.hand.add( Card("Y",1) )
        self.hand.add( Card("Z",5) )
        self.hand.add( Card("Y",13) )
        self.hand.add( Card("Z",13) )
        
        self.assertTrue( self.winchecker.hasThreeOfAKind(self.hand))

    def test_hasNotThreeOfAKind(self):
        self.hand.add( Card("X",1) )
        self.hand.add( Card("Y",1) )
        self.hand.add( Card("Z",5) )
        self.hand.add( Card("X",7) )
        self.hand.add( Card("Y",9) )
        
        self.assertFalse( self.winchecker.hasThreeOfAKind(self.hand) )
        
    def test_hasStraight(self):
        self.hand.add( Card("X",6) )
        self.hand.add( Card("Y",4) )
        self.hand.add( Card("Z",5) )
        self.hand.add( Card("Y",3) )
        self.hand.add( Card("Z",2) )
        
        self.assertTrue( self.winchecker.hasStraight(self.hand))

    def test_hasNotStraight(self):
        self.hand.add( Card("X",6) )
        self.hand.add( Card("Y",4) )
        self.hand.add( Card("Z",5) )
        self.hand.add( Card("Y",10) )
        self.hand.add( Card("Z",2) )
        
        self.assertFalse( self.winchecker.hasStraight(self.hand))

    def test_hasFlush(self):
        self.hand.add( Card("X",6) )
        self.hand.add( Card("X",4) )
        self.hand.add( Card("X",5) )
        self.hand.add( Card("X",10) )
        self.hand.add( Card("X",2) )
        
        self.assertTrue( self.winchecker.hasFlush(self.hand))

    def test_hasNotFlush(self):
        self.hand.add( Card("X",6) )
        self.hand.add( Card("X",4) )
        self.hand.add( Card("Y",5) )
        self.hand.add( Card("X",10) )
        self.hand.add( Card("X",2) )
        
        self.assertFalse( self.winchecker.hasFlush(self.hand))
        
    def test_hasFullHouse(self):
        self.hand.add( Card("X",3) )
        self.hand.add( Card("Y",6) )
        self.hand.add( Card("Z",3) )
        self.hand.add( Card("Y",6) )
        self.hand.add( Card("X",3) )
        
        self.assertTrue( self.winchecker.hasFullHouse(self.hand))

    def test_hasNotFullHouse(self):
        self.hand.add( Card("X",3) )
        self.hand.add( Card("Y",7) )
        self.hand.add( Card("Z",3) )
        self.hand.add( Card("Y",6) )
        self.hand.add( Card("X",3) )
        
        self.assertFalse( self.winchecker.hasFullHouse(self.hand))
        
    def test_hasFourOfAKind(self):
        self.hand.add( Card("X",3) )
        self.hand.add( Card("Y",6) )
        self.hand.add( Card("Z",3) )
        self.hand.add( Card("V",3) )
        self.hand.add( Card("X",3) )
        
        self.assertTrue( self.winchecker.hasFourOfAKind(self.hand))
    
    def test_hasNotFourOfAKind(self):
        self.hand.add( Card("X",3) )
        self.hand.add( Card("Y",6) )
        self.hand.add( Card("Z",3) )
        self.hand.add( Card("V",6) )
        self.hand.add( Card("X",3) )
        
        self.assertFalse( self.winchecker.hasFourOfAKind(self.hand))
        
    def test_hasStraightFlush(self):
        self.hand.add( Card("X",6) )
        self.hand.add( Card("X",4) )
        self.hand.add( Card("X",5) )
        self.hand.add( Card("X",3) )
        self.hand.add( Card("X",2) )
        
        self.assertTrue( self.winchecker.hasStraightFlush(self.hand))

    def test_hasNotStraightFlush_NotFlush(self):
        self.hand.add( Card("X",6) )
        self.hand.add( Card("X",4) )
        self.hand.add( Card("X",5) )
        self.hand.add( Card("Y",3) )
        self.hand.add( Card("X",2) )
        
        self.assertFalse( self.winchecker.hasStraightFlush(self.hand))

    def test_hasNotStraightFlush_NotStraight(self):
        self.hand.add( Card("X",6) )
        self.hand.add( Card("X",4) )
        self.hand.add( Card("X",10) )
        self.hand.add( Card("X",3) )
        self.hand.add( Card("X",2) )
        
        self.assertFalse( self.winchecker.hasStraightFlush(self.hand))