Advertisement
Guest User

Hangman 2.0

a guest
Feb 22nd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Hangman2 {
  4. public static Scanner ScanIn = new Scanner(System.in);
  5. public static void main(String [] args) {
  6. String[] word=
  7. {"geoengineering", "glaciers" , "hunter", "distrubution", "originating" ,"climate"};
  8. System.out.println("Welcome to Hangman");
  9. int whatWord = (int)(Math.random() * 6) ;
  10. String guessThis = word[(whatWord - 1)];
  11. String[] array = guessThis.split("");
  12. String[] array2 = guessThis.split("");
  13. for (int i = guessThis.length() - 1; i >= 0; i = i - 1){
  14. array2[i] = "_";
  15. }
  16. //printArray(array2);
  17. drawHangman(0);
  18. int wrong = 0;
  19. int toWin = guessThis.length();
  20. int correct = 1;
  21. //System.out.println("The first letter is given to you");
  22.  
  23. while (wrong < 6 && correct < toWin + 1){
  24. System.out.println("Guess a letter");
  25.  
  26. String guess = ScanIn.nextLine().trim().toLowerCase();
  27. if (guessThis.contains(guess)){
  28. for (int v = 0; v <= guessThis.length() - 1; v++){
  29. if (array[v].equals(guess)){
  30. array2[v] = guess;
  31. correct++;
  32. }
  33. }
  34. printArray(array2);
  35. }
  36. else
  37. wrong++;
  38. drawHangman(wrong);
  39.  
  40. }
  41. if (correct > wrong){
  42. System.out.println("You Win");
  43. }
  44. else {
  45. System.out.println("You Lose");
  46. }
  47. }
  48. public static void drawHangman(int a){
  49. String A = " ____";
  50. String B = " |/ |";
  51. String C = " |";
  52. String D = " |";
  53. String E = " |";
  54. String F = " |";
  55. String G = "----";
  56. if (a >= 1) C = " | O";
  57. if (a == 2) D = " | |";
  58. if (a == 3) D = " | /|";
  59. if (a >= 4) D = " | /|\\";
  60. if (a == 5) E = " | / ";
  61. if (a >= 6) E = " | / \\ ";
  62.  
  63. System.out.println();
  64. System.out.println(A);
  65. System.out.println(B);
  66. System.out.println(C);
  67. System.out.println(D);
  68. System.out.println(E);
  69. System.out.println(F);
  70. System.out.println(G);
  71.  
  72.  
  73. }
  74. private static void printArray(String[] anArray) {
  75. for (int i = 0; i < anArray.length; i++) {
  76. if (i > 0) {
  77. System.out.print("");
  78. }
  79. System.out.print(anArray[i]);
  80. }
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement