FileDocCategorySizeDatePackage
Hangman.javaAPI DocApache Struts 2.0.9 Apps4146Mon Jul 23 13:43:26 BST 2007org.apache.struts2.showcase.hangman

Hangman

public class Hangman extends Object implements Serializable

Fields Summary
private static final long
serialVersionUID
private Vocab
vocab
private Boolean
win
private int
guessLeft
public List
charactersAvailable
public List
charactersGuessed
Constructors Summary
public Hangman(Vocab vocab)


       
        // Arrays.asList(...) returns List that doesn't support remove(), hence
        // we wrap it with an ArrayList to avoid UnsupportedOperationException
        // when doing a remove()
        charactersAvailable = new ArrayList<Character>(Arrays.asList(
            new Character[] {
                Character.valueOf('A"), Character.valueOf('B"), Character.valueOf('C"),
                Character.valueOf('D"), Character.valueOf('E"), Character.valueOf('F"),
                Character.valueOf('G"), Character.valueOf('H"), Character.valueOf('I"),
                Character.valueOf('J"), Character.valueOf('K"), Character.valueOf('L"),
                Character.valueOf('M"), Character.valueOf('N"), Character.valueOf('O"),
                Character.valueOf('P"), Character.valueOf('Q"), Character.valueOf('R"),
                Character.valueOf('S"), Character.valueOf('T"), Character.valueOf('U"),
                Character.valueOf('V"), Character.valueOf('W"), Character.valueOf('X"),
                Character.valueOf('Y"), Character.valueOf('Z")
            }));
        charactersGuessed = new ArrayList<Character>();
        this.vocab = vocab;
    
Methods Summary
public booleancharacterGuessedBefore(java.lang.Character character)

        return charactersGuessed.contains(character);
    
public java.lang.BooleangameEnded()

        return ((guessLeft < 0) || win);
    
public java.util.ListgetCharactersAvailable()

        synchronized(charactersAvailable) {
             return new ArrayList<Character>(charactersAvailable);
            //return charactersAvailable;
        }
    
public VocabgetVocab()

        return vocab;
    
public voidguess(java.lang.Character character)

        assert(character != null);

        synchronized(charactersAvailable) {
            if (guessLeft < 0) {
                throw new HangmanException(
                    HangmanException.Type.valueOf("GAME_ENDED"), "Game already eneded");
            }
            Character characterInUpperCase = Character.toUpperCase(character);
            boolean ok = charactersAvailable.remove(characterInUpperCase);
            if (ok) {
                charactersGuessed.add(characterInUpperCase);
                if (! vocab.containCharacter(characterInUpperCase)) {
                    guessLeft = guessLeft - 1;
                }
            }
            if (vocab.containsAllCharacter(charactersGuessed)) {
                win = true;
            }
            System.out.println(" *********************************** "+win);
        }
    
public java.lang.IntegerguessLeft()

        return guessLeft;
    
public java.lang.BooleanisWin()

        return this.win;