I'm currently developing a game of connect four using javascript, html, and css. I'm encountering an issue with the refreshGrid() function in game.js. At the moment, when I run my html file, I only see an empty board. The function is meant to enable a chip to appear on the board when a user clicks on an empty space. I'm unsure why this function isn't functioning correctly and would appreciate any assistance. I suspect there may be an issue with how I'm iterating through the rows and columns.
var player=1;
var grid = [
[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]
];
function selectColumn(col) {
if(player==1){
grid[5][col]=1;
player=2;
document.getElementById("box1").innerHTML="Player 1's Turn";
}else{
grid[5][col]=2;
player=1;
document.getElementById("box1").innerHTML="Player 2's Turn";
}
refreshGrid();
}
function refreshGrid() {
for (var row = 0; row < 6; row++) {
for (var col = 0; col < 7; col++) {
if (grid[row][col]==0) {
document.getElementById("cell"+row+col).style.backgroundColor="#FFFFFF";
} else if (grid[row][col]==1) { //1 for yellow
document.getElementById("cell"+row+col).style.backgroundColor="#FFFF00";
} else if (grid[row][col]==2) { //1 for yellow
document.getElementById("cell"+row+col).style.backgroundColor="#FF0000";
}
}
}
}
<div id="box1"><h1>Player 2's turn.</h1></div>
<div id="grid">
<div class="column" id="column-0" data-x="0">
<svg height="75" width="75" stroke="black" stroke-width="5" class="row-5" onclick="selectColumn(5)">
<circle cx="45" cy="45" r="30" class="free" />
</svg>
...
...
...
</div>
...
...
...
</div>