In: Computer Science
Design a program using Raptor Flowcharts that tests your ESP, or extrasensory perception. The program will randomly pick a color from Red, Green, Blue, Orange, Yellow, and Purple, and you will be asked to predict the program's selection before it is revealed. The program should ask the user to enter the color the computer has selected. The program should ensure user enters only one of the six colors. After user has entered his or her guess, the program should display the name of the randomly selected color. The program should repeat this 10 times and then display the number of times the user correctly guessed the selected color.
static List<String> choices = Lists.newArrayList("red", "green", "blue", "orange", "yellow", "violet");
public ststic 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++)
{
Sysyem.out.print("my predition of the color is " + colors + ":");
input = keyboard.next();
while(!isValidChoice(input)) {
System.out.print("Please enter " + colors+ ":");
input = keyboard.next();
}
if(computerChoice().equalsIgnoreCase(input))
{
correctGuesses ++;
}
}
Keyboard.close();
System.out.println("Number of correct guesses: " +correctGuesses);
}
static String computerChoice()
{
Random random = new Random();
OptionalInt computerChoice = random.ints(0, choices.size() -1).findFirst();
return choices.get(computerChoice.getAsInt());
}
static boolean isValidChoice(String input)
{
java.util.Optional<String> val = choices.stream().filter(e -> e.equals(input.toLowerCase())).findAny();
return val.isPresent();
}