-
Notifications
You must be signed in to change notification settings - Fork 0
/
pfeffel.js
190 lines (170 loc) · 4.91 KB
/
pfeffel.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
"use strict";
const pfeffel = function pfeffel() {
/* Initialisation */
const word_length = 5;
let max_guesses = 6;
const tries = [];
const words = [
"AWFUL", "BULLY", "CHEAT", "DRONE", "ERROR", "FRAUD", "GHOST", "HAVOC",
"IRATE", "JOKER", "KNAVE", "LYING", "MOOCH", "NASTY", "OPTIC", "POWER",
"QUACK", "ROGUE", "STEAL", "TRICK", "UNFIT", "VENAL", "WRONG"
];
let cur_guess;
let cur_letter;
let word;
function init() {
for(let i = 0; i < max_guesses; i++) {
tries.push(Array(word_length).fill(" "));
}
cur_guess = 1;
cur_letter = 1;
word = words[Math.floor(Math.random() * words.length)];
draw_gameboard();
draw_keyboard();
}
/* Game */
function restart() {
window.location.reload();
}
function win_game() {
alert(`Well done. You guessed the word in ${cur_guess}/6 tries.\nNo share button though. Tories don't share.`);
restart();
}
function lose_game() {
alert(`Sorry. You did not guess the word. It was ${word}.`);
restart();
}
function pfeffel(mark) {
const options = ["guess-right", "guess-present", "guess-wrong"];
const pfeffeled = options.filter(function(item) { return item !== mark });
return pfeffeled[Math.floor(Math.random() * 2)];
}
function mark_guess(candidate) {
const cletters = candidate.split("");
const wletters = word.split("");
let mark;
let correct = 0;
for (let i = 0; i < word_length; i++) {
if (wletters.includes(cletters[i])) {
if (cletters[i] == wletters[i]) {
// correct
mark = pfeffel("guess-right");
correct++;
} else {
// present but wrong place
mark = pfeffel("guess-present");
}
} else {
// wrong
mark = pfeffel("guess-wrong");
}
const letterbox = document.getElementById(`guess-${cur_guess}-${i + 1}`);
letterbox.classList.add(mark);
if (mark == "guess-wrong") {
const key = document.getElementById(`key-${cletters[i]}`);
key.classList.add("key-wrong");
}
}
if (correct == 5) {
win_game();
}
if (cur_guess == max_guesses) {
lose_game();
}
}
/* Input handlers */
function add_letter(letter) {
if (cur_letter == 6) {
return;
}
const id = `guess-${cur_guess}-${cur_letter}`;
const letterbox = document.getElementById(id);
letterbox.innerHTML = `${letter}`;
cur_letter++;
}
function delete_letter() {
if (cur_letter == 1) {
return;
}
const id = `guess-${cur_guess}-${cur_letter - 1}`;
const letterbox = document.getElementById(id);
letterbox.innerHTML = `\xa0\xa0\xa0\xa0\xa0`;
cur_letter--;
}
function submit_guess() {
if (cur_letter != 6) {
return;
}
let candidate = "";
for (let i = 0; i < word_length; i++) {
const id = `guess-${cur_guess}-${i + 1}`;
candidate += document.getElementById(id).innerHTML;
}
mark_guess(candidate);
cur_guess++;
cur_letter = 1;
}
// key_handler
// Event handler bound to onscreen keyboard
// FIXME: later we should add real keyboard handling too
function key_handler(e) {
const key = e.target.id.split("-", 2)[1];
switch (key) {
case 'Enter':
submit_guess();
break;
case 'Del':
delete_letter();
break;
default:
add_letter(key);
}
}
/* Drawing functions */
function draw_gameboard() {
const container = document.getElementById("gameboard");
let guess = 1;
let letter = 1;
for (const row of tries) {
const guess_div = document.createElement("div");
for (const col of row) {
const letter_span = document.createElement("span");
letter_span.appendChild(document.createTextNode("\xa0\xa0\xa0\xa0\xa0"));
letter_span.classList.add("letter");
letter_span.id = `guess-${guess}-${letter}`;
guess_div.appendChild(letter_span);
letter += 1;
}
guess_div.classList.add("guess");
container.appendChild(guess_div);
guess += 1;
letter = 1;
}
}
function draw_keyboard() {
const keys = [
["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"],
["A", "S", "D", "F", "G", "H", "J", "K", "L"],
["Enter", "Z", "X", "C", "V", "B", "N", "M", "Del"]
];
const container = document.getElementById("keyboard");
for (const row of keys) {
const row_div = document.createElement("div");
for (const key of row) {
const key_span = document.createElement("span");
key_span.appendChild(document.createTextNode(key));
key_span.id = `key-${key}`;
key_span.classList.add("key");
key_span.addEventListener("click", key_handler, false);
row_div.appendChild(key_span);
}
row_div.classList.add("keyrow");
container.appendChild(row_div);
}
}
/* Exported functions */
return {
init: init,
};
}();
pfeffel.init();