Skip to content

Commit

Permalink
Fix abscract cards hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
YegorDB committed Feb 5, 2021
1 parent 9c8013d commit 1b0b496
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Suit symbols: `'c'` (clubs), `'d'` (diamonds), `'h'` (hearts), `'s'` (spades).
>>> from agstuff.cards.core import Card

>>> card = Card('As')
>>> card
>>> print(card)
A♠
>>> card.name
'Ace of spades'
Expand Down Expand Up @@ -148,7 +148,7 @@ There are 13 weights (Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack
>>> from agstuff.cards.core import Deck

>>> deck = Deck()
>>> deck
>>> print(deck)
[
2♣, 2♦, 2♥, 2♠,
3♣, 3♦, 3♥, 3♠,
Expand All @@ -169,7 +169,7 @@ There are 13 weights (Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack
>>> cards = deck.push_cards(3)
>>> cards # generator of 3 random cards
<generator object Deck.push_cards at 0x7f5b1d52e228>
>>> list(cards)
>>> print(list(cards))
[6♦, 4♣, J♠]
>>> deck.size
49
Expand Down Expand Up @@ -201,13 +201,13 @@ Cards could be set from deck
49
>>> cards.size
3
>>> cards
>>> print(cards)
[4♣, 5♠, 7♦]

>>> cards.pull(deck, 2) # add 2 more cards
>>> cards.size
5
>>> cards
>>> print(cards)
[4♣, 5♠, 7♦, 9♥, J♠]

>>> cards.clean()
Expand All @@ -223,7 +223,7 @@ Also cards could be set by cards string
>>> from agstuff.cards.core import Cards

>>> cards = Cards("2c/3c/4c/5c/6c")
>>> cards
>>> print(cards)
[2♣, 3♣, 4♣, 5♣, 6♣]
```

Expand All @@ -233,7 +233,7 @@ Also cards could be set by iterable of Card instanses
>>> from agstuff.cards.core import Card, Cards

>>> cards = Cards(cards=[Card("Jd"), Card("2s"), Card("6c")])
>>> cards
>>> print(cards)
[2♠, 6♣, J♦]
```

Expand All @@ -259,10 +259,10 @@ By default Cards can contain no more than 52 items, and it could be changed
>>> cards1 = Cards(max_count=7)
>>> deck = Deck()
>>> cards1.pull(deck, 10)
>>> cards1
>>> print(cards1)
[5♣, 3♥, Q♠, J♣, J♦, 8♠, 9♣]

>>> cards2 = Cards("2s/3s/4s/5s/6s/7s/8s/9s/Ts/Js/Qs/Ks/As", max_count=5)
>>> cards2
>>> print(cards2)
[2♠, 3♠, 4♠, 5♠, 6♠]
```
10 changes: 5 additions & 5 deletions agstuff/cards/core.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2018-2019 Yegor Bitensky
# Copyright 2018-2021 Yegor Bitensky

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -91,7 +91,7 @@ def __ne__(self, other):

@classmethod
def numbers(cls):
return {cls.symbols[i]: i for i in range(14)}
return {cls.symbols[i]: i + 1 for i in range(14)}

@classmethod
def names(cls):
Expand Down Expand Up @@ -129,7 +129,7 @@ def __str__(self):
return self.pretty_symbol

def __repr__(self):
return self.pretty_symbol
return self.symbol

def __eq__(self, other):
return self.symbol == other.symbol
Expand All @@ -139,7 +139,7 @@ def __ne__(self, other):

@classmethod
def numbers(cls):
return {cls.symbols[i]: i for i in range(4)}
return {cls.symbols[i]: i + 1 for i in range(4)}

@classmethod
def names(cls):
Expand Down Expand Up @@ -175,7 +175,7 @@ def __repr__(self):
return f"{repr(self.weight or 'X')}{repr(self.suit or 'x')}"

def __hash__(self):
return self.weight.number * 10 + self.suit.number
return 10 * (self.weight.number if self.weight else 0) + (self.suit.number if self.suit else 0)

def __lt__(self, other):
return self.weight < other.weight
Expand Down
6 changes: 4 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2019 Yegor Bitensky
# Copyright 2019-2021 Yegor Bitensky

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -22,7 +22,7 @@

setup(
name='AGStuff',
version='1.0.0',
version='1.0.1',
description="Abstract games stuff",
long_description=long_description,
long_description_content_type="text/markdown",
Expand All @@ -38,6 +38,8 @@
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3 :: Only",
],
keywords='cards',
Expand Down
7 changes: 6 additions & 1 deletion tests/test_cards_core.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2018-2019 Yegor Bitensky
# Copyright 2018-2021 Yegor Bitensky

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -60,6 +60,11 @@ def test_comparative(self):
assert Card("Ac") == Card("c")
assert Card("5d") != Card("s")

def test_hash(self):
assert hash(Card("Kd")) == 132
assert hash(Card("T")) == 100
assert hash(Card("s")) == 4


class TestDeck:
def test_validation(self):
Expand Down

0 comments on commit 1b0b496

Please sign in to comment.