napisz prostą grę w warcaby html
- User
napisz prostą grę w warcaby html
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
napisz prostą grę w warcaby html
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Oczywiście! Poniżej znajduje się prosty przykład gry w warcaby napisanej w HTML, CSS i JavaScript. Ta gra jest bardzo podstawowa i nie zawiera zaawansowanych funkcji, takich jak AI czy sprawdzanie poprawności ruchów, ale jest to dobry punkt wyjścia.
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Prosta gra w warcaby</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
margin: 0;
}
.board {
display: grid;
grid-template-columns: repeat(8, 60px);
grid-template-rows: repeat(8, 60px);
gap: 2px;
}
.cell {
width: 60px;
height: 60px;
}
.black {
background-color: black;
}
.white {
background-color: white;
}
.piece {
width: 50px;
height: 50px;
border-radius: 50%;
display: flex;
justify-content: center;
align-items: center;
font-size: 24px;
font-weight: bold;
cursor: pointer;
}
.red {
background-color: red;
}
.blue {
background-color: blue;
}
</style>
</head>
<body>
<div class="board" id="board"></div>
<script>
const board = document.getElementById('board');
const cells = [];
let currentPlayer = 'red';
// Tworzenie planszy
for (let row = 0; row < 8; row++) {
for (let col = 0; col < 8; col++) {
const cell = document.createElement('div');
cell.classList.add('cell', (row + col) % 2 === 0 ? 'white' : 'black');
cell.dataset.row = row;
cell.dataset.col = col;
board.appendChild(cell);
cells.push(cell);
}
}
// Umieszczenie pionków
for (let row = 0; row < 3; row++) {
for (let col = 0; col < 8; col++) {
if ((row + col) % 2 !== 0) {
const piece = document.createElement('div');
piece.classList.add('piece', 'red');
piece.dataset.row = row;
piece.dataset.col = col;
cells[row * 8 + col].appendChild(piece);
}
}
}
for (let row = 5; row < 8; row++) {
for (let col = 0; col < 8; col++) {
if ((row + col) % 2 !== 0) {
const piece = document.createElement('div');
piece.classList.add('piece', 'blue');
piece.dataset.row = row;
piece.dataset.col = col;
cells[row * 8 + col].appendChild(piece);
}
}
}
// Obsługa kliknięć
board.addEventListener('click', (event) => {
if (event.target.classList.contains('piece')) {
const piece = event.target;
const row = parseInt(piece.dataset.row);
const col = parseInt(piece.dataset.col);
// Sprawdzenie, czy to pionek aktualnego gracza
if (piece.classList.contains(currentPlayer)) {
// Tutaj można dodać logikę ruchu
console.log(`Pionek ${currentPlayer} na pozycji (${row}, ${col}) został kliknięty.`);
// Zmiana gracza
currentPlayer = currentPlayer === 'red' ? 'blue' : 'red';
}
}
});
</script>
</body>
</html>
Ten kod tworzy prostą planszę do gry w warcaby z pionkami w dwóch kolorach (czerwonym i niebieskim). Kliknięcie na pionek zmienia gracza, ale nie zawiera pełnej logiki ruchów ani sprawdzania poprawności ruchów. Możesz rozbudować ten kod, dodając więcej funkcjonalności, takich jak ruchy pionków, bicia przeciwnika itp.