I am in the process of creating a game called Dots and Boxes.
The grid is filled with numerous dots:
<table>
<tr>
<td class="vLine" onclick="addLine(this)"></td>
<td class="box" onclick="fillBox(this)"></td>
<td class="vLine" onclick="addLine(this)"></td>
<td class="box" onclick="fillBox(this)"></td>
<td class="vLine" onclick="addLine(this)"></td>
<td class="box" onclick="fillBox(this)"></td>
<td class="vLine" onclick="addLine(this)"></td>
<td class="box" onclick="fillBox(this)"></td>
<td class="vLine" onclick="addLine(this)"></td>
<td class="box" onclick="fillBox(this)"></td>
<td class="vLine" onclick="addLine(this)"></td>
<td class="box" onclick="fillBox(this)"></td>
<td class="vLine" onclick="addLine(this)"></td>
</tr>
<tr>
<td class="gap"></td>
<td class="hLine" onclick="addLine(this)"></td>
<td class="gap"></td>
<td class="hLine" onclick="addLine(this)"></td>
<td class="gap"></td>
<td class="hLine" onclick="addLine(this)"></td>
<td class="gap"></td>
<td class="hLine" onclick="addLine(this)"></td>
<td class="gap"></td>
<td class="hLine" onclick="addLine(this)"></td>
<td class="gap"></td>
<td class="hLine" onclick="addLine(this)"></td>
<td class="gap"></td>
</tr>
</table>
On clicking one side of the box, it changes to black:
function addLine(obj) {
console.log("Function Triggered")
if (obj.style.backgroundColor != "black") {
obj.style.backgroundColor = "black";
changeTurn()
}
After all four sides have turned black, indicating closure, the box adopts the color of the player who completed it:
Currently, players need to manually click on the box to update its color. Ideally, I want the box to automatically fill with the color corresponding to the player once all four sides are painted black around it.
How can I incorporate a JavaScript function to verify whether the lines above, below, left, and right of a box have been colored black?
var currentPlayer = "Blue";
changePlayer();
var counter = 0;
function addLine(obj) {
console.log("Function Triggered")
if (obj.style.backgroundColor != "black") {
obj.style.backgroundColor = "black";
changePlayer()
}
}
function fillBox(obj) {
if (currentPlayer == "Blue") {
obj.style.backgroundColor = "red";
}
else if ( currentPlayer == "Red") {
obj.style.backgroundColor = "blue";
}
}
function changePlayer() {
if (currentPlayer == "Red") {
currentPlayer = "Blue";
document.getElementById('result').style.color = "blue";
}
else if (currentPlayer == "Blue") {
currentPlayer = "Red";
document.getElementById('result').style.color = "red";
};
console.log(currentPlayer);
document.getElementById('result').innerHTML = currentPlayer + "'s Turn";
}
h3 {
font-family: Arial;
}
table {
border-collapse: collapse;
}
.vLine {
width: 10px;
height: 60px;
}
.box {
width: 60px;
height: 60px;
}
.hLine {
width: 60px;
height: 10px;
}
.gap {
width: 10px;
height: 12px;
background-color: black;
}
.vLine:hover, .hLine:hover {
background-color: black;
}
<h3 id="result"></h3>
<table>
<tr>
<td class="gap"></td>
<td class="hLine" onclick="addLine(this)"></td>
<td class="gap"></td>
<td class="hLine" onclick="addLine(this)"></td>
<td class="gap"></td>
<td class="hLine" onclick="addLine(this)"></td>
<td class="gap"></td>
<td class="hLine" onclick="addLine(this)"></td>
<td class="gap"></td>
<td class="hLine" onclick="addLine(this)"></td>
<td class="gap"></td>
<td class="hLine" onclick="addLine(this)"></td>
<td class="gap"></td>
</tr>
<tr>
<td class="vLine" onclick="addLine(this)"></td>
<td class="box" onclick="fillBox(this)"></td>
<td class="vLine" onclick="addLine(this)"></td>
<td class="box" onclick="fillBox(this)"></td>
<td class="vLine" onclick="addLine(this)"></td>
<td class="box" onclick="fillBox(this)"></td>
<td class="vLine" onclick="addLine(this)"></td>
<td class="box" onclick="fillBox(this)"></td>
<td class="vLine" onclick="addLine(this)"></td>
<td class="box" onclick="fillBox(this)"></td>
<td class="vLine" onclick="addLine(this)"></td>
<td class="box" onclick="fillBox(this)"></td>
<td class="vLine" onclick="addLine(this)"></td>
</tr>
(⋭ continue pattern for remaining rows ⋮)
</table>