Anonymous user
Word search: Difference between revisions
get rid of global state
(updated code) |
(get rid of global state) |
||
Line 175:
public class WordSearch {
static class Grid {
int numAttempts;
char[][] cells = new char[nRows][nCols];
}▼
final static int[][] dirs = {{1, 0}, {0, 1}, {1, 1}, {1, -1}, {-1, 0},
{0, -1}, {-1, -1}, {-1, 1}};
Line 182 ⟶ 188:
final static int gridSize = nRows * nCols;
final static int minWords = 25;
final static
final static List<char[]
▲ static List<String> solutions;
public static void main(String[] args) {
readWords("unixdict.txt");
printResult(createWordSearch());
printResult();▼
}
Line 206 ⟶ 209:
}
static
msg = msg.toUpperCase().replaceAll("[^A-Z]", "");▼
int messageLen = msg.length();▼
if (messageLen > 0 && messageLen < gridSize) {▼
int gapSize = gridSize / messageLen;▼
for (int i = 0; i < messageLen; i++) {▼
int pos = i * gapSize + rand.nextInt(gapSize);▼
grid[pos / nCols][pos % nCols] = msg.charAt(i);▼
▲ }
}▼
return messageLen;▼
▲ }
int numAttempts = 0;
Line 228 ⟶ 217:
Collections.shuffle(words);
grid = new
int
int cellsFilled = 0;
for (char[] word : words) {
cellsFilled += tryPlaceWord(grid, word);
if (cellsFilled ==
grid.numAttempts = numAttempts;
break outer;
}
}
}
System.out.println("Attempts: " + numAttempts);▼
}
static int placeMessage(Grid grid, String msg) {
▲ msg = msg.toUpperCase().replaceAll("[^A-Z]", "");
▲ int messageLen = msg.length();
▲ if (messageLen > 0 && messageLen < gridSize) {
▲ int gapSize = gridSize / messageLen;
▲ for (int i = 0; i < messageLen; i++) {
▲ int pos = i * gapSize + rand.nextInt(gapSize);
▲ grid.cells[pos / nCols][pos % nCols] = msg.charAt(i);
}
▲ }
▲ return messageLen;
}
static int tryPlaceWord(Grid grid, char[] word) {
int randDir = rand.nextInt(dirs.length);
int randPos = rand.nextInt(gridSize);
Line 254 ⟶ 259:
pos = (pos + randPos) % gridSize;
int lettersPlaced = tryLocation(grid, word, dir, pos);
if (lettersPlaced > 0)
return lettersPlaced;
Line 262 ⟶ 267:
}
static int tryLocation(Grid grid, char[] word, int dir, int pos) {
int r = pos / nCols;
Line 279 ⟶ 284:
// check cells
for (i = 0, rr = r, cc = c; i < len; i++) {
if (grid.cells[rr][cc] != 0 && grid.cells[rr][cc] != word[i])
return 0;
cc += dirs[dir][0];
Line 287 ⟶ 292:
// place
for (i = 0, rr = r, cc = c; i < len; i++) {
if (grid.cells[rr][cc] == word[i])
overlaps++;
else
grid.cells[rr][cc] = word[i];
if (i < len - 1) {
Line 301 ⟶ 306:
if (lettersPlaced > 0) {
String w = new String(word);
grid.solutions.add(format("%-10s (%d,%d)(%d,%d)", w, c, r, cc, rr));
}
Line 307 ⟶ 312:
}
static void printResult(Grid grid) {
if (grid == null || grid.numAttempts == 0) {
System.out.println("No grid to display");
return;
}
int size = grid.solutions.size();▼
▲ System.out.println("Attempts: " + grid.numAttempts);
System.out.println("Number of words: " + size);
System.out.println("\n 0 1 2 3 4 5 6 7 8 9");
System.out.print("");
Line 313 ⟶ 327:
System.out.printf("%n%d ", r);
for (int c = 0; c < nCols; c++)
System.out.printf(" %c ", grid.cells[r][c]);
}
System.out.println("\n");
▲ int size = solutions.size();
for (int i = 0; i < size - 1; i += 2) {
System.out.printf("%s %s%n", grid.solutions.get(i),
grid.solutions.get(i + 1));
}
if (size % 2 == 1)
System.out.println(grid.solutions.get(size - 1));
}
}</lang>
<pre>Attempts:
Number of words: 27
0 1 2 3 4 5 6 7 8 9
0
1
2
3
4
5
6 o e
7
8
9
|