|
| 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 | +} |
0 commit comments