-
-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
67 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |