Skip to content

Commit

Permalink
Fixed a few problems
Browse files Browse the repository at this point in the history
  • Loading branch information
andy5995 committed Mar 11, 2024
1 parent d3c6411 commit bb240d8
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/root.zig
@@ -1,37 +1,41 @@
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..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;
}
}
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;
// 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];
Expand Down

0 comments on commit bb240d8

Please sign in to comment.