card game stuff

This commit is contained in:
2026-05-15 07:03:31 -07:00
parent 77b38608d9
commit ec8d998f75
4 changed files with 50 additions and 21 deletions
Generated
-7
View File
@@ -18,7 +18,6 @@ checksum = "c4512299f36f043ab09a583e57bceb5a5aab7a73db1805848e8fef3c9e8c78b3"
name = "card_game" name = "card_game"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"deranged",
"rand", "rand",
] ]
@@ -48,12 +47,6 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "deranged"
version = "0.5.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7cd812cc2bc1d69d4764bd80df88b4317eaef9e773c75226407d9bc0876b211c"
[[package]] [[package]]
name = "equivalent" name = "equivalent"
version = "1.0.2" version = "1.0.2"
-1
View File
@@ -4,5 +4,4 @@ version = "0.1.0"
edition = "2024" edition = "2024"
[dependencies] [dependencies]
deranged = "0.5.8"
rand = { version = "0.10.1", default-features = false, features = ["thread_rng"] } rand = { version = "0.10.1", default-features = false, features = ["thread_rng"] }
+32 -4
View File
@@ -13,9 +13,10 @@ pub trait Game {
/// 2 bits for suit ID /// 2 bits for suit ID
/// 4 bits for card Value /// 4 bits for card Value
/// TODO: better encoding for slightly more decks /// TODO: better encoding for slightly more decks
#[derive(Clone, Debug)] #[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Card(u8); pub struct Card(u8);
pub struct CardValue(deranged::RangedU8<1, 13>); pub struct CardValue(u8);
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum Suit { pub enum Suit {
Spades, Spades,
Hearts, Hearts,
@@ -25,8 +26,7 @@ pub enum Suit {
impl Card { impl Card {
pub fn value(&self) -> CardValue { pub fn value(&self) -> CardValue {
let masked = self.0 & 0b1111; let masked = self.0 & 0b1111;
let value = unsafe { deranged::RangedU8::new_unchecked(masked) }; CardValue(masked)
CardValue(value)
} }
pub fn suit(&self) -> Suit { pub fn suit(&self) -> Suit {
let red = self.is_red(); let red = self.is_red();
@@ -67,6 +67,34 @@ impl Stack {
use rand::seq::SliceRandom; use rand::seq::SliceRandom;
self.0.shuffle(rng); self.0.shuffle(rng);
} }
pub fn push(&mut self, card: Card) {
self.0.push(card);
}
pub fn pop(&mut self) -> Option<Card> {
self.0.pop()
}
pub fn is_empty(&mut self) -> bool {
self.0.is_empty()
}
}
pub struct Pile {
face_down: Stack,
face_up: Stack,
}
impl Pile {
pub fn pop(&mut self) -> Option<Card> {
let card = self.face_up.pop()?;
if self.face_up.is_empty() {
if let Some(card) = self.face_down.pop() {
self.face_up.push(card);
}
}
Some(card)
}
pub fn push(&mut self, card: Card) {
self.face_up.push(card);
}
} }
pub struct Session<G: Game> { pub struct Session<G: Game> {
+18 -9
View File
@@ -1,15 +1,11 @@
use crate::Rng; use crate::Rng;
use crate::card_game::{Card, Game, Stack}; use crate::card_game::{Card, Game, Pile, Stack};
struct Pile {
face_down: Stack,
face_up: Stack,
}
struct KlondikeConfig {} struct KlondikeConfig {}
struct KlondikeState { struct KlondikeState {
piles: [Pile; 14], piles: [Pile; 14],
} }
enum KlondikePileId { pub enum KlondikePileId {
Stock, Stock,
Hand, Hand,
Foundation0, Foundation0,
@@ -25,9 +21,21 @@ enum KlondikePileId {
Tableau6, Tableau6,
Tableau7, Tableau7,
} }
impl std::ops::Index<KlondikePileId> for KlondikeState {
type Output = Pile;
fn index(&self, index: KlondikePileId) -> &Self::Output {
&self.piles[index as usize]
}
}
impl std::ops::IndexMut<KlondikePileId> for KlondikeState {
fn index_mut(&mut self, index: KlondikePileId) -> &mut Self::Output {
&mut self.piles[index as usize]
}
}
pub struct KlondikeInstruction { pub struct KlondikeInstruction {
src: KlondikePileId, pub src: KlondikePileId,
dst: KlondikePileId, pub dst: KlondikePileId,
} }
pub struct Klondike { pub struct Klondike {
config: KlondikeConfig, config: KlondikeConfig,
@@ -49,6 +57,7 @@ impl Game for Klondike {
todo!() todo!()
} }
fn process_instruction(&mut self, instruction: Self::Instruction) { fn process_instruction(&mut self, instruction: Self::Instruction) {
todo!() let card = self.state[instruction.src].pop().unwrap();
self.state[instruction.dst].push(card);
} }
} }