I have the following code written so far (still working on formatting the code so that it is smaller). I have added some comments so that you can have a better understanding. I have stated on each case what the output should be. All bar case2 is correct, but case2 seems to be conflicting with the statement below. Can I have some assistance in fixing this issue?
if (arr[i][0].match(/[^AOE]/) !== null) {
return "Invalid"
}
if (arr[i][1].match(/[^AOE]/) !== null) {
return "Invalid"
}
if (arr[i][2].match(/[^AOE]/) !== null) {
return "Invalid"
}
Below is my current code (sorry for the format of the cases, its just an easy way to visualise):
function checkWinner(arr) {
for (var i = 0; i < 3; i++) {
/* Is the Board layout correct*/
if (arr.flat().length !== 9){
return "Not Completed"
}
/*Checks if all indexes == E */
if ((arr[0][0] == "E" && arr[0][1] == "E" && arr[0][2] == "E" && arr[1][0] == "E" && arr[1][1] == "E" && arr[1][2] == "E" && arr[2][0] == "E" && arr[2][1] == "E" && arr[2][2] == "E")) {
return "Please start playing"
}
/*Reading for X and O*/
/*Horizontal*/
if (arr[i][0] == "X" && arr[i][1] == "X" && arr[i][2] == "X") {
return "Valid"
}
if (arr[i][0] == "O" && arr[i][1] == "O" && arr[i][2] == "O") {
return "Valid"
}
/*Vertical*/
if (arr[0][i] == "X" && arr[1][i] == "X" && arr[2][i] == "X") {
return "Valid"
}
if (arr[0][i] == "O" && arr[1][i] == "O" && arr[2][i] == "O") {
return "Valid"
}
/*Diagonal TL to BR */
if (arr[0][0] == "X" && arr[1][1] == "X" && arr[2][2] == "X") {
return "Valid"
}
if (arr[0][0] == "O" && arr[1][1] == "O" && arr[2][2] == "O") {
return "Valid"
}
/*Diagonal TR to BL */
if (arr[0][2] == "X" && arr[1][1] == "X" && arr[2][0] == "X") {
return "Valid"
}
if (arr[0][2] == "O" && arr[1][1] == "O" && arr[2][0] == "O") {
return "Valid"
}
/*Does it contain special characters*/
if (arr[i][0].match(/[^AOE]/) !== null) {
return "Invalid"
}
if (arr[i][1].match(/[^AOE]/) !== null) {
return "Invalid"
}
if (arr[i][2].match(/[^AOE]/) !== null) {
return "Invalid"
}
}
return "Draw"
}
case1 = checkWinner(
[
["X", "X", "E"],
["X", "X", "O"],
["O", "E", "X"]
]
);
console.log(case1) // Valid
case2 = checkWinner(
[
["X", "X", "O"],
["O", "O", "X"],
["X", "O", "O"]
]
);
console.log(case2) //Draw *This is what it should be, but returns invalid*
case3 = checkWinner(
[
["E", "E", "E"],
["E", "E", "E"],
["E", "E", "E"]
]
);
console.log(case3) //Please Start Playing
case4 = checkWinner(
[
[],
[],
[]
]
);
console.log(case4) //Not Completed
case5 = checkWinner(
[
[],
[],
]
);
console.log(case5) //Not Completed
case6 = checkWinner(
[
["X", "X", "O"],
["O", "O", "X"],
[]
]
);
console.log(case6) //Not Completed
case7 = checkWinner(
[
["X", "X"],
["O"],
["E", "O"]
]
);
console.log(case7) //Not Completed
case8 = checkWinner(
[
["!$%", "Y", "E"],
["", "O", undefined],
[3, "X", ""],
]
);
console.log(case8) //Invalid
Aucun commentaire:
Enregistrer un commentaire