Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class HangmanGame
- {
- private String secretWord;
- private char[] dashes;
- private int lives;
- public HangmanGame(String wordIn)
- {
- secretWord = wordIn;
- dashes = new char[secretWord.length()];
- fillDashes();
- lives = 8; //default
- }
- public boolean guessLetter(char letterIn)
- {
- boolean found = false;
- for(int i = 0; i<secretWord.length(); i++)
- {
- if(letterIn == secretWord.charAt(i)){
- dashes[i] = letterIn;
- found = true;
- }
- }
- if(!found)
- lives--;
- return found; //in case needed
- }
- public boolean gameOver()
- {
- if(secretWord.equalsIgnoreCase(showDashes()) || lives == 0)
- return true;
- else
- return false;
- }
- public String showSecretWord()
- {
- return secretWord;
- }
- public String showDashes()
- {
- String s ="";
- for(int i = 0; i< dashes.length; i++)
- s +=dashes[i];
- return s;
- }
- public void fillDashes()
- {
- for(int i = 0; i< dashes.length; i++)
- dashes[i]='-';
- }
- public int getLives()
- {
- return lives;
- }
- public void setLives(int livesIn)
- {
- lives = livesIn;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement