Oto prosty kod HTML, CSS i JavaScript do gry w kółko i krzyżyk:
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Kółko i Krzyżyk</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
margin-top: 50px;
}
.board {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-gap: 5px;
margin-bottom: 20px;
}
.cell {
width: 100px;
height: 100px;
background-color: #f0f0f0;
display: flex;
align-items: center;
justify-content: center;
font-size: 2em;
cursor: pointer;
user-select: none;
}
.cell:hover {
background-color: #ddd;
}
.status {
font-size: 1.5em;
margin-bottom: 20px;
}
.restart {
padding: 10px 20px;
font-size: 1em;
cursor: pointer;
}
</style>
</head>
<body>
<div class="status" id="status">Ruch: X</div>
<div class="board" 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>
<button class="restart" id="restart">Nowa Gra</button>
<script>
let currentPlayer = 'X';
let board = ['', '', '', '', '', '', '', '', ''];
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]
];
const cells = document.querySelectorAll('.cell');
const status = document.getElementById('status');
const restartButton = document.getElementById('restart');
cells.forEach(cell => cell.addEventListener('click', handleCellClick));
restartButton.addEventListener('click', restartGame);
function handleCellClick(event) {
const index = event.target.getAttribute('data-index');
if (board[index] !== '' || !gameActive) return;
board[index] = currentPlayer;
event.target.textContent = currentPlayer;
checkResult();
}
function checkResult() {
let roundWon = false;
for (let condition of winningConditions) {
const [a, b, c] = condition;
if (board[a] && board[a] === board[b] && board[a] === board[c]) {
roundWon = true;
break;
}
}
if (roundWon) {
status.textContent = `Wygrywa ${currentPlayer}!`;
gameActive = false;
return;
}
const roundDraw = !board.includes('');
if (roundDraw) {
status.textContent = 'Remis!';
gameActive = false;
return;
}
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
status.textContent = `Ruch: ${currentPlayer}`;
}
function restartGame() {
currentPlayer = 'X';
board = ['', '', '', '', '', '', '', '', ''];
gameActive = true;
status.textContent = `Ruch: ${currentPlayer}`;
cells.forEach(cell => cell.textContent = '');
}
</script>
</body>
</html>
Funkcjonalności:
- Plansza 3x3 z komórkami do klikania
- Zmiana gracza po każdym ruchu (X i O)
- Sprawdzanie warunków wygranej
- Wyświetlanie wyniku (wygrana/remis)
- Przycisk do rozpoczęcia nowej gry
- Proste efekty wizualne (podświetlanie komórek przy najechaniu)
Instrukcja:
- Otwórz plik w przeglądarce
- Gracze na przemian klikają w komórki
- Gra automatycznie sprawdza warunki wygranej
- Po zakończeniu partii użyj przycisku "Nowa Gra"
Kod jest w pełni funkcjonalny i nie wymaga żadnych dodatkowych plików. Można go łatwo zmodyfikować, np. dodając animacje lub zmieniając kolorystykę.