mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2026-01-16 15:03:50 -08:00
add 00_Alternate_Langage folder and move alternate ports there
This commit is contained in:
4
00_Alternate_Languages/93_23_Matches/java/CoinSide.java
Normal file
4
00_Alternate_Languages/93_23_Matches/java/CoinSide.java
Normal file
@@ -0,0 +1,4 @@
|
||||
public enum CoinSide {
|
||||
HEADS,
|
||||
TAILS
|
||||
}
|
||||
70
00_Alternate_Languages/93_23_Matches/java/Messages.java
Normal file
70
00_Alternate_Languages/93_23_Matches/java/Messages.java
Normal 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!
|
||||
""";
|
||||
}
|
||||
3
00_Alternate_Languages/93_23_Matches/java/README.md
Normal file
3
00_Alternate_Languages/93_23_Matches/java/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [Oracle Java](https://openjdk.java.net/)
|
||||
@@ -0,0 +1,79 @@
|
||||
import java.util.Random;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class TwentyThreeMatches {
|
||||
|
||||
private static final int MATCH_COUNT_START = 23;
|
||||
private static final Random RAND = new Random();
|
||||
private final Scanner scan = new Scanner(System.in);
|
||||
|
||||
public void startGame() {
|
||||
//Initialize values
|
||||
int cpuRemoves = 0;
|
||||
int matchesLeft = MATCH_COUNT_START;
|
||||
int playerRemoves = 0;
|
||||
|
||||
//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);
|
||||
}
|
||||
|
||||
// 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;
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private CoinSide flipCoin() {
|
||||
return RAND.nextBoolean() ? CoinSide.HEADS : CoinSide.TAILS;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user