ESP Game

The problem

Write a program that test your esp (extrasensory perception). The program should randomly select the name of a color from the following list of words:

  • red, green, blue, orange, yellow

To select a select a word, the program can generate a random number. for example, if the number is 0, the selected word is red. Next the program should ask the user to enter a color that the computer has selected. After the user entered his or her guess, the program should display the name of the randomly selected color. the program should repeat 10 times and the display the number of times the user correctly guess the selected color. be sure to modularize the program into methods that perform each major task.

Breaking it down

static List<String> choices = Lists.newArrayList("red", "green", "orange", "blue", "yellow");

public static void main(String[] args) {

    int correctGuesses = 0;

    String input;
    Scanner keyboard = new Scanner(System.in);

    // join list for display
    String colors = String.join(", ", choices);

    // Play the game for 10 rounds.
    for (int round = 1; round <= 10; round++) {

        System.out.print("I'm thinking of a color " + colors + ":");

        input = keyboard.next();

        while (!isValidChoice(input)) {
            System.out.print("Please enter " + colors + ":");
            input = keyboard.next();
        }

        if (computerChoice().equalsIgnoreCase(input)) {
            correctGuesses ++;
        }
    }

    //close scanner
    keyboard.close();

    // show output
    System.out.println("Number of correct guesses: " + correctGuesses);
}

/**
 * Method should return computer choice
 *
 * @return color
 */
static String computerChoice() {
    Random random = new Random();
    OptionalInt computerChoice = random.ints(0, choices.size() - 1).findFirst();

    return choices.get(computerChoice.getAsInt());
}

/**
 * Check if input is contained within list
 *
 * @param input
 * @return
 */
static boolean isValidChoice(String input) {

    java.util.Optional<String> val = choices.stream()
            .filter(e -> e.equals(input.toLowerCase())).findAny();

    return val.isPresent();
}

Output

I'm thinking of a color red, green, orange, blue, yellow:red
I'm thinking of a color red, green, orange, blue, yellow:blue
I'm thinking of a color red, green, orange, blue, yellow:bl
Please enter red, green, orange, blue, yellow:blue
I'm thinking of a color red, green, orange, blue, yellow:green
I'm thinking of a color red, green, orange, blue, yellow:yellow
I'm thinking of a color red, green, orange, blue, yellow:red
I'm thinking of a color red, green, orange, blue, yellow:orange
I'm thinking of a color red, green, orange, blue, yellow:red
I'm thinking of a color red, green, orange, blue, yellow:green
I'm thinking of a color red, green, orange, blue, yellow:yellow
Number of correct guesses: 2

Unit tests

@Test
public void test_computerChoice() {
    String val = ESPGame.computerChoice();

    assertTrue(val != null);
}

@Test
public void test_isValidChoice() {
    boolean val = ESPGame.isValidChoice("green");

    assertTrue(val);
}

@Test
public void test_isNotValidChoice() {
    boolean val = ESPGame.isValidChoice("greennn");

    assertFalse(val);
}

Level Up

  • Adjust program to allow user to play as many times desired