WARNING: Version 0.3.0 represents a major API overhaul and is incompatible with 0.2.x or 0.1.x versions.
A Swift port of ihendley/treys, who provided the original implementation for the logic in Python, which was itself based on Cactus Kev's Algorithm. I have translated the logic into Swift, keeping core features but making "Swifty" adjustments where convenient. I have also retained the vast majority of the original documentation and variables names, again with amendments as needed.
This library is used for the evaluation of poker hands, comprised of 5, 6, or 7 cards. For example:
import SwiftTreys let myHand: [Card] = try! Card.parseArray([["Ah", "Th"]]) let pokerBoard: [Card] = try! Card.parseArray(["4d", "Jh", "Qh", "Ac", "Kh"]) print(Evaluator.evaluate(hand: myHand, board: pokerBoard)) // prints "Royal flush"
Include this line to your dependencies in
Package.swift. Source stability during development is
maintained only through minor versions:
let package = Package( // ... dependencies: [ .package( url: "https://github.com/deus-x-mackina/swift-treys.git", .upToNextMinor(from: "0.3.0")), ] // ... )
If cloning the repo manually, you have a bit more flexibility with the things you can play around with.
git clone https://github.com/deus-x-mackina/swift-treys.git cd swift-treys
Despite being a compiled language, Swift offers a built-in REPL that can import custom modules. From within the repo's directory, simply run:
swift run --repl
Once the REPL boots up, you can
import SwiftTreys and play around with the
A test suite has been written and can be run from within the repo directory via the command:
Note: One of tests involves running through each of the 2+ million five-card hand combinations, so the tests may take around 20 to 40 seconds in total to execute.
These two enumerations make it easier to create
Cards in a type-safe manner.
The base type for representing an evaluable card,
Card instances wrap a
property calculated from its rank and suit that is used for evaluating poker hands
init(_:_:)offers convenient initialization syntax
static func parseArray<Str, Seq>(strings: Seq) throws -> [Card] where Str: StringProtocol, Seq: Sequence, Seq.Element == Str
In order to ensure a non-nil return, each string in
strings should be exactly two
characters long, with the first character, the rank, being one of [[23456789TJQK]] and the
second character, the suit, is one of [[chsd]].
The bread and butter of the library. This static type ships the
method for evaluating hands. The return type of these methods is
Evaluation, which can
be printed to see what hand exactly was evaluated. See the source file
Thanks for making your code available, ihendley! It was a great time working with your code.
|Last commit: 1 week ago|