01: /*
02: * $Id: Letter.java 3364 2006-07-10 10:33:29Z gbevin $ $Revision: 3364 $ $Date:
03: * 2005-10-02 12:04:34 +0200 (So, 02 Okt 2005) $
04: *
05: * ==============================================================================
06: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
07: * use this file except in compliance with the License. You may obtain a copy of
08: * the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15: * License for the specific language governing permissions and limitations under
16: * the License.
17: */
18: package model;
19:
20: import java.io.Serializable;
21:
22: /**
23: * Model for a letter in the game of hangman
24: *
25: * @author Jonathan Locke
26: * @author Geert Bevin
27: */
28: public class Letter implements Serializable {
29: /** True if the letter has been guessed */
30: private boolean guessed;
31:
32: /** The letter */
33: private char letter;
34:
35: /**
36: * Constructor
37: *
38: * @param letter
39: * The letter
40: */
41: public Letter(final char letter) {
42: this .letter = letter;
43: }
44:
45: /**
46: * @return This letter as a string
47: */
48: public String asString() {
49: return Character.toString(letter);
50: }
51:
52: /**
53: * @see java.lang.Object#equals(java.lang.Object)
54: */
55: public boolean equals(final Object object) {
56: if (object instanceof Letter) {
57: final Letter that = (Letter) object;
58: return that.letter == this .letter
59: && that.guessed == this .guessed;
60: }
61: return false;
62: }
63:
64: /**
65: * Guess this letter
66: */
67: public void guess() {
68: this .guessed = true;
69: }
70:
71: /**
72: * @see java.lang.Object#hashCode()
73: */
74: public int hashCode() {
75: return letter << (guessed ? 1 : 0);
76: }
77:
78: /**
79: * @return Returns the isGuessed.
80: */
81: public boolean isGuessed() {
82: return guessed;
83: }
84:
85: /**
86: * Resets this letter into the default state
87: */
88: public void reset() {
89: this .guessed = false;
90: }
91:
92: /**
93: * @see java.lang.Object#toString()
94: */
95: public String toString() {
96: return "[Letter letter = " + letter + ", guessed = " + guessed
97: + "]";
98: }
99: }
|