From f96add0a4f61d6e3228f1154dac77f4ed7307037 Mon Sep 17 00:00:00 2001 From: andy5995 Date: Mon, 11 Mar 2024 17:40:46 -0500 Subject: [PATCH 1/3] Fixed a few problems --- src/root.zig | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/root.zig b/src/root.zig index ceb9396..87bfc8d 100644 --- a/src/root.zig +++ b/src/root.zig @@ -1,10 +1,5 @@ const std = @import("std"); -const Card = struct { - suit: Suit, - value: u4, -}; - const Suit = enum { Clubs, Diamonds, @@ -12,6 +7,11 @@ const Suit = enum { Spades, }; +const Card = struct { + suit: Suit, + value: u4, +}; + const Deck = struct { cards: [52]Card, top: usize, @@ -19,9 +19,10 @@ const Deck = struct { 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..3) |suit| { for (0..13) |value| { - deck.cards[i] = Card{ .suit = suit, .value = value + 1 }; + deck.cards[i] = Card{ .suit = @enumFromInt(suit), .value = @as(u4, @intCast(value)) + 1 }; i += 1; } } @@ -29,9 +30,12 @@ const Deck = struct { } 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; + // This should be moved out of the library; no need + // to seed every time a deck is shuffled. How to pass rng from + // from another function? + var rng = std.rand.DefaultPrng.init(1); + for (deck.cards, 0..) |_, item| { + const i = deck.cards[item]; const j = rng.random().int(usize) % deck.cards.len; const temp = deck.cards[i]; deck.cards[i] = deck.cards[j]; From 496058e2dbf00145c2cd9f3444f9e3e0b2f5fd7b Mon Sep 17 00:00:00 2001 From: andy5995 Date: Mon, 11 Mar 2024 23:18:10 -0500 Subject: [PATCH 2/3] Complete fixing errors, add more tests Thanks to dimdin for the test suggestions https://ziggit.dev/t/zig-build-completes-but-zig-build-test-shows-errors/3537/5?u=andy5995 --- src/root.zig | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/root.zig b/src/root.zig index 87bfc8d..7eeeca9 100644 --- a/src/root.zig +++ b/src/root.zig @@ -20,8 +20,9 @@ const Deck = struct { var deck: Deck = .{ .cards = undefined, .top = 0 }; var i: usize = 0; // inline for (std.meta.fields(Suit)) |suit| { - for (0..3) |suit| { + for (0..4) |suit| { for (0..13) |value| { + // std.debug.print("{d}\n", .{value}); deck.cards[i] = Card{ .suit = @enumFromInt(suit), .value = @as(u4, @intCast(value)) + 1 }; i += 1; } @@ -29,21 +30,16 @@ const Deck = struct { return deck; } - pub fn shuffle(deck: *Deck) void { - // This should be moved out of the library; no need - // to seed every time a deck is shuffled. How to pass rng from - // from another function? - var rng = std.rand.DefaultPrng.init(1); + pub fn shuffle(deck: *Deck, rng: *const std.rand.Random) void { for (deck.cards, 0..) |_, item| { - const i = deck.cards[item]; - const j = rng.random().int(usize) % deck.cards.len; + 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]; @@ -54,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 }); } From 4bf9db3bbebf80364901290d7e9410df216cd50a Mon Sep 17 00:00:00 2001 From: andy5995 Date: Mon, 11 Mar 2024 23:35:32 -0500 Subject: [PATCH 3/3] update README --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ff58f7a..9b88f29 100644 --- a/README.md +++ b/README.md @@ -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).