001: /*
002: * $Id: Game.java 460265 2006-04-16 13:36:52Z jdonnerstag $ $Revision: 460265 $
003: * $Date: 2006-04-16 15:36:52 +0200 (Sun, 16 Apr 2006) $
004: *
005: * ==================================================================== Licensed
006: * under the Apache License, Version 2.0 (the "License"); you may not use this
007: * file except in compliance with the License. You may obtain a copy of the
008: * License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations under
016: * the License.
017: */
018: package wicket.examples.hangman;
019:
020: import java.io.Serializable;
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: /**
025: * Implementation of the actual hangman game model. The model holds the word
026: * generator, the current word, retries remaining and the correctLetters that
027: * have been guessed. It also answers questions such as whether all retries have
028: * been used.
029: *
030: * @author Chris Turner
031: * @author Jonathan Locke
032: */
033: public class Game implements Serializable {
034: /** Number of guesses allowed */
035: private int guessesAllowed;
036:
037: /** Number of guesses remaining */
038: private int guessesRemaining;
039:
040: /** The letters */
041: private final List letters = new ArrayList();
042:
043: /** The word being guessed by the user */
044: private Word word;
045:
046: /** Word generator */
047: private WordGenerator wordGenerator;
048:
049: /**
050: * Return the number of guesses remaining.
051: *
052: * @return The number of guesses
053: */
054: public int getGuessesRemaining() {
055: return guessesRemaining;
056: }
057:
058: /**
059: * @return The letters in the game
060: */
061: public List getLetters() {
062: return letters;
063: }
064:
065: /**
066: * Get the current word that is being guessed or has been guessed.
067: *
068: * @return The current word
069: */
070: public Word getWord() {
071: return word;
072: }
073:
074: /**
075: * Guess the given letter for the current word. If the letter matches then
076: * the word is updated otherwise the guesses remaining counter is reduced.
077: * The letter guessed is also recorded.
078: *
079: * @param letter
080: * The letter being guessed
081: * @return True if guess was correct
082: */
083: public boolean guess(final Letter letter) {
084: if (!letter.isGuessed()) {
085: final boolean correct = word.guess(letter);
086: if (!correct) {
087: guessesRemaining--;
088: }
089: return correct;
090: }
091: return false;
092: }
093:
094: /**
095: * Check whether the user has used up all of their guesses.
096: *
097: * @return Whether all of the user's guesses have been used
098: */
099: public boolean isLost() {
100: return guessesRemaining == 0;
101: }
102:
103: /**
104: * Check whether the user has successfully guessed all of the correctLetters
105: * in the word.
106: *
107: * @return Whether all of the correctLetters have been guessed or not
108: */
109: public boolean isWon() {
110: return word.isGuessed();
111: }
112:
113: /**
114: * Play another game with same settings
115: */
116: public void newGame() {
117: newGame(guessesAllowed, wordGenerator);
118: }
119:
120: /**
121: * Initialise the hangman read for a new game.
122: *
123: * @param guessesAllowed
124: * Number of guesses allowed
125: * @param wordGenerator
126: * The word generator
127: */
128: public void newGame(final int guessesAllowed,
129: final WordGenerator wordGenerator) {
130: this .guessesAllowed = guessesAllowed;
131: this .guessesRemaining = guessesAllowed;
132: this .word = wordGenerator.next();
133: this .wordGenerator = wordGenerator;
134:
135: // Add letters
136: letters.clear();
137: for (char c = 'a'; c <= 'z'; c++) {
138: letters.add(new Letter(c));
139: }
140: }
141: }
|