Advertisement
Guest User

Untitled

a guest
Mar 5th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. public class HangmanGame
  2. {
  3.  
  4. private String secretWord;
  5. private char[] dashes;
  6. private int lives;
  7.  
  8. public HangmanGame(String wordIn)
  9. {
  10. secretWord = wordIn;
  11. dashes = new char[secretWord.length()];
  12. fillDashes();
  13. lives = 8; //default
  14. }
  15.  
  16. public boolean guessLetter(char letterIn)
  17. {
  18. boolean found = false;
  19. for(int i = 0; i<secretWord.length(); i++)
  20. {
  21. if(letterIn == secretWord.charAt(i)){
  22. dashes[i] = letterIn;
  23. found = true;
  24. }
  25.  
  26. }
  27. if(!found)
  28. lives--;
  29.  
  30. return found; //in case needed
  31.  
  32. }
  33.  
  34. public boolean gameOver()
  35. {
  36. if(secretWord.equalsIgnoreCase(showDashes()) || lives == 0)
  37. return true;
  38. else
  39. return false;
  40. }
  41.  
  42. public String showSecretWord()
  43. {
  44. return secretWord;
  45. }
  46.  
  47. public String showDashes()
  48. {
  49. String s ="";
  50. for(int i = 0; i< dashes.length; i++)
  51. s +=dashes[i];
  52.  
  53. return s;
  54. }
  55.  
  56. public void fillDashes()
  57. {
  58. for(int i = 0; i< dashes.length; i++)
  59. dashes[i]='-';
  60. }
  61.  
  62. public int getLives()
  63. {
  64. return lives;
  65. }
  66.  
  67. public void setLives(int livesIn)
  68. {
  69. lives = livesIn;
  70. }
  71.  
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement