JSFiddle - React, Tailwind, and code Playground
by gjgroen
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Boter-Kaas-en-Eieren</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="game-container">
<h1>Boter-Kaas-en-Eieren</h1>
<div class="board" id="board">
<!-- Cells will be dynamically generated -->
</div>
<p id="message">Speler X is aan de beurt</p>
<button id="reset">Opnieuw Spelen</button>
</div>
<script src="script.js"></script>
</body>
</html>
CSS
/* Basic styling */
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
}
.game-container {
text-align: center;
background: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
}
h1 {
margin-bottom: 20px;
}
.board {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-template-rows: repeat(3, 100px);
gap: 5px;
margin: 20px auto;
}
.cell {
width: 100px;
height: 100px;
background-color: #e0e0e0;
display: flex;
justify-content: center;
align-items: center;
font-size: 2rem;
font-weight: bold;
cursor: pointer;
border-radius: 5px;
transition: background-color 0.3s;
}
.cell:hover {
background-color: #d6d6d6;
}
#message {
margin: 20px 0;
font-size: 1.2rem;
}
button {
padding: 10px 20px;
font-size: 1rem;
color: white;
background-color: #007bff;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #0056b3;
}
JavaScript
// JavaScript logic for Tic-Tac-Toe
const board = document.getElementById("board");
const message = document.getElementById("message");
const resetButton = document.getElementById("reset");
let currentPlayer = "X";
let boardState = Array(9).fill(null);
function createBoard() {
board.innerHTML = "";
boardState.forEach((cell, index) => {
const cellElement = document.createElement("div");
cellElement.classList.add("cell");
cellElement.dataset.index = index;
cellElement.textContent = cell;
cellElement.addEventListener("click", handleCellClick);
board.appendChild(cellElement);
});
}
function handleCellClick(event) {
const index = event.target.dataset.index;
if (boardState[index] || checkWinner()) {
return;
}
boardState[index] = currentPlayer;
event.target.textContent = currentPlayer;
if (checkWinner()) {
message.textContent = `Speler ${currentPlayer} wint!`;
return;
}
currentPlayer = currentPlayer === "X" ? "O" : "X";
message.textContent = `Speler ${currentPlayer} is aan de beurt`;
}
function checkWinner() {
const winningCombinations = [
[0, 1, 2], [3, 4, 5], [6, 7, 8],
[0, 3, 6], [1, 4, 7], [2, 5, 8],
[0, 4, 8], [2, 4, 6]
];
return winningCombinations.some(combination => {
const [a, b, c] = combination;
return boardState[a] && boardState[a] === boardState[b] && boardState[a] === boardState[c];
});
}
function resetGame() {
boardState = Array(9).fill(null);
currentPlayer = "X";
message.textContent = "Speler X is aan de beurt";
createBoard();
}
resetButton.addEventListener("click", resetGame);
// Initialize the game
createBoard();