001: /*
002: * $Id: Game.java 3364 2006-07-10 10:33:29Z gbevin $ $Revision: 3364 $
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 model;
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: * @author Geert Bevin
033: */
034: public class Game implements Serializable {
035: /** Number of guesses allowed */
036: private int guessesAllowed;
037:
038: /** Number of guesses remaining */
039: private int guessesRemaining;
040:
041: /** The letters */
042: private final List<Letter> letters = new ArrayList<Letter>();
043:
044: /** The word being guessed by the user */
045: private Word word;
046:
047: /** Word generator */
048: private WordGenerator wordGenerator;
049:
050: /**
051: * Return the number of guesses remaining.
052: *
053: * @return The number of guesses
054: */
055: public int getGuessesRemaining() {
056: return guessesRemaining;
057: }
058:
059: /**
060: * @return The letters in the game
061: */
062: public List<Letter> getLetters() {
063: return letters;
064: }
065:
066: /**
067: * Get the current word that is being guessed or has been guessed.
068: *
069: * @return The current word
070: */
071: public Word getWord() {
072: return word;
073: }
074:
075: /**
076: * Guess the given letter for the current word. If the letter matches then
077: * the word is updated otherwise the guesses remaining counter is reduced.
078: * The letter guessed is also recorded.
079: *
080: * @param letter
081: * The letter being guessed
082: * @return True if guess was correct
083: */
084: public boolean guess(final Letter letter) {
085: if (!letter.isGuessed()) {
086: final boolean correct = word.guess(letter);
087: if (!correct) {
088: guessesRemaining--;
089: }
090: return correct;
091: }
092: return false;
093: }
094:
095: /**
096: * Check whether the user has used up all of their guesses.
097: *
098: * @return Whether all of the user's guesses have been used
099: */
100: public boolean isLost() {
101: return guessesRemaining == 0;
102: }
103:
104: /**
105: * Check whether the user has successfully guessed all of the correctLetters
106: * in the word.
107: *
108: * @return Whether all of the correctLetters have been guessed or not
109: */
110: public boolean isWon() {
111: return word.isGuessed();
112: }
113:
114: /**
115: * Play another game with same settings
116: */
117: public void newGame() {
118: newGame(guessesAllowed, wordGenerator);
119: }
120:
121: /**
122: * Initialise the hangman read for a new game.
123: *
124: * @param guessesAllowed
125: * Number of guesses allowed
126: * @param wordGenerator
127: * The word generator
128: */
129: public void newGame(final int guessesAllowed,
130: final WordGenerator wordGenerator) {
131: this .guessesAllowed = guessesAllowed;
132: this .guessesRemaining = guessesAllowed;
133: this .word = wordGenerator.next();
134: this .wordGenerator = wordGenerator;
135:
136: // Add letters
137: letters.clear();
138: for (char c = 'a'; c <= 'z'; c++) {
139: letters.add(new Letter(c));
140: }
141: }
142: }
|