Advertisement
Guest User

Untitled

a guest
May 30th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. import static java.lang.System.*;
  2. import java.io.IOException;
  3. import java.io.File;
  4. import java.util.Scanner;
  5.  
  6.  
  7. public class Hangman
  8. {
  9.  
  10. private int maxGuesses = 6;
  11. private String word = null;
  12. private int wordLength;
  13. private char hiddenWord[] = null;
  14. private String letterGuesser = "";
  15. public int completeWord = 0;
  16. public boolean complete = false;
  17. private int wrongGuesses = 0;
  18. private String allGuesses = "";
  19. private boolean playAgain = true;
  20.  
  21. public Hangman()
  22. {
  23. System.out.println("Welcome to Hangman! nnYour goal is to guess the letters in the word displayed in blanks nnThese letters, if guessed corectly, will form a word. nnYou will have 5 lives nnWhen you run out of guesses, Game over nnGood luck!nn");
  24.  
  25. while (playAgain==true)
  26. {
  27. System.out.println("nnNew Game!");
  28. complete=false;
  29. wrongGuesses = 0;
  30. letterGuesser = "";
  31. allGuesses = "";
  32. hiddenWord = null;
  33. completeWord = 0;
  34. wordLength = word.length();
  35. createHiddenWord(wordLength);
  36. while (complete==false)
  37. {
  38. isGuessCorrect(letterGuesser());
  39. isWordComplete();
  40. }
  41. askPlayerForAgain();
  42. }
  43.  
  44.  
  45. }
  46.  
  47.  
  48. public void setWord()
  49. {
  50. Scanner scan = new Scanner(new File("Words.txt"));
  51. double random = Math. random() * 1000 + 1;
  52. for(int i=0; random==i; i++)
  53. {
  54. scan.nextLine();
  55. }
  56. word=file.nextLine();
  57.  
  58. }
  59.  
  60. private void createHiddenWord(int length)
  61. {
  62. hiddenWord = new char[length];
  63. char(hiddenWord, '_');
  64. System.out.println("The word is : " + new String(hiddenWord));
  65. }
  66.  
  67. static Scanner key = new Scanner(System.in);
  68. public char letterGuesser()
  69. {
  70. System.out.print("nPick a letter : ");
  71. String a = key.nextLine();
  72. while (a.equals(""))
  73. {
  74. System.out.print("nPick a letter : ");
  75. a = key.nextLine();
  76. }
  77. allGuesses = allGuesses + a.charAt(0);
  78. System.out.println("nGuessed Letters: " +allGuesses);
  79. return a.charAt(0);
  80. }
  81.  
  82. public boolean isGuessCorrect(char guess)
  83. {
  84. char wordArray[] = word.toCharArray();
  85. boolean correct = false;
  86. for(int i = 0; i<wordLength; i++)
  87. if(wordArray[i] == guess)
  88. {
  89. hiddenWord[i] = guess;
  90. correct = true;
  91. }
  92. if(correct==true)
  93. {
  94. System.out.println("nAwesome guess! Keep goin'!");
  95. }
  96. else
  97. {
  98. wrongGuesses++;
  99. int wrongLeft = maxGuesses-wrongGuesses;
  100. System.out.println("nWrong guess! You have " + wrongLeft + " guesses left.");
  101. }
  102. if(wrongGuesses==maxGuesses)
  103. {
  104. System.out.println("nThe word really was : " + word);
  105. complete=true;
  106. }
  107. if(complete==false)
  108. {
  109. System.out.println(hiddenWord);
  110. return (correct);
  111. }
  112. else
  113. {
  114. return (false);
  115. }
  116. }
  117.  
  118. public boolean isWordComplete()
  119. {
  120. String s=new String(hiddenWord);
  121. if (s.equals(word))
  122. {
  123. System.out.println("nYou have successfully completed this word! Nice job!");
  124. complete = true;
  125. completeWord++;
  126. }
  127. return(complete);
  128. }
  129.  
  130. public boolean askPlayerForAgain()
  131. {
  132. System.out.println("nWould you like to play again? y/n");
  133. String response = key.nextLine();
  134. if (response.equals("y"))
  135. {
  136. playAgain=true;
  137. }
  138. else
  139. {
  140. playAgain=false;
  141. }
  142. return (playAgain);
  143. }
  144. }
  145.  
  146. //Hangman
  147.  
  148.  
  149.  
  150. public class HangmanRunner
  151. {
  152. public static void main(String args[]) throws IOException;
  153.  
  154.  
  155.  
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement