# Three Cards Monte game

I have written the following code for the Three Cards Monte game. It works as expected and returns the required output.

However when I tried to turn it in to the teacher, he keeps saying its too long.

Is there any way to shorten it but make sure it returns the same output?

import java.util.Scanner;

public class ThreeCardsMonte {
public static void main(String[]args) {
Scanner keyboard = new Scanner(System.in);
int ace;
int guess;

ace = 1 + (int)(Math.random()*3);

System.out.println("You slide up to Fast Eddie's card table and plop down your cash.");
System.out.println("He glances at you out of the corner of his eye and starts shuffling.");
System.out.println("He lays down three cards.");
System.out.println("");
System.out.println("Which one is the ace?");
System.out.println("");
System.out.println("##  ##  ##");
System.out.println("##  ##  ##");
System.out.println("1   2   3");
guess = keyboard.nextInt();

if ((ace == 1) && (guess == 1)) {
System.out.println("You nailed it! Fast Eddie reluctantly hands over your winnings, scowling.");
System.out.println("AA  ##  ##");
System.out.println("AA  ##  ##");
System.out.println("1   2   3");
}
else if ((ace == 1) && (guess != 1)) {
System.out.println("Ha! Fast Eddie wins again! The ace was card number " +ace+".");
System.out.println("AA  ##  ##");
System.out.println("AA  ##  ##");
System.out.println("1   2   3");
}
else if ((ace == 2) && (guess == 2)) {
System.out.println("You nailed it! Fast Eddie reluctantly hands over your winnings, scowling.");
System.out.println("##  AA  ##");
System.out.println("##  AA  ##");
System.out.println("1   2   3");
}
else if ((ace == 2) && (guess != 2)) {
System.out.println("Ha! Fast Eddie wins again! The ace was card number " +ace+".");
System.out.println("##  AA  ##");
System.out.println("##  AA  ##");
System.out.println("1   2   3");
}
else if ((ace == 3) && (guess == 3)) {
System.out.println("You nailed it! Fast Eddie reluctantly hands over your winnings, scowling.");
System.out.println("##  ##  AA");
System.out.println("##  ##  AA");
System.out.println("1   2   3");
}
else if ((ace == 3) && (guess != 3)) {
System.out.println("Ha! Fast Eddie wins again! The ace was card number " +ace+".");
System.out.println("##  ##  AA");
System.out.println("##  ##  AA");
System.out.println("1   2   3");
}
}
}

• Is there a reason you want it to be shorter? Just out of curiosity. – Alex R Feb 3 '17 at 17:04
• I guess that's what she said eh? Jokes apart, what do you think you can do make it shorter. – CKing Feb 3 '17 at 17:04
• Honestly I have no idea what to do here. I just don't know where to start – jackmasterlooter Feb 3 '17 at 17:06
• why not eliminate all of that just do a simple if (ace == guess) and have an else case as well – Ishnark Feb 3 '17 at 17:08
• Well... You could start by looking at your else statements. There are really only two possibilities. ace == guess and ace != guess. This can be simplified to if(ace == guess){//dostuff}else{do other stuff}; – Chris Schneider Feb 3 '17 at 17:09

You have a lot of repeats

 if (ace == guess) {
System.out.println("You nailed it! Fast Eddie reluctantly hands over your winnings, scowling.");
}
else  {
System.out.println("Ha! Fast Eddie wins again! The ace was card number " +ace+".");
}

if (ace == 1) {
System.out.println("AA  ##  ##");
System.out.println("AA  ##  ##");
}
else if (ace == 2) {
System.out.println("##  AA  ##");
System.out.println("##  AA  ##");
}
else if (ace == 3) {
System.out.println("##  ##  AA");
System.out.println("##  ##  AA");
}
System.out.println("1   2   3");


Could put the ace position in an array and just reference it by index
I don't know java to write an array

Using an array would help a lot here.

import java.util.Scanner;

public class ThreeCardsMonte {
public static void main(String[]args) {
final String[] aces = {
"##  ##  ##",
"AA  ##  ##",
"##  AA  ##",
"##  ##  AA"
};
final String nums = "1   2   3";

System.out.println("You slide up to Fast Eddie's card table and plop down your cash.");
System.out.println("He glances at you System.out of the corner of his eye and starts shuffling.");
System.out.println("He lays down three cards.");
System.out.println("");
System.out.println("Which one is the ace?");
System.out.println("");
System.out.println(aces[0]);
System.out.println(aces[0]);
System.out.println(nums);

final int ace = 1 + (int) (Math.random() * 3);
final Scanner keyboard = new Scanner(System.in);
final int guess = keyboard.nextInt();

System.out.println(
(ace == guess) ?
"You nailed it! Fast Eddie reluctantly hands over your winnings, scowling." :
"Ha! Fast Eddie wins again! The ace was card number " + ace + ".");

System.out.println(aces[ace]);
System.out.println(aces[ace]);
System.out.println(nums);
}
}


Each last three print in each if are the same for equals ace, so what you can do is

if(ace == x) {
if(guess == x)
System.out.println("You nailed it! Fast Eddie reluctantly hands over your winnings, scowling.");
else
System.out.println("Ha! Fast Eddie wins again! The ace was card number " +ace+".");
System.out.println("##  ##  AA");
System.out.println("##  ##  AA");
System.out.println("1   2   3");
}

• Don't encourage learners to omit braces. – slim Feb 3 '17 at 17:14

You can shorten the amount of if statements from 6 to 4:

if (ace == guess) {
if (ace == 1) {
Code for if ace was one;
}
if (ace ==2) {
Code for if ace was two;
}
if (ace == 3) {
Code for if ace was three;
}
else {
Whatever code;
}


You can also probably use a switch which would be less characters but around the same amount of lines.

In addition to Abdenaceur Lichiheb's answer, use the special character \n to make multiple lines if you want to avoid multiple printlns.

System.out.println("##  ##  AA\n##  ##  AA\n1   2   3");