diff --git a/src/card_game.rs b/src/card_game.rs index 47de146..02828b9 100644 --- a/src/card_game.rs +++ b/src/card_game.rs @@ -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() diff --git a/src/klondike.rs b/src/klondike.rs index a446305..2b5ebb6 100644 --- a/src/klondike.rs +++ b/src/klondike.rs @@ -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