From a2f0a489c1900761b68531dddde848a3d7e5c027 Mon Sep 17 00:00:00 2001 From: Rhys Lloyd Date: Fri, 15 May 2026 10:35:34 -0700 Subject: [PATCH] add undo to cli --- src/main.rs | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index aa5ef14..b410aef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -118,6 +118,23 @@ impl core::str::FromStr for Parsed { } } +enum SessionInstruction { + Undo, + Klondike(KlondikeInstruction), +} +impl core::str::FromStr for SessionInstruction { + type Err = Invalid; + fn from_str(s: &str) -> Result { + Ok(match s { + "UNDO" => Self::Undo, + other => { + let Parsed(ki) = other.parse()?; + Self::Klondike(ki) + } + }) + } +} + fn main() -> Result<(), std::io::Error> { let mut session = Session::new(Klondike::new_random_default()); loop { @@ -127,12 +144,15 @@ fn main() -> Result<(), std::io::Error> { // parse input let mut input = String::new(); std::io::stdin().read_line(&mut input)?; - let Ok(Parsed(instruction)) = input.trim().parse() else { + let Ok(instruction) = input.trim().parse() else { println!("Invalid move!"); continue; }; - // perform move - session.process_instruction(instruction); + // run game + match instruction { + SessionInstruction::Undo => session.undo(), + SessionInstruction::Klondike(instruction) => session.process_instruction(instruction), + } } }