-
Notifications
You must be signed in to change notification settings - Fork 0
/
Move.java
260 lines (213 loc) · 6.12 KB
/
Move.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package de.mainaim.scrabblesolver;
import java.util.ArrayList;
import java.util.Iterator;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
public final class Move implements Comparable<Move> {
private ArrayList<Word> mWords = new ArrayList<Word>();
private int mPoints = -1;
private boolean mValid;
private boolean mValidCheck;
private int mRow;
private int mCol;
private String mLetters;
private Board mBoard;
private boolean mHorizontal;
public Move(String letters, int row, int col, boolean horizontal, Board board) {
mRow = row;
mCol = col;
mLetters = letters;
mBoard = board;
mHorizontal = horizontal;
String newLetters = "";
// Get main word
Word mainWord = board.getFullWord(letters,row,col,horizontal);
String wordstring = mainWord.getLetters();
if(wordstring.length() > 1) {
mWords.add(mainWord);
}
for(int i = 0; i < letters.length(); i++) {
if(letters.charAt(i) != ' ') {
if(horizontal) {
if(board.getField(row,col+i).getTile() != null) {
continue;
}
// Check for vertical neighbours if field is empty
if((board.getField(row+1,col+i) != null && board.getField(row+1,col+i).getTile() != null) || (board.getField(row-1,col+i) != null && board.getField(row-1,col+i).getTile() != null)) {
Word vertWord = board.getFullWord(Character.toString(letters.charAt(i)),row,col+i,false);
if(vertWord.getLetters().length() > 1) {
mWords.add(vertWord);
}
}
} else {
if(board.getField(row+i,col).getTile() != null) {
continue;
}
//Check for horizontal neighbours
if((board.getField(row+i,col+1) != null && board.getField(row+i,col+1).getTile() != null) || (board.getField(row+i,col-1) != null && board.getField(row+i,col-1).getTile() != null)) {
Word horWord = board.getFullWord(Character.toString(letters.charAt(i)),row+i,col,true);
if(horWord.getLetters().length() > 1) {
mWords.add(horWord);
}
}
}
}
}
}
public int getPoints() {
if(mPoints != -1) {
return mPoints;
}
mPoints = 0;
Iterator<Word> iter = mWords.iterator();
while(iter.hasNext()) {
Word word = iter.next();
mPoints += word.getPoints();
}
return mPoints;
}
public int getRow() {
return mRow;
}
public int getCol() {
return mCol;
}
public String getLetters() {
return mLetters;
}
public ArrayList<Word> getWords() {
return mWords;
}
public boolean isValid(Database db) {
if(mValidCheck == true) {
return mValid;
}
if(mWords.size() > 0) {
mValid = true;
Iterator<Word> iter = mWords.iterator();
while(iter.hasNext()) {
Word word = iter.next();
if(word.isValid(db) == false) {
mValid = false;
}
}
}
mValidCheck = true;
return mValid;
}
private String getFullWord(String letters, int row, int col, boolean horizontal) {
String word = "";
if(horizontal) {
if(col > 0) {
// Get all letters before new letters
Field prevHor;
int i = 1;
while((prevHor = mBoard.getField(row,col-i)) != null) {
Tile prevTile = prevHor.getTile();
if(prevTile == null)
break;
i++;
word = prevTile.mChar + word;
//Adjust horizontal starting position for move
mCol--;
}
}
// Get letters between new letters
word += mBoard.fillGaps(letters, row, col, horizontal);
if(col + letters.length() < mBoard.getSize() -1) {
// Get all letters after new letters
Field nextHor;
int i = 0;
while((nextHor = mBoard.getField(row,col + letters.length() +i)) != null) {
Tile nextTile = nextHor.getTile();
if(nextTile == null)
break;
i++;
word += nextTile.mChar;
}
}
} else {
// horizontal == false
if(row > 0) {
// Get all letters before new letters
Field prevVer;
int i = 1;
while((prevVer = mBoard.getField(row-i,col)) != null) {
Tile prevTile = prevVer.getTile();
if(prevTile == null)
break;
i++;
word = prevTile.mChar + word;
//Adjust vertical starting position for move
mRow--;
}
}
// Get letters between new letters
try {
word += mBoard.fillGaps(letters, row, col, horizontal);
} catch(NullPointerException npe) {
System.out.println("Caught NPE with '" + letters + "' at " + row + ", " + col + ". Horizontal: " + horizontal);
}
if(row + letters.length() < mBoard.getSize() -1) {
// Get all letters after new letters
Field nextVer;
int i = 0;
while((nextVer = mBoard.getField(row + letters.length() +i,col)) != null) {
Tile nextTile = nextVer.getTile();
if(nextTile == null)
break;
i++;
word += nextTile.mChar;
}
}
}
return word;
}
@Override
public String toString() {
String output = "[";
output += "Letters: \"" + mLetters + "\" ";
output += "Pts: " + getPoints() + ", ";
output += "Pos: " + mRow + "," + mCol + ", ";
output += "Hor:" + mHorizontal + ", ";
output += "Words: " + mWords.toString() + "]";
return output;
}
@Override
public int compareTo(Move move) {
if(move.getPoints() > getPoints()) {
return 1;
} else if(move.getPoints() < getPoints()) {
return -1;
} else {
return 0;
}
}
@Override
public int hashCode() {
return new HashCodeBuilder(17, 31). // two randomly chosen prime numbers
// if deriving: appendSuper(super.hashCode()).
append(mPoints).
append(mLetters).
append(mRow).
append(mCol).
toHashCode();
}
@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (obj == this)
return true;
if (obj.getClass() != getClass())
return false;
Move rhs = (Move) obj;
return new EqualsBuilder().
// if deriving: appendSuper(super.equals(obj)).
append(mPoints, rhs.getPoints()).
append(mLetters, rhs.getLetters()).
append(mRow, rhs.getRow()).
append(mCol, rhs.getCol()).
isEquals();
}
}