I am making a tic tac toe based in pure JS however while defining the win conditions for the players i want it to alert the user when the conditions are matched..
When a box is checked if . it is X who plays it turns red and if it is O's turn then blue and it also registers the id of the box element into an array containing all the boxes that are occupied also depending if the box is red or blue it also tags the id along with a 0 , 1 (0 is red 1 is blue)
so the array looks like this (11,0 22,1 33,0) 11 is first row 1st column 0 means red has occupied that spot (red = X)
In the past i have tried array.includes to check if the boxes that are already clicked are in a row or a diagonal and if they are also checked by the same player.. i used an and operator so it checks if 3 in a row are checked by the same player.. so that only if all three in a row are checked it alerts the win. I have stored the boolean value in redwincond bluewincond
function logic(clickedArr) {
redwinCond = clickedArr.contains(("11,0") && ("22,0") && ("33,0"));
bluewinCond = clickedArr.contains(("11,1") && ("22,1") && ("33,1"));
console.log(bluewinCond, redwinCond)
if (redwinCond === true) {
alert("Red wins !")
} else if (bluewinCond === true) {
alert("Blue wins !")
} else {
}
}
example redwinCond = clickedArr.contains(("11,0") && ("22,0") && ("33,0"));
Expected result: after all 3 are checked by the same color alerts user
Result: only when the third box specified (33,0) is clicked it alerts. doesnt check if other 2 are checked also CHECKED
every
-- here is a link to more detail on what it does and how it works: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… You'll also note it enjoys good browser support.