1
1
var gameOver = false ;
2
- const ans = [ ] ;
2
+ var gameWon = true ;
3
3
4
4
// Prevent keyboard input
5
5
document . addEventListener ( 'keydown' , function ( event ) {
@@ -14,20 +14,19 @@ document.addEventListener('keydown', function (event) {
14
14
event . preventDefault ( ) ;
15
15
} ) ;
16
16
17
- console . log ( "294 activeAnsBox: " + activeAnsBox . id ) ;
18
17
//Set value of focused answer box
19
18
function getInput ( number ) {
20
- if ( activeAnsBox == null ) {
21
- alert ( "Please click on a box to input a number" ) ;
19
+ if ( ! gameOver ) {
20
+ if ( activeAnsBox == null ) {
21
+ alert ( "Please click on a box to input a number" ) ;
22
+ }
23
+ else {
24
+ document . getElementById ( activeAnsBox . id ) . value = number ;
25
+ activeAnsBox = document . getElementById ( activeAnsBox . id ) . nextElementSibling ;
26
+ document . getElementById ( activeAnsBox . id ) . focus ( ) ;
27
+ }
22
28
}
23
- else {
24
- console . log ( "301 activeAnsBox: " + activeAnsBox . id ) ;
25
29
26
- document . getElementById ( activeAnsBox . id ) . value = number ;
27
- activeAnsBox = document . getElementById ( activeAnsBox . id ) . nextElementSibling ;
28
- document . getElementById ( activeAnsBox . id ) . focus ( ) ;
29
- // setFocus();
30
- }
31
30
32
31
}
33
32
@@ -38,14 +37,7 @@ function setFocus(element, focusID){
38
37
39
38
}
40
39
41
- //Submit player's answer
42
- function submitAnswer ( ) {
43
- const playerAnswer = new Array ( ) ;
44
- playerAnswer . push ( answerBox1 . value ) ;
45
- playerAnswer . push ( answerBox2 . value ) ;
46
- playerAnswer . push ( answerBox3 . value ) ;
47
- alert ( playerAnswer ) ;
48
- }
40
+
49
41
50
42
//Set active answer box to null if player clicks elsewhere on the page (other than an answer box or a button)
51
43
function clickOut ( ) {
@@ -55,19 +47,19 @@ function clickOut(){
55
47
}
56
48
57
49
function submitAnswer ( ) {
58
-
50
+
59
51
if ( ! gameOver ) {
60
- document . getElementById ( "answer-box1" ) . value = ans [ 0 ] ;
61
- document . getElementById ( "answer-box2 " ) . value = ans [ 1 ] ;
62
- document . getElementById ( "answer-box3 " ) . value = ans [ 2 ] ;
63
-
52
+ var ans = new Array ( ) ;
53
+ ans . push ( document . getElementById ( "answer-box1 " ) . value ) ;
54
+ ans . push ( document . getElementById ( "answer-box2 " ) . value ) ;
55
+ ans . push ( document . getElementById ( "answer-box3" ) . value ) ;
64
56
var ansCount = 0 ;
65
57
66
58
}
67
59
68
60
for ( var i = 0 ; i < ans . length ; i ++ ) {
69
61
70
- if ( ans [ 0 ] == 'X' || ans [ 1 ] == 'X' || ans [ 2 ] == 'X' ) {
62
+ if ( ans [ i ] . length == 0 ) {
71
63
alert ( "You must input all three numbers" ) ;
72
64
break ;
73
65
}
@@ -84,4 +76,138 @@ function submitAnswer(){
84
76
}
85
77
86
78
}
79
+
80
+ var answerArray = [ ] ;
81
+ for ( var i = 0 ; i < ans . length ; i ++ ) {
82
+ answerArray [ i ] = parseInt ( ans [ i ] ) ;
83
+ }
84
+
85
+ if ( ansCount == clueSize ) {
86
+ checkAnswer ( answerArray ) ;
87
+ if ( gameWon == false ) {
88
+ alert ( "Sorry you didn't crack the code." ) ;
89
+ // var popup = document.getElementById("popup-lost");
90
+ // popup.classList.add("open-popupLost");
91
+ }
92
+ else {
93
+ // var popup = document.getElementById("popup-won");
94
+ // popup.classList.add("open-popupWon");
95
+ alert ( "You cracked the code!" ) ;
96
+
97
+ }
98
+
99
+ }
100
+
101
+ }
102
+
103
+ function checkAnswer ( answer ) {
104
+ gameWon = checkWellPlaced ( answer ) ;
105
+
106
+ if ( gameWon ) {
107
+ gameWon = checkTwoCorrect ( answer ) ;
108
+
109
+ if ( gameWon ) {
110
+
111
+ gameWon = checkOneCorrect ( answer ) ;
112
+ if ( gameWon ) {
113
+ gameWon = checkNoCorrect ( answer ) ;
114
+ }
115
+ }
116
+
117
+ }
118
+ gameOver = true ;
119
+ }
120
+
121
+ function checkWellPlaced ( answer ) {
122
+ var wpAns = - 1 ;
123
+ var wellPlacedCount = 0 ;
124
+
125
+ for ( var i = 0 ; i < answer . length ; i ++ ) {
126
+ if ( wellPlClueArray . includes ( ( answer [ i ] ) ) ) {
127
+ wpAns = answer [ i ] ;
128
+ wellPlacedCount ++ ;
129
+ }
130
+ }
131
+ if ( wellPlacedCount != 1 ) {
132
+
133
+ gameWon = false ;
134
+ }
135
+ else {
136
+
137
+ if ( answer . indexOf ( wpAns ) != wellPlClueArray . indexOf ( wpAns ) ) {
138
+ gameWon = false ;
139
+ }
140
+ }
141
+
142
+ return gameWon ;
143
+ }
144
+
145
+ function checkTwoCorrect ( answer ) {
146
+ var twoCorrectCount = 0 ;
147
+ var cp1 = - 1 ;
148
+ var cp2 = - 1 ;
149
+
150
+ for ( var i = 0 ; i < answer . length ; i ++ ) {
151
+ if ( twoCorrClueArray . includes ( answer [ i ] ) ) {
152
+ if ( cp1 < 0 ) {
153
+ cp1 = answer [ i ] ;
154
+
155
+ }
156
+ else {
157
+ cp2 = answer [ i ] ;
158
+ }
159
+ twoCorrectCount ++ ;
160
+ }
161
+
162
+ }
163
+ if ( twoCorrectCount == 2 ) {
164
+
165
+ if ( answer . indexOf ( cp1 ) == twoCorrClueArray . indexOf ( cp1 ) ) {
166
+ gameWon = false ;
167
+ }
168
+ if ( answer . indexOf ( cp2 ) == twoCorrClueArray . indexOf ( cp2 ) ) {
169
+ gameWon = false ;
170
+ }
171
+
172
+ }
173
+ else {
174
+ gameWon = false ;
175
+ }
176
+ return gameWon ;
177
+ }
178
+
179
+ function checkOneCorrect ( answer ) {
180
+ let oneCorrectCount = 0 ;
181
+ let correct = - 1 ;
182
+ for ( let i = 0 ; i < answer . length ; i ++ ) {
183
+ if ( oneCorrClueArray . includes ( answer [ i ] ) ) {
184
+ correct = answer [ i ] ;
185
+ oneCorrectCount ++ ;
186
+ }
187
+ }
188
+ if ( oneCorrectCount != 1 ) {
189
+ gameWon = false ;
190
+ }
191
+ else {
192
+ for ( let i = 0 ; i < answer . length ; i ++ ) {
193
+ if ( answer . indexOf ( correct ) == oneCorrClueArray . indexOf ( correct ) ) {
194
+ gameWon = false ;
195
+ break ;
196
+ }
197
+ }
198
+ }
199
+
200
+
201
+ return gameWon ;
202
+
203
+ }
204
+
205
+ function checkNoCorrect ( answer ) {
206
+ for ( let i = 0 ; i < answer . length ; i ++ ) {
207
+ if ( noCorrClueArr . includes ( answer [ i ] ) ) {
208
+ gameWon = false ;
209
+ break ;
210
+ }
211
+ }
212
+ return gameWon ;
87
213
}
0 commit comments