01: /*
02: * $Id: Vocab.java 471756 2006-11-06 15:01:43Z husted $
03: *
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "License"); you may not use this file except in compliance
10: * with the License. You may obtain a copy of the License at
11: *
12: * http://www.apache.org/licenses/LICENSE-2.0
13: *
14: * Unless required by applicable law or agreed to in writing,
15: * software distributed under the License is distributed on an
16: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17: * KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations
19: * under the License.
20: */
21: package org.apache.struts2.showcase.hangman;
22:
23: import java.io.Serializable;
24: import java.util.ArrayList;
25: import java.util.Arrays;
26: import java.util.List;
27:
28: public class Vocab implements Serializable {
29:
30: private static final long serialVersionUID = 1L;
31:
32: private String vocab;
33: private String hint;
34: private Character[] characters; // character this vocab is made up of
35:
36: public Vocab(String vocab, String hint) {
37: assert (vocab != null);
38: assert (hint != null);
39:
40: this .vocab = vocab.toUpperCase();
41: this .hint = hint;
42: }
43:
44: public String getVocab() {
45: return this .vocab;
46: }
47:
48: public String getHint() {
49: return this .hint;
50: }
51:
52: public Boolean containCharacter(Character character) {
53: assert (character != null);
54:
55: return (vocab.contains(character.toString())) ? true : false;
56: }
57:
58: public Character[] inCharacters() {
59: if (characters == null) {
60: char[] c = vocab.toCharArray();
61: characters = new Character[c.length];
62: for (int a = 0; a < c.length; a++) {
63: characters[a] = Character.valueOf(c[a]);
64: }
65: }
66: return characters;
67: }
68:
69: public boolean containsAllCharacter(
70: List<Character> charactersGuessed) {
71: Character[] chars = inCharacters();
72: List<Character> tmpChars = Arrays.asList(chars);
73: return charactersGuessed.containsAll(tmpChars);
74: }
75:
76: public static void main(String args[]) throws Exception {
77: Vocab v = new Vocab("JAVA", "a java word");
78:
79: List<Character> list1 = new ArrayList<Character>();
80: list1.add(new Character('J'));
81: list1.add(new Character('V'));
82:
83: List<Character> list2 = new ArrayList<Character>();
84: list2.add(new Character('J'));
85: list2.add(new Character('V'));
86: list2.add(new Character('A'));
87:
88: System.out.println(v.containsAllCharacter(list1));
89: System.out.println(v.containsAllCharacter(list2));
90:
91: }
92: }
|