Single player mode

This commit is contained in:
2026-02-10 08:01:39 +01:00
parent 526ee55699
commit 5e8fad02b0

View File

@@ -272,7 +272,10 @@
<div id="lobby" class="main-card">
<h1 style="text-align: center; margin: 0; border-bottom: 4px solid var(--black); padding-bottom: 10px;">WOLF &
SCHAFE</h1>
<button class="btn btn-green" style="font-size: 1.1rem;" onclick="createRoom()">+ NEUES SPIEL</button>
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 10px;">
<button class="btn btn-green" onclick="createRoom()">+ ONLINE</button>
<button class="btn" onclick="startLocal()">+ LOKAL</button>
</div>
<h3 style="margin: 15px 0 5px 0;">Offene Räume:</h3>
<div id="roomList">Suche Räume...</div>
</div>
@@ -280,7 +283,7 @@
<div id="game" class="main-card hidden">
<div class="info-bar">
<div>
<div style="font-size: 0.7rem; font-weight: 900; display: flex; align-items: center;">
<div id="roomInfo" style="font-size: 0.7rem; font-weight: 900; display: flex; align-items: center;">
<span id="dot" class="conn-dot"></span> RAUM: <span id="roomCode">...</span>
</div>
<div id="idBadge" class="id-box">...</div>
@@ -298,7 +301,7 @@
<div class="controls">
<button class="btn" id="copyBtn">Link Kopieren</button>
<button class="btn" onclick="ws.send(JSON.stringify({type:'restart'}))">Restart</button>
<button class="btn" id="restartBtn">Restart</button>
<button class="btn" onclick="location.href='/'">Lobby</button>
</div>
</div>
@@ -311,6 +314,7 @@
const params = new URLSearchParams(window.location.search);
let roomId = params.get("room");
let isLocal = params.get("mode") === "local";
let ws, myRole, state, selected = null;
const pos = { 0: [80, 210], 1: [180, 110], 2: [180, 210], 3: [180, 310], 4: [280, 110], 5: [280, 210], 6: [280, 310], 7: [380, 110], 8: [380, 210], 9: [380, 310], 10: [480, 210] };
@@ -318,13 +322,24 @@
const bWolf = { 0: [1, 2, 3], 1: [0, 2, 4, 5], 2: [0, 1, 3, 5], 3: [0, 2, 5, 6], 4: [1, 5, 7], 5: [1, 2, 3, 4, 6, 7, 8, 9], 6: [3, 5, 9], 7: [4, 5, 8, 10], 8: [5, 7, 9, 10], 9: [5, 6, 8, 10], 10: [7, 8, 9] };
const bSheep = { 0: [1, 2, 3], 1: [2, 4, 5], 2: [1, 3, 5], 3: [2, 5, 6], 4: [5, 7], 5: [4, 6, 7, 8, 9], 6: [5, 9], 7: [8, 10], 8: [7, 9, 10], 9: [8, 10], 10: [] };
function initLocalState() {
state = { wolf: 5, sheep: [0, 1, 3], turn: "sheep", moveCount: 0, winner: null, lastMove: null };
myRole = "sheep"; // Initial role, will toggle
document.getElementById("lobby").classList.add("hidden");
document.getElementById("game").classList.remove("hidden");
document.getElementById("roomInfo").style.visibility = "hidden";
document.getElementById("copyBtn").disabled = true;
document.getElementById("copyBtn").style.opacity = "0.5";
render();
}
function connect() {
if (isLocal) return initLocalState();
const protocol = location.protocol === "https:" ? "wss" : "ws";
ws = new WebSocket(`${protocol}://${location.host}?room=${roomId}&sessionId=${sessionId}`);
ws.onopen = () => document.getElementById("dot").classList.add("conn-online");
ws.onclose = () => document.getElementById("dot").classList.remove("conn-online");
ws.onmessage = e => {
const msg = JSON.parse(e.data);
if (msg.type === "room_list") updateRoomList(msg.rooms);
@@ -343,8 +358,30 @@
};
}
// Logic for local movement
function handleLocalMove(from, to) {
const allowed = (state.turn === "wolf") ? bWolf[from] : bSheep[from];
if (!allowed?.includes(to) || state.sheep.includes(to) || state.wolf === to) return;
if (state.turn === "wolf") { state.wolf = to; state.turn = "sheep"; }
else { state.sheep[state.sheep.indexOf(from)] = to; state.turn = "wolf"; }
state.moveCount++;
state.winner = checkWinConditions(state);
render();
}
function checkWinConditions(g) {
if (g.wolf === 0) return "wolf";
if (g.moveCount >= 40) return "wolf";
const canWolfMove = bWolf[g.wolf].some(to => !g.sheep.includes(to));
if (!canWolfMove) return "sheep";
return null;
}
function updateRoomList(rooms) {
const list = document.getElementById("roomList");
if (!list) return;
list.innerHTML = rooms.length === 0 ? "<p style='text-align:center'>Keine aktiven Räume.</p>" : "";
rooms.forEach(r => {
const div = document.createElement("div");
@@ -360,26 +397,13 @@
window.location.search = `?room=${id}`;
}
function showToast() {
const toast = document.getElementById("toast");
toast.className = "show";
setTimeout(() => { toast.className = ""; }, 3000);
function startLocal() {
window.location.search = `?mode=local`;
}
document.getElementById("copyBtn").onclick = async () => {
const url = window.location.href;
try {
await navigator.clipboard.writeText(url);
showToast();
} catch (err) {
const t = document.createElement("textarea");
t.value = url;
document.body.appendChild(t);
t.select();
document.execCommand('copy');
document.body.removeChild(t);
showToast();
}
document.getElementById("restartBtn").onclick = () => {
if (isLocal) initLocalState();
else ws.send(JSON.stringify({ type: 'restart' }));
};
function render() {
@@ -392,28 +416,39 @@
l.setAttribute("class", "edge"); svg.appendChild(l);
});
// In local mode, "your role" is always the current turn's role
const effectiveRole = isLocal ? state.turn : myRole;
const badge = document.getElementById("idBadge");
badge.innerText = myRole === "wolf" ? "WOLF" : "SCHAFE";
badge.className = `id-box is-${myRole}`;
badge.innerText = effectiveRole === "wolf" ? "WOLF" : "SCHAFE";
badge.className = `id-box is-${effectiveRole}`;
const statusEl = document.getElementById("status");
if (state.winner) statusEl.innerHTML = `SIEG: ${state.winner.toUpperCase()}!`;
else statusEl.innerHTML = `${state.turn === myRole ? 'DEIN ZUG' : 'WARTEN...'}<br><small>ZUG ${state.moveCount}/40</small>`;
else statusEl.innerHTML = `${(isLocal || state.turn === myRole) ? 'DEIN ZUG' : 'WARTEN...'}<br><small>ZUG ${state.moveCount}/40</small>`;
for (let i = 0; i <= 10; i++) {
const [x, y] = pos[i];
const isWolf = state.wolf === i;
const isSheep = state.sheep.includes(i);
const type = isWolf ? "wolf" : (isSheep ? "sheep" : "empty");
const isPossible = selected !== null && (myRole === "wolf" ? bWolf[selected] : bSheep[selected]).includes(i) && !isWolf && !isSheep;
const isPossible = selected !== null && (effectiveRole === "wolf" ? bWolf[selected] : bSheep[selected]).includes(i) && !isWolf && !isSheep;
const g = document.createElementNS("http://www.w3.org/2000/svg", "g");
g.onclick = () => {
if (state.turn !== myRole || state.winner) return;
const isMine = (myRole === "wolf" && isWolf) || (myRole === "sheep" && isSheep);
if (isMine) selected = i;
else if (isPossible) { ws.send(JSON.stringify({ from: selected, to: i })); selected = null; }
else selected = null;
if (!isLocal && (state.turn !== myRole || state.winner)) return;
if (isLocal && state.winner) return;
const isMine = (effectiveRole === "wolf" && isWolf) || (effectiveRole === "sheep" && isSheep);
if (isMine) {
selected = i;
} else if (isPossible) {
if (isLocal) handleLocalMove(selected, i);
else ws.send(JSON.stringify({ from: selected, to: i }));
selected = null;
} else {
selected = null;
}
render();
};
@@ -430,6 +465,14 @@
svg.appendChild(g);
}
}
// Copy logic
document.getElementById("copyBtn").onclick = async () => {
const url = window.location.href.split('&')[0].split('?')[0] + `?room=${roomId}`;
try { await navigator.clipboard.writeText(url); showToast(); } catch (err) { }
};
function showToast() { const t = document.getElementById("toast"); t.className = "show"; setTimeout(() => { t.className = ""; }, 3000); }
connect();
</script>
</body>