Coder Social home page Coder Social logo

snapcall's Introduction

SnapCall

Fast C# poker hand evaluator for five to seven cards.

Overview

SnapCall is a high performance poker hand evaluation library made for Texas Hold'Em, though it can be used for any poker game with the same hand ranks. High speed lookups are achieved by precomputing all possible hand strengths and storing them in a hash table. The lookup table sizes (using the default load factor) are as follows:
Five Card: ~25MB
Six Card: ~300MB
Siven Card: ~2GB

While the table sizes increase with hand size, the lookup times are constant as long as the whole table fits in memory.

Performance

SnapCall provides a massive improvement over python libraries, but still falls short of a C implementation. SnapCall aims to eventually match or surpass the performance of a fast C library - see the Future Development section. Since SnapCall is based entirely on lookup tables, performance doesn't degrade at all as hand size increases.

Five Card Evals Per Second

Pokerhand-Eval (Python) ......... 100
Deuces (Python) ............. 250,000
SKPokerEval (C++) ........... 400,000
SnapCall (C#) ............. 7,500,000
Poker-Eval (C) ........... 30,000,000

Seven Card Evals Per Second

Pokerhand-Eval (Python) .......... 50
Deuces (Python) .............. 15,000
SKPokerEval (C++) ........... 140,000
SnapCall (C#) ............. 7,500,000
Poker-Eval (C) ........... 30,000,000

Usage

The Evaluator constructor called with no arguments will create a new five card lookup table from scratch, which can then be saved to a file for later reuse. Note that each constructor is inclusive, so the seven card evaluator can evaluate five and six card hands as well.

var fiveCardEvaluator = new Evaluator();
fiveCardEvaluator.WriteToFile("./eval_tables/five_card.ser");

var sixCardEvaluator = new Evaluator(sixCard: true);
sixCardEvaluator.WriteToFile("./eval_tables/six_card.ser");

var sevenCardEvaluator = new Evaluator(sixCard: true, sevenCard: true);
sevenCardEvaluator.WriteToFile("./eval_tables/seven_card.ser");

First time table creation is slow - five card takes about a minute, six card about 10 minutes, and seven card can take over an hour. You'll only want to do this once, and then save the table to a file and reuse it. Loading a seven card table takes about 20 seconds, and five and six cards are less than 5 seconds.

var fiveCardEvaluator = new Evaluator(fileName: "./eval_tables/five_card.ser");
var sixCardEvaluator = new Evaluator(fileName: "./eval_tables/six_card.ser");
var sevenCardEvaluator = new Evaluator(fileName: "./eval_tables/seven_card.ser");

Cards are represented as 64 bit masks, with the most significant 12 bits unused and the remaining 52 bits representing each possible card.

0x00000001 => 2♠
0x00000002 => 2♡
0x00000004 => 2♢
0x00000008 => 2♣
0x00000010 => 3♠
0x00000020 => 3♡
...

A hand is a bitwise OR of all the cards making up the hand. To evaluate a hand, pass its bitmask (UInt64) to the evaluator, and it will return a number from 0 to 7461 representing the hand's strength equivalence class. If two hands have the same equivalence class, they tie. If the class of hand A is greater than the class of hand B, then A beats B. Each equivalence class can contain many unique hands, but there are only 7462 distinct ranks.

7461 => A, K, Q, J, T (flush) Ace high straight flush (4 possible hands)
7460 => K, Q, J, T, 9 (flush) King high straight flush (4 possible hands)
...
7452 => 5, 4, 3, 2, A (flush) Five high straight flush (4 possible hands)
7451 => A, A, A, A, K (no flush) Quad aces, king kicker (4 possible hands)
...
0    => 2, 3, 4, 5, 7 (no flush) Seven high (1020 possible hands)

Dependencies

SnapCall uses the Combinatorics library for generating bitmaps, and the Net-ProtoBuf library for object serialization. Both are available through NuGet.

Future Development Goals

* Remove dependency on Combinatorics package. * Improve HashMap class perfomance to achieve 30M+ evals per second. * Be able to evaluate 2, 3, and 4 card hands efficiently.

snapcall's People

Stargazers

Spiral2k avatar Artem avatar Ozgen Koklu avatar [CTO] 심경환 avatar Luis Finke avatar Soga avatar meraline avatar Hans Vilu avatar Alex Ibraimov avatar  avatar OrganizationUsername avatar Eiliya avatar  avatar  avatar Tsapikov Aleksei avatar Alex Shive avatar cbarr avatar  avatar Terry Mace avatar Yogi avatar Paco Espinoza avatar Dimitri Tholen avatar a avatar  avatar Jasper Ji avatar ObsoleteOne avatar Dennis Pražák avatar Justin Sprigg avatar Michail Naoum avatar

Watchers

 avatar Paco Espinoza avatar  avatar  avatar  avatar

snapcall's Issues

Code

Hi I need help with coding something similar where hands are grouped by strength. Please let me know if you would be interested - I would pay of course. [email protected]

HashMap index out of range exception

HashMap(unint size)

Data List size must always be even, else throws index out of range exception when inserting last element of list

public HashMap(uint size)
{
...
Size = TotalSize / Count;
if (Size % 2 == 1) Size++; // add this line
...

Dependencies are lost to time

Where/how can we get the dependencies? And how do we install them?
Combinatronics? What framework is this from? There are many.
I tried installing through Terminal and NuGet, but both give the same error:
error: Error while adding package 'Combinatronics' to project 'C:\Users\Admin\Documents\GitHub\SnapCall\SnapCall.csproj'. The project does not support adding package references through the add package command.

I havent looked into ProtoBuf dependency yet, because ive been unable to get Combinatronics installed, but im guessing im gonna face the same problems unfortunately.

Any help/input is appreciated so in the future this Repo can become useful again.
Thanks.

Does this work?

I'm having a lot of trouble getting this going. Off the bat I've had to set the evaluator loadFactor higher in order to stop the HashMap from exceeding its bounds.

You also need to set var sourceSet = Enumerable.Range(0, 51).ToList(); in a few places.

Passing a hand to the evaluator

How do I pass the hand to the evaluator?

I don't see any code that computes a bitmask of the Hand itself (even via HandStrength) to be passed to the evaluator.

IOException when saving 7 Card Table

Unhandled Exception: System.IO.IOException: Stream was too long.
at System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count)
at ProtoBuf.ProtoWriter.Flush(ProtoWriter writer)
at ProtoBuf.ProtoWriter.EndSubItem(SubItemToken token, ProtoWriter writer, PrefixStyle style)
at ProtoBuf.ProtoWriter.WriteObject(Object value, Int32 key, ProtoWriter writer)
at proto_1(Object , ProtoWriter )
at ProtoBuf.Meta.TypeModel.SerializeCore(ProtoWriter writer, Object value)
at ProtoBuf.Meta.TypeModel.Serialize(Stream dest, Object value, SerializationContext context)
at ProtoBuf.Serializer.Serialize[T](Stream destination, T instance)
at SnapCall.HashMap.Serialize(HashMap tData) in H:\src\pkrgt\submodules\SnapCall\HashMap.cs
at SnapCall.Evaluator.SaveToFile(String fileName) in H:\src\pkrgt\submodules\SnapCall\Evaluator.cs

I used a load factor of 1.7 (other lower values did not work for other reasons)
Im not sure if that matters.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.