Skip to content

Commit

Permalink
Merge pull request #8 from rahul-nanwani/v3-0-0
Browse files Browse the repository at this point in the history
v3.0.0
  • Loading branch information
rahul-nanwani authored Dec 10, 2022
2 parents ff4be65 + 0d4f2ba commit 4be5e3e
Show file tree
Hide file tree
Showing 9 changed files with 469 additions and 236 deletions.
15 changes: 8 additions & 7 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
blackjack21
===========

.. image:: https://img.shields.io/pypi/v/blackjack21?style=for-the-badge
.. image:: https://img.shields.io/pypi/v/blackjack21?style=flat-square
:alt: PyPI
.. image:: https://img.shields.io/pypi/status/blackjack21?style=for-the-badge
.. image:: https://img.shields.io/pypi/status/blackjack21?style=flat-square
:alt: PyPI - Status
.. image:: https://img.shields.io/pypi/dd/blackjack21?style=for-the-badge
:alt: PyPI - Downloads
.. image:: https://img.shields.io/pypi/l/blackjack21?style=for-the-badge
.. image:: https://img.shields.io/pypi/l/blackjack21?style=flat-square
:alt: PyPI - License

===========
Expand All @@ -17,8 +15,9 @@ A complete package for creating a multiplayer blackjack table.
Features
---------

- Create Tables.
- Max. 5 players per table.
- No limit of Tables count.
- No limit of players per table.
- No limit of decks of cards being used for the table
- Hit, Stand, Double Down, Split.

Wiki Guide
Expand All @@ -36,4 +35,6 @@ Wiki Guide
- `PlayerBase <https://github.com/rahul-nanwani/blackjack21/wiki/Classes#playerbase>`__
- `Player <https://github.com/rahul-nanwani/blackjack21/wiki/Classes#player>`__
- `Dealer <https://github.com/rahul-nanwani/blackjack21/wiki/Classes#dealer>`__
- `Deck <https://github.com/rahul-nanwani/blackjack21/wiki/Classes#deck>`__
- `Card <https://github.com/rahul-nanwani/blackjack21/wiki/Classes#card>`__
- `Exceptions <https://github.com/rahul-nanwani/blackjack21/wiki/Classes#exceptions>`__
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = blackjack21
version = 2.0.1
version = 3.0.0
author = Rahul Nanwani
author_email = [email protected]
description = A complete package for blackjack, with max 5 players on a table, double down, and split features too.
Expand Down
15 changes: 11 additions & 4 deletions src/blackjack21/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,26 @@
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

"""
blackjack21
blackjack21 v3.0.0
MIT License
Rahul Nanwani
Documentation: https://github.com/rahul-nanwani/blackjack21/wiki
"""
from .table import Table

__title__ = "blackjack21"
__version__ = "2.0.1"
__version__ = "3.0.0"
__author__ = "Rahul Nanwani"
__license__ = "MIT License"


from .exceptions import * # noqa
from .deck import * # noqa
from .players import * # noqa
from .table import * # noqa
131 changes: 108 additions & 23 deletions src/blackjack21/deck.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,128 @@
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from itertools import chain
from random import shuffle
from typing import Text

from .exceptions import InvalidSuits, InvalidRanks

class Card:
def __init__(self, suit: Text, rank: Text, value: int):
"""
Create card for the deck
:param suit: suit of the card
:param rank: rank of the card
:param value: value of the card in the game
"""
self.suit = suit
self.rank = rank
self._value = value
__all__ = (
"Card",
"Deck",
)

class Card:
"""Card class
def deck(suits: tuple, ranks: tuple):
:param suit: suit of the card
:param rank: rank of the card
:param value: value of the card in the game
"""
Create a deck of cards

__slots__ = (
"__suit",
"__rank",
"__value",
)

def __init__(self, suit: str, rank: str, value: int):
self.__suit = suit
self.__rank = rank
self.__value = value

def __repr__(self):
return f"{self.rank} of {self.suit}"

def __str__(self):
return f"{self.rank} of {self.suit}"

# properties
@property
def suit(self) -> str:
"""Card suit"""
return self.__suit
@property
def rank(self) -> str:
"""Card rank"""
return self.__rank

@property
def value(self) -> int:
"""Card value"""
return self.__value


