Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. public class HangmanLogic {
  4.  
  5. private String word;
  6. private String guessedLetters;
  7. private int numberOfFaults;
  8. private ArrayList<String> uniqueGuessedLetter = new ArrayList<String>();
  9.  
  10. public HangmanLogic(String word)
  11. {
  12. this.word = word.toUpperCase();
  13. this.guessedLetters = "";
  14. this.numberOfFaults = 0;
  15. }
  16.  
  17. public int numberOfFaults()
  18. {
  19. return this.numberOfFaults;
  20. }
  21.  
  22. public String guessedLetters()
  23. {
  24. return this.guessedLetters;
  25. }
  26.  
  27. public int losingFaultAmount()
  28. {
  29. return 12;
  30. }
  31.  
  32. public void guessLetter(String letter)
  33. {
  34. // program here the functionality for making a guess
  35. // if the letter has already been guessed, nothing happens
  36. // if letter is NOT in uniqueGuessedLetter array
  37. if (!uniqueGuessedLetter.contains(letter))
  38. {
  39. // the letter is added among the already guessed letters
  40. uniqueGuessedLetter.add(letter);
  41.  
  42. // it the word does not contains the guessed letter, number of faults increase
  43. if (!word.contains(letter))
  44. {
  45. this.numberOfFaults++;
  46. }
  47. }
  48. System.out.println("HERE IS " + uniqueGuessedLetter);
  49. }
  50.  
  51. public String hiddenWord()
  52. {
  53. // program here the functionality for building the hidden word
  54. // create the hidden word by interating through this.word letter by letter
  55. char c = word.charAt(0);
  56. String hiddenWord = "";
  57. for (int i = 0; i < this.word.length(); i++)
  58. {
  59. c = word.charAt(i);
  60.  
  61. // if the letter in turn is within the guessed words, put it in to the hidden word
  62. if (uniqueGuessedLetter.contains(Character.toString(c)))
  63. {
  64. hiddenWord += c;
  65. }
  66. else
  67. {
  68. // if the letter is not among guessed, replace it with _ in the hidden word
  69. hiddenWord += "_";
  70. }
  71. }
  72. // return the hidden word at the end
  73. return hiddenWord;
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement