Advertisement
cameronsiu02

Untitled

Dec 11th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. package Hangman;
  2. import java.util.*;
  3.  
  4. public class Hang {
  5.  
  6. public static void main(String[] args) {
  7. // TODO Auto-generated method stub
  8.  
  9. String guessList = "";
  10. int lives, correct;
  11. lives = 5;
  12. correct = 0;
  13. String word = "i like";
  14. String[] underscores = new String[word.length()];
  15.  
  16. play(correct, lives, word, guessList, underscores);
  17.  
  18.  
  19. }
  20.  
  21. public static void changeDisplay(int correct, int lives, String word, String guessList, String[] underscores) {
  22.  
  23. String live = "Lives: " + lives;
  24. String newSpace = "\n";
  25. String right = "Correct: " + correct + newSpace;
  26. String display = "";
  27.  
  28.  
  29. for (int i = 0; i < underscores.length; i++) {
  30. display += underscores[i];
  31. }
  32.  
  33. String fin = live + newSpace + display + newSpace + right;
  34. System.out.println(picture(fin, lives, correct, word));
  35.  
  36. }
  37.  
  38. public static void play(int correct, int lives, String word, String guessList, String[] underscores) {
  39.  
  40. Scanner in = new Scanner(System.in);
  41.  
  42. for (int i = 0; i < underscores.length; i++) {
  43. if (word.charAt(i) == ' ') {
  44. underscores[i] = " ";
  45. }
  46. else {
  47. underscores[i] = "_ ";
  48. }
  49. }
  50. String word1 = word;
  51.  
  52. /*
  53. for (int i = 0; i < word.length(); i++) {
  54. if (word.charAt(i) != ' ') {
  55. word1 += word.charAt(i);
  56. }
  57.  
  58. }
  59. System.out.println(word1);
  60. */
  61.  
  62. changeDisplay(correct, lives, word1, guessList, underscores);
  63.  
  64.  
  65.  
  66. while (lives > 0 && correct < word1.length()) {
  67.  
  68.  
  69.  
  70. char guess = guesser(in.nextLine());
  71.  
  72. guessList += Character.toString(guess);
  73.  
  74. if (!inWord(guess, word1, underscores) && howMany(guessList, guess) == 1) {
  75. lives--;
  76. changeDisplay(correct, lives, word1, guessList, underscores);
  77. }
  78. else if (inWord(guess, word1, underscores) && howMany(guessList, guess) == 1) {
  79. correct = newCorrect(guess, word1, correct);
  80. changeDisplay(correct, lives, word1, guessList, underscores);
  81. }
  82. else {
  83. System.out.println("Already guessed. Guess again: ");
  84. }
  85. }
  86. }
  87. public static char guesser(String guesses) {
  88. char guess = guesses.charAt(0);
  89. return guess;
  90. }
  91.  
  92. public static int howMany(String guessList, char guess) {
  93. int howMany = 0;
  94. for (int i = 0; i < guessList.length(); i++) {
  95. if (guess == guessList.charAt(i)) {
  96. howMany++;
  97. }
  98. }
  99. return howMany;
  100. }
  101. public static int letterDupe(char guess, String word) {
  102. int times = 0;
  103. for (int i = 0; i < word.length(); i++) {
  104. if (guess == word.charAt(i)) {
  105. times++;
  106. }
  107. }
  108. return times;
  109. }
  110. public static int newCorrect(char guess, String word, int correct) {
  111. for (int i = 0; i < letterDupe(guess, word); i++) {
  112. correct++;
  113. }
  114. return correct;
  115. }
  116. public static boolean inWord(char guess, String word, String[] underscores) {
  117. boolean inWord = false;
  118. for(int i = 0; i < word.length(); i++) {
  119. if (guess == word.charAt(i)) {
  120. underscores[i] = Character.toString(guess) + " ";
  121. inWord = true;
  122. }
  123. }
  124.  
  125. return inWord;
  126. }
  127. public static String picture(String fin, int lives, int correct, String word) {
  128.  
  129. String opening = " _______________ \n"
  130. +"| |\n"
  131. +"| 0 0 |\n"
  132. +"| |\n"
  133. +"| \\______/ |\n"
  134. +"| |\n"
  135. +"|_______________|\n";
  136. String hanger = " _______________ \n"
  137. +"| |\n"
  138. +"| 0 0 |\n"
  139. +"| |\n"
  140. +"| ______ |\n"
  141. +"| |\n"
  142. +"|_______________|\n";
  143.  
  144. String hanger1 = " _______________ \n"
  145. +"| |\n"
  146. +"| X 0 |\n"
  147. +"| |\n"
  148. +"| ______ |\n"
  149. +"| |\n"
  150. +"|_______________|\n";
  151.  
  152. String hanger2 = " _______________ \n"
  153. +"| |\n"
  154. +"| X X |\n"
  155. +"| |\n"
  156. +"| ______ |\n"
  157. +"| |\n"
  158. +"|_______________|\n";
  159.  
  160. String hanger3 = " _______________ \n"
  161. +"| |\n"
  162. +"| X X |\n"
  163. +"| |\n"
  164. +"| /---------\\ |\n"
  165. +"| |\n"
  166. +"|_______________|\n";
  167.  
  168. String lose =
  169. "| | ____ | ____ ______ _______ |\n"
  170. +"| || | | | | | | | | | |\n"
  171. +"|______|| | | | | | | |____ |_____| |\n"
  172. +" | | | | | | | | | | |\n"
  173. +" | | | | | | | | | | |\n"
  174. +" | |____| |_____| |_____ |____| _____| |______ .\n";
  175.  
  176. if (lives == 5) {
  177. fin += opening;
  178. }
  179. else if (lives == 4) {
  180. fin += hanger;
  181. }
  182. else if (lives == 3) {
  183. fin += hanger1;
  184. }
  185. else if (lives == 2) {
  186. fin += hanger2;
  187. }
  188. else if (lives == 1) {
  189. fin += hanger3;
  190. }
  191. else {
  192. fin += lose;
  193. }
  194. if (correct == word.length() && lives == 5) {
  195. fin +=" _________ \n"
  196. + "| | _______ | |\n"
  197. + "| | ______ ______ | ______ _________ | |\n"
  198. + "|_________| | | |/ | | | | ____|____ |\n"
  199. + "| |______| | ___|___ |______| | | |\n"
  200. + "| | | | | | | |\n"
  201. + "| | | | | | | |\n"
  202. + "| |_______ | | |_______ |________ | .\n";
  203. }
  204. else if (correct == word.length()) {
  205. fin += "You win!";
  206. }
  207. return fin;
  208. }
  209.  
  210.  
  211.  
  212.  
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement