-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
48 lines (37 loc) · 1.24 KB
/
game.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
/* DO NOT MODIFY THIS FILE - USED FOR TESTING */
const readline = require('readline').createInterface({ input: process.stdin, output: process.stdout });
const wordList = require('./words.js');
const compare = require('./compare.js');
/* DO NOT MODIFY THIS FILE - USED FOR TESTING */
const game = {
word: process.env.OVERRIDE || pickWord(wordList),
turns: 0
};
if(process.env.DEBUG) { console.log(`PSST! The word is ${game.word}`); }
console.log(`The word is ${game.word.length} letters`);
prompt();
function prompt() {
readline.question('your guess? ', guess => takeTurn(game, guess) );
}
/* DO NOT MODIFY THIS FILE - USED FOR TESTING */
function takeTurn(game, guess) {
if(!guess) {
prompt();
}
game.turns++;
if(exactMatch(game.word, guess)) {
console.log(`CORRECT! You won in ${game.turns} turns!`);
readline.close();
return;
}
const match = compare(game.word, guess);
console.log(`You matched ${match} letters out of ${game.word.length}`);
prompt();
}
function exactMatch(word, guess) {
return word.toUpperCase() === guess.toUpperCase(); // Case-insensitive compare
}
function pickWord(wordList) {
return wordList[Math.floor(Math.random() * wordList.length)];
}
/* DO NOT MODIFY THIS FILE - USED FOR TESTING */