Advertisement
Guest User

Hangman 2.0

a guest
Feb 24th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Hangman2 {
  4. public static Scanner ScanIn = new Scanner(System.in);
  5.  
  6. public static void main(String[] args) {
  7. String[] word = { "geoengineering", "glaciers", "hunter", "distrubution", "originating", "climate", "mortality",
  8. "precious", "america", "subordinates", "unique", "susceptible", "tactical", "overload", "combinations",
  9. "broadcasters", "bomb", "karate", "birthday", "song", "consoles", "sentinel", "adept", "hangman",
  10. "season", "widow", "tracer", "competitive", "wrecked", "strawberry", "mass", "effect", "nomad",
  11. "incendiary", "disrupter", "cyrogenic", "pelican", "master", "chief", "covenant" };
  12. System.out.println("Welcome to Hangman");
  13. int whatWord = (int) (Math.random() * 40);
  14. String guessThis = word[(whatWord )];
  15. String[] array = guessThis.split("");
  16. String[] array2 = guessThis.split("");
  17. String[] array3 = "thisisastringthatis26Clong".split("");
  18. for (int i = 0; i < 26; i++) {
  19. array3[i] = "-";
  20. }
  21. for (int i = guessThis.length() - 1; i >= 0; i = i - 1) {
  22. array2[i] = "_";
  23. }
  24. // printArray(array2);
  25. drawHangman(0);
  26. int wrong = 0;
  27. int toWin = guessThis.length();
  28. int correct = 1;
  29. int index = 0;
  30. while (wrong < 6 && correct < toWin + 1) {
  31. System.out.println();
  32. System.out.println("Guess a letter");
  33. String guess = ScanIn.nextLine().trim().toLowerCase();
  34. if (guess.equals(guessThis)) correct = toWin + 1;
  35. for (int b = 0; b <= array3.length - 1; b++) {
  36. if (array3[b].equals(guess)) {
  37. System.out.println("You have already guessed that letter");
  38. if (guessThis.contains(guess)){
  39. correct = correct - 1;
  40. }
  41.  
  42. }
  43. }
  44.  
  45. if (guessThis.contains(guess)) {
  46. for (int v = 0; v <= guessThis.length() - 1; v++) {
  47. if (array[v].equals(guess)) {
  48. array2[v] = guess;
  49. correct++;
  50. array3[index] = guess;
  51.  
  52. }
  53. }
  54.  
  55. } else{
  56. wrong++;
  57. array3[index] = guess;
  58. }
  59. drawHangman(wrong);
  60. printArray(array2);
  61. index++;
  62. }
  63. if (correct > wrong) {
  64. System.out.println();
  65. System.out.println("You Win");
  66. System.out.println("Your ");
  67. } else {
  68. System.out.println();
  69. System.out.println("You Lose");
  70. System.out.println("The word was " + guessThis);
  71. }
  72.  
  73. }
  74. public static void drawHangman(int a) {
  75. String A = " ____";
  76. String B = " |/ |";
  77. String C = " |";
  78. String D = " |";
  79. String E = " |";
  80. String F = " |";
  81. String G = "----";
  82. if (a >= 1)
  83. C = " | O";
  84. if (a == 2)
  85. D = " | |";
  86. if (a == 3)
  87. D = " | /|";
  88. if (a >= 4)
  89. D = " | /|\\";
  90. if (a == 5)
  91. E = " | / ";
  92. if (a >= 6)
  93. E = " | / \\ ";
  94.  
  95. System.out.println();
  96. System.out.println(A);
  97. System.out.println(B);
  98. System.out.println(C);
  99. System.out.println(D);
  100. System.out.println(E);
  101. System.out.println(F);
  102. System.out.println(G);
  103.  
  104. }
  105.  
  106.  
  107. private static void printArray(String[] anArray) {
  108. for (int i = 0; i < anArray.length; i++) {
  109. if (i > 0) {
  110. System.out.print("");
  111. }
  112. System.out.print(anArray[i]);
  113. }
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement