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.
Poniżej znajdziesz prostą grę w warcaby napisaną w HTML, CSS i JavaScript. Gra jest bardzo podstawowa — plansza 8x8, bez skoków ani bic z przepisaniem, ale zawiera logikę ruchu pionków (tylko do przodu) i podstawowe zaznaczenie aktywnej figury.
index.html<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<title>Proste Warcaby</title>
<style>
body {
font-family: sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
}
table {
border-collapse: collapse;
}
td {
width: 60px;
height: 60px;
text-align: center;
vertical-align: middle;
font-size: 30px;
cursor: pointer;
}
.black {
background-color: #769656;
}
.white {
background-color: #eeeed2;
}
.selected {
outline: 3px solid red;
}
</style>
</head>
<body>
<script>
const boardSize = 8;
let selectedCell = null;
// Tworzenie planszy
function createBoard() {
const board = document.createElement('table');
let isBlack = true;
for (let row = 0; row < boardSize; row++) {
const tr = document.createElement('tr');
for (let col = 0; col < boardSize; col++) {
const td = document.createElement('td');
td.classList.add(isBlack ? 'black' : 'white');
// Dodanie pionka
if ((row + col) % 2 === 1 && row < 3) {
td.textContent = '⚫'; // Pionki gracza 1
} else if ((row + col) % 2 === 1 && row >= 5) {
td.textContent = '⚪'; // Pionki gracza 2
}
td.addEventListener('click', () => handleMove(row, col, td));
tr.appendChild(td);
isBlack = !isBlack;
}
board.appendChild(tr);
isBlack = !isBlack;
}
document.body.appendChild(board);
}
// Obsługa kliknięcia na pole
function handleMove(row, col, td) {
// Jeśli kliknięto na pustą komórkę i wcześniej wybrano figurę
if (!td.textContent && selectedCell) {
const fromRow = parseInt(selectedCell.dataset.row);
const fromCol = parseInt(selectedCell.dataset.col);
// Sprawdzenie poprawności ruchu (tylko do przodu)
const direction = selectedCell.textContent === '⚫' ? 1 : -1;
if (Math.abs(col - fromCol) === 1 && row - fromRow === direction) {
td.textContent = selectedCell.textContent;
selectedCell.textContent = '';
deselect();
return;
}
}
// Zaznacz lub odznacz figurę
if (td.textContent) {
deselect();
selectedCell = td;
selectedCell.dataset.row = row;
selectedCell.dataset.col = col;
selectedCell.classList.add('selected');
}
}
// Usunięcie zaznaczenia
function deselect() {
if (selectedCell) {
selectedCell.classList.remove('selected');
selectedCell = null;
}
}
createBoard();
</script>
</body>
</html>
#769656) mają pionki:
Chcesz wersję z pełną logiką? Napisz, a rozbuduję grę!