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];
static List<String> solutions = new ArrayList<>();
}
 
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 Random rand = new Random();
 
final static List<char[]>Random wordsrand = new ArrayList<>Random();
final static List<char[][]> words = new gridArrayList<>();
static List<String> solutions;
 
public static void main(String[] args) {
readWords("unixdict.txt");
printResult(createWordSearch());
printResult();
}
 
Line 206 ⟶ 209:
}
 
static intGrid placeMessagecreateWordSearch(String msg) {
Grid grid = }null;
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;
}
 
static void createWordSearch() {
int numAttempts = 0;
 
Line 228 ⟶ 217:
Collections.shuffle(words);
 
grid = new char[nRows][nCols]Grid();
solutionsint messageLen = new ArrayList<>placeMessage(grid, "Rosetta Code");
int messageLentarget = placeMessage("RosettagridSize Code")- messageLen;
int numCellsToFill = gridSize - messageLen;
 
int cellsFilled = 0;
for (char[] word : words) {
cellsFilled += tryPlaceWord(grid, word);
if (cellsFilled == numCellsToFilltarget && grid.solutions.size() >= minWords) {
grid.numAttempts = numAttempts;
break outer;
}
}
}
 
System.out.println("Attempts: " + numAttempts);
printResult()return grid;
}
 
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), solutions.get(i + 1));
grid.solutions.get(i + 1));
}
if (size % 2 == 1)
System.out.println(grid.solutions.get(size - 1));
}
}</lang>
 
<pre>Attempts: 12
Number of words: 27
 
0 1 2 3 4 5 6 7 8 9
 
0 bR sp fd Ri po wr to t lr Oa
1 pO ua to ye as gb il po ac S
2 rm es rt hl uf ye mt l xa gy
3 pE et ne Ei ty ao Tt es yT ei
4 xe by sl tb et cg ar fs cp tl
5 nr ul T e ei A bh lo ae e l
6 o e gl nh lt mj c Cn hs r pC
7 rz gl io tu a b oa O t ma t
8 ou yk gr Dg ic en lD sz ei ol
9 Eo zt zr ua bv le rl eE tv cw
 
pentecostrototill (8,0,1)(81,97) buzz polygonal (41,90)(1,9,8)
resemblefill (04,2)(71,95) tag goer (73,08)(90,25)
pyrex travel (41,09)(06,49) compete deforest (92,90)(9,37)
tahoe toroid (97,40)(52,80) amp truth (51,39)(75,15)
buggyestes (18,45)(4,1,8) yuh ipecac (59,23)(34,28)
untilouzo (10,59)(50,96) wily pasty (51,0)(81,34)
facetdote (72,40)(32,43) nerolay (07,52)(09,82)
cartewitch (89,49)(85,85) realhan (3,6,9)(35,68)
sue bloc (5,1,0)(8,1,2) petill (09,3)(29,15)
lax slot (87,03)(87,20) albart (89,50)(67,50)
gayore (50,16)(30,14) meetbye (43,64)(45,32)
eggelk (01,6)(21,8) elijan (4,5,6)(25,78)
mitliz (69,28)(67,08) eyedam (72,30)(90,32)
furvia (28,09)(08,2) rub (2,2)(0,07)</pre>
Anonymous user