Skip to content

Commit

Permalink
Implemented Deck
Browse files Browse the repository at this point in the history
draw and shuffle functionality
  • Loading branch information
kissgyorgy committed Feb 21, 2024
1 parent 5f25a65 commit 0a47855
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 12 deletions.
25 changes: 13 additions & 12 deletions poker/__init__.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
# ruff: noqa: F401

from poker._common import PokerEnum
from poker.card import Suit, Rank, Card, FACE_RANKS, BROADWAY_RANKS
from poker.hand import (
Shape,
Hand,
Combo,
Range,
PAIR_HANDS,
OFFSUIT_HANDS,
SUITED_HANDS,
)
from poker.card import BROADWAY_RANKS, FACE_RANKS, Card, Rank, Suit
from poker.constants import (
PokerRoom,
Action,
Currency,
Game,
GameType,
Limit,
MoneyType,
Action,
PokerRoom,
Position,
)
from poker.deck import Deck
from poker.hand import (
OFFSUIT_HANDS,
PAIR_HANDS,
SUITED_HANDS,
Combo,
Hand,
Range,
Shape,
)
from poker.strategy import Strategy
27 changes: 27 additions & 0 deletions poker/deck.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import secrets

from .card import Card


class Deck:
def __init__(self):
self._cards = list(Card)
self._drawn = []

def shuffle(self):
"""Shuffles the deck."""
# Algorithm taken from the Python standard library 3.12 Random.shuffle,
# which is based on the Fisher-Yates shuffle.
for i in range(len(self._cards) - 1, 0, -1):
# pick an element in cards[:i+1] with which to exchange cards[i]
j = secrets.randbelow(i + 1)
self._cards[i], self._cards[j] = self._cards[j], self._cards[i]

def __len__(self):
return len(self._cards)

def draw(self):
"""Draws a card from the top of the deck."""
card = self._cards.pop()
self._drawn.append(card)
return card
27 changes: 27 additions & 0 deletions tests/test_deck.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from poker import Card, Deck


def test_initial_state():
deck = Deck()
assert len(deck) == 52
assert deck._drawn == []
assert deck._cards == list(Card)


def test_draw():
deck = Deck()
card = deck.draw()
assert len(deck) == 51
assert card not in deck._cards
assert deck._drawn == [card]


def test_shuffle():
deck = Deck()
deck2 = Deck()
assert deck._cards == deck2._cards

deck3 = Deck()
deck3.shuffle()
assert deck._cards != deck3._cards
assert deck2._cards != deck3._cards

0 comments on commit 0a47855

Please sign in to comment.