From 32e6b885ce3d8e6c05f3722dba8633a0e59c3af7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torben=20M=C3=B6ller?= Date: Sat, 1 Jan 2022 20:17:09 +0100 Subject: [PATCH] feat: Implement 93 23_Matches in Java --- 93_23_Matches/java/CoinSide.java | 4 + 93_23_Matches/java/Messages.java | 70 ++++++ 93_23_Matches/java/TwentyThreeMatches.java | 218 ++++++------------ .../java/TwentyThreeMatchesGame.java | 24 ++ 4 files changed, 165 insertions(+), 151 deletions(-) create mode 100644 93_23_Matches/java/CoinSide.java create mode 100644 93_23_Matches/java/Messages.java create mode 100644 93_23_Matches/java/TwentyThreeMatchesGame.java diff --git a/93_23_Matches/java/CoinSide.java b/93_23_Matches/java/CoinSide.java new file mode 100644 index 00000000..7126e9b4 --- /dev/null +++ b/93_23_Matches/java/CoinSide.java @@ -0,0 +1,4 @@ +public enum CoinSide { + HEADS, + TAILS +} diff --git a/93_23_Matches/java/Messages.java b/93_23_Matches/java/Messages.java new file mode 100644 index 00000000..c0e52b7c --- /dev/null +++ b/93_23_Matches/java/Messages.java @@ -0,0 +1,70 @@ +public class Messages { + + // This is a utility class and contains only static members. + // Utility classes are not meant to be instantiated. + private Messages() { + throw new IllegalStateException("Utility class"); + } + + public static final String INTRO = """ + 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. + """; + + public static final String HEADS = """ + HEADS! I WIN! HA! HA! + PREPARE TO LOSE, MEATBALL-NOSE!! + + I TAKE 2 MATCHES + """; + + public static final String TAILS = """ + TAILS! YOU GO FIRST. + """; + + public static final String MATCHES_LEFT = """ + THE NUMBER OF MATCHES IS NOW %d + + YOUR TURN -- YOU MAY TAKE 1, 2 OR 3 MATCHES. + """; + + public static final String REMOVE_MATCHES_QUESTION = "HOW MANY DO YOU WISH TO REMOVE? "; + + public static final String REMAINING_MATCHES = """ + THERE ARE NOW %d MATCHES REMAINING. + """; + + public static final String INVALID = """ + VERY FUNNY! DUMMY! + DO YOU WANT TO PLAY OR GOOF AROUND? + NOW, HOW MANY MATCHES DO YOU WANT? + """; + + public static final String WIN = """ + YOU WON, FLOPPY EARS ! + THINK YOU'RE PRETTY SMART ! + LETS PLAY AGAIN AND I'LL BLOW YOUR SHOES OFF !! + """; + + public static final String CPU_TURN = """ + MY TURN ! I REMOVE %d MATCHES. + """; + + public static final String LOSE = """ + YOU POOR BOOB! YOU TOOK THE LAST MATCH! I GOTCHA!! + HA ! HA ! I BEAT YOU !!! + + GOOD BYE LOSER! + """; +} diff --git a/93_23_Matches/java/TwentyThreeMatches.java b/93_23_Matches/java/TwentyThreeMatches.java index 46b12793..f0c9836a 100644 --- a/93_23_Matches/java/TwentyThreeMatches.java +++ b/93_23_Matches/java/TwentyThreeMatches.java @@ -1,163 +1,79 @@ +import java.util.Random; import java.util.Scanner; -import java.lang.Math; -/** - * Game of 23 Matches - *

- * Based on the BASIC game of 23 Matches here - * https://github.com/coding-horror/basic-computer-games/blob/main/93%2023%20Matches/23matches.bas - *

- * Note: The idea was to create a version of the 1970's BASIC game in Java, without introducing - * new features - no additional text, error checking, etc has been added. - * - * Converted from BASIC to Java by Darren Cardenas. - */ - -public class TwentyThreeMatches { +public class TwentyThreeMatches { - private static final int MATCH_COUNT_START = 23; + private static final int MATCH_COUNT_START = 23; + private static final Random RAND = new Random(); + private final Scanner scan = new Scanner(System.in); - private static final int HEADS = 1; - - private final Scanner scan; // For user input - - public TwentyThreeMatches() { - - scan = new Scanner(System.in); - - } // End of constructor TwentyThreeMatches + public void startGame() { + //Initialize values + int cpuRemoves = 0; + int matchesLeft = MATCH_COUNT_START; + int playerRemoves = 0; - public void play() { - - showIntro(); - startGame(); - - } // End of method play - - private static void showIntro() { - - System.out.println(" ".repeat(30) + "23 MATCHES"); - System.out.println(" ".repeat(14) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"); - System.out.println("\n\n"); + //Flip coin and decide who goes first. + CoinSide coinSide = flipCoin(); + if (coinSide == CoinSide.HEADS) { + System.out.println(Messages.HEADS); + matchesLeft -= 2; + } else { + System.out.println(Messages.TAILS); + } - System.out.println(" THIS IS A GAME CALLED '23 MATCHES'."); - System.out.println(""); - System.out.println("WHEN IT IS YOUR TURN, YOU MAY TAKE ONE, TWO, OR THREE"); - System.out.println("MATCHES. THE OBJECT OF THE GAME IS NOT TO HAVE TO TAKE"); - System.out.println("THE LAST MATCH."); - System.out.println(""); - System.out.println("LET'S FLIP A COIN TO SEE WHO GOES FIRST."); - System.out.println("IF IT COMES UP HEADS, I WILL WIN THE TOSS."); - System.out.println(""); - - } // End of method showIntro - - private void startGame() { + // Game loop + while (true) { + //Show matches left if CPU went first or Player already removed matches + if (coinSide == CoinSide.HEADS) { + System.out.format(Messages.MATCHES_LEFT, matchesLeft); + } + coinSide = CoinSide.HEADS; - int coinSide = (int) (3 * Math.random()); - int cpuRemoves = 0; - int matchesLeft = MATCH_COUNT_START; - int playerRemoves = 0; - - if (coinSide == HEADS) { - - System.out.println("HEADS! I WIN! HA! HA!"); - System.out.println("PREPARE TO LOSE, MEATBALL-NOSE!!"); - System.out.println(""); - System.out.println("I TAKE 2 MATCHES"); - - matchesLeft -= 2; - - } else { - - System.out.println("TAILS! YOU GO FIRST. "); - System.out.println(""); - + // Player removes matches + System.out.println(Messages.REMOVE_MATCHES_QUESTION); + playerRemoves = turnOfPlayer(); + matchesLeft -= playerRemoves; + System.out.format(Messages.REMAINING_MATCHES, matchesLeft); + + // If 1 match is left, the CPU has to take it. You win! + if (matchesLeft <= 1) { + System.out.println(Messages.WIN); + return; + } + + // CPU removes matches + // At least two matches are left, because win condition above was not triggered. + if (matchesLeft <= 4) { + cpuRemoves = matchesLeft - 1; + } else { + cpuRemoves = 4 - playerRemoves; + } + System.out.format(Messages.CPU_TURN, cpuRemoves); + matchesLeft -= cpuRemoves; + + // If 1 match is left, the Player has to take it. You lose! + if (matchesLeft <= 1) { + System.out.println(Messages.LOSE); + return; + } + } } - // Begin outer while loop - while (true) { - - if (coinSide == HEADS) { - - System.out.println("THE NUMBER OF MATCHES IS NOW " + matchesLeft); - System.out.println(""); - System.out.println("YOUR TURN -- YOU MAY TAKE 1, 2 OR 3 MATCHES."); - - } - - coinSide = HEADS; + private CoinSide flipCoin() { + return RAND.nextBoolean() ? CoinSide.HEADS : CoinSide.TAILS; + } - System.out.print("HOW MANY DO YOU WISH TO REMOVE? "); - - // Begin match removal while loop - while (true) { - - playerRemoves = scan.nextInt(); - - // Handle invalid entries - if ((playerRemoves > 3) || (playerRemoves <= 0)) { - - System.out.println("VERY FUNNY! DUMMY!"); - System.out.println("DO YOU WANT TO PLAY OR GOOF AROUND?"); - System.out.print("NOW, HOW MANY MATCHES DO YOU WANT? "); - continue; - + private int turnOfPlayer() { + while (true) { + int playerRemoves = scan.nextInt(); + // Handle invalid entries + if ((playerRemoves > 3) || (playerRemoves <= 0)) { + System.out.println(Messages.INVALID); + continue; + } + return playerRemoves; } - - break; - - } // End match removal while loop - - matchesLeft -= playerRemoves; - - System.out.println("THERE ARE NOW " + matchesLeft + " MATCHES REMAINING."); - - // Win condition - if (matchesLeft <= 1) { - - // Win condition - System.out.println("YOU WON, FLOPPY EARS !"); - System.out.println("THINK YOU'RE PRETTY SMART !"); - System.out.println("LETS PLAY AGAIN AND I'LL BLOW YOUR SHOES OFF !!"); - System.out.println(""); - return; - - } else if ((matchesLeft >= 2) && (matchesLeft <= 4)) { - - cpuRemoves = matchesLeft - 1; - - } else { - - cpuRemoves = 4 - playerRemoves; - - } - - System.out.println("MY TURN ! I REMOVE " + cpuRemoves + " MATCHES"); - - matchesLeft -= cpuRemoves; + } - // Lose condition - if (matchesLeft <= 1) { - - System.out.println(""); - System.out.println("YOU POOR BOOB! YOU TOOK THE LAST MATCH! I GOTCHA!!"); - System.out.println("HA ! HA ! I BEAT YOU !!!"); - System.out.println(""); - System.out.println("GOOD BYE LOSER!"); - return; - - } - - } // End outer while loop - - } // End of method startGame - - public static void main(String[] args) { - - TwentyThreeMatches game = new TwentyThreeMatches(); - game.play(); - - } // End of method main - -} // End of class TwentyThreeMatches +} diff --git a/93_23_Matches/java/TwentyThreeMatchesGame.java b/93_23_Matches/java/TwentyThreeMatchesGame.java new file mode 100644 index 00000000..01735a91 --- /dev/null +++ b/93_23_Matches/java/TwentyThreeMatchesGame.java @@ -0,0 +1,24 @@ +/** + * Game of 23 Matches + *

+ * Based on the BASIC game of 23 Matches here + * https://github.com/coding-horror/basic-computer-games/blob/main/93%2023%20Matches/23matches.bas + *

+ * Note: The idea was to create a version of the 1970's BASIC game in Java, without introducing + * new features - no additional text, error checking, etc has been added. + *

+ * Converted from BASIC to Java by Darren Cardenas. + */ +public class TwentyThreeMatchesGame { + + public static void main(String[] args) { + showIntro(); + TwentyThreeMatches game = new TwentyThreeMatches(); + game.startGame(); + } + + private static void showIntro() { + System.out.println(Messages.INTRO); + } + +}