card game stuff

This commit is contained in:
2026-05-15 06:35:01 -07:00
parent 5fb96becbb
commit 09edadc822
4 changed files with 64 additions and 0 deletions
Generated
+16
View File
@@ -7,6 +7,7 @@ name = "card_game"
version = "0.1.0"
dependencies = [
"deranged",
"rand",
]
[[package]]
@@ -14,3 +15,18 @@ name = "deranged"
version = "0.5.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7cd812cc2bc1d69d4764bd80df88b4317eaef9e773c75226407d9bc0876b211c"
[[package]]
name = "rand"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2e8e8bcc7961af1fdac401278c6a831614941f6164ee3bf4ce61b7edb162207"
dependencies = [
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69"
+1
View File
@@ -5,3 +5,4 @@ edition = "2024"
[dependencies]
deranged = "0.5.8"
rand = { version = "0.10.1", default-features = false }
+41
View File
@@ -11,6 +11,7 @@ pub trait Game {
/// 2 bits for suit ID
/// 4 bits for card Value
/// TODO: better encoding for slightly more decks
#[derive(Clone, Debug)]
pub struct Card(u8);
pub struct CardValue(deranged::RangedU8<1, 13>);
pub enum Suit {
@@ -19,8 +20,48 @@ pub enum Suit {
Clubs,
Diamonds,
}
impl Card {
pub fn value(&self) -> CardValue {
let masked = self.0 & 0b1111;
let value = unsafe { deranged::RangedU8::new_unchecked(masked) };
CardValue(value)
}
pub fn suit(&self) -> Suit {
let red = self.is_red();
let kiki = self.is_kiki();
match (kiki, red) {
(false, false) => Suit::Spades,
(false, true) => Suit::Hearts,
(true, false) => Suit::Clubs,
(true, true) => Suit::Diamonds,
}
}
/// Is the suit red.
pub fn is_red(&self) -> bool {
self.0 & 0b010000 != 0
}
/// Is the suit shape spikey. (Bouba/kiki)
pub fn is_kiki(&self) -> bool {
self.0 & 0b100000 != 0
}
pub fn deck(&self) -> u8 {
self.0 >> 6
}
}
pub struct Stack(Vec<Card>);
impl Stack {
/// Generate a full deck of cards with the specified deck id.
pub fn full_deck(deck_id: u8) -> Stack {
let mut stack = Vec::with_capacity(52);
for suit in 0..4 {
for value in 1..=13 {
stack.push(Card(deck_id << 6 | suit << 4 | value));
}
}
Stack(stack)
}
}
pub struct Session<G: Game> {
state: G,
+6
View File
@@ -32,3 +32,9 @@ pub struct Klondike {
config: KlondikeConfig,
state: KlondikeState,
}
impl Klondike {
pub fn new() -> Self {
let deck = Stack::full_deck(0);
unimplemented!()
}
}