-
Notifications
You must be signed in to change notification settings - Fork 0
/
mastermind.js
174 lines (161 loc) · 3.6 KB
/
mastermind.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
var app;
function main() {
app = new Vue({
el: '#app',
data: {
dictionary: [],
words: [],
guess: "?????",
n_best: 0,
history: [],
score: { exact: 0, misplaced: 0 },
won: false,
},
computed: {
best_score: function() { return this.words.length ? this.words[0].score : 0; },
dictionary_loaded: function() { return this.dictionary.length > 0; },
welcome: function() { return this.words.length == 0; },
},
watch: {
'score.exact': watch('exact'),
'score.misplaced': watch('misplaced'),
},
methods: {
start: start,
keepGuessing: function() { this.n_best = 2; },
win: win,
}
});
var xhr = new XMLHttpRequest();
xhr.open('GET', 'words.json', true);
xhr.onload = function (e) {
if (this.status == 200) {
app.dictionary = JSON.parse(this.response);
}
}
xhr.send();
}
function watch(type) {
return function(to, from) {
document.querySelector(`label[for='${type}-${from}']`).classList.toggle('is-checked', false);
document.querySelector(`label[for='${type}-${to}']`).classList.toggle('is-checked', true);
};
}
function start() {
app.score.exact = 0;
app.score.misplaced = 0;
app.words = app.dictionary.map(word => { return { word: word, score: 0 }; });
app.history = [];
app.won = false;
makeGuess();
}
function makeGuess() {
const s0 = app.words[0].score;
bests = app.words.filter(w => w.score == s0);
app.n_best = bests.length;
if (bests.length > 1) { // filter last guessed word to avoid repetitions
bests = bests.filter(w => w.word != app.guess);
}
app.guess = bests[Math.floor(Math.random() * bests.length)].word;
}
function submit() {
evaluate(app.guess, app.score.exact, app.score.misplaced);
if (!app.won) {
app.score.exact = 0;
app.score.misplaced = 0;
makeGuess();
}
}
function evaluate(guess, exact, misplaced) {
app.history.push({ guess:guess, exact:exact, misplaced:misplaced });
if (exact == 5) {
win();
} else {
app.words = app.words.filter(w => w.word != guess);
for (var w of app.words) {
const s = score(guess, w.word);
w.score += (Math.abs(exact - s.exact) + Math.abs(misplaced - s.misplaced) +
Math.abs(exact + misplaced - s.exact - s.misplaced)) / 2;
}
app.words.sort((a, b) => a.score - b.score);
}
}
function score(w1, w2) {
var e = 0;
var m = 0;
var r1 = [];
var r2 = [];
// count exact matches
for (var i = 0; i < w1.length; i++) {
if (w1[i] == w2[i]) {
e++;
} else {
r1.push(w1[i]);
r2.push(w2[i]);
}
}
// count misplaced matches
r1.sort();
r2.sort();
var i = 0;
var j = 0;
while (i < r1.length && j < r2.length) {
if (r1[i] == r2[j]) {
m++;
i++;
j++;
} else if (r1[i] < r2[j]) {
i++;
} else {
j++;
}
}
return { exact: e, misplaced: m };
}
function win() {
app.won = true;
checkHistory();
}
function checkHistory() {
for (var i = 0; i < app.history.length; i++) {
const h = app.history[i];
var s = score(app.guess, h.guess);
for (var t of ['exact', 'misplaced']) {
if (s[t] != h[t]) {
s['bad-' + t] = h[t];
}
}
if (Object.keys(s).length > 2) {
s.guess = h.guess;
s.bad = true;
Vue.set(app.history, i, s);
}
}
}
function replay(log) {
start();
// convert log if it's a string
if (typeof("log") == "string") {
var tokens = log.split(/\s+/);
var guesses = [];
while (tokens.length) {
guesses.push(tokens.splice(0, 3));
}
log = [];
for (g of guesses) {
log.push({
guess: g[0],
exact: parseInt(g[1]) || 0,
misplaced: parseInt(g[2]) || 0,
});
}
}
// replay log
while (log.length) {
const entry = log.shift();
evaluate(entry.guess, entry.exact, entry.misplaced);
}
makeGuess();
}
// execute main
main();