DEV Community

Cover image for How to Create a Draughts Game Board with HTML, CSS, and JavaScript
Ayodeji Akintubi
Ayodeji Akintubi

Posted on

How to Create a Draughts Game Board with HTML, CSS, and JavaScript

Otherwise known as Checkers, draughts "is a group of strategy board games for two players which involve diagonal moves of uniform game pieces and mandatory captures by jumping over opponent pieces."

You can create an amazing draughts game board with HTML, CSS, and JavaScript.

HTML Session

In the HTML section, create the board's skeleton with the table tag and its elements.

The draughts board consists of 64 cells with alternating black and white colors.

<tr>
  <td></td>
  <td></td>
  <td></td>
  <td></td>
  <td></td>
  <td></td>
  <td></td>
  <td></td>
</tr>

<tr>
  <td></td>
  <td></td>
  <td></td>
  <td></td>
  <td></td>
  <td></td>
  <td></td>
  <td></td>
</tr>

<tr>
  <td></td>
  <td></td>
  <td></td>
  <td></td>
  <td></td>
  <td></td>
  <td></td>
  <td></td>
</tr>

<tr>
  <td></td>
  <td></td>
  <td></td>
  <td></td>
  <td></td>
  <td></td>
  <td></td>
  <td></td>
</tr>

<tr>
  <td></td>
  <td></td>
  <td></td>
  <td></td>
  <td></td>
  <td></td>
  <td></td>
  <td></td>
</tr>

<tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
  </tr>

  <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
  </tr>

  <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
  </tr>

CSS Section

Define the draught's properties such as the board size, cells, and color in this section.

table {
border-collapse: collapse;
}

td {
  border: 1px solid black;
  padding: 3px 5px;
  width: 50px; 
  height: 50px;
}
Enter fullscreen mode Exit fullscreen mode

body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}

.game-board {
border-collapse: collapse;
border: 2px solid #000;
}

.cell {
width: 50px;
height: 50px;
text-align: center;
vertical-align: middle;
font-size: 24px;
cursor: pointer;
}

.black {
background-color: #000;
color: #fff;
}

JavaScript Section

Here is where you define the alternating black and white colors.

let table = document.getElementById("game-board");

let rows = table.getElementsByTagName("tr");

for (let i = 0; i < rows.length; i++) {
let cells = rows[i].getElementsByTagName("td");

for (let j = 0; j < cells.length; j++) {
if ((i + j) % 2 === 0) {
cells[j].style.backgroundColor = "black";
} else {
cells[j].style.backgroundColor = "white";
}
}
}

Conclusion
You can effortlessly create the a checkers board with the above code

Top comments (0)