Spaces:
Running
Running
File size: 7,112 Bytes
073c4dc 52b4b86 073c4dc f45f133 073c4dc f45f133 073c4dc f45f133 073c4dc f45f133 073c4dc f45f133 073c4dc f45f133 073c4dc f45f133 073c4dc f45f133 073c4dc f45f133 073c4dc f45f133 073c4dc f45f133 073c4dc f45f133 073c4dc f45f133 073c4dc f45f133 073c4dc f45f133 073c4dc f45f133 073c4dc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
<!DOCTYPE html>
<html>
<head>
<title>Chess Game</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background: #f0f0f0;
}
#chessboard {
display: grid;
grid-template-columns: repeat(8, 60px);
grid-template-rows: repeat(8, 60px);
border: 2px solid #333;
}
.square {
width: 60px;
height: 60px;
display: flex;
justify-content: center;
align-items: center;
font-size: 40px;
cursor: pointer;
position: relative;
}
.white {
background-color: #EEEED2;
}
.black {
background-color: #769656;
}
.highlight {
background-color: rgba(255, 255, 0, 0.4) !important;
}
.possible-move {
position: relative;
}
.possible-move::before {
content: '';
position: absolute;
width: 20px;
height: 20px;
background-color: rgba(0,0,0,0.2);
border-radius: 50%;
}
.piece {
user-select: none;
}
.white-piece {
color: white;
text-shadow: 1px 1px 2px black;
}
.black-piece {
color: black;
}
</style>
</head>
<body>
<div id="chessboard"></div>
<script>
let selectedPiece = null;
let currentTurn = 'white';
const pieces = {
'white': {
'king': 'β',
'queen': 'β',
'rook': 'β',
'bishop': 'β',
'knight': 'β',
'pawn': 'β'
},
'black': {
'king': 'β',
'queen': 'β',
'rook': 'β',
'bishop': 'β',
'knight': 'β',
'pawn': 'β'
}
};
function createBoard() {
const board = document.getElementById('chessboard');
for (let row = 0; row < 8; row++) {
for (let col = 0; col < 8; col++) {
const square = document.createElement('div');
square.classList.add('square');
square.classList.add((row + col) % 2 ? 'black' : 'white');
square.dataset.row = row;
square.dataset.col = col;
// Add pieces
if (row < 2 || row > 5) {
const color = row < 2 ? 'black' : 'white';
let piece;
if (row === 1 || row === 6) {
piece = 'pawn';
} else {
if (col === 0 || col === 7) piece = 'rook';
if (col === 1 || col === 6) piece = 'knight';
if (col === 2 || col === 5) piece = 'bishop';
if (col === 3) piece = 'queen';
if (col === 4) piece = 'king';
}
if (piece) {
square.innerHTML = pieces[color][piece];
square.dataset.piece = piece;
square.dataset.color = color;
square.classList.add('piece', `${color}-piece`);
}
}
square.addEventListener('click', handleClick);
board.appendChild(square);
}
}
}
function clearHighlights() {
document.querySelectorAll('.square').forEach(square => {
square.classList.remove('highlight', 'possible-move');
});
}
function showPossibleMoves(square) {
const piece = square.dataset.piece;
const row = parseInt(square.dataset.row);
const col = parseInt(square.dataset.col);
const color = square.dataset.color;
if (piece === 'pawn') {
const direction = color === 'white' ? -1 : 1;
const startRow = color === 'white' ? 6 : 1;
// Forward moves
let nextRow = row + direction;
if (nextRow >= 0 && nextRow < 8) {
const forwardSquare = document.querySelector(`[data-row="${nextRow}"][data-col="${col}"]`);
if (!forwardSquare.dataset.piece) {
forwardSquare.classList.add('possible-move');
// Initial two-square move
if (row === startRow) {
const twoAheadSquare = document.querySelector(`[data-row="${row + 2 * direction}"][data-col="${col}"]`);
if (!twoAheadSquare.dataset.piece) {
twoAheadSquare.classList.add('possible-move');
}
}
}
}
// Captures
[-1, 1].forEach(offset => {
const captureSquare = document.querySelector(`[data-row="${nextRow}"][data-col="${col + offset}"]`);
if (captureSquare && captureSquare.dataset.piece && captureSquare.dataset.color !== color) {
captureSquare.classList.add('possible-move');
}
});
}
}
function handleClick(event) {
const square = event.target;
const piece = square.dataset.piece;
const color = square.dataset.color;
clearHighlights();
if (selectedPiece) {
if (square.classList.contains('possible-move')) {
// Move piece
square.innerHTML = selectedPiece.innerHTML;
square.dataset.piece = selectedPiece.dataset.piece;
square.dataset.color = selectedPiece.dataset.color;
square.classList.add('piece', `${selectedPiece.dataset.color}-piece`);
selectedPiece.innerHTML = '';
selectedPiece.removeAttribute('data-piece');
selectedPiece.removeAttribute('data-color');
selectedPiece.classList.remove('piece', 'white-piece', 'black-piece');
currentTurn = currentTurn === 'white' ? 'black' : 'white';
}
selectedPiece = null;
} else if (piece && color === currentTurn) {
selectedPiece = square;
square.classList.add('highlight');
showPossibleMoves(square);
}
}
createBoard();
</script>
</body>
</html> |