[Edu-sig] more casino math

kirby urner kirby.urner at gmail.com
Fri Sep 4 22:56:15 CEST 2009

By Kirby Urner, 4Dsolutions.net
for Oregon Curriculum Network
(copyleft) 2009

casino_math was originally developed as part of a pilot project
for gnu math teachers in the Greater Portland area

Bug reports:  kirby.urner at gmail.com
Code repository:  4dsolutions.net/ocn/python

import random

class Deck:

    Primitive 54 card deck, including the two jokers
    S13 means King of Spades, H12 means Queen of Hearts
    You could enhance this, have less austere labeling.

    def __init__(self):
        self.deck = [   # our list of 54 cards
                    (suit + str(rank))
                     for suit in ['H','D','C','S']
                     for rank in range(1,14)
                     ] + ['joker']*2

    def deal_off_top(self):
        if len(self.deck) > 0:
            return self.deck.pop(0)

    def deal_off_bottom(self):
        if len(self.deck) > 0:
            return self.deck.pop(-1)

    def shuffle(self):
            # typically done immediately after __init__

    #read only
    def howmany(self):
            return len(self.deck)

class Hand:
    Subclass at will!  To see A's cards, go A() i.e. A is "running its mouth"
    def __init__(self, playername):
        self.playername = playername
        self.cards = []

    def sort_cards(self):
        # alpha sort, so H10 before H7, feel free to enhance

    def get_card(self, thecard):

    def random_card(self):
        if len(self.cards)>0:
            return self.cards.pop(random.choice(

    def __repr__(self):
        return "Hand(%s)" % self.playername

    def __call__(self):
        # blab about cards
        return self.cards

def unit_test():
    Deal five cards round robin to four players from shuffled deck.
    Each player shows sorta sorted cards.
    Each player discards random card.
    Each player shows cards a 2nd time.

    >>> import casino_math
    >>> casino_math.unit_test()
    Hand(Tagrid) ['H10', 'H6', 'S2', 'S4', 'S5']
    Hand(Jeena) ['D12', 'D8', 'H13', 'S13', 'joker']
    Hand(Abaz) ['C10', 'C12', 'D11', 'H7', 'S6']
    Hand(Foobot) ['C11', 'C2', 'C4', 'D9', 'H11']
    Hand(Tagrid) S4
    Hand(Jeena) joker
    Hand(Abaz) D11
    Hand(Foobot) H11
    Hand(Tagrid) ['H10', 'H6', 'S2', 'S5']
    Hand(Jeena) ['D12', 'D8', 'H13', 'S13']
    Hand(Abaz) ['C10', 'C12', 'H7', 'S6']
    Hand(Foobot) ['C11', 'C2', 'C4', 'D9']
    thedeck = Deck()
    theplayers = []

    for player in ['Tagrid','Jeena','Abaz','Foobot']:

    for deal in range(5):  # 0-4
        for player in theplayers:

    for player in theplayers:
        #blabber cycle
        print(player, player())

    for player in theplayers:
        print(player, player.random_card())

    for player in theplayers:
        #blabber cycle
        print(player, player())

# explain this to your students
if __name__ == "__main__":

More information about the Edu-sig mailing list