Swiftpack.co -  deus-x-mackina/swift-treys as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
A Swift package for evaluating poker hands
.package(url: "https://github.com/deus-x-mackina/swift-treys.git", from: "0.3.0")

Swift Treys

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"


Using Swift Package Manager

Include this line to your dependencies in Package.swift. Source stability during development is maintained only through minor versions:

let package = Package(
    // ...
    dependencies: [
            url: "https://github.com/deus-x-mackina/swift-treys.git",
            .upToNextMinor(from: "0.3.0")),
    // ...

Cloning the repo

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

Using the REPL

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 code.

Running Tests

A test suite has been written and can be run from within the repo directory via the command:

swift test

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.

At A Glance

Rank and Suit

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 uniqueInteger property calculated from its rank and suit that is used for evaluating poker hands algorithmically.


  • Conforms to Equatable, Hashable, CustomStringConvertible, and Codable
  • init(_:_:) offers convenient initialization syntax Card(.ace, .spades)
  • Can create multiple cards from the static method declared as:
  static func parseArray<Str, Seq>(strings: Seq) throws -> [Card]
    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 evaluate overloaded 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 for more.


Thanks for making your code available, ihendley! It was a great time working with your code.


Stars: 1
Last commit: 1 week ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.


Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API