Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed a few problems #2

Merged
merged 3 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@

# zigdeck

A non-functioning library that shuffles a deck of cards and deals them out
A library that creates and shuffles a deck of cards from which you can draw

The code builds but it's not yet complete.

Tested with the the development version of [zig](https://ziglang.org/) (may
not build with the last release).
43 changes: 29 additions & 14 deletions src/root.zig
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
const std = @import("std");

const Card = struct {
suit: Suit,
value: u4,
};

const Suit = enum {
Clubs,
Diamonds,
Hearts,
Spades,
};

const Card = struct {
suit: Suit,
value: u4,
};

const Deck = struct {
cards: [52]Card,
top: usize,

pub fn init() Deck {
var deck: Deck = .{ .cards = undefined, .top = 0 };
var i: usize = 0;
for (Suit) |suit| {
// inline for (std.meta.fields(Suit)) |suit| {
for (0..4) |suit| {
for (0..13) |value| {
deck.cards[i] = Card{ .suit = suit, .value = value + 1 };
// std.debug.print("{d}\n", .{value});
deck.cards[i] = Card{ .suit = @enumFromInt(suit), .value = @as(u4, @intCast(value)) + 1 };
i += 1;
}
}
return deck;
}

pub fn shuffle(deck: *Deck) void {
var rng = std.rand.DefaultPrng.seed(@intCast(u64, std.time.milliTimestamp()));
for (deck.cards.enumerate()) |item| {
const i = item.index;
const j = rng.random().int(usize) % deck.cards.len;
pub fn shuffle(deck: *Deck, rng: *const std.rand.Random) void {
for (deck.cards, 0..) |_, item| {
const i = item;
const j = rng.int(usize) % deck.cards.len;
const temp = deck.cards[i];
deck.cards[i] = deck.cards[j];
deck.cards[j] = temp;
}
}


pub fn getTopCard(deck: *Deck) ?Card {
if (deck.top >= deck.cards.len) return null;
const card = deck.cards[deck.top];
Expand All @@ -50,7 +50,22 @@ const Deck = struct {

test "Deck operations" {
var deck = Deck.init();
Deck.shuffle(&deck);
try std.testing.expectEqual(Suit.Clubs, deck.cards[0].suit);
try std.testing.expectEqual(1, deck.cards[0].value);
try std.testing.expectEqual(Suit.Clubs, deck.cards[1].suit);
try std.testing.expectEqual(2, deck.cards[1].value);
try std.testing.expectEqual(Suit.Diamonds, deck.cards[13].suit);
try std.testing.expectEqual(1, deck.cards[13].value);
try std.testing.expectEqual(Suit.Hearts, deck.cards[26].suit);
try std.testing.expectEqual(1, deck.cards[26].value);
try std.testing.expectEqual(Suit.Spades, deck.cards[48].suit);
try std.testing.expectEqual(13, deck.cards[51].value);

// var rng = std.rand.DefaultPrng.init(@as(u64, @intCast(std.time.milliTimestamp())));
var rng = std.rand.DefaultPrng.init(1);
Deck.shuffle(&deck, &rng.random());
const card = Deck.getTopCard(&deck) orelse return;
try std.testing.expectEqual(Suit.Clubs, card.suit);
try std.testing.expectEqual(12, card.value);
std.debug.print("Top card: Suit = {}, Value = {}\n", .{ card.suit, card.value });
}