예제 #1
0
파일: Player.py 프로젝트: berryhill/VP
 def submit_play(self, temp=None):
     temp = DiscardPile()
     for k in range(len(self.holds)):
         if self.holds[k] == 0:
             temp.append(self.hand.get_card(k))
             self.hand[k] = Card(-1, -1)
     # self.clear_holds()
     return temp
예제 #2
0
파일: Game.py 프로젝트: mrijke/pyuno
 def playerDrawCard(self, player, amount):
     try:
         player.giveCard(self._deck.drawCard(amount))
     except EmptyDeckException:
         self._discardPile.shuffle()
         self._deck = self._discardPile
         self._discardPile = DiscardPile()
         print "Deck was empty, shuffled discard pile as new deck."
         player.giveCard(self._deck.drawCard(amount))
예제 #3
0
파일: Game.py 프로젝트: mrijke/pyuno
 def __init__(self):
     ''' Creates a new Game. '''
     self._playerList = []
     self._deck = Deck()
     self._deck.shuffle()
     self._discardPile = DiscardPile()
     self._currentPlayer = 0
     self._currentCard = self._deck.drawCard()
     self._blankCards = {
             'blue' : Card('blue', 'blank'),
             'yellow' : Card('yellow', 'blank'),
             'red'    : Card('red', 'blank'),
             'green'  : Card('green', 'blank')}
예제 #4
0
    def __init__(self, name, deck_name, game_config):
        self.deck = Deck(deck_name)

        self.home_pile = HomePile(game_config.home_pile_size)
        self.cell_cards = CellCards(self.home_pile)
        self.stock_pile = StockPile()
        self.right_hand = RightHand()
        self.discard_pile = DiscardPile()

        self.home_pile.take_from(self.deck)
        self.cell_cards.take_from(self.deck)
        self.stock_pile.take_from(self.deck)

        self.set_locations()

        # Move cards to their proper locations
        self.home_pile.calibrate()
        self.cell_cards.calibrate()
        self.stock_pile.calibrate()

        # Start out with cell cards face down
        for cell in self.cell_cards.each_cell():
            cell.face_down()

        #
        self.selection = Selection()

        self.drawables = [self.home_pile,
                          self.cell_cards,
                          self.stock_pile,
                          self.discard_pile,
                          self.right_hand,
                          self.selection]

        self.clickables = [self.home_pile,
                           self.cell_cards,
                           self.discard_pile]

        self.updateables = []

        self.xxxcount = 0

        self.score = 0

        self.cards_in_transit = []

        self.name = name

        louie.connect(self.card_grabbed, Card.grabbed)
        louie.connect(self.card_thrown, Card.thrown)
        louie.connect(self.home_emptied, HomePile.emptied)
