concept deck in category python

appears as: deck, decks, deck
Hello World! Third Edition

This is an excerpt from Manning's book Hello World! Third Edition.

Computers can also simulate the random behavior of rolling dice or shuffling a deck of cards. This makes it possible to create computer games with cards or dice (or other randomly behaving objects). For example, almost everyone has tried playing Solitaire on Windows, which is a card game where the program randomly shuffles the cards before each game. Computer Backgammon, which uses two dice, is also very popular.

In this chapter, you’ll learn how to use the random module to make computer-generated dice and decks of cards that you can play games with. We’ll also look at how you can use computer-generated random events to explore the idea of probability, which is how likely something is to happen.

Now we need to make some instances of cards—in fact, a whole deck would be nice! To test our Card class, let’s make a program to create a deck of 52 cards and then pick 5 cards at random and display their attributes. Here’s some code for that.

Listing 23.5. Making a deck of cards
import random
from cards import Card                        #1

deck = []
for suit_id in range(1, 5):                   #2
    for rank_id in range(1, 14):              #2
        deck.append(Card(suit_id, rank_id))   #2

hand = []                                     #3
for cards in range(0, 5):                     #3
    a = random.choice(deck)                   #3
    hand.append(a)                            #3
    deck.remove(a)                            #3

print()
for card in hand:
    print(card.short_name, '=' ,card.long_name, "    Value:", card.value)

2 The inner loop goes through each card in the suit, and the outer loop goes through each suit (13 cards * 4 suits = 52 cards). 3 Then the code picks five cards from the deck and puts them in a hand. It also removes the cards from the deck.

Listing 23.11. The computer’s turn
def computer_turn():
    global c_hand, deck, up_card, active_suit, blocked
    options = []
    for card in c_hand:
        if card.rank == '8':                                        #1
            c_hand.remove(card)
            up_card = card
            print("  Computer played ", card.short_name)
            #suit totals:  [diamonds, hearts, spades, clubs]
            suit_totals = [0, 0, 0, 0]                              #2
            for suit in range(1, 5):
                for card in c_hand:
                    if card.suit_id == suit:
                        suit_totals[suit-1] += 1
            long_suit = 0
            for i in range (4):
                if suit_totals[i] > long_suit:
                    long_suit = i
            if long_suit == 0:  active_suit = "Diamonds"           #3
            if long_suit == 1:  active_suit = "Hearts"             #3
            if long_suit == 2:  active_suit = "Spades"             #3
            if long_suit == 3:  active_suit = "Clubs"              #3
            print("  Computer changed suit to ", active_suit)
            return                                                 #4
        else:
            if card.suit == active_suit:                           #5
                options.append(card)                               #5
            elif card.rank == up_card.rank:                        #5
                options.append(card)                               #5

    if len(options) > 0:
        best_play = options[0]
        for card in options:                                       #6
            if card.value > best_play.value:                       #6
                best_play = card                                   #6
         c_hand.remove(best_play)                                  #7
         up_card = best_play                                       #7
         active_suit = up_card.suit                                #7
         print("  Computer played ", best_play.short_name)         #7
    else:
        if len(deck) > 0:
            next_card = random.choice(deck)                        #8
            c_hand.append(next_card)                               #8
            deck.remove(next_card)                                 #8
            print("  Computer drew a card")                        #8
        else:
            print("  Computer is blocked")                         #9
            blocked += 1                                           #9
    print("Computer has %i cards left" % (len(c_hand)))

We’re almost done—just a couple more things to add. You might have noticed that the computer’s turn was defined as a function, and we used some global variables in that function. We could also have passed the variables to the function, but using globals works just as well and is more like the real world, where the deck is “global”—anybody can reach over and take a card from it.

Get Programming: Learn to code with Python

This is an excerpt from Manning's book Get Programming: Learn to code with Python.

Sorts a list named deck and aliases it to a variable named cards

You want to simulate playing the card game War. Each round, players will take a card from one deck and compare the cards. The one with the higher card wins the round and gives their card to the other player. The winner is determined after numerous rounds, when the deck is empty. The winner is the person with fewer cards in their hand. You’ll create two types of objects: a Player and a CardDeck. After defining the classes, you’ll write code that simulates a game between two players. You’ll first ask users for their names then create two Player objects. Both players will use the same card deck. Then you’ll use methods defined in the Player and CardDeck classes to automatically simulate the rounds and determine the winner.

Listing 34.5. Initializing game variables and objects
name1 = input("What's your name? Player 1: ")           #1
player1 = Player(name1)                                 #2

name2 = input("What's your name? Player 2: ")
player2 = Player(name2)

deck = CardDeck()                                       #3

After initializing the object you’ll use in the game, you can now simulate the game.