001: /*
002: * $Id: Word.java 3364 2006-07-10 10:33:29Z gbevin $ $Revision: 3364 $ $Date:
003: * 2005-03-31 15:26:49 +0200 (Do, 31 Mrz 2005) $
004: *
005: * ==============================================================================
006: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
007: * use this file except in compliance with the License. You may obtain a copy of
008: * the 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.Iterator;
023: import java.util.List;
024:
025: /**
026: * Model for a word in the game of Game.
027: *
028: * @author Jonathan Locke
029: * @author Geert Bevin
030: */
031: public class Word implements Serializable {
032: /** The word */
033: final List letters = new ArrayList();
034:
035: /**
036: * Constructor
037: *
038: * @param word
039: * The word
040: */
041: public Word(final String word) {
042: for (int i = 0; i < word.length(); i++) {
043: letters.add(new Letter(word.charAt(i)));
044: }
045: }
046:
047: /**
048: * @return This word
049: */
050: public String asString() {
051: return asString(false);
052: }
053:
054: /**
055: * @param hideUnguessed
056: * True if unguessed letters should be hidden
057: * @return This word as a String
058: */
059: public String asString(final boolean hideUnguessed) {
060: final StringBuffer buffer = new StringBuffer();
061: for (Iterator iterator = letters.iterator(); iterator.hasNext();) {
062: final Letter letter = (Letter) iterator.next();
063: if (hideUnguessed) {
064: buffer.append(letter.isGuessed() ? letter.asString()
065: : "_");
066: } else {
067: buffer.append(letter.asString());
068: }
069: }
070: return buffer.toString();
071: }
072:
073: /**
074: * @see java.lang.Object#equals(java.lang.Object)
075: */
076: public boolean equals(final Object object) {
077: if (object instanceof Word) {
078: final Word that = (Word) object;
079: return this .asString().equalsIgnoreCase(that.asString());
080: }
081: return false;
082: }
083:
084: /**
085: * @param letter
086: * The letter to guess
087: * @return True if guess was correct
088: */
089: public boolean guess(final Letter letter) {
090: boolean correct = false;
091: for (Iterator iterator = letters.iterator(); iterator.hasNext();) {
092: final Letter current = (Letter) iterator.next();
093: if (current.equals(letter)) {
094: current.guess();
095: correct = true;
096: }
097: }
098: letter.guess();
099: return correct;
100: }
101:
102: /**
103: * @see java.lang.Object#hashCode()
104: */
105: public int hashCode() {
106: return asString().hashCode();
107: }
108:
109: /**
110: * @return True if the word has been guessed
111: */
112: public boolean isGuessed() {
113: for (final Iterator iterator = letters.iterator(); iterator
114: .hasNext();) {
115: final Letter letter = (Letter) iterator.next();
116: if (!letter.isGuessed()) {
117: return false;
118: }
119: }
120: return true;
121: }
122:
123: /**
124: * @see java.lang.Object#toString()
125: */
126: public String toString() {
127: return "[Word letters = " + letters + "]";
128: }
129: }
|