I am just doing some practice with a color alignment game. I want to run a function after every move to see if it's winner but I can't figure out the best way to check if 3 colors are in a row or diagonal. I guess I could iterate down the DOM tree each time and see if certain multiples are fulfilled but I don't know if that's the best option.
(() => {
let counter = 0;
let conts = document.querySelectorAll('.parent div')
conts.forEach(x => {
x.addEventListener('click', (evt) => {
if (!!evt.target.style.backgroundColor) {
return;
}
counter++
if (counter % 2 == 0) {
evt.target.style.backgroundColor = "red"
} else {
evt.target.style.backgroundColor = "green"
}
})
});
})();
html, body {
height: 100%;
}
.parent {
height: 100%;
display: grid;
grid-template-columns: 50px 50px 50px;
grid-template-rows: 50px 50px 50px;
grid-gap: 2px;
width: 40%;
}
.parent div {
border: 1px solid #000;
}
<div class="parent">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>