Oto prosta implementacja gry "Kółko i Krzyżyk" w HTML, CSS i JavaScript:
html
<!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;
background-color: #f0f0f0;
}
h1 {
color: #333;
}
.board {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-gap: 5px;
margin: 20px;
}
.cell {
width: 100px;
height: 100px;
background-color: #fff;
display: flex;
justify-content: center;
align-items: center;
font-size: 2em;
cursor: pointer;
border: 2px solid #333;
}
.cell:hover {
background-color: #eee;
}
.status {
font-size: 1.5em;
margin: 20px;
color: #333;
}
.restart {
padding: 10px 20px;
font-size: 1em;
cursor: pointer;
background-color: #007BFF;
color: #fff;
border: none;
border-radius: 5px;
}
.restart:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<h1>Kółko i Krzyżyk</h1>
<div class="status" id="status">rucj开始游戏!</div>
<div class="board" id="board"></div>
<button class="restart" id="restart" onclick="restartGame()">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]
];
function renderBoard() {
const boardElement = document.getElementById('board');
boardElement.innerHTML = '';
board.forEach((cell, index) => {
const cellElement = document.createElement('div');
cellElement.classList.add('cell');
cellElement.textContent = cell;
cellElement.onclick = () => cellClicked(index);
boardElement.appendChild(cellElement);
});
}
function cellClicked(index) {
if (board[index] !== '' || !gameActive) return;
board[index] = currentPlayer;
renderBoard();
checkResult();
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
document.getElementById('status').textContent = `Gracz ${currentPlayer} jest na ruchu`;
}
function checkResult() {
let roundWon = false;
for (let i = 0; i < winningConditions.length; i++) {
const winCondition = winningConditions[i];
let a = board[winCondition[0]];
let b = board[winCondition[1]];
let c = board[winCondition[2]];
if (a === '' || b === '' || c === '') continue;
if (a === b && b === c) {
roundWon = true;
break;
}
}
if (roundWon) {
gameActive = false;
document.getElementById('status').textContent = `Gracz ${currentPlayer === 'X' ? 'O' : 'X'} wygrał!`;
return;
}
let roundDraw = !board.includes('');
if (roundDraw) {
gameActive = false;
document.getElementById('status').textContent = 'Remis!';
}
}
function restartGame() {
currentPlayer = 'X';
board = ['', '', '', '', '', '', '', '', ''];
gameActive = true;
document.getElementById('status').textContent = `Gracz ${currentPlayer} jest na ruchu`;
renderBoard();
}
// Inicjalizacja gry
renderBoard();
document.getElementById('status').textContent = `Gracz ${currentPlayer} jest na ruchu`;
</script>
</body>
</html>
Instrukcje:
Kopiuj powyższy kod i wklej go do pliku o nazwie tic-tac-toe.html.
Otwórz plik w przeglądarce internetowej (np. Chrome, Firefox).
Gra rozpocznie się automatycznie. Gracz X zaczyna pierwszy.
Klikaj na komórki, aby umieścić swój znak (X lub O).
Wygrywa gracz, który pierwszy ułoży trzy swoje znaki w poziomie, pionie lub skosie.
Remis następuje, gdy wszystkie komórki są zajęte, a żaden gracz nie wygrał.
Nowa gra rozpoczyna się po kliknięciu przycisku "Nowa gra".
Uwaga: Kod jest prosty i nie zawiera zaawansowanych funkcji, takich jak gra z komputerem czy zapis stanu gry. Możesz go jednak rozszerzyć i dostosować do swoich potrzeb.