card game stuff

This commit is contained in:
2026-05-15 07:57:01 -07:00
parent 9dd379cb1b
commit 4ffd2a338f
2 changed files with 19 additions and 14 deletions
+8
View File
@@ -66,6 +66,7 @@ impl Card {
}
}
#[derive(Hash)]
pub struct Stack(Vec<Card>);
impl Stack {
pub fn new() -> Self {
@@ -99,6 +100,7 @@ impl std::ops::DerefMut for Stack {
}
}
#[derive(Hash)]
pub struct Pile {
face_down: Stack,
face_up: Stack,
@@ -134,6 +136,12 @@ impl Pile {
pub fn push(&mut self, card: Card) {
self.face_up.push(card);
}
pub fn face_up(&self) -> &[Card] {
&self.face_up
}
pub fn face_down(&self) -> &[Card] {
&self.face_down
}
}
pub struct Session<G: Game> {
+11 -14
View File
@@ -7,9 +7,11 @@ impl Default for KlondikeConfig {
KlondikeConfig {}
}
}
#[derive(Hash)]
struct KlondikeState {
piles: [Pile; 13],
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum KlondikePileId {
Tableau0,
Tableau1,
@@ -25,18 +27,7 @@ pub enum KlondikePileId {
Foundation2,
Foundation3,
}
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]
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct KlondikeInstruction {
pub src: KlondikePileId,
pub dst: KlondikePileId,
@@ -82,6 +73,12 @@ impl Klondike {
};
Self { config, state }
}
pub fn pile(&self, index: KlondikePileId) -> &Pile {
&self.state.piles[index as usize]
}
fn pile_mut(&mut self, index: KlondikePileId) -> &mut Pile {
&mut self.state.piles[index as usize]
}
}
impl Game for Klondike {
type Instruction = KlondikeInstruction;
@@ -92,8 +89,8 @@ impl Game for Klondike {
todo!()
}
fn process_instruction(&mut self, instruction: Self::Instruction) {
let card = self.state[instruction.src].pop().unwrap();
self.state[instruction.dst].push(card);
let card = self.pile_mut(instruction.src).pop().unwrap();
self.pile_mut(instruction.dst).push(card);
}
fn is_win(&self) -> bool {
// assuming only valid moves, tableau empty and stock empty means win