From a358e441cee622f018d852c6055cedead39d52b9 Mon Sep 17 00:00:00 2001 From: Guido Knips Date: Wed, 30 Aug 2023 21:51:03 +0200 Subject: [PATCH 1/2] Add rust implementation for 23 Matches --- 93_23_Matches/README.md | 2 +- 93_23_Matches/rust/Cargo.lock | 16 +++++ 93_23_Matches/rust/Cargo.toml | 9 +++ 93_23_Matches/rust/README.md | 3 + 93_23_Matches/rust/src/main.rs | 108 +++++++++++++++++++++++++++++++++ 5 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 93_23_Matches/rust/Cargo.lock create mode 100644 93_23_Matches/rust/Cargo.toml create mode 100644 93_23_Matches/rust/README.md create mode 100644 93_23_Matches/rust/src/main.rs diff --git a/93_23_Matches/README.md b/93_23_Matches/README.md index 8ef8d6f8a..e4ece5fd5 100644 --- a/93_23_Matches/README.md +++ b/93_23_Matches/README.md @@ -19,4 +19,4 @@ http://www.vintage-basic.net/games.html #### Porting Notes -(please note any difficulties or challenges in porting here) +There is an oddity (you can call it a bug, but it is no big deal) in the original code. If there are only two or three matches left at the player's turn and the player picks all of them (or more), the game would still register that as a win for the player. diff --git a/93_23_Matches/rust/Cargo.lock b/93_23_Matches/rust/Cargo.lock new file mode 100644 index 000000000..96e2f79ef --- /dev/null +++ b/93_23_Matches/rust/Cargo.lock @@ -0,0 +1,16 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "fastrand" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6999dc1837253364c2ebb0704ba97994bd874e8f195d665c50b7548f6ea92764" + +[[package]] +name = "twenty-three-matches" +version = "0.1.0" +dependencies = [ + "fastrand", +] diff --git a/93_23_Matches/rust/Cargo.toml b/93_23_Matches/rust/Cargo.toml new file mode 100644 index 000000000..a4227bbd4 --- /dev/null +++ b/93_23_Matches/rust/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "twenty-three-matches" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +fastrand = "^2.0.0" diff --git a/93_23_Matches/rust/README.md b/93_23_Matches/rust/README.md new file mode 100644 index 000000000..6249450a1 --- /dev/null +++ b/93_23_Matches/rust/README.md @@ -0,0 +1,3 @@ +Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html) + +Conversion to [rust](https://www.rust-lang.org/) diff --git a/93_23_Matches/rust/src/main.rs b/93_23_Matches/rust/src/main.rs new file mode 100644 index 000000000..a14c39f58 --- /dev/null +++ b/93_23_Matches/rust/src/main.rs @@ -0,0 +1,108 @@ +use std::io; +use std::io::{stdin, stdout, BufRead, Write}; + +fn main() -> io::Result<()> { + intro(); + let mut input = stdin().lock(); + let mut buf = String::with_capacity(16); + // variable `N` in the original game + let mut matches: u8 = 23; + if fastrand::bool() { + println!("TAILS! YOU GO FIRST. \n"); + } else { + println!("HEADS! I WIN! HA! HA!\nPREPARE TO LOSE, MEATBALL-NOSE!!\n\nI TAKE 2 MATCHES"); + matches -= 2; + println!("THE NUMBER OF MATCHES IS NOW {matches}\n"); + println!("YOUR TURN -- YOU MAY TAKE 1, 2 OR 3 MATCHES."); + } + loop { + // variable `K` in the original game + let human_picked = read_matches(&mut input, &mut buf)?; + matches = matches.saturating_sub(human_picked); + if matches == 0 { + // this can only happen if the player could win with the next turn but they take too + // many matches (e.g. if there are three matches left and the player takes three instead of + // two). In the original game, this would count as a win for the player. + println!("\nYOU POOR BOOB! YOU TOOK THE LAST MATCH! I GOTCHA!!\nHA ! HA ! I BEAT YOU !!!\n\nGOOD BYE LOSER!"); + return Ok(()); + } + + println!("THERE ARE NOW {matches} MATCHES REMAINING."); + + if matches <= 1 { + println!("YOU WON, FLOPPY EARS !\nTHINK YOU'RE PRETTY SMART !\nLETS PLAY AGAIN AND I'LL BLOW YOUR SHOES OFF !!"); + return Ok(()); + } + + // variable `Z` in the original game + let ai_picked = ai_pick(matches, human_picked); + println!("MY TURN ! I REMOVE {ai_picked} MATCHES"); + matches = matches.saturating_sub(ai_picked); + // The AI will never pick the last match except for the case where only one match is left (which is handled above) + if matches == 1 { + println!("\nYOU POOR BOOB! YOU TOOK THE LAST MATCH! I GOTCHA!!\nHA ! HA ! I BEAT YOU !!!\n\nGOOD BYE LOSER!"); + return Ok(()); + } + println!("THE NUMBER OF MATCHES IS NOW {matches}\n"); + println!("YOUR TURN -- YOU MAY TAKE 1, 2 OR 3 MATCHES."); + } +} + +fn intro() { + println!( + r" 23 MATCHES + CREATIVE COMPUTING MORRISTOWN, NEW JERSEY + + + + THIS IS A GAME CALLED '23 MATCHES'. + +WHEN IT IS YOUR TURN, YOU MAY TAKE ONE, TWO, OR THREE +MATCHES. THE OBJECT OF THE GAME IS NOT TO HAVE TO TAKE +THE LAST MATCH. + +LET'S FLIP A COIN TO SEE WHO GOES FIRST. +IF IT COMES UP HEADS, I WILL WIN THE TOSS. +" + ); +} + +fn read_matches(mut input: R, buf: &mut String) -> io::Result { + print!("HOW MANY DO YOU WISH TO REMOVE ?? "); + stdout().flush()?; + loop { + let input = read_int(&mut input, buf)?; + if input <= 0 || input > 3 { + print!("VERY FUNNY! DUMMY!\nDO YOU WANT TO PLAY OR GOOF AROUND?\nNOW, HOW MANY MATCHES DO YOU WANT ?? "); + stdout().flush()?; + } else { + return Ok(input as u8); + } + } +} + +fn read_int(mut input: R, buf: &mut String) -> io::Result { + loop { + buf.clear(); + input.read_line(buf)?; + let line = buf.trim(); + // This is implicit behaviour in the original code: empty input is equal to 0 + if line.is_empty() { + return Ok(0); + } + if let Ok(n) = line.parse::() { + return Ok(n); + } else { + print!("??REENTER\n?? "); + stdout().flush()?; + } + } +} + +fn ai_pick(matches: u8, human_picked: u8) -> u8 { + if matches < 4 { + matches - 1 + } else { + 4 - human_picked + } +} From b11b45e7906637c94d4c45c75712bc021ecf0781 Mon Sep 17 00:00:00 2001 From: Guido Knips Date: Wed, 30 Aug 2023 22:09:30 +0200 Subject: [PATCH 2/2] List 23 Matches as implemented for rust --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0544ea2e0..0ddd2748f 100644 --- a/README.md +++ b/README.md @@ -170,7 +170,7 @@ NOTE: per [the official blog post announcement](https://blog.codinghorror.com/up | 90_Tower | x | x | x | | | x | x | | x | x | | 91_Train | x | x | x | | | x | x | x | x | x | | 92_Trap | x | x | x | | | x | x | x | x | x | -| 93_23_Matches | x | x | x | | | x | x | x | | x | +| 93_23_Matches | x | x | x | | | x | x | x | x | x | | 94_War | x | x | x | x | | x | x | x | x | x | | 95_Weekday | x | x | x | | | x | x | | x | x | | 96_Word | x | x | x | | | x | x | x | x | x |