forked from CodeNextCoaches/te2018grade9term1
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathflower.js
274 lines (212 loc) · 9.66 KB
/
flower.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
// Author: FirstName lastName
/******************************************************************************
constant variables
These are global variables that should stay the same throughout the run of the
program. After being initialized, JavaScript won't let you change them ever
again. Great for when you want to "protect" certain variables from accidental
tampering!
*******************************************************************************/
const READLINE = require("readline-sync");
const FLOWER_PICS = [
" # # #\n" +
" # O #\n" +
" # # #\n" +
" | \n" +
" | \n",
" # # #\n" +
" # O #\n" +
" # #\n" +
" | \n" +
" | \n",
" # # #\n" +
" O #\n" +
" # #\n" +
" | \n" +
" | \n",
" # #\n" +
" O #\n" +
" # #\n" +
" | \n" +
" | \n",
" #\n" +
" O #\n" +
" # #\n" +
" | \n" +
" | \n",
" \n" +
" O #\n" +
" # #\n" +
" | \n" +
" | \n",
" \n" +
" O \n" +
" # #\n" +
" | \n" +
" | \n",
" \n" +
" O \n" +
" # \n" +
" | \n" +
" | \n",
" \n" +
" x \n" +
" | \n" +
" | \n" +
" | \n"
]
const WORDS = ("ant baboon badger bat bear beaver camel cat clam cobra cougar coyote " +
"crow deer dog donkey duck eagle ferret fox frog goat goose hawk lion lizard llama " +
"mole monkey moose mouse mule newt otter owl panda parrot pigeon python rabbit ram " +
"rat raven rhino salmon seal shark sheep skunk sloth snake spider stork swan tiger " +
"toad trout turkey turtle weasel whale wolf wombat zebra").split(" ");
/******************************************************************************
global variables
missedLetters
String Array. Each string is an individual letter the player has missed
so far. Initialized as an empty array at the start of each game.
correctLetters
String Array. Each string is an individual letter the player has guessed
correctly so far. Initialized as an empty array at the start of each game.
secretWord
String. This is the word the player has to guess! Initialized by
getRandomWord().
running
Boolean. Represents if the program should continue running (true) or not
(false).Initialized to true in setupGame(), can be changed in
processGameOver();
*******************************************************************************/
let missedLetters;
let correctLetters;
let secretWord;
let running;
/******************************************************************************
printGreeting()
Prints a simple greeting. Be as creative as you want here. Be sure to include
your name as the author!
*******************************************************************************/
function printGreeting() {
}
/******************************************************************************
setupGame()
Initialize global variables as follows:
-missedLetters and correctLetters should be initialized as empty arrays.
-secretWord should be initialized as a random string from the WORD constant.
-running should be initialized to true.
*******************************************************************************/
function setupGame() {
}
/******************************************************************************
printMissedLetters()
Prints the string "Missed letters: ", along with all elements in missedLetters
as a single string, with a space between each letter. For example, if
missedLetters is ["a", "b", "c", "d"], this function prints "Missed letters:
a b c d" on one line.
*******************************************************************************/
function printMissedLetters() {
}
/******************************************************************************
printCorrectLetters()
Prints the string "Secret Word: ", along with whatever letters the player has
guessed correctly, as a single string. The correct letters should appear where
they normally do in the secret word. Letters the player has not yet guessed
should appear as underscores ("blanks"). There should be a space between each
letter or blank.
For example, if the secret word is "google" and the player has guessed "o",
this function prints "Correct letters: _ o o _ _ _".
*******************************************************************************/
function printCorrectLetters() {
}
/******************************************************************************
printBoard()
Prints the "board", which is the current flower pic, missed letters, and
correct letters. The current flower pic is determined by how many letters the
player has missed so far.
*******************************************************************************/
function printBoard() {
}
/******************************************************************************
getRandomWord()
Returns a random word from WORDS.
The formula for generating a random integer is:
Math.floor(Math.random() * (max - min + 1) + min)
*******************************************************************************/
function getRandomWord() {
}
/******************************************************************************
getGuess()
In an infinite loop, prompt the player to guess a letter. The program can exit
the loop only if the player's guess is "valid", which means it passes all
three of these conditions:
1) The length of the guess must be exactly 1.
2) The guess cannot be a letter they have already guessed (alreadyGuessed is
an array passed into the function).
3) The guess must be a letter in the English alphabet.
Return the guess if it is valid. If a guess does not satisfy a condition,
print a message communicating this to the player. For example, if the player
enters "abc" or "", they have failed condition #1, so you should print
something like, "Please guess one letter at a time.".
Valid guesses should include both uppercase and lowercase letters. To make
things easier on yourself, try setting the player's guess to all lowercase
before validating it against the three conditions.
*******************************************************************************/
function getGuess(alreadyGuessed) {
}
/******************************************************************************
processGuess()
Get a player's guess by calling getGuess(). If the guess appears in the secret
word, append that letter to correctLetters. Otherwise append it to
missedLetters.
Note that getGuess() has a parameter called alreadyGuessed, which prevents the
player from guessing the same letter more than once. When calling getGuess(),
you need to pass an array of guesses the user has already made, which is
simply a concatenation of missedLetters and correctLetters arrays.
You can concatenate arrays using the array.concat() method. For example:
let array1 = ["a", "b", "c"];
let array2 = ["d", "e", "f"];
let array3 = array1.concat(array2);
The value of array3 is ["a", "b", "c", "d", "e", "f"].
*******************************************************************************/
function processGuess() {
}
/******************************************************************************
checkWinLose()
Check if the player has won or lost, and call processGameOver() in either
case. More details:
-If they have won, print a congratulatory message, then call
processGameOver().
-Otherwise, if they have lost, print the board one last time. Then print a
message telling them they have run out of guesses, along with another message
telling them what the secret word was. Finally, call processGameOver().
-If they have neither won nor lost, do nothing.
To check for a win, you should see if each letter in the secretWord string is
contained in the correctLetters array. You can do this with a for loop that
passes through the length of secretWord, but exits the moment it finds a
letter that isn't contained in correctLetters. This is the trickiest part of
this project and requires you to think out of the box a bit (at least, if
you want to do it the "neat" way).
*******************************************************************************/
function checkWinLose() {
}
/******************************************************************************
processGameOver()
Asks the player if they would like to play again. If they do, call setupGame()
to reset the game's global variables. Otherwise set the global variable
running to false and print a simple "Goodbye" message.
*******************************************************************************/
function processGameOver() {
}
/******************************************************************************
run()
The "mother function" of the program. Runs the game by doing the following:
1) Print a greeting.
2) Setup the game.
3) In a loop that continues while the global variable running is true, do
the following:
A) Print the board.
B) Process a guess.
C) Check if the player has won or lost.
*******************************************************************************/
function run() {
}
// Run the program!
run();