Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.game.hangman;
- import java.util.Arrays;
- public class HangmanWord {
- private char[] progress;
- private int wrongCount;
- private String word;
- private String[] possibleWords = {
- "virus",
- "insect",
- "ferocious",
- "trojan",
- "feather",
- "stream",
- "happily",
- "forever",
- "peacemaker",
- "universe"
- };
- public HangmanWord() {
- word = possibleWords[randomValue()];
- progress = new char[word.length()];
- Arrays.fill(progress, '-');
- }
- public void display() {
- for (char c : progress) {
- System.out.print(c);
- }
- System.out.println();
- }
- private int randomValue() {
- int value = (int) (Math.random() * 10);
- if (value < possibleWords.length) {
- return value;
- }
- return randomValue();
- }
- public boolean guess(char c) {
- boolean matchFound = false;
- for (int i = 0; i < word.length(); i++) {
- if (c == word.charAt(i)) {
- progress[i] = c;
- matchFound = true;
- }
- }
- return matchFound;
- }
- public boolean isSolved() {
- for (int i = 0; i < word.length(); i++) {
- if (word.charAt(i) == '-') {
- return false;
- }
- }
- return true;
- }
- public String getWord() {
- return word;
- }
- public int getWrongCount() {
- return wrongCount;
- }
- public void incrementWrongCount() {
- wrongCount++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement