Advertisement
Guest User

HangmanWord Class

a guest
May 29th, 2015
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.32 KB | None | 0 0
  1. package com.game.hangman;
  2.  
  3. import java.util.Arrays;
  4.  
  5. public class HangmanWord {
  6.    
  7.     private char[] progress;
  8.    
  9.     private int wrongCount;
  10.    
  11.     private String word;
  12.     private String[] possibleWords = {
  13.             "virus",
  14.             "insect",
  15.             "ferocious",
  16.             "trojan",
  17.             "feather",
  18.             "stream",
  19.             "happily",
  20.             "forever",
  21.             "peacemaker",
  22.             "universe"
  23.     };
  24.    
  25.     public HangmanWord() {
  26.         word = possibleWords[randomValue()];
  27.         progress = new char[word.length()];
  28.         Arrays.fill(progress, '-');
  29.     }
  30.    
  31.     public void display() {
  32.         for (char c : progress) {
  33.             System.out.print(c);
  34.         }
  35.         System.out.println();
  36.     }
  37.    
  38.     private int randomValue() {
  39.         int value = (int) (Math.random() * 10);
  40.         if (value < possibleWords.length) {
  41.             return value;
  42.         }
  43.         return randomValue();
  44.     }
  45.    
  46.     public boolean guess(char c) {
  47.         boolean matchFound = false;
  48.         for (int i = 0; i < word.length(); i++) {
  49.             if (c == word.charAt(i)) {
  50.                 progress[i] = c;
  51.                 matchFound = true;
  52.             }
  53.         }
  54.         return matchFound;
  55.     }
  56.    
  57.     public boolean isSolved() {
  58.         for (int i = 0; i < word.length(); i++) {
  59.             if (word.charAt(i) == '-') {
  60.                 return false;
  61.             }
  62.         }
  63.         return true;
  64.     }
  65.    
  66.     public String getWord() {
  67.         return word;
  68.     }
  69.    
  70.     public int getWrongCount() {
  71.         return wrongCount;
  72.     }
  73.    
  74.     public void incrementWrongCount() {
  75.         wrongCount++;
  76.     }
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement