Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- public class HangmanLogic {
- private String word;
- private String guessedLetters;
- private int numberOfFaults;
- private ArrayList<String> uniqueGuessedLetter = new ArrayList<String>();
- public HangmanLogic(String word)
- {
- this.word = word.toUpperCase();
- this.guessedLetters = "";
- this.numberOfFaults = 0;
- }
- public int numberOfFaults()
- {
- return this.numberOfFaults;
- }
- public String guessedLetters()
- {
- return this.guessedLetters;
- }
- public int losingFaultAmount()
- {
- return 12;
- }
- public void guessLetter(String letter)
- {
- // program here the functionality for making a guess
- // if the letter has already been guessed, nothing happens
- // if letter is NOT in uniqueGuessedLetter array
- if (!uniqueGuessedLetter.contains(letter))
- {
- // the letter is added among the already guessed letters
- uniqueGuessedLetter.add(letter);
- // it the word does not contains the guessed letter, number of faults increase
- if (!word.contains(letter))
- {
- this.numberOfFaults++;
- }
- }
- System.out.println("HERE IS " + uniqueGuessedLetter);
- }
- public String hiddenWord()
- {
- // program here the functionality for building the hidden word
- // create the hidden word by interating through this.word letter by letter
- char c = word.charAt(0);
- String hiddenWord = "";
- for (int i = 0; i < this.word.length(); i++)
- {
- c = word.charAt(i);
- // if the letter in turn is within the guessed words, put it in to the hidden word
- if (uniqueGuessedLetter.contains(Character.toString(c)))
- {
- hiddenWord += c;
- }
- else
- {
- // if the letter is not among guessed, replace it with _ in the hidden word
- hiddenWord += "_";
- }
- }
- // return the hidden word at the end
- return hiddenWord;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement