Jasne! Oto prosty, kompletny kod HTML z wbudowanym JavaScript do gry w kółko i krzyżyk (tic-tac-toe). Możesz go skopiować i wkleić do pliku .html, a następnie otworzyć w przeglądarce, aby zagrać.
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Kółko i krzyżyk</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin-top: 50px;
}
#board {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-template-rows: repeat(3, 100px);
gap: 5px;
justify-content: center;
margin: 20px auto;
}
.cell {
background: #f0f0f0;
font-size: 60px;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
user-select: none;
border: 2px solid #333;
}
.cell:hover {
background: #ddd;
}
#status {
font-size: 24px;
margin-top: 20px;
}
button {
margin-top: 20px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
</style>
</head>
<body>
<h1>Kółko i krzyżyk</h1>
<div id="board">
<div class="cell" data-index="0"></div>
<div class="cell" data-index="1"></div>
<div class="cell" data-index="2"></div>
<div class="cell" data-index="3"></div>
<div class="cell" data-index="4"></div>
<div class="cell" data-index="5"></div>
<div class="cell" data-index="6"></div>
<div class="cell" data-index="7"></div>
<div class="cell" data-index="8"></div>
</div>
<div id="status">Ruch: X</div>
<button id="reset">Restartuj grę</button>
<script>
const board = document.getElementById('board');
const cells = document.querySelectorAll('.cell');
const status = document.getElementById('status');
const resetBtn = document.getElementById('reset');
let currentPlayer = 'X';
let gameState = ['', '', '', '', '', '', '', '', ''];
let gameActive = true;
const winningConditions = [
[0,1,2],
[3,4,5],
[6,7,8],
[0,3,6],
[1,4,7],
[2,5,8],
[0,4,8],
[2,4,6]
];
function handleCellClick(e) {
const clickedCell = e.target;
const clickedIndex = parseInt(clickedCell.getAttribute('data-index'));
if (gameState[clickedIndex] !== '' || !gameActive) {
return;
}
gameState[clickedIndex] = currentPlayer;
clickedCell.textContent = currentPlayer;
if (checkWin()) {
status.textContent = `Gracz ${currentPlayer} wygrał!`;
gameActive = false;
return;
}
if (gameState.every(cell => cell !== '')) {
status.textContent = 'Remis!';
gameActive = false;
return;
}
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
status.textContent = `Ruch: ${currentPlayer}`;
}
function checkWin() {
return winningConditions.some(condition => {
const [a, b, c] = condition;
return gameState[a] === currentPlayer &&
gameState[b] === currentPlayer &&
gameState[c] === currentPlayer;
});
}
function resetGame() {
currentPlayer = 'X';
gameState = ['', '', '', '', '', '', '', '', ''];
gameActive = true;
status.textContent = `Ruch: ${currentPlayer}`;
cells.forEach(cell => cell.textContent = '');
}
cells.forEach(cell => cell.addEventListener('click', handleCellClick));
resetBtn.addEventListener('click', resetGame);
</script>
</body>
</html>
Jak to działa?
- Plansza to siatka 3x3 z divami o klasie
.cell.
- Kliknięcie w pole ustawia znak aktualnego gracza (X lub O).
- Po każdym ruchu sprawdzany jest warunek zwycięstwa lub remis.
- Komunikat informuje, czy ktoś wygrał, czy jest remis, albo czyja jest kolej.
- Przycisk "Restartuj grę" resetuje planszę i zaczyna od nowa.
Jeśli chcesz, mogę pomóc rozbudować tę grę o dodatkowe funkcje!