class Deck:
"""Deck of cards class (Iterable)
:param suits: tuple of 4 suits
:param ranks: tuple of 13 card ranks
:return: list of card object instances
:param count: int number of decks to be merged
:raises InvalidSuits: if length of suits is not 4
:raises InvalidRanks: if length of ranks is not 13
"""
values = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]

deck_of_cards = []
__slots__ = (
"__cards",
"__index",
)

def __init__(self, suits: tuple, ranks: tuple, **kwargs):
if len(suits) != 4:
raise InvalidSuits(suits)
if len(ranks) != 13:
raise InvalidRanks(ranks)

self.__index = -1
self.__cards = map(
lambda suit: map(
lambda rank, value: Card(suit, rank, value),
ranks, [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
),
suits
)
self.__cards = list(chain(*self.__cards))
self.__cards *= kwargs.get('count', 1)
shuffle(self.__cards)

def __iter__(self):
self.__index = -1
return self

def __next__(self):
try:
self.__index += 1
return self.__cards[self.__index]
except IndexError:
raise StopIteration

for suit in suits:
for i in range(13):
deck_of_cards.append(Card(suit, ranks[i], values[i]))
def __getitem__(self, index):
return self.__cards[index]

shuffle(deck_of_cards)
return deck_of_cards
def __len__(self):
return len(self.__cards)

# properties
@property
def cards(self) -> list:
"""List of Card class objects"""
return self.__cards

# methods
def shuffle(self):
"""Shuffle the cards in the deck"""
shuffle(self.__cards)

def draw_card(self) -> Card:
"""Draw a card from the deck
:return: Card object
"""
return self.__cards.pop()
5 changes: 2 additions & 3 deletions src/blackjack21/examples/advanced_example.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from blackjack21 import Table
from blackjack21.players import Dealer
from blackjack21 import Table, Dealer

def print_cards(player):
print(f"\n{player.name}")
for i, card in enumerate(player.hand):
if ((type(player) != Dealer) or (type(player) == Dealer and i == 0)):
if (type(player) != Dealer) or (type(player) == Dealer and i == 0):
print(f"{card.rank} of {card.suit}")
if type(player) != Dealer:
print(player.total)
Expand Down
5 changes: 2 additions & 3 deletions src/blackjack21/examples/basic_example.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from blackjack21 import Table
from blackjack21.players import Dealer
from blackjack21 import Table, Dealer

def print_cards(player):
print(f"\n{player.name}")
for i, card in enumerate(player.hand):
if ((type(player) != Dealer) or (type(player) == Dealer and i == 0)):
if (type(player) != Dealer) or (type(player) == Dealer and i == 0):
print(f"{card.rank} of {card.suit}")
if type(player) != Dealer:
print(player.total)
Expand Down
41 changes: 41 additions & 0 deletions src/blackjack21/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
__all__ = (
"InvalidSuits",
"InvalidRanks",
"PlayDealerFailure",
"PlayFailure",
"InvalidPlayersData",
)

class BlackjackException(Exception):
"""Blackjack base class exception"""
def __init__(self, *args, **kwargs):
super().__init__(*args)


class InvalidSuits(BlackjackException):
"""Raised when length of suits tuple is not 4"""
def __init__(self, suits: tuple):
super().__init__(f"Failed to create Deck.\nExpected 4 suits but got {len(suits)} suits instead.")


class InvalidRanks(BlackjackException):
"""Raised when length of ranks tuple is not 13"""
def __init__(self, ranks: tuple):
super().__init__(f"Failed to create Deck.\nExpected 13 ranks but got {len(ranks)} ranks instead.")


class PlayFailure(BlackjackException):
"""Raised when player tries to play double down/split when they are not eligible"""
def __init__(self, name: str, action: str):
super().__init__(f"Attempted to play {action}.\n{name} is not eligible to play this.")


class PlayDealerFailure(BlackjackException):
def __init__(self, name: str):
"""Raised when dealer tries to play own hand before all the player(s) have finished their hand(s)"""
super().__init__(f"Failed to play dealer.\n{name} has not finished playing their hand yet.")

class InvalidPlayersData(BlackjackException):
"""Raised when the input player data is invalid"""
def __init__(self):
super().__init__(f"Players should have both names and bet amounts.")
Loading

0 comments on commit 4be5e3e

Please sign in to comment.