mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 07:10:42 -08:00
feat: Implement 93 23_Matches in Java
This commit is contained in:
@@ -1,163 +1,79 @@
|
||||
import java.util.Random;
|
||||
import java.util.Scanner;
|
||||
import java.lang.Math;
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user