-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordle.js
157 lines (140 loc) · 4.89 KB
/
wordle.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
const data = require("./data.json");
const otherWords = require("./experimentalwords.json");
var config = {};
var log = "";
var responseTypes = {
alphalight: ["w", "y", "g"],
alphadark: ["b", "y", "g"],
numbers: ["0", "1", "2"],
emojilight: ["⬜", "🟨", "🟩"],
emojidark: ["⬛", "🟨", "🟩"]
};
exports.data = data;
// config so far:
// logWarnings: boolean, whether to save warnings in a log variable or not.
// emojis: boolean, whether or not to use emojis. deprecated
// TODO remove this ^
// responseType: string, what response to give based on the responseTypes variable
// TODO add customization
// forceValidity: number, whether to always pick a valid word, despite input. Can be -1 for always all words, 0 for input, 1 for always valid words.
exports.Config = class {
EnterData(configData) {
config = configData;
//manage .logWarnings
if (typeof config.logWarnings === "undefined") {
config.logWarnings = false;
}
//manage .emojis
if (typeof config.emojis !== "undefined") {
warn(
"Using config.emojis is deprecated, please use config.responseType instead.\nTo preserve backwards compatability, this setting has been automatically fixed for you.",
"config"
);
switch (config.emojis) {
case true:
config.responseType = "emojidark";
break;
default:
config.responseType = "numbers";
break;
}
}
//manage .responseType
if (
typeof config.responseType === "undefined" ||
typeof responseTypes[config.responseType] === "undefined"
) {
warn('No valid responseType given, defaulting to "numbers"', "config");
config.responseType = "numbers";
}
//manage .forceValidity
if (typeof config.forceValidity !== "number") {
warn("No valid forceValidity given, defaulting to 0", "config");
}
};
addReturnType(name, array, doSwitch=true) {
if (array.length != 5) {
warn("array length not valid (!= 5)", "addReturnType")
} else
{
responseTypes[name] = array;
}
}
}
function warn(message, source) {
if (config.logWarnings) {
log += "[" + source + "] " + message + "\n";
} else {
console.warn(
`[${source}] ${message}\nIf you want to disable these messages, set logWarnings to true`
);
}
}
exports.getLog = function () {
return log;
};
exports.checkGuess = function (guess, answer, returnType = config.responseType) {
console.log(returnType)
returnType = responseTypes[returnType]
returnString = ""
checkGuessPrivate(guess, answer).correctArray.forEach(element => {
try {
returnString = returnString.concat(returnType[element])
} catch (e) {
throw new Error("Letter type value " + element + "was not valid for return type string " + returnType)
}
});
return returnString
}
checkGuessPrivate = function (guess, answer) {
if (guess.length != answer.length) {
throw new Error("guess and answer must be the same length");
}
let gFreq = {};
corrArr = []
// get frequency of letters in answer
let aFreq = {};
for (let i = 0; i < guess.length; i++) {
if (!aFreq[answer[i]]) aFreq[answer[i]] = 0; //if we haven't encountered this letter yet, set the value to 0
aFreq[answer[i]]++; // increment the value by 1
}
for (let i = 0; i < guess.length; i++) {
if (!gFreq[guess[i]]) {
gFreq[guess[i]] = 0;
} //if we haven't encountered this letter yet, set the value to 0
gFreq[guess[i]]++; // increment the value by 1
if (
gFreq[guess[i]] > aFreq[guess[i]] ||
typeof aFreq[guess[i]] != "number"
) {
corrArr.push(0)
continue;
}
if (guess[i] != answer[i]) {
corrArr.push(1)
continue;
}
if (guess[i] == answer[i]) {
corrArr.push(2)
continue;
}
}
return {
correctArray: this.corrArr,
guess: guess,
answer: answer
};
};
exports.getWord = function (isValid, length = 5) {
if (length == 5) {
if (isValid + config.forceValidity) {
return data.possible[
Math.floor(Math.random() * data.possible.length)
];
} else {
return data.all[Math.floor(Math.random() * data.all.length)];
}
} else {
return otherWords[length][Math.floor(Math.random() * otherWords[length].length)]
}
};