-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWord.java
More file actions
183 lines (166 loc) · 4.67 KB
/
Word.java
File metadata and controls
183 lines (166 loc) · 4.67 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
*<p><b>You may add additional methods, but may not change existing methods</b></p>
*
* The Word class has two components:
*
*<ol>
* <li>the word (String)</li>
* <li>path: the location on the board for each consecutive letter of the word (ArrayList of Location)</li>
*</ol>
*
*<p><i>Note that for words that contain "QU", the length of a word is longer
*than its path by 1 since "QU" in a word is represented by "Q" on the board</i></p>
*
*<p>The constructors allocate an ArrayList of Locations for the path</p>
*
*
*@author Philip Chan
*/
import java.util.*;
public class Word {
private String word; // word
private ArrayList<Location> path; // row and column of each consecutive letter
/**
* default constructor with 8 locations as the initial capacity for the path.
* Location objects on the path are not allocated since actual path length is not known.
*
* Max for a 4x4 board is 16, but rare
*/
public Word() {
word = "";
path = new ArrayList<Location>(8);
}
/**
* constructor with an initial maximum path length;
* path is intialized, location objects are allocated with row 0 and column 0
* @param initialMaxPathLength initial maximum path length
*/
public Word(int initialMaxPathLength) {
word = "";
path = new ArrayList<Location>(initialMaxPathLength);
for (int index = 0; index < initialMaxPathLength; index++)
path.add(new Location(0, 0));
}
/**
* constructor with a word;
* path is intialized, location objects are not allocated
* @param aWord the corresponding word
*/
public Word(String aWord) {
word = aWord;
path = new ArrayList<Location>(word.length());
}
/**
* set the word field (the path field is not modified or checked)
* @param aWord the corresponding word
*/
public void setWord(String aWord) {
if (word != null)
word = aWord;
else
System.out.println("Warning: Word:setPath()--aWord is null");
}
/**
* set the path field (the word field is not modified or checked)
* @param aPath the corresponding path
*/
public void setPath(ArrayList<Location> aPath) {
if (aPath != null)
path = aPath;
else
System.out.println("Warning: Word:setPath()--aPath is null");
}
/**
* add the row and column of a letter to the end of the path
* @param row row
* @param col column
*/
public void addLetterRowAndCol(int row, int col) {
path.add(new Location(row, col));
}
/**
* set the row and column of a letter at letterIndex on the path
* @param letterIndex index of the letter on the path
* @param row row
* @param col column
*/
public void setLetterRowAndCol(int letterIndex, int row, int col) {
if ((letterIndex >= 0) && (letterIndex < path.size()))
{
Location loc = path.get(letterIndex);
if (loc != null) // overwrite the location
{
loc.row = row;
loc.col = col;
}
else // create a new location object
path.set(letterIndex, new Location(row, col));
}
else
System.err.println("Word.setLetterRowAndCol(): letterIndex out of bound: " + letterIndex);
}
/**
* return the word
* @return the corresponding word
*/
public String getWord() {
return word;
}
/**
* get the path length
* @return length of the path
*/
public int getPathLength() {
return path.size();
}
/**
* get row at letterIndex on the path;
* return -1 if invalid Index or no row at letterIndex
*
* @param letterIndex index of letter on the path
* @return row index; -1 if invalid letterIndex or no row at letterIndex
*/
public int getLetterRow(int letterIndex) {
Location loc = getLetterLocation(letterIndex);
if (loc != null)
return loc.row;
else
return -1;
}
/**
* get column at letterIndex on the path;
* return -1 if invalid column or no column at letterIndex
*
* @param letterIndex index of letter on the path
* @return row index; -1 if invalid letterIndex or no column at letterIndex
*/
public int getLetterCol(int letterIndex) {
Location loc = getLetterLocation(letterIndex);
if (loc != null)
return loc.col;
else
return -1;
}
/**
* get location at letterIndex on the path;
* return null if invalid column or no location at letterIndex
*
* @param letterIndex index of letter on the path
* @return row index; -1 if invalid letterIndex or no column at letterIndex
*/
public Location getLetterLocation(int letterIndex) {
if ((letterIndex >= 0) && (letterIndex < path.size())) {
Location loc = path.get(letterIndex);
if (loc != null)
return loc;
else {
System.err.println("Word.getLetterLocation(): no location at letterIndex" + letterIndex);
return null;
}
}
else {
System.err.println("Word.getLetterLocation(): out of bound at letterIndex" + letterIndex);
return null;
}
}
}