Skip to content

Commit 1ebccd6

Browse files
Merge pull request #896 from GKnirps/rust_23_matches
Add rust implementation for 23 Matches
2 parents f062b60 + b11b45e commit 1ebccd6

File tree

6 files changed

+138
-2
lines changed

6 files changed

+138
-2
lines changed

93_23_Matches/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ http://www.vintage-basic.net/games.html
1919

2020
#### Porting Notes
2121

22-
(please note any difficulties or challenges in porting here)
22+
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.

93_23_Matches/rust/Cargo.lock

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

93_23_Matches/rust/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "twenty-three-matches"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
fastrand = "^2.0.0"

93_23_Matches/rust/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
2+
3+
Conversion to [rust](https://www.rust-lang.org/)

93_23_Matches/rust/src/main.rs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
use std::io;
2+
use std::io::{stdin, stdout, BufRead, Write};
3+
4+
fn main() -> io::Result<()> {
5+
intro();
6+
let mut input = stdin().lock();
7+
let mut buf = String::with_capacity(16);
8+
// variable `N` in the original game
9+
let mut matches: u8 = 23;
10+
if fastrand::bool() {
11+
println!("TAILS! YOU GO FIRST. \n");
12+
} else {
13+
println!("HEADS! I WIN! HA! HA!\nPREPARE TO LOSE, MEATBALL-NOSE!!\n\nI TAKE 2 MATCHES");
14+
matches -= 2;
15+
println!("THE NUMBER OF MATCHES IS NOW {matches}\n");
16+
println!("YOUR TURN -- YOU MAY TAKE 1, 2 OR 3 MATCHES.");
17+
}
18+
loop {
19+
// variable `K` in the original game
20+
let human_picked = read_matches(&mut input, &mut buf)?;
21+
matches = matches.saturating_sub(human_picked);
22+
if matches == 0 {
23+
// this can only happen if the player could win with the next turn but they take too
24+
// many matches (e.g. if there are three matches left and the player takes three instead of
25+
// two). In the original game, this would count as a win for the player.
26+
println!("\nYOU POOR BOOB! YOU TOOK THE LAST MATCH! I GOTCHA!!\nHA ! HA ! I BEAT YOU !!!\n\nGOOD BYE LOSER!");
27+
return Ok(());
28+
}
29+
30+
println!("THERE ARE NOW {matches} MATCHES REMAINING.");
31+
32+
if matches <= 1 {
33+
println!("YOU WON, FLOPPY EARS !\nTHINK YOU'RE PRETTY SMART !\nLETS PLAY AGAIN AND I'LL BLOW YOUR SHOES OFF !!");
34+
return Ok(());
35+
}
36+
37+
// variable `Z` in the original game
38+
let ai_picked = ai_pick(matches, human_picked);
39+
println!("MY TURN ! I REMOVE {ai_picked} MATCHES");
40+
matches = matches.saturating_sub(ai_picked);
41+
// The AI will never pick the last match except for the case where only one match is left (which is handled above)
42+
if matches == 1 {
43+
println!("\nYOU POOR BOOB! YOU TOOK THE LAST MATCH! I GOTCHA!!\nHA ! HA ! I BEAT YOU !!!\n\nGOOD BYE LOSER!");
44+
return Ok(());
45+
}
46+
println!("THE NUMBER OF MATCHES IS NOW {matches}\n");
47+
println!("YOUR TURN -- YOU MAY TAKE 1, 2 OR 3 MATCHES.");
48+
}
49+
}
50+
51+
fn intro() {
52+
println!(
53+
r" 23 MATCHES
54+
CREATIVE COMPUTING MORRISTOWN, NEW JERSEY
55+
56+
57+
58+
THIS IS A GAME CALLED '23 MATCHES'.
59+
60+
WHEN IT IS YOUR TURN, YOU MAY TAKE ONE, TWO, OR THREE
61+
MATCHES. THE OBJECT OF THE GAME IS NOT TO HAVE TO TAKE
62+
THE LAST MATCH.
63+
64+
LET'S FLIP A COIN TO SEE WHO GOES FIRST.
65+
IF IT COMES UP HEADS, I WILL WIN THE TOSS.
66+
"
67+
);
68+
}
69+
70+
fn read_matches<R: BufRead>(mut input: R, buf: &mut String) -> io::Result<u8> {
71+
print!("HOW MANY DO YOU WISH TO REMOVE ?? ");
72+
stdout().flush()?;
73+
loop {
74+
let input = read_int(&mut input, buf)?;
75+
if input <= 0 || input > 3 {
76+
print!("VERY FUNNY! DUMMY!\nDO YOU WANT TO PLAY OR GOOF AROUND?\nNOW, HOW MANY MATCHES DO YOU WANT ?? ");
77+
stdout().flush()?;
78+
} else {
79+
return Ok(input as u8);
80+
}
81+
}
82+
}
83+
84+
fn read_int<R: BufRead>(mut input: R, buf: &mut String) -> io::Result<i8> {
85+
loop {
86+
buf.clear();
87+
input.read_line(buf)?;
88+
let line = buf.trim();
89+
// This is implicit behaviour in the original code: empty input is equal to 0
90+
if line.is_empty() {
91+
return Ok(0);
92+
}
93+
if let Ok(n) = line.parse::<i8>() {
94+
return Ok(n);
95+
} else {
96+
print!("??REENTER\n?? ");
97+
stdout().flush()?;
98+
}
99+
}
100+
}
101+
102+
fn ai_pick(matches: u8, human_picked: u8) -> u8 {
103+
if matches < 4 {
104+
matches - 1
105+
} else {
106+
4 - human_picked
107+
}
108+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ NOTE: per [the official blog post announcement](https://blog.codinghorror.com/up
170170
| 90_Tower | x | x | x | | | x | x | | x | x |
171171
| 91_Train | x | x | x | | | x | x | x | x | x |
172172
| 92_Trap | x | x | x | | | x | x | x | x | x |
173-
| 93_23_Matches | x | x | x | | | x | x | x | | x |
173+
| 93_23_Matches | x | x | x | | | x | x | x | x | x |
174174
| 94_War | x | x | x | x | | x | x | x | x | x |
175175
| 95_Weekday | x | x | x | | | x | x | | x | x |
176176
| 96_Word | x | x | x | | | x | x | x | x | x |

0 commit comments

Comments
 (0)