Spaces:
Running
Running
File size: 12,852 Bytes
de7d29f |
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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
const boardSize = 8;
let board = [],
currentPlayer = 1,û
selectedPiece = null,
validMoves = [],
gameRunning = false,
vsCPU = false;
const playerColors = {
1: "168, 208, 230",
2: "246, 171, 182",
};
let players = {
1: { name: "Spieler 1", color: "168, 208, 230" },
2: { name: "Spieler 2", color: "246, 171, 182" },
};
const captureHighlightColors = {
1: "128, 168, 190",
2: "206, 131, 142",
};
const pieceColors = {
1: "77, 129, 199",
2: "214, 106, 139",
};
const body = document.body;
body.style.setProperty("--player1-color", players[1].color);
body.style.setProperty("--player2-color", players[2].color);
const inputPlayerName1 = document.getElementById("inputPlayerName1");
inputPlayerName1.value = players[1].name;
const inputPlayerName2 = document.getElementById("inputPlayerName2");
inputPlayerName2.value = players[2].name;
const savePlayerNamesButton = document.getElementById("savePlayerNames");
const boardElement = document.getElementById("board");
const statusElement = document.getElementById("status");
const newGameButton = document.getElementById("new-game");
const winMessageElement = document.getElementById("winMessage");
const cpuToggle = document.getElementById("cpuToggle");
function inBounds(row, col) {
return row >= 0 && row < boardSize && col >= 0 && col < boardSize;
}
function isEmpty(row, col) {
return board[row][col] === 0;
}
function isOpponent(piece, player) {
return piece !== 0 && (player === 1 ? piece === 2 || piece === 4 : piece === 1 || piece === 3);
}
function isPlayerPiece(piece, player) {
return player === 1 ? piece === 1 || piece === 3 : player === 2 && (piece === 2 || piece === 4);
}
function isKing(piece) {
return piece === 3 || piece === 4;
}
function initGame() {
board = Array.from({ length: boardSize }, () => Array(boardSize).fill(0));
for (let row = 0; row < 3; row++) {
for (let col = 0; col < boardSize; col++) {
if ((row + col) % 2 === 1) board[row][col] = 1;
}
}
for (let row = boardSize - 3; row < boardSize; row++) {
for (let col = 0; col < boardSize; col++) {
if ((row + col) % 2 === 1) board[row][col] = 2;
}
}
currentPlayer = 1;
selectedPiece = null;
validMoves = [];
gameRunning = true;
winMessageElement.style.display = "none";
newGameButton.style.display = "inline-block";
vsCPU = cpuToggle.checked;
cpuToggle.disabled = false;
updateStatus();
updateBackground();
renderBoard();
if (vsCPU && currentPlayer === 2) {
setTimeout(cpuMove, 500);
}
}
function updateStatus() {
statusElement.textContent = `${players[currentPlayer].name} ist am Zug`;
}
function updateBackground() {
body.style.setProperty("--active-background-color", players[currentPlayer].color);
}
function getValidMoves(row, col) {
const moves = [];
const piece = board[row][col];
if (!isPlayerPiece(piece, currentPlayer)) return moves;
const directions = isKing(piece)
? [[1, 1], [1, -1], [-1, 1], [-1, -1]]
: currentPlayer === 1
? [[1, -1], [1, 1]]
: [[-1, -1], [-1, 1]];
directions.forEach(([dr, dc]) => {
const newRow = row + dr, newCol = col + dc;
if (inBounds(newRow, newCol) && isEmpty(newRow, newCol)) {
moves.push({ row: newRow, col: newCol, capture: false });
}
const captureRow = row + 2 * dr, captureCol = col + 2 * dc;
if (inBounds(newRow, newCol) && inBounds(captureRow, captureCol) && isOpponent(board[newRow][newCol], currentPlayer) && isEmpty(captureRow, captureCol)) {
moves.push({ row: captureRow, col: captureCol, capture: true, captured: { row: newRow, col: newCol } });
}
});
return moves;
}
function getValidMovesForPlayer(row, col, player) {
const moves = [];
const piece = board[row][col];
if (!isPlayerPiece(piece, player)) return moves;
const directions = isKing(piece)
? [[1, 1], [1, -1], [-1, 1], [-1, -1]]
: player === 1
? [[1, -1], [1, 1]]
: [[-1, -1], [-1, 1]];
directions.forEach(([dr, dc]) => {
const newRow = row + dr, newCol = col + dc;
if (inBounds(newRow, newCol) && isEmpty(newRow, newCol)) {
moves.push({ row: newRow, col: newCol, capture: false });
}
const captureRow = row + 2 * dr, captureCol = col + 2 * dc;
if (inBounds(newRow, newCol) && inBounds(captureRow, captureCol) && isOpponent(board[newRow][newCol], player) && isEmpty(captureRow, captureCol)) {
moves.push({ row: captureRow, col: captureCol, capture: true, captured: { row: newRow, col: newCol } });
}
});
return moves;
}
function hasAnyMoves(player) {
for (let row = 0; row < boardSize; row++) {
for (let col = 0; col < boardSize; col++) {
if (isPlayerPiece(board[row][col], player) && getValidMovesForPlayer(row, col, player).length > 0) {
return true;
}
}
}
return false;
}
function hasAnyCapture() {
for (let row = 0; row < boardSize; row++) {
for (let col = 0; col < boardSize; col++) {
if (isPlayerPiece(board[row][col], currentPlayer) && getValidMoves(row, col).some(move => move.capture)) {
return true;
}
}
}
return false;
}
function makeMove(fromRow, fromCol, move) {
board[move.row][move.col] = board[fromRow][fromCol];
board[fromRow][fromCol] = 0;
if (move.capture) {
const captured = move.captured;
board[captured.row][captured.col] = 0;
}
if (currentPlayer === 1 && move.row === boardSize - 1 && board[move.row][move.col] === 1) {
board[move.row][move.col] = 3;
} else if (currentPlayer === 2 && move.row === 0 && board[move.row][move.col] === 2) {
board[move.row][move.col] = 4;
}
}
function switchPlayer() {
currentPlayer = currentPlayer === 1 ? 2 : 1;
selectedPiece = null;
renderBoard();
validMoves = [];
updateStatus();
updateBackground();
if (hasAnyMoves(currentPlayer)) {
if (vsCPU && currentPlayer === 2) {
setTimeout(cpuMove, 500);
}
} else {
endGame(currentPlayer === 1 ? 2 : 1);
}
}
function cpuMove() {
let possibleMoves = [];
const mustCapture = hasAnyCapture();
for (let row = 0; row < boardSize; row++) {
for (let col = 0; col < boardSize; col++) {
if (isPlayerPiece(board[row][col], currentPlayer)) {
let moves = getValidMoves(row, col);
if (mustCapture) {
moves = moves.filter(move => move.capture);
}
if (moves.length > 0) {
possibleMoves.push({ row, col, moves });
}
}
}
}
if (possibleMoves.length === 0) {
endGame(currentPlayer === 1 ? 2 : 1);
return;
}
const { row, col, moves } = possibleMoves[Math.floor(Math.random() * possibleMoves.length)];
const move = moves[Math.floor(Math.random() * moves.length)];
setTimeout(() => {
makeMove(row, col, move);
renderBoard();
if (move.capture) {
cpuCaptureChain(move.row, move.col);
} else {
checkWin() || switchPlayer();
}
}, 500);
}
function cpuCaptureChain(row, col) {
let captureMoves = getValidMoves(row, col).filter(move => move.capture);
if (captureMoves.length > 0) {
const move = captureMoves[Math.floor(Math.random() * captureMoves.length)];
setTimeout(() => {
makeMove(row, col, move);
renderBoard();
cpuCaptureChain(move.row, move.col);
}, 500);
} else {
checkWin() || switchPlayer();
}
}
function checkWin() {
let player1Pieces = 0, player2Pieces = 0;
for (let row = 0; row < boardSize; row++) {
for (let col = 0; col < boardSize; col++) {
if (board[row][col] === 1 || board[row][col] === 3) {
player1Pieces++;
} else if (board[row][col] === 2 || board[row][col] === 4) {
player2Pieces++;
}
}
}
if (player1Pieces === 0) {
endGame(2);
return true;
} else if (player2Pieces === 0) {
endGame(1);
return true;
} else if (!hasAnyMoves(currentPlayer === 1 ? 2 : 1)) {
endGame(currentPlayer);
return true;
}
return false;
}
function endGame(winner) {
gameRunning = false;
newGameButton.style.display = "none";
winMessageElement.textContent = `Spieler ${winner} hat gewonnen! (Zum Neustart klicken)`;
winMessageElement.style.display = "block";
winMessageElement.style.backgroundColor = `rgba(${players[winner].color}, 1.0)`;
cpuToggle.disabled = false;
winMessageElement.addEventListener("click", initGame, { once: true });
}
function renderBoard() {
boardElement.innerHTML = "";
for (let row = 0; row < boardSize; row++) {
for (let col = 0; col < boardSize; col++) {
const cell = document.createElement("div");
cell.classList.add("cell");
cell.classList.add((row + col) % 2 === 0 ? "light" : "dark");
cell.dataset.row = row;
cell.dataset.col = col;
if (validMoves.some(move => move.row === row && move.col === col)) {
cell.classList.add("highlight");
}
if (selectedPiece && selectedPiece.row === row && selectedPiece.col === col) {
cell.classList.add("selected");
}
const piece = board[row][col];
if (piece !== 0) {
const pieceElement = document.createElement("div");
pieceElement.classList.add("piece");
pieceElement.style.setProperty("--player-color", players[piece % 2 === 0 ? 2 : 1].color);
if (isKing(piece)) {
pieceElement.classList.add("king");
}
if (getValidMoves(row, col).some(move => move.capture)) {
pieceElement.classList.add("must-capture");
} else {
pieceElement.classList.remove("must-capture");
}
cell.appendChild(pieceElement);
}
cell.addEventListener("click", handleCellClick);
boardElement.appendChild(cell);
}
}
}
function handleCellClick(event) {
if (!gameRunning || (vsCPU && currentPlayer === 2)) return;
const row = parseInt(event.currentTarget.dataset.row);
const col = parseInt(event.currentTarget.dataset.col);
if (selectedPiece) {
const move = validMoves.find(move => move.row === row && move.col === col);
if (move) {
makeMove(selectedPiece.row, selectedPiece.col, move);
renderBoard();
if (move.capture) {
const captureMoves = getValidMoves(move.row, move.col).filter(move => move.capture);
if (captureMoves.length > 0) {
selectedPiece = { row: move.row, col: move.col };
validMoves = captureMoves;
return;
}
}
selectedPiece = null;
validMoves = [];
checkWin() || switchPlayer();
return;
}
}
if (isPlayerPiece(board[row][col], currentPlayer)) {
if (hasAnyCapture()) {
const captureMoves = getValidMoves(row, col).filter(move => move.capture);
if (captureMoves.length === 0) return;
selectedPiece = { row, col };
validMoves = captureMoves;
} else {
selectedPiece = { row, col };
validMoves = getValidMoves(row, col);
}
renderBoard();
}
}
function openSavePlayerNamesDialog() {
if (!cpuToggle.checked) {
inputPlayerName2.value = inputPlayerName2.value === "CPU" ? "Spieler 2" : inputPlayerName2.value;
} else {
inputPlayerName2.value = "CPU";
}
inputPlayerName2.disabled = cpuToggle.checked;
document.getElementById("setPlayerNamesWrapperBackdrop").style.display = "block";
}
savePlayerNamesButton.addEventListener("click", () => {
players[1].name = inputPlayerName1.value.length > 0 ? inputPlayerName1.value : "Spieler 1";
players[2].name = inputPlayerName2.value.length > 0 ? inputPlayerName2.value : "Spieler 2";
document.getElementById("setPlayerNamesWrapperBackdrop").style.display = "none";
cpuToggle.disabled = false;
initGame();
});
newGameButton.addEventListener("click", openSavePlayerNamesDialog);
cpuToggle.addEventListener("click", openSavePlayerNamesDialog);
initGame(); |