Oczywiście! Poniżej znajdziesz prostą grę kółko i krzyżyk (Tic-Tac-Toe) w przeglądarce, ale zamiast standardowej planszy 3x3, będzie to plansza 4x4 (16 pól). Gra działa w HTML + CSS + JavaScript i możesz ją łatwo uruchomić – wystarczy zapisać to jako plik .html i otworzyć w przeglądarce.
✅ Zasady gry:
- Gracze na przemian klikają pola, aby postawić "X" lub "O".
- Wygrywa gracz, który pierwszy ustawi 4 znaki w rzędzie – w pionie, poziomie lub na skosie.
- Jeśli wszystkie pola są zajęte i nikt nie wygrał – remis.
🔧 Kod źródłowy
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<title>Kółko i Krzyżyk 4x4</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin-top: 50px;
}
#game {
display: grid;
grid-template-columns: repeat(4, 100px);
grid-gap: 5px;
justify-content: center;
margin: 20px auto;
}
.cell {
width: 100px;
height: 100px;
background-color: #f0f0f0;
font-size: 2.5em;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
border: 2px solid #ccc;
}
button {
padding: 10px 20px;
font-size: 1em;
}
h2 {
margin-top: 20px;
}
</style>
</head>
<body>
<h1>Kółko i Krzyżyk 4x4</h1>
<div id="game"></div>
<h2 id="status">Ruch gracza: X</h2>
<button onclick="resetGame()">Zacznij od nowa</button>
<script>
const gameContainer = document.getElementById('game');
const statusText = document.getElementById('status');
let currentPlayer = 'X';
let gameState = Array(16).fill('');
let gameActive = true;
// Tworzenie pól
function createBoard() {
gameContainer.innerHTML = '';
for (let i = 0; i < 16; i++) {
const cell = document.createElement('div');
cell.classList.add('cell');
cell.dataset.index = i;
cell.addEventListener('click', handleCellClick);
gameContainer.appendChild(cell);
}
}
// Obsługa kliknięcia w pole
function handleCellClick(e) {
const index = e.target.dataset.index;
if (!gameActive || gameState[index] !== '') return;
gameState[index] = currentPlayer;
e.target.textContent = currentPlayer;
if (checkWin(currentPlayer)) {
statusText.textContent = `Gracz ${currentPlayer} wygrał!`;
gameActive = false;
return;
}
if (!gameState.includes('')) {
statusText.textContent = 'Remis!';
gameActive = false;
return;
}
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
statusText.textContent = `Ruch gracza: ${currentPlayer}`;
}
// Sprawdzenie wygranej
function checkWin(player) {
const wins = [
// Rzędy
[0,1,2,3], [4,5,6,7], [8,9,10,11], [12,13,14,15],
// Kolumny
[0,4,8,12], [1,5,9,13], [2,6,10,14], [3,7,11,15],
// Przekątne
[0,5,10,15], [3,6,9,12]
];
return wins.some(combination => {
return combination.every(i => gameState[i] === player);
});
}
// Reset gry
function resetGame() {
gameState = Array(16).fill('');
gameActive = true;
currentPlayer = 'X';
statusText.textContent = `Ruch gracza: ${currentPlayer}`;
createBoard();
}
// Inicjalizacja
createBoard();
</script>
</body>
</html>
📝 Jak korzystać?
- Skopiuj powyższy kod do edytora tekstu.
- Zapisz plik jako np.
tic-tac-toe-4x4.html.
- Otwórz go w dowolnej przeglądarce internetowej.
- Gramy naprzemiennie klikając na pola!
Jeśli chcesz dodać np. AI (komputer), animacje albo lepszy styl – mogę pomóc rozbudować tę grę :)