Advertisement
Guest User

Hangman

a guest
Jan 21st, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.18 KB | None | 0 0
  1. // Name: Erik Marquez
  2. // Description: Program that runs a game of hangman wih 3 possible difficulty levels and replayability
  3. // Date Created: January 21, 2019
  4. // Date Revised: January 22, 2019
  5.  
  6.  
  7. import java.util.Scanner;
  8. import java.util.Random;
  9.  
  10. public class hangmanMAR
  11. {
  12.  
  13. public static void main (String [] args)
  14. {
  15.  
  16. String[] ezWords = {"hapon", "wisp", "kirby", "atari", "metroid", "brexit", "luigi", "saints", "hornet", "jello"}; // Declares required variables along with arrays with words for each difficulty
  17. String[] normWords = {"king lear", "emperor palpatine", "sicko mode", "puffer fish", "ubiquitous", "hallucinations", "indefatigable", "computer science"};
  18. String[] hardWords = {"james t. kirk", "barney the dinosaur", "incomprehensible", "multidisciplinary", "supercalifragilisticexpialidocious", "palaeoanthropology", "fitnessgram pacer test", "rambunctiousness"};
  19. int difficulty;
  20.  
  21. Scanner scanner = new Scanner(System.in); // Code required for scnner class and randomization
  22. Random random = new Random();
  23.  
  24. boolean gameRunning = true; // Replay function
  25. while (gameRunning)
  26. {
  27.  
  28. System.out.print("Hola. PLease choose your desired difficulty\n 1. Easy\n 2. Normal\n 3. Hard\n ");
  29. difficulty = scanner.nextInt();
  30.  
  31. if (difficulty == 1)
  32. {
  33.  
  34. char[] randomEz = ezWords[random.nextInt(ezWords.length)].toCharArray(); // Breaks word(s) into individual characters
  35. int guessNum = randomEz.length; // Number of guesses alloted until game over
  36. char[] playerGuess = new char [guessNum]; // Visualization --> _ _ _ _
  37.  
  38. for (int i = 0; i < playerGuess.length; i++)
  39. {
  40.  
  41. playerGuess[i] = '_';
  42.  
  43. }
  44.  
  45. boolean wordSuccess = false;
  46. int attempts = 0;
  47.  
  48. while (!wordSuccess && attempts != guessNum) // While word is not guessed and attempts have not all been used, run the following
  49. {
  50.  
  51. System.out.print("Current guesses--> ");
  52. printArray(playerGuess); // Calls printArray
  53. System.out.printf("You have %d attempts left.\n ", guessNum - attempts); // Takes amount of characters minus amount of attempts to display remaining amount of guesses
  54. System.out.println("Enter a single character (lowercase) ");
  55. char input = scanner.nextLine().charAt(0); // Makes it so if user unputs more than a single character it'll still only use the first character inputted
  56. attempts++;
  57.  
  58. if (input == '-') // If user inputs minus sign, game instantly ends
  59. {
  60.  
  61. gameRunning = false;
  62. wordSuccess = true;
  63.  
  64. }
  65. else
  66. {
  67.  
  68. for (int i = 0; i < randomEz.length; i++)
  69. {
  70.  
  71. if (randomEz[i] == input)
  72. {
  73.  
  74. playerGuess[i] = input;
  75.  
  76. }
  77.  
  78. }
  79.  
  80. if (wordSuccess(playerGuess))
  81. {
  82.  
  83. wordSuccess = true;
  84. System.out.println("Congrats, you won! ");
  85.  
  86. }
  87.  
  88. }
  89.  
  90. }
  91.  
  92. if (!wordSuccess) // if player loses, asks if they'd like to replay
  93. {
  94.  
  95. System.out.println("You ran out of guesses! ");
  96. System.out.println("Do you want to play again? (yes/no) ");
  97. String playAgain = scanner.nextLine();
  98.  
  99. if (playAgain.equals("no"))
  100. {
  101. gameRunning = false;
  102. }
  103.  
  104. }
  105.  
  106. }
  107.  
  108. if (difficulty == 2)
  109. {
  110.  
  111. char[] randomNorm = normWords[random.nextInt(normWords.length)].toCharArray(); // Breaks word(s) into individual characters
  112. int guessNum = randomNorm.length; // Number of guesses alloted until game over
  113. char[] playerGuess = new char [guessNum]; // Visualization --> _ _ _ _
  114.  
  115. for (int i = 0; i < playerGuess.length; i++)
  116. {
  117.  
  118. playerGuess[i] = '_';
  119.  
  120. }
  121.  
  122. boolean wordSuccess = false;
  123. int attempts = 0;
  124.  
  125. while (!wordSuccess && attempts != guessNum) // While word is not guessed and attempts have not all been used, run the following
  126. {
  127.  
  128. System.out.print("Current guesses--> ");
  129. printArray(playerGuess); // Calls printArray
  130. System.out.printf("You have %d attempts left.\n ", guessNum - attempts); // Takes amount of characters minus amount of attempts to display remaining amount of guesses
  131. System.out.println("Enter a single character (lowercase) ");
  132. char input = scanner.nextLine().charAt(0); // Makes it so if user unputs more than a single character it'll still only use the first character inputted
  133. attempts++;
  134.  
  135. if (input == '-') // If user inputs minus sign, game instantly ends
  136. {
  137.  
  138. gameRunning = false;
  139. wordSuccess = true;
  140.  
  141. }
  142. else
  143. {
  144.  
  145. for (int i = 0; i < randomNorm.length; i++)
  146. {
  147.  
  148. if (randomNorm[i] == input)
  149. {
  150.  
  151. playerGuess[i] = input;
  152.  
  153. }
  154.  
  155. }
  156.  
  157. if (wordSuccess(playerGuess))
  158. {
  159.  
  160. wordSuccess = true;
  161. System.out.println("Congrats, you won! ");
  162.  
  163. }
  164.  
  165. }
  166.  
  167. }
  168.  
  169. if (!wordSuccess) // if player loses, asks if they'd like to replay
  170. {
  171.  
  172. System.out.println("You ran out of guesses! ");
  173. System.out.println("Do you want to play again? (yes/no) ");
  174. String playAgain = scanner.nextLine();
  175.  
  176. if (playAgain.equals("no"))
  177. {
  178. gameRunning = false;
  179. }
  180.  
  181. }
  182.  
  183. }
  184.  
  185. if (difficulty == 3)
  186. {
  187.  
  188. char[] randomHard = hardWords[random.nextInt(hardWords.length)].toCharArray(); // Breaks word(s) into individual characters
  189. int guessNum = randomHard.length; // Number of guesses alloted until game over
  190. char[] playerGuess = new char [guessNum]; // Visualization --> _ _ _ _
  191.  
  192. for (int i = 0; i < playerGuess.length; i++)
  193. {
  194.  
  195. playerGuess[i] = '_';
  196.  
  197. }
  198.  
  199. boolean wordSuccess = false;
  200. int attempts = 0;
  201.  
  202. while (!wordSuccess && attempts != guessNum) // While word is not guessed and attempts have not all been used, run the following
  203. {
  204.  
  205. System.out.print("Current guesses--> ");
  206. printArray(playerGuess); // Calls printArray
  207. System.out.printf("You have %d attempts left.\n ", guessNum - attempts); // Takes amount of characters minus amount of attempts to display remaining amount of guesses
  208. System.out.println("Enter a single character (lowercase) ");
  209. char input = scanner.nextLine().charAt(0); // Makes it so if user unputs more than a single character it'll still only use the first character inputted
  210. attempts++;
  211.  
  212. if (input == '-') // If user inputs minus sign, game instantly ends
  213. {
  214.  
  215. gameRunning = false;
  216. wordSuccess = true;
  217.  
  218. }
  219. else
  220. {
  221.  
  222. for (int i = 0; i < randomHard.length; i++)
  223. {
  224.  
  225. if (randomHard[i] == input)
  226. {
  227.  
  228. playerGuess[i] = input;
  229.  
  230. }
  231.  
  232. }
  233.  
  234. if (wordSuccess(playerGuess))
  235. {
  236.  
  237. wordSuccess = true;
  238. System.out.println("Congrats, you won! ");
  239.  
  240. }
  241.  
  242. }
  243.  
  244. }
  245.  
  246. if (!wordSuccess) // if player loses, asks if they'd like to replay
  247. {
  248.  
  249. System.out.println("You ran out of guesses! ");
  250. System.out.println("Do you want to play again? (yes/no) ");
  251. String playAgain = scanner.nextLine();
  252.  
  253. if (playAgain.equals("no"))
  254. {
  255. gameRunning = false;
  256. }
  257.  
  258. }
  259.  
  260. }
  261.  
  262. }
  263.  
  264. System.out.print("Game over");
  265.  
  266. }
  267.  
  268. public static void printArray (char[] array)
  269. {
  270.  
  271. for (int i = 0; i < array.length; i++)
  272. {
  273. System.out.print(array[i] + " ");
  274. }
  275.  
  276. System.out.println();
  277.  
  278. }
  279.  
  280. public static boolean wordSuccess (char[] array)
  281. {
  282.  
  283. for (int i = 0; i < array.length; i++)
  284. {
  285. if (array[i] == '_') return false;
  286. }
  287.  
  288. return true;
  289.  
  290. }
  291.  
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement