forked from joinpursuit/FSW-CLI-Snowman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnowman.js
128 lines (116 loc) · 3.38 KB
/
snowman.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
const { get } = require("https");
const readline = require("readline-sync");
const dictionary = require("./dictionary");
let obj = {
guessesRemaining: 9,
correctGuesses: [],
incorrectGuesses: [],
secretWord: "",
board: [],
};
const randomWord = () => {
let word = dictionary[Math.floor(Math.random() * dictionary.length)];
return word;
};
const buildBoard = () => {
for (let i = 0; i < obj.secretWord.length; i++) {
obj.board[i] = "_";
}
return obj.board;
};
const getValidLetterGuess = () => {
function guessIsValid(letter) {
return letter.length === 1 && letter.toUpperCase() !== letter.toLowerCase();
}
let letter = "";
while (!letter) {
let input = readline.question("\nPlease enter your guess: ");
if (guessIsValid(input)) {
letter = input;
if (obj.correctGuesses.concat(obj.incorrectGuesses).includes(letter)) {
letter = "";
console.log("This letter was already guessed. Please select another.");
}
} else {
console.log("\nPlease enter a valid letter");
}
}
return letter.toLowerCase();
};
const theEnd = () => {
if (obj.guessesRemaining === 0) {
console.log("The word was " + obj.secretWord.toUpperCase() + ".\n");
console.log(
"You had a total of " +
(obj.correctGuesses.length + obj.incorrectGuesses.length) +
" valid guesses."
);
console.log("Better luck next time.\n");
} else if (obj.secretWord === obj.board.join("")) {
console.log("The word was " + obj.secretWord.toUpperCase() + ".\n");
console.log(
"You had a total of " +
(obj.correctGuesses.length + obj.incorrectGuesses.length) +
" valid guesses."
);
console.log("You win! Congratulations!\n");
}
if (readline.keyInYNStrict("\nWould you like to play again? \n")) {
(obj.guessesRemaining = 9),
(obj.correctGuesses = []),
(obj.incorrectGuesses = []),
(obj.secretWord = ""),
(obj.board = []),
console.log();
startGame();
theEnd();
console.clear();
} else {
console.log("Thanks for playing.\n", "Goodbye!\n");
process.exit();
}
};
const wordCompare = (playerInput) => {
return obj.secretWord.includes(playerInput);
};
const boardUpdate = (playerInput) => {
for (let j = 0; j < obj.secretWord.length; j++) {
if (obj.secretWord[j] === playerInput) {
obj.board[j] = playerInput;
}
}
return obj.board;
};
const keepPlaying = () => {
return obj.guessesRemaining > 0 && obj.board.join("") !== obj.secretWord;
};
const startGame = () => {
readline.question(
" Welcome to the Snowman Game.\n Try to predict the word by guessing individual letters.\n Good luck!\n\nPress ENTER/RETURN to begin."
);
obj.secretWord = randomWord();
buildBoard().join(" ");
while (keepPlaying()) {
console.clear();
console.log(
`SNOWMAN\n Guesses Remaining: ${
obj.guessesRemaining
}\n Correct Guesses : ${obj.correctGuesses}\n Incorrect Guesses: ${
obj.incorrectGuesses
}\n ${obj.board.join(" ")}\n`,
obj.secretWord
);
let guessLetter = getValidLetterGuess();
if (wordCompare(guessLetter)) {
console.log("\nYou guessed right!");
boardUpdate(guessLetter);
obj.correctGuesses.push(guessLetter);
} else {
console.log("\nWrong guess.");
obj.guessesRemaining -= 1;
obj.incorrectGuesses.push(guessLetter);
}
}
theEnd();
};
startGame();