Skip to content

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

License

Notifications You must be signed in to change notification settings

andy5995/zigdeck

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Linux CI

zigdeck

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

Tested with the the development version of zig (may not build with the last release).

Example

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