feat: Implement 93 23_Matches in Java

This commit is contained in:
Torben Möller
2022-01-01 20:17:09 +01:00
parent 5439ece531
commit 32e6b885ce
4 changed files with 165 additions and 151 deletions

View File

@@ -0,0 +1,4 @@
public enum CoinSide {
HEADS,
TAILS
}

View File

@@ -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!
""";
}

View File

@@ -1,163 +1,79 @@
import java.util.Random;
import java.util.Scanner; import java.util.Scanner;
import java.lang.Math;
/** public class TwentyThreeMatches {
* Game of 23 Matches
* <p>
* Based on the BASIC game of 23 Matches here
* https://github.com/coding-horror/basic-computer-games/blob/main/93%2023%20Matches/23matches.bas
* <p>
* 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 {
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; public void startGame() {
//Initialize values
private final Scanner scan; // For user input int cpuRemoves = 0;
int matchesLeft = MATCH_COUNT_START;
public TwentyThreeMatches() { int playerRemoves = 0;
scan = new Scanner(System.in);
} // End of constructor TwentyThreeMatches
public void play() { //Flip coin and decide who goes first.
CoinSide coinSide = flipCoin();
showIntro(); if (coinSide == CoinSide.HEADS) {
startGame(); System.out.println(Messages.HEADS);
matchesLeft -= 2;
} // End of method play } else {
System.out.println(Messages.TAILS);
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");
System.out.println(" THIS IS A GAME CALLED '23 MATCHES'."); // Game loop
System.out.println(""); while (true) {
System.out.println("WHEN IT IS YOUR TURN, YOU MAY TAKE ONE, TWO, OR THREE"); //Show matches left if CPU went first or Player already removed matches
System.out.println("MATCHES. THE OBJECT OF THE GAME IS NOT TO HAVE TO TAKE"); if (coinSide == CoinSide.HEADS) {
System.out.println("THE LAST MATCH."); System.out.format(Messages.MATCHES_LEFT, matchesLeft);
System.out.println(""); }
System.out.println("LET'S FLIP A COIN TO SEE WHO GOES FIRST."); coinSide = CoinSide.HEADS;
System.out.println("IF IT COMES UP HEADS, I WILL WIN THE TOSS.");
System.out.println("");
} // End of method showIntro
private void startGame() {
int coinSide = (int) (3 * Math.random()); // Player removes matches
int cpuRemoves = 0; System.out.println(Messages.REMOVE_MATCHES_QUESTION);
int matchesLeft = MATCH_COUNT_START; playerRemoves = turnOfPlayer();
int playerRemoves = 0; matchesLeft -= playerRemoves;
System.out.format(Messages.REMAINING_MATCHES, matchesLeft);
if (coinSide == HEADS) {
// If 1 match is left, the CPU has to take it. You win!
System.out.println("HEADS! I WIN! HA! HA!"); if (matchesLeft <= 1) {
System.out.println("PREPARE TO LOSE, MEATBALL-NOSE!!"); System.out.println(Messages.WIN);
System.out.println(""); return;
System.out.println("I TAKE 2 MATCHES"); }
matchesLeft -= 2; // CPU removes matches
// At least two matches are left, because win condition above was not triggered.
} else { if (matchesLeft <= 4) {
cpuRemoves = matchesLeft - 1;
System.out.println("TAILS! YOU GO FIRST. "); } else {
System.out.println(""); 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 private CoinSide flipCoin() {
while (true) { return RAND.nextBoolean() ? CoinSide.HEADS : CoinSide.TAILS;
}
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;
System.out.print("HOW MANY DO YOU WISH TO REMOVE? "); private int turnOfPlayer() {
while (true) {
// Begin match removal while loop int playerRemoves = scan.nextInt();
while (true) { // Handle invalid entries
if ((playerRemoves > 3) || (playerRemoves <= 0)) {
playerRemoves = scan.nextInt(); System.out.println(Messages.INVALID);
continue;
// Handle invalid entries }
if ((playerRemoves > 3) || (playerRemoves <= 0)) { return playerRemoves;
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;
} }
}
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

View File

@@ -0,0 +1,24 @@
/**
* Game of 23 Matches
* <p>
* Based on the BASIC game of 23 Matches here
* https://github.com/coding-horror/basic-computer-games/blob/main/93%2023%20Matches/23matches.bas
* <p>
* 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.
* <p>
* 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);
}
}