-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.zig
110 lines (100 loc) · 3.59 KB
/
lib.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// MIT License
//
// Copyright (c) 2024 Andy Alt ([email protected])
// Project URL: https://github.com/andy5995/zigdeck
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
const std = @import("std");
pub const Suit = enum {
Clubs,
Diamonds,
Hearts,
Spades,
};
pub const Face = enum(u8) {
Ace = 1,
Two,
Three,
Four,
Five,
Six,
Seven,
Eight,
Nine,
Ten,
Jack,
Queen,
King,
};
pub const Card = struct {
suit: Suit,
face: Face,
};
pub const Deck = struct {
cards: [52]Card,
top: usize,
pub fn init() Deck {
var deck: Deck = .{ .cards = undefined, .top = 0 };
var i: usize = 0;
inline for (@typeInfo(Suit).Enum.fields) |suit| {
inline for (@typeInfo(Face).Enum.fields) |face| {
deck.cards[i] = Card{ .suit = @enumFromInt(suit.value), .face = @enumFromInt(face.value) };
i += 1;
}
}
return deck;
}
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];
deck.top += 1;
return card;
}
};
test "Deck operations" {
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.Clubs, deck.cards[1].suit);
try std.testing.expectEqual(Face.Two, deck.cards[1].face);
try std.testing.expectEqual(Suit.Diamonds, deck.cards[13].suit);
try std.testing.expectEqual(Face.Ace, deck.cards[13].face);
try std.testing.expectEqual(Suit.Hearts, deck.cards[26].suit);
try std.testing.expectEqual(Face.Ace, deck.cards[26].face);
try std.testing.expectEqual(Suit.Spades, deck.cards[48].suit);
try std.testing.expectEqual(Face.King, deck.cards[51].face);
// 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(Face.Queen, card.face);
std.debug.print("Top card: Suit = {}, Value = {}\n", .{ card.suit, card.face });
}