W
W
WarriorKodeK2018-02-15 20:56:41
JavaScript
WarriorKodeK, 2018-02-15 20:56:41

How to check horizontally and vertically?

I'm making a bingo game. I need to validate the cells horizontally and vertically to determine who won.
That is, vertically it should be like this: https://prnt.sc/if7wcy
And horizontally like this: https: https://prnt.sc/if7xqa
If the cell number matches the generated number, then the cell is highlighted, respectively, if all cells are horizontals or verticals in some column (row) will be highlighted, then the winner's pop-up will appear (I'll do more later).
There was an idea to make an array of cells Array.from(document.querySelectorAll(".cell")) and loop through and check classes. But that when I reach the implementation, I'm stupid.
Tell me some algorithm how would you do it?
The code itself ishttps://codepen.io/anon/pen/LQOYVd?editors=1010
Thanks!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2018-02-15
@WarriorKodeK

For a 5x5 card, the search is something like this:

for (let i=0; i < 5; i+=5) {
  if (cells[i].isFlipped
    && cells[i+1].isFlipped
    && cells[i+2].isFlipped
    && cells[i+3].isFlipped
    && cells[i+4].isFlipped
  ) {
  // весь столбец подсвечен
 }
} 
for (let i=0; i < 5; i++) {
  if (cells[i].isFlipped
    && cells[i+5].isFlipped
    && cells[i+10].isFlipped
    && cells[i+15].isFlipped
    && cells[i+20].isFlipped
  ) {
  // вся строка подсвечена
 }
}
Instead of isFlipped, do your own check, by class or some property.
But for good, you need to keep a copy of the data from the cells in the array, and not pull it out of the DOM every time (this is a rather expensive operation).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question