fix stock reset

This commit is contained in:
2026-05-15 08:38:56 -07:00
parent 30f2fec61b
commit daf7fcd8f3
2 changed files with 15 additions and 4 deletions
+2 -2
View File
@@ -138,8 +138,8 @@ impl Pile {
face_up: Stack::new(),
}
}
pub fn make_face_down(&mut self) {
self.face_down.extend(self.face_up.drain(..));
pub fn swap_up_down(&mut self) {
core::mem::swap(&mut self.face_up, &mut self.face_down);
}
pub fn is_empty(&self) -> bool {
self.face_down.is_empty() && self.face_up.is_empty()
+13 -2
View File
@@ -215,8 +215,19 @@ impl Game for Klondike {
self.state.is_instruction_valid(instruction)
}
fn process_instruction(&mut self, instruction: Self::Instruction) {
let card = self.pile_mut(instruction.src).pop().unwrap();
self.pile_mut(instruction.dst).push(card);
match instruction {
// Reset the stock if it's empty
KlondikeInstruction {
src: KlondikePileId::Stock,
dst: KlondikePileId::Stock,
} if self.pile(KlondikePileId::Stock).is_empty() => {
self.pile_mut(KlondikePileId::Stock).swap_up_down();
}
KlondikeInstruction { src, dst } => {
let card = self.pile_mut(src).pop().unwrap();
self.pile_mut(dst).push(card);
}
}
}
fn is_win(&self) -> bool {
// assuming only valid moves, tableau empty and stock empty means win