예제 #5
0
class Player:
    """ A player in the game. """

    finished = louie.Signal()

    def __init__(self, name, deck_name, game_config):
        self.deck = Deck(deck_name)

        self.home_pile = HomePile(game_config.home_pile_size)
        self.cell_cards = CellCards(self.home_pile)
        self.stock_pile = StockPile()
        self.right_hand = RightHand()
        self.discard_pile = DiscardPile()

        self.home_pile.take_from(self.deck)
        self.cell_cards.take_from(self.deck)
        self.stock_pile.take_from(self.deck)

        self.set_locations()

        # Move cards to their proper locations
        self.home_pile.calibrate()
        self.cell_cards.calibrate()
        self.stock_pile.calibrate()

        # Start out with cell cards face down
        for cell in self.cell_cards.each_cell():
            cell.face_down()

        #
        self.selection = Selection()

        self.drawables = [self.home_pile,
                          self.cell_cards,
                          self.stock_pile,
                          self.discard_pile,
                          self.right_hand,
                          self.selection]

        self.clickables = [self.home_pile,
                           self.cell_cards,
                           self.discard_pile]

        self.updateables = []

        self.xxxcount = 0

        self.score = 0

        self.cards_in_transit = []

        self.name = name

        louie.connect(self.card_grabbed, Card.grabbed)
        louie.connect(self.card_thrown, Card.thrown)
        louie.connect(self.home_emptied, HomePile.emptied)

    def draw(self, surface):
        for drawable in self.drawables:
            drawable.draw(surface)

        for card in self.cards_in_transit:
            card.draw(surface)

    def set_locations(self):
        """ Position cards """
        some_card = self.home_pile.top_card()
        card_width, card_height = some_card.rect.width, some_card.rect.height
        left_margin, top_margin = 15, 450
        hand_top_margin = top_margin + card_height + 10

        x, y = left_margin, top_margin

        # Top row
        self.home_pile.move_to(x, y)
        x += card_width * 1.7
        distance_between = card_width * 1.3
        self.cell_cards.move_to(x, y, distance_between)

        x = left_margin + card_width * 0.2
        y = hand_top_margin

        # Bottom row
        self.stock_pile.move_to(x, y)
        x += card_width * 1.5
        self.discard_pile.move_to(x, y)
        x += card_width * 1.5
        self.right_hand.move_to(x, y, 40)


    def update(self):
        for updateable in self.updateables:
            updateable.update()

        for card in self.cards_in_transit:
            card.update()

    def handle(self, event):
        """ Handle events that the player knows about. """
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                self.handle_left_mouse_down(event)

            elif event.button == 3:
                self.handle_right_mouse_down(event)

        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                self.handle_space_bar()

    def handle_left_mouse_down(self, event):
        """ Handle a left click. """
        x, y = event.pos[0], event.pos[1]

        for clickable in self.clickables:
            card = clickable.get_card(x, y)
            if card is not None:
                if not self.selection.empty() and \
                       self.selection.card == card:
                    # Clear the selection if the card is already selected
                    self.selection.clear()
                else:
                    # Otherwise select the card
                    self.selection.set(card, clickable)
                return

        # Stock pile clicks
        if self.stock_pile.contains(x, y):
            self.deal_card()

    def handle_right_mouse_down(self, event):
        """ Handle a right click. """
        self.deal_card()

    def handle_space_bar(self):
        """ Handle a space bar keypress. """
        self.deal_card()

    def deal_card(self):
        """ Shuffle from stock_pile to discard_pile
            Place in hand if count < 3,
            otherwise place in discard pile, count = 0 """

        if self.xxxcount != 0 and self.stock_pile.cards.empty():
            self.xxxcount = 3

        self.xxxcount += 1
        if self.xxxcount == 4:
            self.discard_pile.take_from(self.right_hand)
            self.discard_pile.calibrate()
            self.xxxcount = 0

            if self.has_selection() and \
               self.selection.home == self.discard_pile:
                # Clear the selection if it's the discard pile
                self.clear_selection()
        else:
            if self.stock_pile.cards.empty():
                self.stock_pile.take_from(self.discard_pile.cards)
                self.stock_pile.calibrate()

            self.right_hand.take_from(self.stock_pile.cards)
            self.right_hand.calibrate()

    def home_emptied(self):
        louie.send(Player.finished)

    def inc_score(self):
        self.score += 1

    def get_score(self):
        """ Returns the total score """
        return self.num_good_cards() - self.num_bad_cards() * 2

    def num_good_cards(self):
        """ Returns the number of cards put out """
        return self.score

    def num_bad_cards(self):
        """ Returns the number of cards still in the home pile """
        return self.home_pile.cards.num_cards()

    def card_thrown(self, card):
        self.cards_in_transit.append(card)

    def card_grabbed(self, card):
        self.cards_in_transit.remove(card)

    def get_selection(self):
        """ Returns the current selection. """
        return self.selection

    def has_selection(self):
        """ Returns true if there is a card selected. """
        return not self.selection.empty()

    def clear_selection(self):
        self.selection.clear()

    def get_name(self):
        return self.name

    def flip_cell(self, cell_index):
        self.cell_cards.get_cell(cell_index).flip()
