-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathwordle.cn.core.js
105 lines (83 loc) · 2.31 KB
/
wordle.cn.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
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
'use strict';
const pinyin = require('pinyin');
const makePinyin = (word) => {
const tokens = pinyin.pinyin(word, {
style: 'TONE2',
segment: '@node-rs/jieba',
});
const result = [];
if (tokens.length !== word.length) {
return null;
}
for (const i in word) {
const match = tokens[i][0].match(/^([bcdfghjklmnpqrstwxyz]|ch|sh|zh|)([aeiouv]+(?:n|ng|)|n|ng|er)(\d?)$/);
if (!match) {
return null;
}
if (match[2] === 'n') {
match[2] = 'en';
}
if (match[2] === 'ng') {
match[2] = 'eng';
}
result.push([word[i], match[1], match[2], match[3]]);
}
return result;
};
const dictInit = () => {
return {
language: 'cn',
list: [],
};
};
const dictAdd = (dict, word, enabled) => {
// <enabled> is not used
dict.list.push(word);
};
const dictSelect = (dict) => {
return dict.list[Math.floor(Math.random() * dict.list.length)];
};
const guess = (dict, word, answer) => {
// <dict> is not used
const guessPinyin = makePinyin(word);
const targetPinyin = makePinyin(answer);
const tag = ['', '', '', ''];
if (!guessPinyin) {
return -1;
}
tag.pinyin = () => {
return guessPinyin;
};
for (let i = 0; i < 4; i += 1) {
const guessCounts = {};
const answerCounts = {};
const pos = [];
for (let j = 0; j < answer.length; j += 1) {
if (guessPinyin[j][i] === targetPinyin[j][i]) {
pos.push(0);
} else {
guessCounts[guessPinyin[j][i]] = (guessCounts[guessPinyin[j][i]] || 0) + 1;
answerCounts[targetPinyin[j][i]] = (answerCounts[targetPinyin[j][i]] || 0) + 1;
pos.push(guessCounts[guessPinyin[j][i]]);
}
}
for (let j = 0; j < answer.length; j += 1) {
if (pos[j]) {
if (pos[j] <= (answerCounts[guessPinyin[j][i]] || 0)) {
tag[i] += '1';
} else {
tag[i] += '0';
}
} else {
tag[i] += '2';
}
}
}
return tag;
};
module.exports = {
dictInit: dictInit,
dictAdd: dictAdd,
dictSelect: dictSelect,
guess: guess,
};