-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.js
382 lines (354 loc) · 9.04 KB
/
init.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
"use strict";
var HORIZONTAL = 1;
var VERTICAL = 2;
var MIN_LENGTH = 3;
var MAX_LENGTH = 5;
function getWord(node) {
// converts tree node to word
var parent = node[1];
if (parent == null)
return '';
var last = _.indexOf(parent, node);
var letter = parent[last-1];
return getWord(parent) + letter;
}
function isEmpty(arr) {
if ( !arr || arr.length==0 ) {
return true
}
for (var i = 0; i < arr.length; i++) {
if(arr[i])
return false;
};
return true;
}
function isFull(arr) {
if ( !arr || arr.length==0 ) {
return false
}
for (var i = 0; i < arr.length; i++) {
if(!arr[i])
return false
}
return true;
}
function getRandomWord() {
// gets random word directly from the dictionary
return dictionary[_.random(dictionary.length-1)]
}
var COLORS = ['black', 'red', 'orange', 'yellow', 'green', 'blue', 'white'];
var ALPHA = new Array(
new Array(),
new Array('A', 'D', 'N', 'F', 'W'),
new Array('E', 'C', 'P', 'K', 'J'),
new Array('I', 'S', 'B', 'Y', 'X'),
new Array('O', 'M', 'R', 'H', 'V'),
new Array('U', 'T', 'L', 'G', 'Q', 'Z')
);
var GROUP = new Array(1,3,2,1,2,1,5,4,3,2,2,5,4,1,4,2,5,4,3,5,5,4,1,3,3,5);
function group(char) {
return GROUP[char.charCodeAt(0)-65]
}
var ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
// ALPHA[GROUP['B'.charCodeAt(0)-65]]
// not needed actually
// use String.fromCharCode(65) instead
var puzzle, wordCount, wordList, word; // word is the selected code
var redWords; // list of indices of words which need fix
// list of words in red box
// is the last guess repeated word
var repeatFlag
var current_guess
var guesses, hints
var scroll_index
var guessList = []
function resetGuessList() {
guessList = []
}
function addGuess(word_index) {
var guess = _.last(guesses[word_index]).join('')
var key = word_index+' '+guess
guessList.push(key)
}
function getGuessIndex(word_index, guess_index) {
var guess = guesses[word_index][guess_index].join('')
var key = word_index+' '+guess
return _.indexOf(guessList, key)
}
function removeGuess(word_index, guess_index) {
var index = getGuessIndex(word_index, guess_index)
if (index >= 0) {
guessList.splice(index, 1)
}
guesses[word_index].splice(guess_index, 1)
}
var time = 0, timing = false
var clockHandle = setInterval(function clock() {
if (timing) {
time ++
drawPuzzle()
}
}, 1000)
function Word() {
this.index = 0;
// start point
this.startx = 0;
this.starty = 0;
// end point
this.endx = 0;
this.endy = 0;
// direction xp=1 is horizontal yp=1 is vertical , logically of course
this.xp = 0;
this.yp = 0;
this.length = 0;
this.data = [];
this.hint = [];
}
function init() {
// create puzzle
puzzle = [];
guesses = [];
hints = [];
for (var i=0; i<COLUMNS; i++) {
puzzle[i] = [];
hints[i] = [];
}
// initialize puzzle
for (var i=0; i<COLUMNS; i++) {
for(var j=0; j<ROWS; j++) {
puzzle[i][j] = null;
hints[i][j] = null;
}
}
wordList = []; // information about the words in the puzzle
// { startx:0, starty:0, xp:0, yp:0, endx:0, endy:0, length:0, data:Array(), choices:Array() };
wordCount = 0; // number of words
word = null; // selected word
for(var i=0; i<COLUMNS && wordCount*2+2 <= $("#number_of_words:checked").val(); i+=2+Math.floor(Math.random()*1.1)) {
obj = new Word();
// choose a word
var _word = getRandomWord();
// then put it in the row randomly
j = Math.floor(Math.random()*(ROWS-_word.length+1));
obj.startx = i;
obj.starty = j;
obj.length = _word.length-1;
obj.yp = 1;
obj.endx = i;
obj.endy = j + obj.length;
obj.index = wordCount;
guesses[wordCount] = [[]];
wordList[wordCount++] = obj;
if (j > 0) {
puzzle[i][j-1] = -1;
}
if (j +obj.length +1 < ROWS) {
puzzle[i][j+obj.length+1] = -1;
};
for (var counter = 0; counter < _word.length; counter++) {
puzzle[i][j+counter] = _word[counter];
obj.data[counter] = _word[counter];
}
}
var count = fillHorizontals();
// look for single
for (var i = 0; i < wordCount; i++) {
var single = true;
var obj = wordList[i];
if(obj.yp == 0)
continue;
for (var j = 0; j < obj.length+1; j++) {
if(obj.startx>0 && puzzle[obj.startx-1][obj.starty+j] != null && puzzle[obj.startx-1][obj.starty + j] != -1) {
single = false
break
}
if(obj.startx<COLUMNS-1 && puzzle[obj.startx+1][obj.starty+j] != null && puzzle[obj.startx+1][obj.starty + j] != -1) {
single = false
break
}
};
if(single) {
// remove word and try filling again
wordCount--;
wordList.splice(i,1);
i--;
if(obj.starty>0)
puzzle[obj.startx][obj.starty-1] = null;
if(obj.endy<ROWS-1)
puzzle[obj.startx][obj.endy+1] = null;
for (var j = 0; j <= obj.length; j++) {
puzzle[obj.startx][obj.starty+j] = null;
}
}
};
return wordCount
}
function fillHorizontals() {
var count = 0;
for(var j=0; j<ROWS; j++) {
var tube = [];
var last_start = 0;
for(var i=0; i<COLUMNS; i++) {
if ( puzzle[i][j] != -1 ) {
tube.push(puzzle[i][j])
} else {
count += processTube(last_start, j,tube)
tube = [] // empty tube, maybe we can fit in more
last_start = i+1
}
}
count += processTube(last_start, j,tube)
}
return count;
}
function processTube(i, j, tube) {
if(tube.length < MIN_LENGTH || wordCount == $("#number_of_words:checked").val()) {
return 0;
}
var original_length = tube.length // copy tube array into variable called original
var originali = i
// tube must be connected, so check if it has value?
var output = [];
var choices = [];
while(tube != null && tube.length >= 3 && _.some(tube)) {
// process tube
var example = [tree];
var lastCrossing = -1;
for (var k = 0; k < tube.length; k++) {
// no word longer than MAX_LENGTH
var temp_example = [];
for (var n = 0; n < example.length; n++) {
var next = example[n];
if(!next) continue;
if (_.contains(next, 'YES') && tube[k]==null) {
if (lastCrossing != -1 ) { // and far enough
output.push(i,next);
}
// but it can be part of longer word as well
}
if (tube[k] == null) {
//if(k>0 && )
for (var m = 3; m < next.length; m+=2) {
temp_example.push(next[m]);
}
} else {
lastCrossing = k;
var index = _.indexOf(next, tube[k]);
if (index != -1) {
temp_example.push(next[index+1]);
}
}
};
example = temp_example;
};
for (var n = 0; n < example.length; n++) {
if(!example[n]) continue;
if (_.contains(example[n], 'YES')) {
if (lastCrossing != -1 && tube.length==example[n][0]) { // and far enough
output.push(i, example[n]);
}
}
}
i++;
while(tube.shift() != null)
i++;
}
if (output.length == 0)
return 0
var c = 0
var max = '';
var maxi = -1;
for (var k = 1; k < output.length; k+=2) {
var _word = getWord(output[k]);
if(max.length<_word.length || (max.length==_word.length && Math.random()>0.5)) {
// find the biggest possible word
maxi = output[k-1];
max = _word;
}
}
if (maxi != -1) {
// check to see if we have more than 3 sq on either side to work on
choices.push(maxi, max)
if(maxi-originali>3) {
// fit one before
max = ''
maxi = -1
for (var k = 1; k < output.length; k+=2) {
var _word = getWord(output[k])
if (_.contains(wordList, _word) || _.contains(choices, _word) ) {
// try another word
continue;
};
var _wordi = output[k-1];
if(_wordi+_word.length< choices[0])
if(max.length<_word.length || (max.length==_word.length && Math.random()>0.5)) {
maxi = output[k-1]
max = _word
}
};
if(maxi != -1) {
choices.push(maxi, max)
}
}
if(original_length-max.length-choices[0]>3) {
// fit one after
max = ''
maxi = -1
for (var k = 1; k < output.length; k+=2) {
var _word = getWord(output[k])
if (_.contains(wordList, _word) || _.contains(choices, _word)) {
// try another word
continue
}
var _wordi = output[k-1]
if(choices[0]+choices[1].length<_wordi)
if(max.length<_word.length || (max.length==_word.length && Math.random()>0.5)) {
maxi = _wordi
max = _word
}
}
if(maxi != -1) {
choices.push(maxi, max)
}
}
for (; c < choices.length; c+=2) {
if (wordCount < $("#number_of_words:checked").val()) {
maxi = choices[c]
max = choices[c+1]
// create word for it
var obj = new Word()
// choose a word
_word = max
// then put it in the row randomly
obj.startx = maxi
obj.starty = j
obj.length = _word.length-1
obj.xp = 1
obj.endx = maxi + obj.length
obj.endy = j
obj.index = wordCount
guesses[wordCount] = [[]]
wordList[wordCount++] = obj
if (maxi> 0) {
puzzle[maxi-1][j] = -1
}
if (maxi +obj.length +1 < COLUMNS) {
puzzle[maxi+obj.length+1][j] = -1
}
for (var counter = 0; counter < _word.length; counter++) {
puzzle[maxi+counter][j] = _word[counter]
obj.data[counter] = _word[counter]
if(j>0)
if(puzzle[maxi+counter][j-1] == null)
puzzle[maxi+counter][j-1] = -1
if(j<ROWS-1)
if(puzzle[maxi+counter][j+1] == null)
puzzle[maxi+counter][j+1] = -1
}
} else {
c = choices.length
}
}
}
return c/2
}