Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class ESPGame
- {
- static int colourNum, correctGuesses = 0;;
- static String colour;
- static Scanner kbd = new Scanner(System.in);
- static Random rnd = new Random();
- public static void main(String[] args)
- {
- userChoice();
- System.out.println("You have guessed " + correctGuesses + " colours correct!");
- }
- /**
- The userChoice method get's their color guess while determining if it is correct to the computer's
- */
- public static void userChoice()
- {
- for(int i = 0; i < 5; i++)
- {
- userValidation();
- userCorrect(colour, colourNum);
- computerChoice(colourNum);
- }
- }
- /**
- The computerChoice method prints the color the computer chooses.
- @param colourNum The random color selected by the computer
- */
- public static void computerChoice(int colourNum)
- {
- switch(colourNum)
- {
- case(0):
- System.out.println("The colour is Red");
- break;
- case(1):
- System.out.println("The colour is Green");
- break;
- case(2):
- System.out.println("The colour is Blue");
- break;
- case(3):
- System.out.println("The colour is Orange");
- break;
- case(4):
- System.out.println("The colour is Yellow");
- break;
- }
- }
- /**
- The userChoice method prints the correct guesses and increments correctGuesses counter
- @param colour The color entered by user
- @param colourNum The random color selected by the computer
- @return boolean for is the user was correct or not
- */
- public static boolean userCorrect(String colour, int colourNum)
- {
- switch(colour.toLowerCase())
- {
- case("red"):
- if(colourNum == 0)
- {
- System.out.println("Correct!");
- correctGuesses ++;
- return true;
- }
- break;
- case("green"):
- if(colourNum == 1)
- {
- System.out.println("Correct!");
- correctGuesses ++;
- return true;
- }
- break;
- case("blue"):
- if(colourNum == 2)
- {
- System.out.println("Correct!");
- correctGuesses ++;
- return true;
- }
- break;
- case("orange"):
- if(colourNum == 3)
- {
- System.out.println("Correct!");
- correctGuesses ++;
- return true;
- }
- break;
- case("yellow"):
- if(colourNum == 4)
- {
- System.out.println("Correct!");
- correctGuesses ++;
- return true;
- }
- break;
- }
- return false;
- }
- /**
- The userValidation method repeats if user enters an incorrect answer.
- */
- public static void userValidation()
- {
- colour = "";
- String answer = colour;
- while(!(answer.equalsIgnoreCase("red")|| answer.equalsIgnoreCase("green") || answer.equalsIgnoreCase("blue") || answer.equalsIgnoreCase("orange") || answer.equalsIgnoreCase("yellow")))
- {
- colourNum = rnd.nextInt(5);
- System.out.println("Please enter the colour selected by the computer:");
- colour = kbd.nextLine();
- answer = colour.toLowerCase();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement