1
1
// No guesses? -> ***
2
2
// Guessed "c", "b" and "t"? -> c*t
3
3
4
+ // 1. Disable new guesses unless "playing"
5
+ // 2. Setup a new method to get back a status message
6
+
7
+ // Playing -> Guesses left: 3
8
+ // Failed -> Nice Try! The word was "Cat"
9
+ // Finished -> Great work! You guessed the word
10
+
4
11
const Hangman = function ( word , remainingGuesses ) {
5
12
const wordAlteration = ( word . toLowerCase ( ) ) . split ( '' )
6
13
this . word = word . toLowerCase ( ) . split ( '' )
7
14
this . remainingGuesses = remainingGuesses
8
15
this . guessedLetters = [ 'job' ]
9
- this . status = 'playing '
16
+ this . status = 'Playing '
10
17
}
11
18
12
19
// The below is a status report. It will either be finished of failed depending on the guesses you make.
13
20
Hangman . prototype . calculateStatus = function ( ) {
14
21
15
- let finished = true
16
- this . word . forEach ( ( letter ) => {
17
- if ( this . guessedLetters . includes ( letter ) ) {
18
- finished = true
19
- } else {
20
- finished = false
21
- }
22
- } )
22
+ const finished = this . word . every ( ( letter ) => this . guessedLetters . includes ( letter ) )
23
23
24
24
if ( this . remainingGuesses === 0 ) {
25
- this . status = 'Failed'
25
+ this . status = 'Failed' // .join - Joins the array together so it doesn't come out like c,a,t instead cat
26
26
} else if ( finished ) {
27
27
this . status = 'Finished'
28
28
} else {
29
29
this . status = 'Playing'
30
30
}
31
31
} ;
32
32
33
+ Hangman . prototype . getStatusMessage = function ( ) {
34
+ if ( this . status === 'Playing' ) {
35
+ return `Guesses left: ${ this . remainingGuesses } `
36
+ } else if ( this . status === 'Failed' ) {
37
+ return `Nice try! The word was: "${ this . word . join ( '' ) } "`
38
+ } else {
39
+ return 'Great work! You guessed the word'
40
+ }
41
+ }
42
+
33
43
Hangman . prototype . getPuzzle = function ( ) {
34
44
let puzzle = ''
35
45
@@ -49,6 +59,11 @@ Hangman.prototype.makeGuess = function (guess){
49
59
const isUnique = ! this . guessedLetters . includes ( guess )
50
60
const isBadGuess = ! this . word . includes ( aGuess )
51
61
// If this.guessedLetters is empty & bad guess:
62
+
63
+ if ( this . status !== 'Playing' ) {
64
+ return ;
65
+ }
66
+
52
67
if ( isUnique ) {
53
68
this . guessedLetters . push ( aGuess )
54
69
// bad guess - lose 1 guess
0 commit comments