/** * putWordInSlot() * * <p>Puts each letter from the word it's passed into the slot it's passed, and marks the word * USED. Also increments the positions in the letterUsage array corresponding to the slot to * indicate that one more word is now using these letters. */ private void putWordInSlot(CrosswordWord w, CrosswordSpace slot) { Point position = new Point(slot.getStart()); for (int i = 0; i < slot.getLength(); i++) { // Put each letter from the word into this slot of // the puzzle: puzzle[position.x][position.y] = w.getWord().charAt(i); // Record the fact that one more word is now using the // letter at this position: letterUsage[position.x][position.y]++; // Advance to the next position in the slot: position.x += slot.getDirection().x; position.y += slot.getDirection().y; } // Mark the word as USED: w.setUsed(true); }
/** * removeWordFromSlot() * * <p>Clears each position in the slot it's passed, but ONLY those positions containing letters * NOT used by any other words according to the letterUsage array. Also marks the word that used * to be in the slot as UNUSED. */ private void removeWordFromSlot(CrosswordWord w, CrosswordSpace slot) { Point position = new Point(slot.getStart()); for (int i = 0; i < slot.getLength(); i++) { // One fewer word is now using the letter at this position: letterUsage[position.x][position.y]--; // If no words are now using this letter, clear it: if (letterUsage[position.x][position.y] == 0) { puzzle[position.x][position.y] = BLANK; } // Advance to the next position in the slot: position.x += slot.getDirection().x; position.y += slot.getDirection().y; } // Mark the word as UNUSED -- ie., available to be placed // elsewhere in the puzzle: w.setUsed(false); }
/** * wordFitsInSlot() * * <p>Returns true if the word passed to it fits into the slot passed to it AND is unused, * otherwise returns false. */ private boolean wordFitsInSlot(CrosswordWord w, CrosswordSpace slot) { // If the length of the word doesn't match the length of the // slot, or the word is already used, we can't put this word // here and so we return false: if (w.getWord().length() != slot.getLength() || w.isUsed()) { return false; } // Otherwise we examine each position in the slot. If // there are letters in the slot already, and those letters // DON'T match the letters at the corresponding positions // in our word, the word won't fit and we return false: Point position = new Point(slot.getStart()); for (int i = 0; i < slot.getLength(); i++) { if (puzzle[position.x][position.y] != BLANK && puzzle[position.x][position.y] != w.getWord().charAt(i)) { return false; } // Advance to the next position in the slot: position.x += slot.getDirection().x; position.y += slot.getDirection().y; } // If we get here, it means the word is unused, the right // length for the current slot, and its letters match any // letters already in the slot: return true; }