Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!