else: return self.cards[i] def addCard(self, card): self.cards.append(card) def mostFrequentColor(self): freq={color.BLUE: 0, color.GREEN: 0, color.RED: 0, color.YELLOW: 0} for card in self.cards: if card.getColor() != color.BLACK: freq[card.getColor()] += 1 freqView = freq.items() freqView = sorted(freqView, key = lambda x: x[1]) return freqView[-1][0] def __str__(self): #cardsStr = [str(card) for card in self.cards] cardsStr = ['%2d: %s' % ( index + 1, card) for index, card in enumerate(self.cards)] #return ', '.join(cardsStr) return '\n'.join(cardsStr) if __name__ == '__main__': #TEST from discardpile import Discardpile from deck import Deck discardPile = Discardpile() #Build the initial (empty) discard pile deck = Deck(discardPile) #Build a deck with 108 cards h = Hand(deck) print(h) print(color.toString(h.mostFrequentColor()))
return players def showNoOfCardsInHand(players, idCurrentPlayer): for player in players: if player.getId() == idCurrentPlayer: continue cardsInHand = player.getHand().noOfCardsInHand() print('%s has %d cards' % (player.getName(), cardsInHand), end='; ') print() if __name__ == '__main__': colorama.init() #Initialize color output print(colorama.Back.WHITE + colorama.Fore.BLACK) #Background white, textcolor black clear_screen() #To make background white printCopyright() discardPile = Discardpile() #Build the initial (empty) discard pile deck = Deck(discardPile) #Build a deck with 108 cards players = makePlayers(deck) #Initialize players with deck noOfPlayers = len(players) idCurrentPlayer = randint(0, noOfPlayers-1) #ID of first player in first round discardPile.putCard(deck.drawCard()[0], players[idCurrentPlayer]) #Put first card at discard pile isClockwise = True #Direction of play. Can be changed by reverse card while True: #Game loop clear_screen() topCard = discardPile.getCardOnTop() currentPlayer = players[idCurrentPlayer] showNoOfCardsInHand(players, idCurrentPlayer) #Print how many cards the other players have print('Pile: %s\n' % topCard) #Show top card on discard pile cardToPlay = currentPlayer.takeTurn(deck, topCard, noOfPlayers) if cardToPlay == None: #It's None if a card was drawn print('%s draws a card.' % currentPlayer.getName())
def showNoOfCardsInHand(players, idCurrentPlayer): for player in players: if player.getId() == idCurrentPlayer: continue cardsInHand = player.getHand().noOfCardsInHand() print('%s has %d cards' % (player.getName(), cardsInHand), end='; ') print() if __name__ == '__main__': colorama.init() #Initialize color output print(colorama.Back.WHITE + colorama.Fore.BLACK) #Background white, textcolor black clear_screen() #To make background white printCopyright() discardPile = Discardpile() #Build the initial (empty) discard pile deck = Deck(discardPile) #Build a deck with 108 cards players = makePlayers(deck) #Initialize players with deck noOfPlayers = len(players) idCurrentPlayer = randint(0, noOfPlayers - 1) #ID of first player in first round discardPile.putCard( deck.drawCard()[0], players[idCurrentPlayer]) #Put first card at discard pile isClockwise = True #Direction of play. Can be changed by reverse card while True: #Game loop clear_screen() topCard = discardPile.getCardOnTop() currentPlayer = players[idCurrentPlayer] showNoOfCardsInHand( players,
assert deck.noOfCards() == 107 deck.drawCard(7) assert deck.noOfCards() == 100 deck.drawCard(99) deck=Deck([], [Card(color.BLUE, 1), Card(color.RED, 2), Card(color.YELLOW, 7), Card(color.GREEN, 9)]) print(deck) print(deck.noOfCards()) deck=Deck([], [Card(color.BLUE, Card.WILD), Card(color.RED, Card.WILDDRAW4), Card(color.YELLOW, Card.WILDDRAW4), Card(color.GREEN, Card.WILD)]) assert deck.noOfCards() == 0 from discardpile import Discardpile from player import Player d=Discardpile() deck = Deck(d) p = Player(0,deck,"TEST") d.putCard(Card(color.RED,4), p) d.putCard(Card(color.BLUE,7), p) d.putCard(Card(color.BLACK, 14), p) d.putCard(Card(color.GREEN,0), p) print("pile",d) deck = Deck(d,[Card(color.BLUE, 1), Card(color.RED, 2), Card(color.YELLOW, 7)]) print("new",deck) deck.drawCard() print(deck) deck.drawCard() print(deck) deck.drawCard()