-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
223 lines (209 loc) · 5.5 KB
/
app.js
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
// Words to be used in the game
let words = ["set", "tea", "eat", "east", "seat"];
const gridMatrix = () => {
let grid = [];
for (let i = 0; i < 7; i++) {
grid.push([]);
for (let j = 0; j < 7; j++) {
grid[i].push("_");
}
}
return grid;
};
const initializeGame = (grid) => {
let tempWords = words;
let word = tempWords[Math.floor(Math.random() * tempWords.length)];
tempWords = tempWords.filter((filter) => filter !== word);
let wordArray = word.split("");
let wordLength = wordArray.length;
// choose random direction
let randomDirection = Math.floor(Math.random() * 2);
// place the word in grid
if (randomDirection === 0) {
for (let i = 0; i < wordLength; i++) {
grid[0][0 + i] = wordArray[i];
}
} else {
for (let i = 0; i < wordLength; i++) {
grid[0 + i][0] = wordArray[i];
}
}
return {
grid: grid,
words: tempWords,
};
};
const edgeCheck = (grid, coordinates) => {
let edgeCounter = 0;
if (coordinates[0] - 1 >= 0 && coordinates[0] + 1 <= 6 && coordinates[1] - 1 >= 0 && coordinates[1] + 1 <= 6) {
if (grid[coordinates[0] + 1][coordinates[1]] !== "_") {
edgeCounter++;
}
if (grid[coordinates[0] - 1][coordinates[1]] !== "_") {
edgeCounter++;
}
if (grid[coordinates[0]][coordinates[1] + 1] !== "_") {
edgeCounter++;
}
if (grid[coordinates[0]][coordinates[1] - 1] !== "_") {
edgeCounter++;
}
}
if (edgeCounter >= 3) {
return false;
}
return true;
};
const canBePlaced = (grid, word, direction, coordinates) => {
let wordArray = word.split("");
let wordLength = wordArray.length;
let canPlace = true;
if (direction === 0) {
for (let i = 0; i < wordLength; i++) {
if (coordinates[1] + i + 1 >= 6) {
canPlace = false;
break;
}
let coord = [coordinates[0], coordinates[1] + i + 1];
if (!edgeCheck(grid, coord)) {
canPlace = false;
break;
}
if (grid[coordinates[0]][coordinates[1] + i + 1] !== "_") {
canPlace = false;
break;
}
}
} else {
for (let i = 0; i < wordLength; i++) {
if (coordinates[0] + i + 1 >= 6) {
canPlace = false;
break;
}
let coord = [coordinates[0] + i + 1, coordinates[1]];
if (!edgeCheck(grid, coord)) {
canPlace = false;
break;
}
if (grid[coordinates[0] + i + 1][coordinates[1]] !== "_") {
canPlace = false;
break;
}
}
}
// check if direction is not same at the same index
if (direction === 0) {
if (coordinates[1] - 1 >= 0) {
if (grid[coordinates[0]][coordinates[1] - 1] !== "_") {
canPlace = false;
return canPlace;
}
}
} else {
if (coordinates[0] - 1 >= 0) {
if (grid[coordinates[0] - 1][coordinates[1]] !== "_") {
canPlace = false;
return canPlace;
}
}
}
if (!canPlace) {
if (coordinates.length > 2) {
coordinates.shift();
coordinates.shift();
canPlace = canBePlaced(grid, word, direction, coordinates);
if (!canPlace) {
direction = direction === 0 ? 1 : 0;
canPlace = canBePlaced(grid, word, direction, coordinates);
}
}
}
return canPlace;
};
const addWord = (grid, word) => {
let wordArray = word.split("");
let wordLength = wordArray.length;
// find matching letter
let coordinates = [];
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++) {
if (grid[i][j] === wordArray[0]) {
coordinates.push(i, j);
}
}
}
if (coordinates.length === 0) {
return false;
}
// choose random direction
let randomDirection = Math.floor(Math.random() * 2);
if (!canBePlaced(grid, word, randomDirection, coordinates)) return false;
// place the word in grid
if (randomDirection === 0) {
for (let i = 0; i < wordLength; i++) {
grid[coordinates[0]][coordinates[1] + i] = wordArray[i];
}
} else {
for (let i = 0; i < wordLength; i++) {
grid[coordinates[0] + i][coordinates[1]] = wordArray[i];
}
}
return grid;
};
const createBoard = (grid, words) => {
let iterator = 0;
let failedIndex = [];
while (words.length > iterator) {
let state = addWord(grid, words[iterator]);
if (state != false) {
iterator++;
failCounter = 0;
} else {
failedIndex.push(iterator);
iterator++;
}
}
let failedWords = [];
let retry = 0;
while (failedIndex.length > 0 && retry < 100) {
let index = failedIndex.pop();
let isFailed = addWord(grid, words[index]);
if (isFailed == false) {
failedWords.push(words[index]);
failedIndex.splice(0, 0, index);
retry++;
}
}
failedWords = [...new Set(failedWords)];
return {
grid: grid,
failedWords: failedWords,
};
};
let generatedBoards = [];
let boardGenerationLimit = 0;
while (generatedBoards.length < 10 && boardGenerationLimit < 200) {
let grid = gridMatrix();
game = initializeGame(grid);
let board = createBoard(game.grid, game.words);
while (board.failedWords.length > 0) {
grid = gridMatrix();
game = initializeGame(grid);
board = createBoard(game.grid, game.words);
}
generatedBoards.push(board.grid);
generatedBoards.forEach((element, index) => {
for (let i = 0; i < generatedBoards.length; i++) {
if (i !== index) {
if (JSON.stringify(element) === JSON.stringify(generatedBoards[i])) {
generatedBoards.splice(i, 1);
}
}
}
});
boardGenerationLimit++;
}
generatedBoards.forEach((element) => {
console.table(element);
});
console.log(`Successfully generated ${generatedBoards.length} unique and valid boards`, "in", boardGenerationLimit, "attempts");