private void checkWordExistence(
boolean isVertical, int axis, int first, int last)
throws BoardWrongWordPlace {
StringBuilder word = new StringBuilder("");
BoardCase boardCase = null;
List<Letter> jokers = new ArrayList<Letter>(1);
int firstP = first, lastP = last;
// Get complete word
if (isVertical) {
while (this.getCase(firstP - 1, axis).getState() != FREE) {
firstP--; // Top index
}
while (this.getCase(lastP + 1, axis).getState() != FREE) {
lastP++; // Down index
}
// Current word letter
for (int v = firstP; v <= lastP; v++) {
Letter letter = this.getCase(v, axis).getLetter();
word = word.append(letter.getName());
if (letter.getName() == Bag.JOKER) {
jokers.add(letter);
}
}
}
else {
while (this.getCase(axis, firstP - 1).getState() != FREE) {
firstP--; // Left index
}
while (this.getCase(axis, lastP + 1).getState() != FREE) {
lastP++; // Right index
}
// Current word letter
for (int h = firstP; h <= lastP; h++) {
Letter letter = this.getCase(axis, h).getLetter();
word = word.append(letter.getName());
if (letter.getName() == Bag.JOKER) {
jokers.add(letter);
}
}
}
// Apply joker char to use (if has and found)
int numOfJoker = jokers.size();
if (numOfJoker > 0) {
Character[] joker = this.tryJokers(word);
jokers.get(0).setJokerChar(joker[0]);
if (numOfJoker == 2) {
jokers.get(1).setJokerChar(joker[1]);
}
jokers.clear();
jokers = null;
joker = null;
}
// Check existence
if (!this.dictionary.contains(word)) {
if (this.isAI) {
this.valide = false;
return;
}
else {
throw new BoardWrongWordPlace(
"This word doesn't exist:", " " + word);
}
}
// If word exists, calculate points
int score = this.calculateWordPoints(isVertical, axis, firstP, lastP);
Debug.console(
"checkWordExistence", "word = ", word + " | score = " + score);
this.words.add(word.toString());
this.points += score;
// Check now adjacents words (for each word letters)
if (isVertical) {
for (int v = firstP; v <= lastP; v++) {
boardCase = this.getCase(v, axis);
// Letter has adjacent letters (right or left)
if (!boardCase.getAdjacentIgnorence() && !boardCase.isChecked()
&& boardCase.getState() == NEW) {
if (this.getCase(v, axis - 1).getState() == OLD
|| this.getCase(v, axis + 1).getState() == OLD) {
boardCase.setChecked(true);
this.checkedCases.add(boardCase);
this.checkWordExistence(!isVertical, v, axis, axis);
}
}
}
}
else {
for (int h = firstP; h <= lastP; h++) {
boardCase = this.getCase(axis, h);
// Letter has adjacent letters (up or down)
if (!boardCase.getAdjacentIgnorence() && !boardCase.isChecked()
&& boardCase.getState() == NEW) {
if (this.getCase(axis - 1, h).getState() == OLD
|| this.getCase(axis + 1, h).getState() == OLD) {
boardCase.setChecked(true);
this.checkedCases.add(boardCase);
this.checkWordExistence(!isVertical, h, axis, axis);
}
}
}