-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
140 lines (130 loc) · 4.51 KB
/
main.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
Vue.component("modal", {
template: "#modal-template"
});
var app = new Vue({
el: '#app',
data: {
showStep: null,
words: allWords || [],
boardState: [
'',
'',
'',
'',
'',
'',
],
evaluations: [
['absent', 'absent', 'absent', 'absent', 'absent'],
['absent', 'absent', 'absent', 'absent', 'absent'],
['absent', 'absent', 'absent', 'absent', 'absent'],
['absent', 'absent', 'absent', 'absent', 'absent'],
['absent', 'absent', 'absent', 'absent', 'absent'],
['absent', 'absent', 'absent', 'absent', 'absent'],
],
keyboard: [
['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'],
['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'],
['↵', 'z', 'x', 'c', 'v', 'b', 'n', 'm', '←'],
],
remainingWords: {},
currentIndex: 0
},
methods: {
setEvaluation: function (wordIndex, letterIndex) {
if (this.currentIndex !== wordIndex) return;
Vue.set(this.evaluations[wordIndex], letterIndex, this.nextEvValue(this.evaluations[wordIndex][letterIndex]));
},
nextEvValue: function (current) {
if (current == 'absent') {
return 'present';
} else if (current == 'present') {
return 'correct';
} else {
return 'absent';
}
},
writeWord: function (e) {
let word = this.boardState[this.currentIndex]
if (e.key.length == 1) {
if (word.length < 5) {
word += e.key
Vue.set(this.boardState, this.currentIndex, word);
}
}
// backspace
if (e.key == 'Backspace') {
if (word.length > 0) {
word = word.substring(0, word.length - 1);
Vue.set(this.boardState, this.currentIndex, word);
}
}
// Enter
if (e.key == 'Enter') {
this.filterWords();
}
},
pressKey: function (key) {
let event = { key }
if (key == '↵') {
event.key = 'Enter';
}
if (key == '←') {
event.key = 'Backspace';
}
this.writeWord(event);
},
splitWord: function (word) {
let arr = word.split('');
let remainingLength = 5 - arr.length;
for (let i = 0; i < remainingLength; i++) {
arr.push('');
}
return arr;
},
filterWords: function () {
let word = this.boardState[this.currentIndex];
let evaluation = this.evaluations[this.currentIndex];
if (word.length < 5) return;
let remainingWords = this.words
for (let i = 0; i < word.length; i++) {
let char = word[i];
let ev = evaluation[i];
if (!this.isSkippable(word, char, i)) {
remainingWords = (ev == 'correct') ? remainingWords.withCharAndIndex(char, i)
: (ev == 'present') ? remainingWords = remainingWords.withCharactersButNotInIndex(char, i)
: remainingWords.cancelCharacters(char)
}
}
this.words = remainingWords;
this.remainingWords[this.currentIndex] = remainingWords;
this.currentIndex++;
},
isSkippable: function (word, character, k) {
let evaluation = this.evaluations[this.currentIndex];
let index = [];
for (let i = 0; i < word.length; i++) {
if (word[i] == character) {
index.push(i);
}
}
if (index.length < 2) return false;
if (evaluation[k] != 'absent') return false;
let skippableIndex;
for (let i = 0; i < index.length; i++) {
if (evaluation[index[i]] === 'absent') {
skippableIndex = index[i];
break;
}
}
return skippableIndex == k;
},
showRemainingWords: function (i) {
console.log(this.remainingWords[i]);
this.showStep = `${i}`;
}
},
created: function () {
window.addEventListener('keyup', this.writeWord)
},
})