From 2f33323f5477b50451ad9cc15c2fd169111a51b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Babi=C4=8D?= Date: Sun, 3 Jan 2021 19:32:25 +0100 Subject: [PATCH] implement game over --- src/App.svelte | 48 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/src/App.svelte b/src/App.svelte index 8df9974..bc785ae 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -12,6 +12,7 @@ const jumps = new Graph() let circles = [] + let playing = true const numCircles = 15 for (let i = 0; i < numCircles; i++) { @@ -72,21 +73,28 @@ (_, i) => (circles[i] = circles[i] == dest ? hole : circles[i]) ) + const getDests = curr => { + let dests = [] + + jumps.adjacencyList[curr].forEach(jump => { + const jumpedOver = commonBetween(curr, jump) + + if (circles[jump] == hole && circles[jumpedOver] == pole) { + dests.push(jump) + } + }) + + return dests + } + const change = curr => { const prev = circles.indexOf(pick) if (circles[curr] == pole && prev == -1) { - let destExists = false - jumps.adjacencyList[curr].forEach(jump => { - const jumpedOver = commonBetween(curr, jump) + let dests = getDests(curr) + dests.forEach(d => (circles[d] = dest)) - if (circles[jump] == hole && circles[jumpedOver] == pole) { - circles[jump] = dest - destExists = true - } - }) - - if (destExists) { + if (dests.length > 0) { circles[curr] = pick } @@ -100,8 +108,19 @@ circles[jumpedOver] = hole clearDests() - const remaining = circles.filter(circle => circle == pole) - console.log(remaining.length) + let hints = [] + circles.forEach((circle, i) => { + if (circle == pole) { + const dests = getDests(i) + if (dests.length > 0) { + hints.push(i) + } + } + }) + + if (hints.length == 0) { + playing = false + } return } @@ -192,9 +211,14 @@ .blue { background-color: blue; } + + .playing { + visibility: hidden; + }
+
GAME OVER
{#each circles as _, i}