Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding restorable state to Deck objects #11

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Deuces
========

blah blah
A pure Python poker hand evaluation library

[ 2 ❤ ] , [ 2 ♠ ]
Expand Down
36 changes: 23 additions & 13 deletions deuces/deck.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
from random import shuffle
from random import shuffle, random
from card import Card


class Deck:
"""
Class representing a deck. The first time we create, we seed the static
Class representing a deck. The first time we create, we seed the static
deck with the list of unique card integers. Each object instantiated simply
makes a copy of this object and shuffles it.
makes a copy of this object and shuffles it.

The state of a deck is defined by the random float [0.0, 1.0) used to seed
the shuffle() operation, combined with the number of cards that have
already been drawn.
"""
_FULL_DECK = []

def __init__(self):
self.shuffle()
def __init__(self, seed=None, num_drawn=0):
if not seed:
seed = random()
self.seed = seed
self.shuffle(seed)

self.num_drawn = 0
if num_drawn:
self.draw(num_drawn)

def shuffle(self):
def shuffle(self, seed):
# and then shuffle
self.cards = Deck.GetFullDeck()
shuffle(self.cards)
shuffle(self.cards, lambda: seed)

def draw(self, n=1):
self.num_drawn += n
if n == 1:
return self.cards.pop(0)

cards = []
for i in range(n):
cards.append(self.draw())
return cards
return [self.cards.pop(0) for _ in range(n)]

def __str__(self):
return Card.print_pretty_cards(self.cards)
Expand All @@ -36,7 +46,7 @@ def GetFullDeck():

# create the standard 52 card deck
for rank in Card.STR_RANKS:
for suit,val in Card.CHAR_SUIT_TO_INT_SUIT.iteritems():
for suit, val in Card.CHAR_SUIT_TO_INT_SUIT.iteritems():
Deck._FULL_DECK.append(Card.new(rank + suit))

return list(Deck._FULL_DECK)
return list(Deck._FULL_DECK)
33 changes: 33 additions & 0 deletions test/test_deck.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import unittest

from deuces.deuces import Deck


class TestDeck(unittest.TestCase):
def test_shuffle(self):
d1 = Deck(seed=0.1)
d2 = Deck(seed=0.1)

self.assertEquals(d1.draw(), d2.draw())
self.assertEquals(1, d1.num_drawn)
self.assertEquals(1, d2.num_drawn)

def test_predrawn(self):
d1 = Deck(seed=0.1)
d2 = Deck(seed=0.1, num_drawn=2)
self.assertEquals(0, d1.num_drawn)
self.assertEquals(2, d2.num_drawn)

d1.draw()
d1.draw()
self.assertEquals(2, d1.num_drawn)

self.assertEquals(d1.draw(), d2.draw())

def test_multi_draw(self):
d1 = Deck(seed=0.1)
d2 = Deck(seed=0.1)

self.assertEquals(d1.draw(3), d2.draw(3))
self.assertEquals(3, d1.num_drawn)
self.assertEquals(3, d2.num_drawn)