/*
 * $Id$ $Revision$
 * $Date$
 * 
 * ==============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package wicket.examples.hangman;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Model for a word in the game of Game.
 * 
 * @author Jonathan Locke
 */
public class Word
{
	/** The word */
	final List letters = new ArrayList();

	/**
	 * Constructor
	 * 
	 * @param word
	 *            The word
	 */
	public Word(final String word)
	{
		for (int i = 0; i < word.length(); i++)
		{
			letters.add(new Letter(word.charAt(i)));
		}
	}

	/**
	 * @return This word
	 */
	public String asString()
	{
		return asString(false);
	}
	
	/**
	 * @param hideUnguessed
	 *            True if unguessed letters should be hidden
	 * @return This word as a String
	 */
	public String asString(final boolean hideUnguessed)
	{
		final StringBuffer buffer = new StringBuffer();
		for (Iterator iterator = letters.iterator(); iterator.hasNext();)
		{
			final Letter letter = (Letter)iterator.next();
			if (hideUnguessed)
			{
				buffer.append(letter.isGuessed() ? letter.asString() : "_");
			}
			else
			{
				buffer.append(letter.asString());
			}
		}
		return buffer.toString();
	}
	
	/**
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(final Object object)
	{
		if (object instanceof Word)
		{
			final Word that = (Word)object;
			return this.asString().equalsIgnoreCase(that.asString());
		}
		return false;
	}

	/**
	 * @param letter
	 *            The letter to guess
	 * @return True if guess was correct
	 */
	public boolean guess(final Letter letter)
	{
		boolean correct = false;
		for (Iterator iterator = letters.iterator(); iterator.hasNext();)
		{
			final Letter current = (Letter)iterator.next();
			if (current.equals(letter))
			{
				current.guess();
				correct = true;
			}
		}
		letter.guess();
		return correct;
	}
	
	/**
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode()
	{
		return asString().hashCode();
	}

	/**
	 * @return True if the word has been guessed
	 */
	public boolean isGuessed()
	{
		for (final Iterator iterator = letters.iterator(); iterator.hasNext();)
		{
			final Letter letter = (Letter)iterator.next();
			if (!letter.isGuessed())
			{
				return false;
			}
		}
		return true;
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString()
	{
		return "[Word letters = " + letters + "]";
	}
}

@@ -27,18 +27,17 @@
 Date$%0A *
- 
 %0A
+
  * =====
@@ -290,17 +290,16 @@
 se at%0A *
- 
 %0A * http
@@ -339,17 +339,16 @@
 E-2.0%0A *
- 
 %0A * Unle
@@ -699,16 +699,45 @@
 ngman;%0A%0A
+import java.io.Serializable;%0A
 import j
@@ -854,17 +854,16 @@
 Game.%0A *
- 
 %0A * @aut
@@ -902,16 +902,40 @@
 ass Word
+ implements Serializable
 %0A%7B%0A%09/** 
@@ -1010,17 +1010,16 @@
 ctor%0A%09 *
- 
 %0A%09 * @pa
@@ -1275,26 +1275,25 @@
 (false);%0A%09%7D%0A
-%09
 %0A
+
 %09/**%0A%09 * @pa
@@ -1819,26 +1819,25 @@
 tring();%0A%09%7D%0A
-%09
 %0A
+
 %09/**%0A%09 * @se
@@ -2508,17 +2508,16 @@
 ect;%0A%09%7D%0A
-%09
 %0A%09/**%0A%09 

Word class must be serializable for clustering