Guest User

Untitled

a guest
Feb 18th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. package hangman2;
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4.  
  5. public class Hangman2 {
  6.  
  7. static int guessesRemaining;
  8. static String stringWord;
  9. static char[] charWord;
  10.  
  11. public static void playHangman() {
  12.  
  13. System.out.print("Welcome to Hangman! ");
  14. System.out.println("Player 1: Enter a word");
  15. Scanner scan = new Scanner(System.in);
  16. stringWord = scan.nextLine();
  17.  
  18. //Loop to get user to enter in letters only
  19. while (stringWord.matches(".*[^a-z].*")) {
  20. System.out.println("Please enter letters only, try again:");
  21. stringWord = scan.nextLine();
  22. }
  23.  
  24. System.out.println("The length of the word is " + stringWord.length()); //Print out length of word.
  25. stringWord = stringWord.toLowerCase();
  26.  
  27. int lettersRemaining = stringWord.length(); //Letters to be gussed is equal to the word length.
  28.  
  29. //Create a new char array equal to the word length, add * to each index.
  30. charWord = new char[stringWord.length()];
  31. for (int i = 0; i < stringWord.length(); i++) {
  32. charWord[i] = ('u002A');
  33. }
  34. int livesRemaining = 7; // 7 lives remaining!
  35. MainLoop:
  36. /*
  37. Main loop, gets user input using Scanner, ensures it is only a letter (not a number or special character)
  38. Converts string to char, uses the guess method which compares the secret word to the character entered and
  39. returns true if it found it, false if it didn't.
  40. If found, letters remaining - 1, if the char array contains any asterisks, envoke the gameWon() method.
  41. If not found, lives remaining - 1, if no more lives, envoke the gameLost() method.
  42. */
  43. while (livesRemaining > 0) {
  44. System.out.println("Player 2: Guess a Letter:!");
  45. char guessLetter;
  46. String b = scan.next();
  47. while (b.matches(".*[^a-z].*")) {
  48. System.out.println("Please enter letters only, try again:");
  49. b = scan.next();
  50. }
  51. guessLetter = b.toLowerCase().charAt(0);
  52. String a = new Hangman2().print(guessLetter);
  53. if (guess(guessLetter, stringWord)) {
  54. lettersRemaining--;
  55. if (!a.contains("*")) {
  56. gameWon();
  57. break;
  58. }
  59. System.out.println("Well done, you guessed correctly! You have " + lettersRemaining + " letters remaining");
  60. } else {
  61. livesRemaining--;
  62. if (livesRemaining == 0) {
  63. gameLost();
  64. }
  65. System.out.println("Too bad, you guessed incorrectly, you have " + livesRemaining + " lives remaining");
  66. }
  67. }
  68. }
  69.  
  70. public String print(char c) {
  71. //Prints asterisks and replaces characters that have been guessed.
  72. String misses;
  73. char[] guess = stringWord.toCharArray();
  74. for (int i = 0; i < charWord.length; i++) {
  75. if (guess[i] == c) {
  76. charWord[i] = c;
  77. }
  78. }
  79. misses = Arrays.toString(charWord);
  80. System.out.print(misses + " ");
  81. return misses;
  82. }
  83.  
  84. public static boolean guess(char c, String s) {
  85. //Evaluates the chracter entered and compares it to the word, returns a boolean (correct or incorrect).
  86. boolean result = false;
  87. char[] a = s.toCharArray();
  88. for (int i = 0; i < s.length(); i++) {
  89. if (a[i] == c) {
  90. result = true;
  91. }
  92. }
  93. return result;
  94. }
  95.  
  96. public static void gameWon() {
  97. char yesno;
  98. boolean enter;
  99. System.out.print("Congratulations, you won!");
  100. Scanner input = new Scanner(System.in);
  101. System.out.println(" Would you like to play again? Y / N");
  102. do {
  103. yesno = input.next().toUpperCase().charAt(0);
  104. switch (yesno) {
  105. case 'Y':
  106. playHangman();
  107. case 'N':
  108. System.exit(0);
  109. default:
  110. System.out.println("Please enter Y / N");
  111. enter = false;
  112. break;
  113. }
  114. } while (!enter);
  115. }
  116.  
  117. public static void gameLost() {
  118. char yesno;
  119. boolean enter;
  120. System.out.print("Too bad, you lost...");
  121. Scanner input = new Scanner(System.in);
  122. System.out.println(" Would you like to play again? Y / N");
  123. do {
  124. yesno = input.next().toUpperCase().charAt(0);
  125. switch (yesno) {
  126. case 'Y':
  127. playHangman();
  128. case 'N':
  129. System.exit(0);
  130. default:
  131. System.out.println("Please enter Y / N");
  132. enter = false;
  133. break;
  134. }
  135. } while (!enter);
  136. }
  137.  
  138. public static void main(String[] args) {
  139. playHangman();
  140. }
Add Comment
Please, Sign In to add comment