Advertisement
Tintin1985

hangman

Dec 8th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. package hangmanproject;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import java.util.Scanner;
  6.  
  7. public class HangmanProject {
  8.  
  9.  
  10. static String word = "patience";
  11.  
  12.  
  13. public static void main(String[] args) {
  14.  
  15.  
  16. int life = 5;
  17. System.out.println("Hello and Welcome to Hangman Game");
  18. System.out.println("In this game you have 5 chances to guess the word or a letter in the secret Word");
  19. System.out.println();
  20.  
  21. System.out.print("Here is your secret word ");
  22.  
  23. char [] dash =word.toCharArray();
  24.  
  25. for(int i = 0; i < word.length(); i++ ) {
  26. System.out.print(dash[i] = '-');
  27. if (word.charAt(i) == ' ') {
  28. dash[i] = ' ';
  29.  
  30. }
  31.  
  32. } System.out.println();
  33.  
  34. playGame(life, dash);
  35.  
  36. }
  37.  
  38.  
  39. public static void playGame ( int life, char[]dash) {
  40. @SuppressWarnings("resource")
  41. Scanner sc = new Scanner (System.in);
  42.  
  43. while(life > 0 ) {
  44. System.out.print("\nPress 1 : To guess the secret word \nPress 2 : To guess a letter");
  45. int num = sc.nextInt();
  46.  
  47. if(num == 1) {
  48. guessTheword( life, dash);
  49.  
  50. }
  51.  
  52. if(num == 2) {
  53. guessAletter( life, dash);
  54.  
  55. }
  56.  
  57. }
  58. }
  59. public static void guessTheword( int chance, char[]dash) {
  60.  
  61. ArrayList <String> wordContainer = new ArrayList <String>();
  62. @SuppressWarnings("resource")
  63. Scanner scan = new Scanner(System.in);
  64. System.out.println("Please type your Guess word:");
  65. scan = new Scanner(System.in);
  66. String yourGuess = scan.nextLine();
  67.  
  68. if(wordContainer.contains(yourGuess)) {
  69. System.out.println("You have tried it before!");
  70.  
  71. }
  72. wordContainer.add(yourGuess);
  73.  
  74. if (yourGuess.equals(word)) {
  75. System.out.println("Congratulations You Guessed the word!!! ");
  76.  
  77.  
  78. } else {
  79. chance --;
  80. System.out.println(chance);
  81. System.out.println("Sorry Wrong Guess! " + chance + " chances remaining");
  82. drawTheHangman(chance);
  83. playGame (chance,dash);
  84. }
  85.  
  86.  
  87. }
  88.  
  89. public static void guessAletter( int chance, char [] dash) {
  90. @SuppressWarnings("resource")
  91. Scanner input = new Scanner (System.in);
  92. ArrayList <Character> letterContainer = new ArrayList <Character>();
  93.  
  94. System.out.println("Please Guess a letter ");
  95. input = new Scanner(System.in);
  96. char letter = input.next().charAt(0);
  97.  
  98. if(letterContainer.contains(letter)) {
  99. System.out.println("You have tried it before");
  100. } letterContainer.add(letter);
  101.  
  102.  
  103. if(word.contains(letter + "")) {
  104. updatedGuessed(word, dash,letter);
  105.  
  106. }else {
  107. chance --;
  108. System.out.println("Sorry Wrong Guess! " + chance + " chances remaining");
  109. drawTheHangman(chance);
  110. playGame (chance,dash);
  111. }
  112.  
  113. if (word.equals(String.valueOf(dash))) {
  114. System.out.println("Congratulations you guessed the Word!");
  115. chance = 0;
  116. }
  117. System.out.println(dash);
  118. }
  119.  
  120.  
  121.  
  122. public static void updatedGuessed(String word, char[]dash, char letter) {
  123. for (int y = 0; y < word.length(); y++) {
  124. if (word.charAt(y) == letter) {
  125. dash[y] = letter;
  126. }
  127. }
  128. }
  129.  
  130. public static void drawTheHangman(int remainingGuess) {
  131.  
  132. if (remainingGuess == 0) {
  133. System.out.println("You Lose! R.I.P." +
  134. "\n ________" +
  135. "\n | |"+
  136. "\n | Ö"+
  137. "\n | /|\\"
  138. + "\n | / \\" +
  139. "\n | " +
  140. "\n/|\\ ");
  141. System.out.println();
  142. System.out.println("The secret word is " + word);
  143.  
  144. }
  145.  
  146. else if (remainingGuess == 1) {
  147. System.out.println(" ________"
  148. + "\n | |" +
  149. "\n |"
  150. + "\n |" +
  151. "\n |" +
  152. "\n |" +
  153. "\n/|\\");
  154.  
  155. } else if (remainingGuess == 2) {
  156.  
  157. System.out.println(" ________" +
  158. "\n |" +
  159. "\n |" +
  160. "\n |" +
  161. "\n |" +
  162. "\n |" +
  163. "\n/|\\");
  164.  
  165. } else if (remainingGuess == 3) {
  166. System.out.println(" |"
  167. + "\n |"
  168. + "\n |"
  169. + "\n |"
  170. + "\n |"
  171. + "\n |"
  172. + "\n/|\\");
  173.  
  174. } else if (remainingGuess == 4) {
  175. System.out.println("/|\\");
  176. }
  177.  
  178. }
  179.  
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement