-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordFinder.js
213 lines (186 loc) · 5.1 KB
/
wordFinder.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
'use strict'
const readline = require('readline');
const fs = require('fs');
const jimp = require('jimp');
const Trie = require('./Trie.js');
const Image = require('./image.js');
const screenshot = require('./screenshot.js');
const mouse = require('./mouse.js');
let trie = new Trie()
let usedWords = new Trie()
let grid = []
let letters = null
let possibleWordData = []
let wordcount = 0
const listOfDirections = ['NORTH','SOUTH','EAST','WEST','NORTHEAST','NORTHWEST','SOUTHEAST','SOUTHWEST']
const directionEnum = {
NORTH: [0, -1],
SOUTH: [0, 1],
EAST: [1, 0],
WEST: [-1, 0],
NORTHEAST: [1, -1],
NORTHWEST: [-1, -1],
SOUTHEAST: [1, 1],
SOUTHWEST: [-1, 1]
}
const positionsUsed = []
function init(data) {
return new Promise((resolve, reject) => {
// grid = [
// ['er', 'a', 'b', 'f'],
// ['e', 's', 'o', 'k'],
// ['o', 'r', 's', 'e'],
// ['b', 'a', 'r', 't']
// ]
grid = data
let faultyIndex = -1, temp = []
for(let i = 0; i < grid.length; i++) {
if(grid[i].length > 4) {
faultyIndex = i
break
}
}
new Promise((res, rej) => {
if(faultyIndex > -1) {
console.log("faulty");
let x = faultyIndex * 4
Image.recognize('images/' + (x + 0) + '.png')
.then((result) => {
temp.push(result)
return Image.recognize('images/' + (x + 1) + '.png')
})
.then((result) => {
temp.push(result)
return Image.recognize('images/' + (x + 2) + '.png')
})
.then((result) => {
temp.push(result)
return Image.recognize('images/' + (x + 3) + '.png')
})
.then((result) => {
temp.push(result)
grid[faultyIndex] = temp;
console.log(grid);
res('success')
})
} else {
res('success')
}
})
.then(() => {
let reader = readline.createInterface( {
input: fs.createReadStream('wordlist.txt')
})
reader.on('line', (line) => trie.add(line))
reader.on('close', () => {
console.log('read files')
resolve(trie)
})
})
})
}
function makeWords(wordData, index = 0) {
if (index === wordData.length) {
return
}
console.log(wordData[index].word);
setTimeout(() => {
mark(wordData[index].positions)
makeWords(wordData, index + 1)
}, 250)
}
function mark(list) {
let cords = []
for(let i = 0; i < list.length; i++) {
let fullIndex = list[i][1] * 4 + list[i][0]
cords.push([letters[fullIndex].center.x, letters[fullIndex].center.y])
}
mouse(cords)
}
function convertFormat(object) {
return new Promise((resolve, reject) => {
let text = ''
for(let i = 0; i < object.text.length; i++) {
if(object.text[i] === ' ' || (object.text[i] === '\n' && text[text.length - 1] === '\n'))
continue
if(object.text[i] === '|') {
text += 'i'
continue
}
text += object.text[i]
}
letters = object.letterRegions
let rows = text.split('\n')
let cols = rows.map(row => row.split(''))
console.log(cols);
resolve(cols)
})
}
function isValid(x, y) {
if(grid.length === 0 ||
x >= grid[0].length || x < 0 ||
y >= grid.length || y < 0)
return false
return true
}
function isUsed(x, y) {
for(let i = 0; i < positionsUsed.length; i++) {
if(positionsUsed[i][0] === x && positionsUsed[i][1] === y)
return true
}
return false
}
function freePosition(x, y) {
let index = -1
for(let i = 0; i < positionsUsed.length; i++) {
if(positionsUsed[i][0] === x && positionsUsed[i][1] === y) {
index = i
break
}
}
if(index > -1) positionsUsed.splice(index, 1)
}
function findWords(x, y, prevWord = '') {
// if(possibleWordData.length === 160) return false
if(!isValid(x, y) || isUsed(x, y))
return false
positionsUsed.push([x, y])
let newWord = prevWord + grid[y][x]
let searchResult = trie.search(newWord)
if(searchResult.status && !usedWords.search(newWord).status) {
wordcount += 1
usedWords.add(newWord)
possibleWordData.push({word: newWord, positions: positionsUsed.slice()})
}
if(grid[y][x].indexOf('-') > -1 || !searchResult.hasWordChain) {
freePosition(x, y)
return false
}
listOfDirections.forEach((direction) => {
findWords(x + directionEnum[direction][0], y + directionEnum[direction][1], newWord)
})
freePosition(x, y)
}
function solve() {
console.log(grid);
return new Promise((resolve, reject) => {
usedWords = new Trie();
for(let i = 0; i < grid.length; i++) {
for(let j = 0; j < grid[i].length; j++) {
findWords(i, j)
}
}
console.log('found words: ', wordcount);
resolve(possibleWordData)
})
}
screenshot('images/screenshot.png', 2500)
.then((path) => jimp.read(path))
// jimp.read('images/screenshot.png')
.then((image) => Image.checkImage(image))
.then((result) => convertFormat(result))
.then((grid) => init(grid))
// init()
.then(() => solve(), (err) => console.log(err))
.then((wordData) => makeWords(wordData))
// .then(() => console.log(trie.search('rises')))