Advertisement
Guest User

Untitled

a guest
Apr 21st, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1.  
  2.  
  3. public class Hangman { //This is the my Hangman Class.
  4.  
  5. public static String[] wordBank = {"hello", "computer", "pen", "building", "game", "airplane" }; //An array to hold words.
  6. public static String word = wordBank[(int) (Math.random() * wordBank.length)];
  7.  
  8. public static String star = new String(new char[word.length()]).replace("\0", "*"); // this converts every letter of the word into stars.
  9. public static int count = 0; //this is to see the number of counts.
  10.  
  11.  
  12. public static void hangGuess(String guessL) { //this is my first method which takes in the guesses letter to compare.
  13. String newStar = "";
  14. for (int loop = 0; loop < word.length(); loop++) { //for loop
  15. if (word.charAt(loop) == guessL.charAt(0)) {
  16. newStar += guessL.charAt(0);
  17. } else if (star.charAt(loop) != '*') {
  18. newStar += word.charAt(loop);
  19. } else {
  20. newStar += "*";
  21. }
  22. }
  23.  
  24. if (star.equals(newStar)) {
  25. count++;
  26. drawHangman();
  27. } else {
  28. star = newStar;
  29. }
  30. if (star.equals(word)) {
  31. System.out.println("Correct! You win! The word was " + "------->>>>>[" + word +"]");
  32. }
  33. }
  34.  
  35. public static void drawHangman() { // This is my second method to give details about the game.
  36. if (count == 1) {
  37. System.out.println("Wrong letter, try again!");
  38. System.out.println("You have 6 guesses remaining! ");
  39. System.out.println();
  40. System.out.println();
  41. System.out.println();
  42. System.out.println();
  43. System.out.println("___|___");
  44. System.out.println();
  45. }
  46. if (count == 2) {
  47. System.out.println("Wrong letter, try again!");
  48. System.out.println("You have 5 guesses remaining! ");
  49. System.out.println(" |");
  50. System.out.println(" |");
  51. System.out.println(" |");
  52. System.out.println(" |");
  53. System.out.println(" |");
  54. System.out.println(" |");
  55. System.out.println(" |");
  56. System.out.println("___|___");
  57. }
  58. if (count == 3) {
  59. System.out.println("Wrong letter, try again!");
  60. System.out.println("You have 4 guesses remaining! ");
  61. System.out.println(" ____________");
  62. System.out.println(" |");
  63. System.out.println(" |");
  64. System.out.println(" |");
  65. System.out.println(" |");
  66. System.out.println(" |");
  67. System.out.println(" |");
  68. System.out.println(" | ");
  69. System.out.println("___|___");
  70. }
  71. if (count == 4) {
  72. System.out.println("Wrong letter, try again!");
  73. System.out.println("You have 3 guesses remaining! ");
  74. System.out.println(" ____________");
  75. System.out.println(" | _|_");
  76. System.out.println(" | / \\");
  77. System.out.println(" | | |");
  78. System.out.println(" | \\_ _/");
  79. System.out.println(" |");
  80. System.out.println(" |");
  81. System.out.println(" |");
  82. System.out.println("___|___");
  83. }
  84. if (count == 5) {
  85. System.out.println("Wrong letter, try again!");
  86. System.out.println("You have 2 guesses remaining! ");
  87. System.out.println(" ____________");
  88. System.out.println(" | _|_");
  89. System.out.println(" | / \\");
  90. System.out.println(" | | |");
  91. System.out.println(" | \\_ _/");
  92. System.out.println(" | |");
  93. System.out.println(" | |");
  94. System.out.println(" |");
  95. System.out.println("___|___");
  96. }
  97. if (count == 6) {
  98. System.out.println("Wrong letter, try again!");
  99. System.out.println("You have 1 guesses remaining! ");
  100. System.out.println(" ____________");
  101. System.out.println(" | _|_");
  102. System.out.println(" | / \\");
  103. System.out.println(" | | |");
  104. System.out.println(" | \\_ _/");
  105. System.out.println(" | |");
  106. System.out.println(" | |");
  107. System.out.println(" | / \\ ");
  108. System.out.println("___|___ / \\");
  109. }
  110. if (count == 7) {
  111. System.out.println("GAME OVER!");
  112. System.out.println("You have 0 guesses remaining! ");
  113. System.out.println(" ____________");
  114. System.out.println(" | _|_");
  115. System.out.println(" | / \\");
  116. System.out.println(" | | |");
  117. System.out.println(" | \\_ _/");
  118. System.out.println(" | _|_");
  119. System.out.println(" | / | \\");
  120. System.out.println(" | / \\ ");
  121. System.out.println("___|___ / \\");
  122. System.out.println("GAME OVER! The word was -------->" + word);
  123. }
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement