Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class ESPGame
  5. {
  6. static int colourNum, correctGuesses = 0;;
  7. static String colour;
  8. static Scanner kbd = new Scanner(System.in);
  9. static Random rnd = new Random();
  10. public static void main(String[] args)
  11. {
  12. userChoice();
  13. System.out.println("You have guessed " + correctGuesses + " colours correct!");
  14. }
  15. /**
  16.  
  17. The userChoice method get's their color guess while determining if it is correct to the computer's
  18.  
  19. */
  20. public static void userChoice()
  21. {
  22. for(int i = 0; i < 5; i++)
  23. {
  24. userValidation();
  25. userCorrect(colour, colourNum);
  26. computerChoice(colourNum);
  27. }
  28. }
  29. /**
  30.  
  31. The computerChoice method prints the color the computer chooses.
  32. @param colourNum The random color selected by the computer
  33.  
  34. */
  35. public static void computerChoice(int colourNum)
  36. {
  37. switch(colourNum)
  38. {
  39. case(0):
  40. System.out.println("The colour is Red");
  41. break;
  42. case(1):
  43. System.out.println("The colour is Green");
  44. break;
  45. case(2):
  46. System.out.println("The colour is Blue");
  47. break;
  48. case(3):
  49. System.out.println("The colour is Orange");
  50. break;
  51. case(4):
  52. System.out.println("The colour is Yellow");
  53. break;
  54. }
  55. }
  56. /**
  57.  
  58. The userChoice method prints the correct guesses and increments correctGuesses counter
  59. @param colour The color entered by user
  60. @param colourNum The random color selected by the computer
  61. @return boolean for is the user was correct or not
  62.  
  63. */
  64. public static boolean userCorrect(String colour, int colourNum)
  65. {
  66. switch(colour.toLowerCase())
  67. {
  68. case("red"):
  69. if(colourNum == 0)
  70. {
  71. System.out.println("Correct!");
  72. correctGuesses ++;
  73. return true;
  74. }
  75. break;
  76. case("green"):
  77. if(colourNum == 1)
  78. {
  79. System.out.println("Correct!");
  80. correctGuesses ++;
  81. return true;
  82. }
  83. break;
  84. case("blue"):
  85. if(colourNum == 2)
  86. {
  87. System.out.println("Correct!");
  88. correctGuesses ++;
  89. return true;
  90. }
  91. break;
  92. case("orange"):
  93. if(colourNum == 3)
  94. {
  95. System.out.println("Correct!");
  96. correctGuesses ++;
  97. return true;
  98. }
  99. break;
  100. case("yellow"):
  101. if(colourNum == 4)
  102. {
  103. System.out.println("Correct!");
  104. correctGuesses ++;
  105. return true;
  106. }
  107. break;
  108. }
  109. return false;
  110. }
  111. /**
  112.  
  113. The userValidation method repeats if user enters an incorrect answer.
  114.  
  115. */
  116. public static void userValidation()
  117. {
  118. colour = "";
  119. String answer = colour;
  120. while(!(answer.equalsIgnoreCase("red")|| answer.equalsIgnoreCase("green") || answer.equalsIgnoreCase("blue") || answer.equalsIgnoreCase("orange") || answer.equalsIgnoreCase("yellow")))
  121. {
  122. colourNum = rnd.nextInt(5);
  123. System.out.println("Please enter the colour selected by the computer:");
  124. colour = kbd.nextLine();
  125. answer = colour.toLowerCase();
  126. }
  127.  
  128. }
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement