-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
428 lines (358 loc) · 14.8 KB
/
index.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
* IMPORTS
*/
const CHALK = require("chalk");
const PROMPT = require("prompt-sync")({ sigint: true });
/*
* CLASSES
*/
class Screen {
/**
*
* @param {boolean} has_header - wether the screen shows the header or not
* @param {string[]} lines - 10 lines to display
*/
constructor(has_header, lines) {
this.has_header = has_header;
this.lines = lines;
this.action_key_text = CHALK.gray(" Press Enter To Continue");
this.key_action_callback = () => {};
if (has_header) {
while (this.lines.length > 10) { this.lines.pop(); }
while (this.lines.length < 10) { this.lines.push(""); }
} else {
while (this.lines.length > 14) { this.lines.pop(); }
while (this.lines.length < 14) { this.lines.push(""); }
}
}
activate() {
console.clear();
if (this.has_header) console.log(HEADER);
console.log(this.lines.join("\n") + "\n");
var selection = PROMPT(this.action_key_text);
if (this.key_action_callback != null) this.key_action_callback(selection);
}
get action_key_text() { return this._action_key_text; }
set action_key_text(new_action_key_text) { this._action_key_text = new_action_key_text; }
get key_action_callback() { return this._key_action_callback; }
set key_action_callback(new_key_action_callback) { if (new_key_action_callback != null) this._key_action_callback = new_key_action_callback; else return; }
}
class AltScreen {
/**
*
* @param {string[]} lines - 10 lines to display
*/
constructor(lines) {
this.lines = lines;
this.action_key_text = CHALK.gray(" Press Enter To Continue");
this.key_action_callback = () => {};
}
activate() {
console.clear();
console.log(HEADER);
console.log(this.lines.join("\n") + "\n");
var selection = PROMPT(this.action_key_text);
if (this.key_action_callback != null) this.key_action_callback(selection);
}
get lines() { return this._lines; }
set lines(new_lines) { this._lines = new_lines; }
get action_key_text() { return this._action_key_text; }
set action_key_text(new_action_key_text) { this._action_key_text = new_action_key_text; }
get key_action_callback() { return this._key_action_callback; }
set key_action_callback(new_key_action_callback) { if (new_key_action_callback != null) this._key_action_callback = new_key_action_callback; else return; }
}
/*
* CONSTANTS
*/
const { STANDARD_BANK, FULL_BANK } = require("./wordlists.json");
const HEADER = CHALK.magentaBright(" ┐ ┬ │╷╭─╮ ┬┌─┤\n │││┌─┐├─┐┌─┤│├─╯┌ │└─┐\n └┴┘└─┘╵ └─┘╵╰─╴└─┘├─┘\n");
const WIN_MESSAGES = [
" Well done!",
" Congratulations!",
" Winner Winner\n Chicken Dinner"
];
const LOSS_MESSAGES = [
" Better Luck Next Time"
];
/*
* SCREENS & MENUS
*/
const AUTHOR_NOTE_SCREEN = new Screen(true, [
" Thank you for playing WordleJS",
" WordleJS was simply a personal",
" project I had made to improve",
" my understanding of JavaScript",
" and NodeJS, but I figured some",
" people would like to play it",
" so I released it!",
"",
" Coded By: Jacob Paulin",
" Inspired By Wordle"
]);
const DISCLAIMER_SCREEN = new Screen(true, [
` ${CHALK.bgYellow.black("[!]")} ${CHALK.yellow("DISCLAIMER")} ${CHALK.bgYellow.black("[!]")}`,
CHALK.rgb(255, 69, 0)(" I AM NOT AFFILIATED WITH "),
CHALK.rgb(255, 69, 0)(" WORDLE OR THE NEW YORK TIMES."),
CHALK.rgb(255, 69, 0)(" I DO NOT CLAIM OWNERSHIP OF "),
CHALK.rgb(255, 69, 0)(" THE CONCEPT OR THE GAME."),
CHALK.rgb(255, 69, 0)(" THIS IS SIMPLY A RECREATION"),
CHALK.rgb(255, 69, 0)(" AND IS NOT INTENDED TO BE"),
CHALK.rgb(255, 69, 0)(" INTERPERTED ANY OTHER WAY."),
CHALK.rgb(255, 255, 255)(" LINK TO PLAY THE OFFICIAL GAME"),
CHALK.rgb(255, 255, 255)(" www.nytimes.com/games/wordle")
]);
const MAIN_MENU_SCREEN = new Screen(true, [
CHALK.rgb(0, 109, 170)(" MAIN MENU"),
"",
CHALK.rgb(3, 83, 164)(" [0] ------ Close The Game"),
CHALK.rgb(3, 83, 164)(" [1] --------- How To Play"),
CHALK.rgb(3, 83, 164)(" [2] ---------- Word Banks"),
CHALK.rgb(3, 83, 164)(" [3] -------- Start A Game"),
]);
const HOW_TO_PLAY_SCREEN = new Screen(false, [
CHALK.white.bold(" Your Goal:"),
CHALK.white.bold(" Guess The Word In 6 Guesses"),
"",
` ${CHALK.white.bgHex("#538D4E").bold(" D ")}`,
" A Green Letter Means It's In ",
" The Word & In The Right Spot",
"",
` ${CHALK.white.bgHex("#B59F3B").bold(" A ")}`,
" A Yellow Letter Means It's In",
" The Word But In The Wrong Spot",
"",
` ${CHALK.white.bgHex("#3A3A3C").bold(" D ")}`,
" A Gray Letter Means It's",
" Not In The Word"
]);
const WORD_BANKS_SCREEN = new Screen(false, [
" WordleJS Contains 2",
" Different Word Banks",
"",
` ${CHALK.hex("#29AB87").bold("STANDARD WORD BANK")}`,
" The Standard Word Bank Is The",
" Easier Word Bank. It Contains",
" Standard Issue Wordle Words And",
" Has A Total Of 2,315 Words.",
` ${CHALK.hex("#E34234").bold("FULL WORD BANK")}`,
" The Standard Word Bank Is The",
" Harder Word Bank. It Contains",
" Every 5 Letter Word Recognized",
" By Wordle And Has A Total Of",
" 12,972 Words."
]);
const GAME_PREP_SCREEN = new Screen(true, [
" Select A Word Bank",
"",
" Reference The Word Bank",
" Menu For More Info",
"",
" [1] ------- Standard Bank",
" [2] ----------- Full Bank",
]);
/*
* LINKING SCREENS & MENUS
*/
AUTHOR_NOTE_SCREEN.key_action_callback = (selection) => { return DISCLAIMER_SCREEN.activate(); };
DISCLAIMER_SCREEN.key_action_callback = (selection) => { return MAIN_MENU_SCREEN.activate(); };
MAIN_MENU_SCREEN.action_key_text = CHALK.gray(" Please Choose An Option: ");
MAIN_MENU_SCREEN.key_action_callback = mainMenuLogic;
HOW_TO_PLAY_SCREEN.action_key_text = CHALK.gray(" Press Enter To Go Back");
HOW_TO_PLAY_SCREEN.key_action_callback = (selection) => { return MAIN_MENU_SCREEN.activate(); };
WORD_BANKS_SCREEN.action_key_text = CHALK.gray(" Press Enter To Go Back");
WORD_BANKS_SCREEN.key_action_callback = (selection) => { return MAIN_MENU_SCREEN.activate(); };
GAME_PREP_SCREEN.action_key_text = CHALK.gray(" Please Choose An Option: ");
GAME_PREP_SCREEN.key_action_callback = gamePrepMenuLogic;
function selectWordFromBank(word_bank) { return word_bank[Math.floor(Math.random()*word_bank.length)]; }
function getLettersFromWord(word) { return word.split(""); }
function doesWordContainLetter(word, letter) { return (getLettersFromWord(word).includes(letter.toLowerCase())) ? true : false; }
function doesWordContainLetterAtIndex(word, letter, position) { return (getLettersFromWord(word)[position - 1] == letter.toLowerCase()) ? true : false; }
function isWordInWordBank(word) { return (FULL_BANK.includes(word.toLowerCase())) ? true : false; }
function isWordInGuessHistory(GUESS_HISTORY, word) { return (GUESS_HISTORY.length == 0 || !GUESS_HISTORY.includes(word.toLowerCase())) ? false : true; }
function generateColourString(board_row) {
var coloured_string_array = [];
board_row.forEach(slot => {
switch (slot.colour) {
case "B":
coloured_string_array.push( (slot.letter == null) ? CHALK.white.bgHex("#3A3A3C").bold(" ") : CHALK.white.bgHex("#3A3A3C").bold(" %s ".replace("%s", slot.letter.toUpperCase())) );
break;
case "G":
coloured_string_array.push( (slot.letter == null) ? CHALK.white.bgHex("#538D4E").bold(" ") : CHALK.white.bgHex("#538D4E").bold(" %s ".replace("%s", slot.letter.toUpperCase())) );
break;
case "Y":
coloured_string_array.push( (slot.letter == null) ? CHALK.white.bgHex("#B59F3B").bold(" ") : CHALK.white.bgHex("#B59F3B").bold(" %s ".replace("%s", slot.letter.toUpperCase())) );
break;
case "D":
coloured_string_array.push( (slot.letter == null) ? CHALK.white.bgHex("#121213").bold(" ") : CHALK.white.bgHex("#121213").bold(" %s ".replace("%s", slot.letter.toUpperCase())) );
break;
default:
break;
}
});
return coloured_string_array.join("");
}
function printBoard(board) {
const LINES = []
for (const r in board) {
if (Object.hasOwnProperty.call(board, r)) {
const row = board[r];
LINES.push(" " + generateColourString(row));
}
}
console.clear();
console.log(HEADER);
console.log(LINES.join("\n"));
}
function getGuess(BOARD, GUESS_HISTORY) {
var is_word_valid = false;
var word = null;
do {
console.log(" Enter a word:")
var guess = PROMPT(" ").toLowerCase();
// is the word 5 letters?
if (guess.length != 5 || guess.match(" ")) {
console.clear();
printBoard(BOARD);
console.log(CHALK.red(" You must guess\n a 5 letter word!"));
is_word_valid = false;
} else {
// is the word valid and in the word bank?
if (!isWordInWordBank(guess)) {
console.clear();
printBoard(BOARD);
console.log(CHALK.red(" The word ") + CHALK.white(guess) + CHALK.red(" is\n not a real word!"));
is_word_valid = false;
} else {
// has the word been guessed already?
if (isWordInGuessHistory(GUESS_HISTORY, guess)) {
console.clear();
printBoard(BOARD);
console.log(CHALK.red(" The word ") + CHALK.white(guess) + CHALK.red("\n has already\n been guessed!"));
is_word_valid = false;
} else {
is_word_valid = true;
word = guess;
}
}
}
} while (!is_word_valid);
return word;
}
function runGame(word_bank) {
const WORD_BANK = (word_bank == 1) ? STANDARD_BANK : (word_bank == 2) ? FULL_BANK : null;
const SELECTED_WORD = selectWordFromBank(WORD_BANK);
const BOARD = {
row_1: [
{ colour: "D", letter: null },
{ colour: "D", letter: null },
{ colour: "D", letter: null },
{ colour: "D", letter: null },
{ colour: "D", letter: null }
],
row_2: [
{ colour: "D", letter: null },
{ colour: "D", letter: null },
{ colour: "D", letter: null },
{ colour: "D", letter: null },
{ colour: "D", letter: null }
],
row_3: [
{ colour: "D", letter: null },
{ colour: "D", letter: null },
{ colour: "D", letter: null },
{ colour: "D", letter: null },
{ colour: "D", letter: null }
],
row_4: [
{ colour: "D", letter: null },
{ colour: "D", letter: null },
{ colour: "D", letter: null },
{ colour: "D", letter: null },
{ colour: "D", letter: null }
],
row_5: [
{ colour: "D", letter: null },
{ colour: "D", letter: null },
{ colour: "D", letter: null },
{ colour: "D", letter: null },
{ colour: "D", letter: null }
],
row_6: [
{ colour: "D", letter: null },
{ colour: "D", letter: null },
{ colour: "D", letter: null },
{ colour: "D", letter: null },
{ colour: "D", letter: null }
]
};
const GUESS_HISTORY = [];
const GAME_STATE = {
running: true,
outcome: null // outcome 1 = win, 2 = loss
};
printBoard(BOARD);
while (GAME_STATE["running"]) {
// console.log(SELECTED_WORD, GAME_STATE["running"], GAME_STATE["outcome"])
var guess = getGuess(BOARD, GUESS_HISTORY)
var guess_letters = getLettersFromWord(guess);
GUESS_HISTORY.push(guess);
// Set the setters on the board
for (let i = 0; i < BOARD[`row_${GUESS_HISTORY.length}`].length; i++) {
const ROW_ENTRY = BOARD[`row_${GUESS_HISTORY.length}`][i];
const GUESS_LETTER = guess_letters[i];
ROW_ENTRY["letter"] = GUESS_LETTER;
}
// Figure out if each letter is G Y or B
for (let i = 0; i < BOARD[`row_${GUESS_HISTORY.length}`].length; i++) {
const ROW_ENTRY = BOARD[`row_${GUESS_HISTORY.length}`][i];
const LETTER = ROW_ENTRY["letter"];
const POSITION = i + 1;
if (doesWordContainLetterAtIndex(SELECTED_WORD, LETTER, POSITION)) ROW_ENTRY["colour"] = "G";
else if (doesWordContainLetter(SELECTED_WORD, LETTER)) ROW_ENTRY["colour"] = "Y";
else ROW_ENTRY["colour"] = "B";
}
printBoard(BOARD);
if (guess == SELECTED_WORD) { GAME_STATE["running"] = false; GAME_STATE["outcome"] = 1; }
else if (guess != SELECTED_WORD && GUESS_HISTORY.length == 6) { GAME_STATE["running"] = false; GAME_STATE["outcome"] = 2; }
}
if (!GAME_STATE["running"]) {
switch (GAME_STATE["outcome"]) {
case 1:
printBoard(BOARD);
console.log(`${WIN_MESSAGES[Math.floor(Math.random()*WIN_MESSAGES.length)]}`)
PROMPT(CHALK.gray(" Press Enter To Continue"));
MAIN_MENU_SCREEN.activate();
break;
case 2:
printBoard(BOARD);
console.log(`${LOSS_MESSAGES[Math.floor(Math.random()*LOSS_MESSAGES.length)]}`)
PROMPT(CHALK.gray(" Press Enter To Continue"));
MAIN_MENU_SCREEN.activate();
break;
default:
break;
}
}
}
function mainMenuLogic(selection) {
// console.log((selection == ""))
switch (selection) {
case "0": break;
case "1": HOW_TO_PLAY_SCREEN.activate(); break;
case "2": WORD_BANKS_SCREEN.activate(); break;
case "3": GAME_PREP_SCREEN.activate(); break;
default: MAIN_MENU_SCREEN.activate(); break;
}
}
function gamePrepMenuLogic(selection) {
// console.log((selection == ""))
switch (selection) {
case "1": runGame(1); break;
case "2": runGame(2); break;
default: GAME_PREP_SCREEN.activate(); break;
}
}
/**
* STARTING THE SCRIPT
*/
AUTHOR_NOTE_SCREEN.activate(); // Very fancy I know