Skip to content

Commit

Permalink
Merge pull request #896 from GKnirps/rust_23_matches
Browse files Browse the repository at this point in the history
Add rust implementation for 23 Matches
  • Loading branch information
coding-horror authored Aug 30, 2023
2 parents f062b60 + b11b45e commit 1ebccd6
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 2 deletions.
2 changes: 1 addition & 1 deletion 93_23_Matches/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
16 changes: 16 additions & 0 deletions 93_23_Matches/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions 93_23_Matches/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
3 changes: 3 additions & 0 deletions 93_23_Matches/rust/README.md
Original file line number Diff line number Diff line change
@@ -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/)
108 changes: 108 additions & 0 deletions 93_23_Matches/rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -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<R: BufRead>(mut input: R, buf: &mut String) -> io::Result<u8> {
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<R: BufRead>(mut input: R, buf: &mut String) -> io::Result<i8> {
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::<i8>() {
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
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down

0 comments on commit 1ebccd6

Please sign in to comment.