Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/andy5995/zigdeck

A library that creates and shuffles a deck of cards from which you can draw
https://github.com/andy5995/zigdeck

cards library mit-license simulation zig zig-library zig-package

Last synced: 28 days ago
JSON representation

A library that creates and shuffles a deck of cards from which you can draw

Awesome Lists containing this project

README

        

[![Linux CI](https://github.com/andy5995/zigdeck/actions/workflows/linux.yml/badge.svg)](https://github.com/andy5995/zigdeck/actions/workflows/linux.yml)

# zigdeck

A library that creates and shuffles a deck of cards from which you can draw

Tested with the the development version of [zig](https://ziglang.org/) (may
not build with the last release).

## Example

```zig
// initialize the deck. This must be done before shuffle.
var deck = Deck.init();

try std.testing.expectEqual(Suit.Clubs, deck.cards[0].suit);
try std.testing.expectEqual(Face.Ace, deck.cards[0].face);
try std.testing.expectEqual(Suit.Spades, deck.cards[48].suit);
try std.testing.expectEqual(Face.King, deck.cards[51].face);

// Seed the random number generator
var rng = std.rand.DefaultPrng.init(@as(u64, @intCast(std.time.milliTimestamp())));

// Shuffle
Deck.shuffle(&deck, &rng.random());

// Draw one card
const card = Deck.getTopCard(&deck) orelse return;

try std.testing.expectEqual(Suit.Clubs, card.suit);
try std.testing.expectEqual(Face.Queen, card.face);

std.debug.print("Top card: Suit = {}, Value = {}\n", .{ card.suit, card.face });
```

This library is used by [zigpokerhands](https://github.com/andy5995/zigpokerhands).