Skip to content

Commit

Permalink
Improve python support and documentation
Browse files Browse the repository at this point in the history
- make Card, Rank, Suit, CardSet hashab
- clean up README.md
- suppress warning from boost::math::binomial_coefficient
- create some sets in the test script
  • Loading branch information
andrewprock-anari committed Nov 20, 2024
1 parent 790907e commit 5249fe0
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 15 deletions.
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
PokerStove
==========

[![OpenHUB Metrics](https://www.openhub.net/p/pokerstove/widgets/project_thin_badge.gif)](https://www.openhub.net/p/pokerstove)
Code available at: https://github.com/andrewprock/pokerstove

PokerStove is a highly hand optimized C++ poker hand evaluation library. The Win32 Hold'em
GUI was first released in 2002, and has been available as freeware since
it's first release.
PokerStove is a highly hand optimized C++ poker hand evaluation library.

The core libraries of pokerstove are being open sourced. The project is
The core libraries of pokerstove have been open sourced. The project is
currently in the process of reviewing and publishing the code. As code is
reviewed and code sanitized further commits will be added.

Expand Down Expand Up @@ -162,7 +160,7 @@ Once you have built the project with Python support there will be a
python loader file and a shared object file. To test run the script
from the build directory:

PYTHONPATH=python/pokerstove/ ../src/lib/python/test-python
PYTHONPATH=build/python/pokerstove/ ./src/lib/python/test-python

For regular use you'll want to export the PYTHONPATH variable to your
shell:
Expand All @@ -179,7 +177,8 @@ installable wheel for the pythong package. The commands below can be
used to build/install/verify the package.

git clean -fxd && pipx run build
python3 -m venv venv && . venv/bin/activate && pip install dist/pokerstove-*.whl
python3 -m venv venv && . venv/bin/activate
pip install dist/pokerstove-*.whl
python src/lib/python/test-python
deactivate

Expand Down
6 changes: 6 additions & 0 deletions src/lib/pokerstove/peval/CardSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ class CardSet

uint64_t mask() const { return _cardmask; } //!< 1 bit per card

/**
* needed for SWIG, this will truncate if sizeof(long) < 8 bytes. This should be ok
* as hashes are not expected to be unique for python.
*/
long getHash() { return (long)_cardmask; }

std::vector<Card> cards() const; //!< returns cards in code sorted order
std::vector<CardSet> cardSets() const; //!< break into one card/CardSet

Expand Down
2 changes: 2 additions & 0 deletions src/lib/pokerstove/peval/Rank.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ class Rank
* to these functions to friends.
*/
void encode(uint8_t c) { _rank = c%NUM_RANK; } // was encode
public:
uint8_t code() const { return _rank; }
private:
int rankBit() const { return 0x01 << _rank; }

static bool isRankChar(char c);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/pokerstove/util/combinations.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ TEST(Combinations, time_boost)
for (int i=0; i<6000; i++) {
for (int k=0; k<10; k++) {
for (int n=k; n<10; n++) {
static_cast<size_t>(boost::math::binomial_coefficient<double>(n, k));
(void)static_cast<size_t>(boost::math::binomial_coefficient<double>(n, k));
}
}
}
Expand Down
27 changes: 21 additions & 6 deletions src/lib/python/pokerstove.i
Original file line number Diff line number Diff line change
Expand Up @@ -55,31 +55,46 @@

// support __repr__() with extension boilerplate
%extend pokerstove::Rank {
std::string __repr__()
{
std::string __repr__() {
return $self->str();
}

long __hash__() {
return (long)$self->code();
}
}

%extend pokerstove::Suit {
std::string __repr__()
{
std::string __repr__() {
return $self->str();
}

long __hash__() {
return (long)$self->code();
}
}

%extend pokerstove::Card {
std::string __repr__()
{
return $self->str();
}

long __hash__() {
return (long)$self->code();
}
}

%extend pokerstove::CardSet {
std::string __repr__()
{
std::string __repr__() {
return $self->str();
}

// this will truncate if sizeof(long) < 8 bytes. This should be ok
// as hashes are not expected to be unique for python.
long __hash__() {
return (long)$self->mask();
}
}

%extend pokerstove::PokerHand {
Expand Down
5 changes: 4 additions & 1 deletion src/lib/python/test-python
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ r2 = Rank(4)
r3 = Rank('4')
print('-- rank --')
print(r1, r2, r3)
s = {r1, r2, r3}

s1 = Suit()
s2 = Suit(2)
Expand All @@ -18,6 +19,8 @@ c2 = Card(2)
c3 = Card('As')
print('-- cards --')
print(c1, c2, c3)
s = {c1, c2, c3}
l = [c1, c2, c3]

cs1 = CardSet()
cs2 = CardSet(0x8E)
Expand All @@ -27,6 +30,7 @@ cs5 = CardSet('Ac8dKc')
print('-- card sets --')
print('empty card set: ', cs1)
print(cs2, cs3, cs4)
s = {cs1, cs2, cs3, cs4, cs5}

e1 = PokerEvaluation()
e2 = PokerEvaluation(26214408)
Expand Down Expand Up @@ -97,5 +101,4 @@ print('holdem: ', pocket, flop)
print('dealt (dead): ', dead)
print('deck: ', d)

print('-- card distribution --')

0 comments on commit 5249fe0

Please sign in to comment.