001: /*
002: * $Id: Hangman.java 471756 2006-11-06 15:01:43Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.showcase.hangman;
022:
023: import java.io.Serializable;
024: import java.util.ArrayList;
025: import java.util.Arrays;
026: import java.util.List;
027:
028: public class Hangman implements Serializable {
029:
030: private static final long serialVersionUID = 8566954355839652509L;
031:
032: private Vocab vocab;
033:
034: private Boolean win = false;
035:
036: private int guessLeft = 5;
037: public List<Character> charactersAvailable;
038: public List<Character> charactersGuessed;
039:
040: public Hangman(Vocab vocab) {
041: // Arrays.asList(...) returns List that doesn't support remove(), hence
042: // we wrap it with an ArrayList to avoid UnsupportedOperationException
043: // when doing a remove()
044: charactersAvailable = new ArrayList<Character>(Arrays
045: .asList(new Character[] { Character.valueOf('A'),
046: Character.valueOf('B'), Character.valueOf('C'),
047: Character.valueOf('D'), Character.valueOf('E'),
048: Character.valueOf('F'), Character.valueOf('G'),
049: Character.valueOf('H'), Character.valueOf('I'),
050: Character.valueOf('J'), Character.valueOf('K'),
051: Character.valueOf('L'), Character.valueOf('M'),
052: Character.valueOf('N'), Character.valueOf('O'),
053: Character.valueOf('P'), Character.valueOf('Q'),
054: Character.valueOf('R'), Character.valueOf('S'),
055: Character.valueOf('T'), Character.valueOf('U'),
056: Character.valueOf('V'), Character.valueOf('W'),
057: Character.valueOf('X'), Character.valueOf('Y'),
058: Character.valueOf('Z') }));
059: charactersGuessed = new ArrayList<Character>();
060: this .vocab = vocab;
061: }
062:
063: public void guess(Character character) {
064: assert (character != null);
065:
066: synchronized (charactersAvailable) {
067: if (guessLeft < 0) {
068: throw new HangmanException(HangmanException.Type
069: .valueOf("GAME_ENDED"), "Game already eneded");
070: }
071: Character characterInUpperCase = Character
072: .toUpperCase(character);
073: boolean ok = charactersAvailable
074: .remove(characterInUpperCase);
075: if (ok) {
076: charactersGuessed.add(characterInUpperCase);
077: if (!vocab.containCharacter(characterInUpperCase)) {
078: guessLeft = guessLeft - 1;
079: }
080: }
081: if (vocab.containsAllCharacter(charactersGuessed)) {
082: win = true;
083: }
084: System.out.println(" *********************************** "
085: + win);
086: }
087: }
088:
089: public Boolean isWin() {
090: return this .win;
091: }
092:
093: public Vocab getVocab() {
094: return vocab;
095: }
096:
097: public Boolean gameEnded() {
098: return ((guessLeft < 0) || win);
099: }
100:
101: public Integer guessLeft() {
102: return guessLeft;
103: }
104:
105: public List<Character> getCharactersAvailable() {
106: synchronized (charactersAvailable) {
107: return new ArrayList<Character>(charactersAvailable);
108: //return charactersAvailable;
109: }
110: }
111:
112: public boolean characterGuessedBefore(Character character) {
113: return charactersGuessed.contains(character);
114: }
115: }
|