I made a Three Monte Card game using Java
Problem:
This is the original "ball and cups" game where you try to find out which cup has the ball under it. You may play with three cups and a ball, three cards (two jacks and an ace), or three doors and a car. Basically, randomly select a cup to hide the "ball". Let the player guess which cup hides the ball. The player wins if they guess correctly.
Problem taken from:
https://programmingbydoing.com/a/three-card-monte.html
My code:
/*
* Code by CLint
*/
import java.util.Random;
import java.util.Scanner;
public class ThreeCardMonte {
public static void main(String[] args) {
Random random = new Random();
Scanner input = new Scanner(System.in);
int userInput;
int randomN = random.nextInt(3)+1;
System.out.println("You slide up to Fast Eddie's card table and plop down your cash.\n" +
"He glances at you out of the corner of his eye and starts shuffling.\n" +
"He lays down three cards.\n");
System.out.println("Which one is the ace?\n" +
"\t##\t##\t##\n" +
"\t##\t##\t##\n" +
"\t1\t2\t3");
System.out.print("\n> ");
userInput = input.nextInt();
if (userInput == randomN) {
if (randomN == 1) {
System.out.println("\nYou nailed it! Fast Eddie reluctantly hands over your winnings, scowling.\n" +
"\tAA\t##\t##\n" +
"\tAA\t##\t##\n" +
"\t1\t2\t3");
} else if (randomN == 2) {
System.out.println("\nYou nailed it! Fast Eddie reluctantly hands over your winnings, scowling.\n" +
"\t##\tAA\t##\n" +
"\t##\tAA\t##\n" +
"\t1\t2\t3");
} else if (randomN == 3) {
System.out.println("\nYou nailed it! Fast Eddie reluctantly hands over your winnings, scowling.\n" +
"\t##\t##\tAA\n" +
"\t##\t##\tAA\n" +
"\t1\t2\t3");
}
}
if (userInput != randomN) {
if (randomN == 1) {
System.out.println("\nHa! Fast Eddie wins again! The ace was card number 1.\n" +
"\tAA\t##\t##\n" +
"\tAA\t##\t##\n" +
"\t1\t2\t3");
} else if (randomN == 2) {
System.out.println("\nHa! Fast Eddie wins again! The ace was card number 2.\n" +
"\t##\tAA\t##\n" +
"\t##\tAA\t##\n" +
"\t1\t2\t3");
} else if (randomN == 3) {
System.out.println("\nHa! Fast Eddie wins again! The ace was card number 3.\n" +
"\t##\t##\tAA\n" +
"\t##\t##\tAA\n" +
"\t1\t2\t3");
}
}
}
}
The output:
PS: I am open for corrections to make my code for efficient and clean.