Advertisement
Guest User

Untitled

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