Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

161
Visualizações
How to determine if a gameboard grid initialized as an array contains win conditions

So I'm trying to create a browser version of connect 4 using HTML, CSS and Javascript, and I have my gameboard initialized into a 2d array. I've managed to switch turns, and change the values in the array to then change the color of the gameboard's internal divs, but I'm having trouble finding a way to determine if any win conditions are present, (i.e, the array contains 4 consecutive 1s or 2s horizontally, vertically, and diagonally). I'm assuming the .includes method will be relevant here, but I'm getting a return of false if I include the numbers as an array, or strings.

function init() {
    board = [
        [0, 0, 0, 0, 0, 0],
      
        [0, 0, 0, 0, 0, 0],
        
        [0, 0, 0, 0, 0, 0],
        
        [0, 0, 0, 0, 0, 0],
        
        [0, 0, 0, 0, 0, 0],
       
        [0, 0, 0, 0, 0, 0],
        
        [0, 0, 0, 0, 0, 0]
        
    ];
    
    render();
}
//Functions controlling user input & declare render(),

//render function
function render() {
    board.forEach(function (colArr, colIdx) {
        colArr.forEach(function (cellVal, rowIdx) {
            //console.log(colIdx, rowIdx);
            const div = document.getElementById(`c${colIdx}r${rowIdx}`);
            
          
            
            if (board[colIdx][rowIdx] === 1){
                console.log(colIdx, rowIdx);
                div.style.backgroundColor = "red";
            } else if (board[colIdx][rowIdx] === 2) {
                div.style.backgroundColor = "blue";
            } else {
                div.style.backgroundColor = "white";
            };
            

        });
        
        
    });
    
}
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda