-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractPlayer.java
122 lines (110 loc) · 3.17 KB
/
AbstractPlayer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* Copyright (C) 2002 Michael Green <[email protected]>
*
* Copyright (C) 2002 Paul Kube <[email protected]>
*
* Copyright (C) 2005 Owen Astrachan <[email protected]>
*
* Copyright (C) 2011 Hoa Long Tam <[email protected]> and Armin Samii
*
* This file is part of CS Boggle.
*
* CS Boggle is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* CS Boggle is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* CS boggle. If not, see <http://www.gnu.org/licenses/>.
*/
import java.util.Iterator;
import java.util.TreeSet;
/**
* Supplies default implementation and state for a player. Allows a player to a
* view, a scorekeeper, a lexicon and to store words found in a game. This
* implementation uses a set to store strings/words and updates score for new
* words added to the player. Words are not checked for being in a lexicon or on
* a board int the <code>add</code> method.
* <P>
*
* @author Owen Astrachan
*
*/
public abstract class AbstractPlayer implements Player {
protected LexiconInterface myLexicon;
protected PlayerViewInterface myView;
protected TreeSet<String> myWords;
protected BoggleScore myScoreKeeper;
protected int myScore;
/**
* Make this player have an associated lexicon and initialize other state
* appropriately.
*
* @param lex
* is the lexicon for this player
*/
@Override
public void initialize(LexiconInterface lex) {
myLexicon = lex;
myWords = new TreeSet<String>();
myScoreKeeper = new BoggleScore();
myScore = 0;
}
/**
* Set a veiw for this player, if not called the player will have a null view.
*
* @param view
* is this player's view
*/
@Override
public void setView(PlayerViewInterface view) {
myView = view;
}
/**
* Return the player's score (as calculated via updates to the
* <code>add</code> method).
*
* @return this player's score
*/
@Override
public int getScore() {
return myScore;
}
/**
* Add a word to this player's list of words, adjust score if word is new to
* this player. Duplicate words are not added, board and lexicon are <em>not
* checked</em>.
*
* @param word
* is the word added to this player
*/
@Override
public boolean add(String word) {
if (!myWords.contains(word)) {
myScore += myScoreKeeper.getScore(word, 4);
myWords.add(word);
return true;
}
return false;
}
@Override
public Iterator<String> iterator() {
return myWords.iterator();
}
@Override
public abstract String getName();
@Override
public void clear() {
myWords.clear();
myScore = 0;
}
@Override
public int wordCount() {
return myWords.size();
}
}