예제 #6
0
파일: Game.py 프로젝트: mrijke/pyuno
class Game(object):
    '''
    This represents a game of UNO. It keeps track of a list of the players currently involved in the game.
    '''

    def __init__(self):
        ''' Creates a new Game. '''
        self._playerList = []
        self._deck = Deck()
        self._deck.shuffle()
        self._discardPile = DiscardPile()
        self._currentPlayer = 0
        self._currentCard = self._deck.drawCard()
        self._blankCards = {
                'blue' : Card('blue', 'blank'),
                'yellow' : Card('yellow', 'blank'),
                'red'    : Card('red', 'blank'),
                'green'  : Card('green', 'blank')}
            
    def getDeck(self):
        ''' Returns the deck used for this game '''
        return self._deck
    
    def addPlayerToGame(self, name):
        ''' Add a human player to the game '''
        self._playerList.append(Player(name, self._deck.draw7Cards()))
        
    def addAIPlayerToGame(self, name):
        ''' Add an AI player to the game '''
        self._playerList.append(AIPlayer(name, self._deck.draw7Cards()))
    
    def getCurrentCard(self):
        ''' Returns the current card in play for this game '''
        return self._currentCard
    
    def isFinished(self):
        ''' Returns True if the game is finished, that is if one player has 0 cards '''
        for player in self._playerList:
            if len(player.getCurrentHand()) == 0:
                return True
        return False
    
    def testMove(self, card):
        ''' Test to see if the Card card is a valid move given the current card in play '''
        if not self.currentPlayer().testMove(self._currentCard, card):
                raise InvalidMoveException
        
    def hasUno(self, player):
        ''' Returns True if the Player player has UNO, that is has only one card left '''
        return len(player.getCurrentHand()) == 1

    def nextPlayerHasUno(self):
        ''' Returns true if the next player has UNO (to help the AI) '''
        try:
            return len(self._playerList[self._currentPlayer-1].getCurrentHand) == 1
        except:
            return len(self._playerList[0].getCurrentHand()) == 1
        
    def applySpecial(self,card, color=None):
        ''' Applies the special ability of Card card. '''
        ability = card.getData()
        if ability == 'skip':
            self._currentPlayer+=1
            if self._currentPlayer >= len(self._playerList):
                self._currentPlayer = 0
            return True
        elif ability == 'reverse':
            self._playerList = self._playerList[::-1] #reverse the list
            if len(self._playerList) > 2: 
                self._currentPlayer = abs(self._currentPlayer-len(self._playerList)) #fix order
            if self._currentPlayer > len(self._playerList)-1:
                self._currentPlayer=0
            return len(self._playerList) == 2
        elif ability == 'draw 2':
            try:
                nextplayer = self._playerList[self._currentPlayer+1]
                self.playerDrawCard(nextplayer, 2)
                self._currentPlayer+=1
            except:
                self.playerDrawCard(self._playerList[0], 2)
                self._currentPlayer = 0
            return True
        elif ability == 'wild' or ability == 'wild draw four':
            self._currentCard = self._blankCards[color]
            if ability == "wild draw four":
                try:
                    nextplayer = self._playerList[self._currentPlayer+1]
                    self.playerDrawCard(nextplayer, 4)
                    self._currentPlayer+=1
                except:
                    self.playerDrawCard(self._playerList[0], 4)
                    self._currentPlayer=0
            return True

    def playerDrawCard(self, player, amount):
        try:
            player.giveCard(self._deck.drawCard(amount))
        except EmptyDeckException:
            self._discardPile.shuffle()
            self._deck = self._discardPile
            self._discardPile = DiscardPile()
            print "Deck was empty, shuffled discard pile as new deck."
            player.giveCard(self._deck.drawCard(amount))

    def doMove(self, player, card, color=None):
        ''' 
        Let the Player player play the Card card.
        First, test if the move is valid. Then apply any special abilities the card may have.
        After that, remove the card from the player's Hand.
        Finally, update the currentPlayer variable
        '''
        self.testMove(card)
        nextPlayer = True
        if card.isSpecial(): nextPlayer = self.applySpecial(card, color)
        player.removeFromHand(card)
        self._discardPile.addCard(card)
        if not card.isWild(): self._currentCard = card
        if nextPlayer:
            self._currentPlayer += 1
            if self._currentPlayer >= len(self._playerList):
                self._currentPlayer=0        
        
    def currentPlayer(self):
        ''' Returns the Player whose turn it is '''
        return self._playerList[self._currentPlayer] 
    
    def getNextPlayerName(self):
        ''' Return the name of the player whose turn it is next '''
        try:
            return self._playerList[self._currentPlayer-1].getName()
        except IndexError:
            return self._playerList[self._currentPlayer].getName()
        
    def playerPasses(self):
        ''' Update the currentPlayer variable when a Player passes '''
        self._currentPlayer += 1
        if self._currentPlayer >= len(self._playerList):
            self._currentPlayer = 0