-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtryworker.js
More file actions
158 lines (141 loc) · 3.81 KB
/
tryworker.js
File metadata and controls
158 lines (141 loc) · 3.81 KB
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
var nofWords = 10;
var wordLenghts = 8;
onmessage = function(event) {
var used = new Array(nofWords);
for (var i=0; i<used.length; i++) {
used[i] = Boolean(false);
}
tryWord(0, event.data.words, used, event.data.textArray);
self.postMessage('done!');
};
function tryWord(pos, words, used, inTextArray) {
var myTextArray = cloneArray(inTextArray);
for ( var i = 0; i < used.length; i++) {
if (!used[i]) {
if (tryFillWord(pos, words[i], myTextArray)) {
postResult(myTextArray);
// next word does fit
used[i] = Boolean(true);
if (pos < (nofWords - 1)) {
if (!tryWord(pos + 1, words, used, myTextArray)) {
// reset used since it was not successfull
used[i] = Boolean(false);
myTextArray = cloneArray(inTextArray);
// is there an other unused word to try with?
postResult(myTextArray);
continue;
} else {
copyArray(inTextArray, myTextArray);
return Boolean(true);
}
} else {
console.log("yes, we found the solution");
copyArray(inTextArray, myTextArray);
return Boolean(true);
}
} else {
// next word does not fit
// is there an other unused word to try with?
continue;
}
}
}
// no word does fit => fall back
postResult(myTextArray);
return Boolean(false);
}
function tryFillWord(pos, word, textArray) {
switch (pos) {
case 0:
return tryFillWordVertical(0, 3, word, textArray);
break;
case 1:
return tryFillWordVertical(3, 4, word, textArray);
break;
case 2:
return tryFillWordVertical(0, 5, word, textArray);
break;
case 3:
return tryFillWordVertical(3, 6, word, textArray);
break;
case 4:
return tryFillWordVertical(0, 7, word, textArray);
break;
case 5:
return tryFillWordHorizontal(3, 3, word, textArray);
break;
case 6:
return tryFillWordHorizontal(4, 0, word, textArray);
break;
case 7:
return tryFillWordHorizontal(5, 3, word, textArray);
break;
case 8:
return tryFillWordHorizontal(6, 0, word, textArray);
break;
case 9:
return tryFillWordHorizontal(7, 3, word, textArray);
break;
}
return Boolean(false);
}
function tryFillWordVertical(row, column, word, inTextArray) {
var i = 0;
var myTextArray = cloneArray(inTextArray);
for ( var r = row; i < word.length && r < myTextArray[column].length; r++) {
if (myTextArray[r][column] == ' ' || myTextArray[r][column] == word[i]) {
myTextArray[r][column] = word[i];
i++;
} else {
return Boolean(false);
}
}
copyArray(myTextArray, inTextArray);
return Boolean(true);
}
function tryFillWordHorizontal(row, column, word, inTextArray) {
var i = 0;
var myTextArray = cloneArray(inTextArray);
for ( var c = column; i < word.length && c < myTextArray.length; c++) {
if (myTextArray[row][c] == ' ' || myTextArray[row][c] == word[i]) {
myTextArray[row][c] = word[i];
i++;
} else {
return Boolean(false);
}
}
copyArray(myTextArray, inTextArray);
return Boolean(true);
}
function postResult(textArray) {
self.postMessage(textArray);
}
/**
* Deep clone of a two dimensional array.
*
* @returns The cloned array.
*/
function cloneArray(srcArray) {
var destArray = new Array();
for (var c=0; c<srcArray.length; c++) {
destArray.push(srcArray[c].slice(0));
}
return destArray;
}
/**
* Copies the values from one array to an other one.
*/
function copyArray(srcArray, destArray) {
for (var c=0; c<srcArray.length; c++) {
for (var r=0; r<srcArray[c].length; r++) {
destArray[c][r] = srcArray[c][r];
}
}
}
function pause(millis) {
var date = new Date();
var curDate = null;
do {
curDate = new Date();
} while (curDate - date < millis)
}