forked from hczhcz/telegram-kuso-bots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordle.en.core.js
64 lines (53 loc) · 1.45 KB
/
wordle.en.core.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
'use strict';
const dictInit = () => {
return {
language: 'en',
list: [],
words: {},
};
};
const dictAdd = (dict, word, enabled) => {
if (enabled) {
dict.list.push(word);
}
dict.words['#' + word] = true;
};
const dictSelect = (dict) => {
return dict.list[Math.floor(Math.random() * dict.list.length)];
};
const guess = (dict, word, answer) => {
if (dict.words['#' + word]) {
const guessCounts = [];
const answerCounts = [];
const pos = [];
for (let i = 0; i < answer.length; i += 1) {
if (word[i] === answer[i]) {
pos.push(0);
} else {
guessCounts[word[i].charCodeAt(0)] = (guessCounts[word[i].charCodeAt(0)] || 0) + 1;
answerCounts[answer[i].charCodeAt(0)] = (answerCounts[answer[i].charCodeAt(0)] || 0) + 1;
pos.push(guessCounts[word[i].charCodeAt(0)]);
}
}
let tag = '';
for (let i = 0; i < answer.length; i += 1) {
if (pos[i]) {
if (pos[i] <= (answerCounts[word[i].charCodeAt(0)] || 0)) {
tag += '1';
} else {
tag += '0';
}
} else {
tag += '2';
}
}
return tag;
}
return -1;
};
module.exports = {
dictInit: dictInit,
dictAdd: dictAdd,
dictSelect: dictSelect,
guess: guess,
};