-
Notifications
You must be signed in to change notification settings - Fork 0
/
scramble.js
256 lines (206 loc) · 6.63 KB
/
scramble.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
/**********************************************
* STARTER CODE
**********************************************/
/**
* shuffle()
* Shuffle the contents of an array
* depending the datatype of the source
* Makes a copy. Does NOT shuffle the original.
* Based on Steve Griffith's array shuffle prototype
* @Parameters: Array or string
* @Return: Scrambled Array or string, based on the provided parameter
*/
function shuffle (src) {
const copy = [...src]
const length = copy.length
for (let i = 0; i < length; i++) {
const x = copy[i]
const y = Math.floor(Math.random() * length)
const z = copy[y]
copy[i] = z
copy[y] = x
}
if (typeof src === 'string') {
return copy.join('')
}
return copy
}
/**
* help()
* Displays the game instructions.
* @Return: String
*/
function help () {
return `Welcome to Scramble.
The game where you unscramble letters to make words.
Once you start the game, you will be given a scrambled word.
If you correctly guess the word, you will receive a point.
If you guess incorrectly you will receive a strike.
You can also pass on a word.
To start a new game use start().
To make guess use guess('word').
To skip a word use pass().
To show these instructions again use help().`
}
// Displays the instructions when the page loads.
console.log(help())
/**********************************************
* YOUR CODE BELOW
**********************************************/
/* Pseudo code
/* Array of String (words)
Must Contain at least 10 items
*/
const words = ['DOG', 'GOOSE', 'DONKEY', 'CHEETAH', 'COUGAR', 'KANGAROO', 'FLAMINGO']
/*
* Game object used to hold game status
* - active: boolean
* - words: array(shuffled)
* - word: string
* - scrambled: string
* - strikes: numbers
* - points: numbers
* - maxStrikes: numbers
* - passes: numbers
* - maxPasses: numbers
*/
const game = {
words: [],
word: '',
scrambled: '',
active: false,
strikes: 0,
passes: 0,
points: 0,
maxStrikes: 3,
maxPasses: 1
}
/*
* The start() function
* check if game.active is false (needs to be false to create a new game)
* set strikes to 0
* set points to 0
* set passes to 0
* set game.active to true
* Use Shuffle function to Create a copy of the words array and store in game.words
* Get the first word off of the games.words array and store in game.word (****USE SHIFT to remove item off of the array***)
* Use shuffle function to scramble the game.word and store in game.scrambled
* Respond to player: the scrambled word
* else
Respond to player: game already started
*/
function start () {
if ( game.active ) {
return 'The game is already started'
}
game.active = true
game.strikes = 0
game.points = 0
game.passes = 0
game.words = shuffle(words)
getNewScrambledWord()
return useCurrentScrambledWord()
}
function guess (guessedWord) {
if ( !game.active ) {
console.log('There is no current game')
return 'Please use start() to start a new game.'
}
if (guessedWord.toLowerCase() === game.word.toLowerCase()) {
game.points++
console.log(`Correct! You have ${game.points} points`)
return tryNextWord()
}
// guessedWord is incorrect
game.strikes++
console.log(`Incorrect! You have ${game.strikes} strikes (out of ${game.maxStrikes} allowed)`)
if ( game.strikes >= game.maxStrikes ) {
return endTheGame()
}
return useCurrentScrambledWord()
}
function pass () {
if ( !game.active ) {
console.log('There is no current game')
return 'Please use start() to start a new game.'
}
if ( game.passes >= game.maxPasses ) {
console.log(`You have already used ${game.passes} of ${game.maxPasses} passes`)
return useCurrentScrambledWord()
}
game.passes++
console.log(`Pass! You have used ${game.passes} passes (out of ${game.maxPasses} allowed)`)
return tryNextWord()
}
/* The status function is helpful for debugging */
function status() {
console.log(`Active : ${game.active}`)
console.log(`Points : ${game.points}`)
console.log(`Strikes : ${game.strikes} out of ${game.maxStrikes}`)
console.log(`Passes : ${game.passes} out of ${game.maxPasses}`)
console.log(`Words : ${game.words}`)
console.log(`Word : ${game.word}`)
console.log(`Scrambled : ${game.scrambled}`)
}
function useCurrentScrambledWord() {
console.log(`The word is:`)
return game.scrambled
}
function tryNextWord() {
if ( anyWordsLeft() ) {
console.log('The new word is:')
return getNewScrambledWord()
}
return endTheGame()
}
function getNewScrambledWord () {
game.word = game.words.shift()
game.scrambled = game.word.slice(0)
game.scrambled = shuffle(game.scrambled)
return game.scrambled
}
function endTheGame() {
game.active = false
if ( game.strikes >= game.maxStrikes ) {
console.log(`You used all of your strikes.`)
return "Sorry, you lost."
}
else {
console.log(`You win!`)
console.log(`You got ${game.points} points.`)
console.log(`You got ${game.strikes} strikes (out of ${game.maxStrikes} allowed)`)
console.log(`You used ${game.passes} passes (out of ${game.maxPasses} allowed)`)
return "Congratulations, you won!"
}
}
function anyWordsLeft() {
return game.words.length > 0
}
/** The guess (accepts a parameter) function */
/**
Don’t forget to check for end of game requirements inside the guess() function.
* check if game.active is false (game needs to be started first)
* return Must start a game in order to take a guess
if to check that player doesnt have too many strikes (game.strike < game.maxStrikes || game.words.length > 0 )
* check if game.word == game.scramble.toLowerCase() (**case does not matter**)
Use splice to remove the game.word from game.words array
* set game.points to 1
return Correct and display current score
else check game.word =! game.scramble.toLowerCase()
game.strike++
* return wrong and display how many strikes are left
else
game.active = false
Return you are out of strikes, game over and display final score
*/
/*
The pass() function (make sure player does not receive a point)
* check if game.active is false (game needs to be started first)
* return Must start a game in order to pass
else game.passess >= game.maxPasses
return You have used up all of your passes
else if
* Use splice to remove the game.word from game.words array
game.passes++
Return You used a pass and how many passes are left
*/