KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hangman > Game


1 /* This work is hereby released into the Public Domain.
2  * To view a copy of the public domain dedication, visit
3  * http://creativecommons.org/licenses/publicdomain/
4  * or send a letter to Creative Commons, 559 Nathan Abbott Way,
5  * Stanford, California 94305, USA.
6  */

7
8 package hangman;
9
10 /**
11  * Tracks the players progress through the game, including guessed letters and
12  * incorrect guesses remaining.
13  *
14  * @author Howard Lewis Ship
15  */

16 public class Game
17 {
18     private String JavaDoc _targetWord;
19     private int _incorrectGuessesLeft;
20     private char[] _letters;
21     private boolean[] _guessed = new boolean[26];
22     private boolean _win;
23
24     /**
25      * Returns true if the player has guessed all letters in the word.
26      *
27      */

28
29     public boolean isWin()
30     {
31         return _win;
32     }
33
34     /**
35      * Returns an array of letters that have been guessed by the player, with
36      * an underscore for each unguessed position. Once the player loses,
37      * this returns the array of actual letters in the target word.
38      *
39      * <p>
40      * The caller must not modify this array.
41      *
42      */

43
44     public char[] getLetters()
45     {
46         return _letters;
47     }
48
49     /**
50      * Returns the number of incorrect guesses remaining.
51      * An incorrect guess when this is already zero results
52      * in a loss.
53      *
54      */

55
56     public int getIncorrectGuessesLeft()
57     {
58         return _incorrectGuessesLeft;
59     }
60
61     /**
62      * Returns an array of flags indicating which letters have already been guessed.
63      * There are 26 flags, one for each letter, starting with 'A' at index 0, up to 'Z' at
64      * index 25.
65      *
66      * <p>The caller must not modify this array.
67      *
68      */

69
70     public boolean[] getGuessedLetters()
71     {
72         return _guessed;
73     }
74
75     /**
76      * Initializes the Game with a new target word. This resets the
77      * incorrectGuessesLeft count, and initializes the array of
78      * letters and letters guessed.
79      *
80      */

81
82
83     public void start(String JavaDoc word)
84     {
85         _targetWord = word;
86         _incorrectGuessesLeft = 5;
87         _win = false;
88
89         int count = word.length();
90
91         _letters = new char[count];
92
93         for (int i = 0; i < count; i++)
94             _letters[i] = '_';
95
96         for (int i = 0; i < 26; i++)
97             _guessed[i] = false;
98     }
99         
100     /**
101      * The player makes a guess. If the letter has already been
102      * guessed, then no change occurs. Otherwise, there's a check
103      * to see if the guess fills in any positions in the target word.
104      * This may result in a win. If the guess doesn't match any
105      * letter of the word, then a failure occurs; when enough
106      * failures occur, the game results in a loss.
107      *
108      * @return true if further guesses are allowed (this guess did
109      * not result in a win or a loss), or false if further guesses
110      * are not allowed (the player guessed the word, or used up
111      * all possible incorrect guesses).
112      *
113      */

114
115     public boolean makeGuess(char letter)
116     {
117         char ch = Character.toLowerCase(letter);
118
119         if (ch < 'a' || ch > 'z')
120             throw new IllegalArgumentException JavaDoc("Must provide an alphabetic character.");
121
122         int index = ch - 'a';
123
124         // If the player (somehow) guesses the same letter more than once, it does not affect
125
// state of the game.
126

127         if (_guessed[index])
128             return true;
129
130         _guessed[index] = true;
131
132         boolean good = false;
133         boolean complete = true;
134
135         for (int i = 0; i < _letters.length; i++)
136         {
137             if (_letters[i] != '_')
138                 continue;
139
140             if (_targetWord.charAt(i) == ch)
141             {
142                 good = true;
143                 _letters[i] = ch;
144                 continue;
145             }
146
147             // An empty slot that does not match
148
// the guess, so the word is not
149
// complete.
150

151             complete = false;
152         }
153
154         if (good)
155         {
156             _win = complete;
157
158             return !complete;
159         }
160
161
162         if (_incorrectGuessesLeft == 0)
163         {
164             // Replace the letters array with the solution
165

166             _letters = _targetWord.toCharArray();
167
168             return false;
169         }
170
171         _incorrectGuessesLeft--;
172
173         // Not a good guess, but not a loss yet
174

175         return true;
176     }
177 }
Popular Tags