Skip to content

Commit 7640e44

Browse files
committed
now working checking answers, no pop-up, just alert
1 parent a29f8aa commit 7640e44

File tree

1 file changed

+151
-25
lines changed

1 file changed

+151
-25
lines changed

crackTC/player-script.js

Lines changed: 151 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var gameOver = false;
2-
const ans = [];
2+
var gameWon = true;
33

44
// Prevent keyboard input
55
document.addEventListener('keydown', function (event) {
@@ -14,20 +14,19 @@ document.addEventListener('keydown', function (event) {
1414
event.preventDefault();
1515
});
1616

17-
console.log("294 activeAnsBox: " + activeAnsBox.id);
1817
//Set value of focused answer box
1918
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+
}
2228
}
23-
else{
24-
console.log("301 activeAnsBox: " + activeAnsBox.id);
2529

26-
document.getElementById(activeAnsBox.id).value = number;
27-
activeAnsBox = document.getElementById(activeAnsBox.id).nextElementSibling;
28-
document.getElementById(activeAnsBox.id).focus();
29-
// setFocus();
30-
}
3130

3231
}
3332

@@ -38,14 +37,7 @@ function setFocus(element, focusID){
3837

3938
}
4039

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+
4941

5042
//Set active answer box to null if player clicks elsewhere on the page (other than an answer box or a button)
5143
function clickOut(){
@@ -55,19 +47,19 @@ function clickOut(){
5547
}
5648

5749
function submitAnswer(){
58-
50+
5951
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);
6456
var ansCount = 0;
6557

6658
}
6759

6860
for(var i = 0; i < ans.length; i++) {
6961

70-
if(ans[0] == 'X' || ans[1] == 'X' || ans[2] == 'X'){
62+
if(ans[i].length == 0){
7163
alert("You must input all three numbers");
7264
break;
7365
}
@@ -84,4 +76,138 @@ function submitAnswer(){
8476
}
8577

8678
}
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;
87213
}

0 commit comments

Comments
 (0)