Dame 2

by Sebastian Kay

HTML

<div id="game-container">
  <div id="status">Spieler 1 am Zug</div>
  <div id="board"></div>
  <div class="buttons">
    <button id="new-game">Neues Spiel</button>
  </div>
</div>
<div id="winMessage"></div>
<div id="setPlayerNamesWrapperBackdrop">
  <div id="setPlayerNamesWrapper">
    <div id="setPlayerNamesInputs">
      <input type="text" id="inputPlayerName1" autocomplete="off" />
      <div id="modeSelect">
        <input type="text" id="inputPlayerName2" autocomplete="off" /><label>
          <input type="checkbox" id="cpuToggle"
        /></label>
      </div>

      <span id="vs">gegen</span>
    </div>
    <button id="savePlayerNames">Los</button>
  </div>
</div>

CSS

@font-face {
	font-family: "Caveat Brush";
	font-style: normal;
	font-weight: 400;
	font-display: swap;
	src: url(https://fonts.gstatic.com/s/caveatbrush/v11/EYq0maZfwr9S9-ETZc3fKXtMWw.ttf) format("truetype");
}

body {
	background-color: rgba(var(--active-background-color), 1);
	/* background-image: radial-gradient(circle, rgba(var(--active-background-color), 1) 0%, color-mix(in srgb, rgba(var(--active-background-color), 1) 60%, black) 100%); */
	background-image: radial-gradient(circle, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0.6) 100%);
	color: #222;
	font-family: Arial, sans-serif;
	margin: 0;
	padding: 0;
	display: flex;
	justify-content: center;
	align-items: center;
	height: 100vh;
	max-height: 100vh;
	width: 100vw;
	max-width: 100vw;
	overflow: hidden;
	transition: background-color 0.6s ease-out;
}

#game-container {
	padding: 20px;
	border-radius: 8px;
	text-align: center;
	position: relative;
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
}

.buttons {
	position: relative;
	z-index: 20;
	display: flex;
	flex-wrap: wrap;
	justify-content: center;
	align-items: center;
	gap: 4px;
	margin-top: 15px;
	#new-game {
		padding: 10px 20px;
		background-color: #555;
		border: none;
		color: #fff;
		font-size: 16px;
		border-radius: 4px;
		cursor: pointer;
	}

	#new-game:hover {
		background-color: #777;
	}
}

#status {
	margin-bottom: 10px;
	font-family: "Caveat Brush", serif;
	font-size: 3rem;
	font-weight: 600;
	color: color-mix(in srgb, rgba(var(--active-background-color), 1) 30%, black);
}

#board {
	display: grid;
	grid-template-columns: repeat(8, 1fr);
	gap: 2px;
	margin: 0;
	background-color: rgba(36, 36, 36, 1);
	border: none;
	border-radius: 0.35rem;
	overflow: hidden;
	padding: 8px;
	width: 90%;
	max-height: 90vw;
}

@media screen and (orientation: landscape) {
	#game-container {
		width: 100vh;
		max-width: 100vh;
		aspect-ratio: 4 / 3;
	}

	#board {
		display: grid;
		grid-template-columns: repeat(8, 1fr);
		gap:...

JavaScript

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 +...