From f5da6c01aa88e1a9a714eda76846399fd50d6fe1 Mon Sep 17 00:00:00 2001 From: Gleb Koval Date: Sat, 13 Aug 2022 21:04:12 +0000 Subject: [PATCH] Connect four initial --- frontend/package-lock.json | 14 +- frontend/package.json | 2 +- .../connect-four/ConnectFourBoard.svelte | 71 ++++ .../connect-four/ConnectFourRed.svelte | 12 + .../connect-four/ConnectFourYellow.svelte | 12 + .../src/lib/components/o-x/OXNoneIcon.svelte | 8 - frontend/src/routes/__layout.svelte | 1 + frontend/src/routes/connect-four.svelte | 323 ++++++++++++++++++ frontend/src/routes/o-x.svelte | 16 +- frontend/vite.config.js | 2 +- 10 files changed, 432 insertions(+), 29 deletions(-) create mode 100644 frontend/src/lib/components/connect-four/ConnectFourBoard.svelte create mode 100644 frontend/src/lib/components/connect-four/ConnectFourRed.svelte create mode 100644 frontend/src/lib/components/connect-four/ConnectFourYellow.svelte delete mode 100644 frontend/src/lib/components/o-x/OXNoneIcon.svelte create mode 100644 frontend/src/routes/connect-four.svelte diff --git a/frontend/package-lock.json b/frontend/package-lock.json index 383940e..fb5304f 100644 --- a/frontend/package-lock.json +++ b/frontend/package-lock.json @@ -8,7 +8,7 @@ "name": "frontend", "version": "0.0.1", "dependencies": { - "@game-algorithms/connect-four-rust": "^0.0.1", + "@game-algorithms/connect-four-rust": "^0.0.2", "@game-algorithms/o-x-rust": "^0.1.0" }, "devDependencies": { @@ -82,9 +82,9 @@ } }, "node_modules/@game-algorithms/connect-four-rust": { - "version": "0.0.1", - "resolved": "https://git.koval.net/api/packages/cyclane/npm/%40game-algorithms%2Fconnect-four-rust/-/0.0.1/connect-four-rust-0.0.1.tgz", - "integrity": "sha512-XFVCupzmxi6lL9jfrETenwQwo7JDvGby8PB2jOfRuRpmGBZwgx9GQvc9euw+LwlGCXUhN8zYqKIdoU7IDfd6ng==", + "version": "0.0.2", + "resolved": "https://git.koval.net/api/packages/cyclane/npm/%40game-algorithms%2Fconnect-four-rust/-/0.0.2/connect-four-rust-0.0.2.tgz", + "integrity": "sha512-wJ10cBbrMFVqv6gjrWEGNt0kBk1BoDtz8x3VFpiZjcwvarIGh0y1x0F+VnKJiYNoDh3ozdtaeiOFViDluH3xOw==", "license": "GNU GPLv3" }, "node_modules/@game-algorithms/o-x-rust": { @@ -2935,9 +2935,9 @@ } }, "@game-algorithms/connect-four-rust": { - "version": "0.0.1", - "resolved": "https://git.koval.net/api/packages/cyclane/npm/%40game-algorithms%2Fconnect-four-rust/-/0.0.1/connect-four-rust-0.0.1.tgz", - "integrity": "sha512-XFVCupzmxi6lL9jfrETenwQwo7JDvGby8PB2jOfRuRpmGBZwgx9GQvc9euw+LwlGCXUhN8zYqKIdoU7IDfd6ng==" + "version": "0.0.2", + "resolved": "https://git.koval.net/api/packages/cyclane/npm/%40game-algorithms%2Fconnect-four-rust/-/0.0.2/connect-four-rust-0.0.2.tgz", + "integrity": "sha512-wJ10cBbrMFVqv6gjrWEGNt0kBk1BoDtz8x3VFpiZjcwvarIGh0y1x0F+VnKJiYNoDh3ozdtaeiOFViDluH3xOw==" }, "@game-algorithms/o-x-rust": { "version": "0.1.0", diff --git a/frontend/package.json b/frontend/package.json index 08201df..acf44f4 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -33,6 +33,6 @@ "type": "module", "dependencies": { "@game-algorithms/o-x-rust": "^0.1.0", - "@game-algorithms/connect-four-rust": "^0.0.1" + "@game-algorithms/connect-four-rust": "^0.0.2" } } diff --git a/frontend/src/lib/components/connect-four/ConnectFourBoard.svelte b/frontend/src/lib/components/connect-four/ConnectFourBoard.svelte new file mode 100644 index 0000000..5ee3ff2 --- /dev/null +++ b/frontend/src/lib/components/connect-four/ConnectFourBoard.svelte @@ -0,0 +1,71 @@ + + +
+ {#each board as b, idx} + + {/each} +
+ + + diff --git a/frontend/src/lib/components/connect-four/ConnectFourRed.svelte b/frontend/src/lib/components/connect-four/ConnectFourRed.svelte new file mode 100644 index 0000000..b7911cc --- /dev/null +++ b/frontend/src/lib/components/connect-four/ConnectFourRed.svelte @@ -0,0 +1,12 @@ +
+ + diff --git a/frontend/src/lib/components/connect-four/ConnectFourYellow.svelte b/frontend/src/lib/components/connect-four/ConnectFourYellow.svelte new file mode 100644 index 0000000..2528edd --- /dev/null +++ b/frontend/src/lib/components/connect-four/ConnectFourYellow.svelte @@ -0,0 +1,12 @@ +
+ + diff --git a/frontend/src/lib/components/o-x/OXNoneIcon.svelte b/frontend/src/lib/components/o-x/OXNoneIcon.svelte deleted file mode 100644 index e5630ab..0000000 --- a/frontend/src/lib/components/o-x/OXNoneIcon.svelte +++ /dev/null @@ -1,8 +0,0 @@ -
- - diff --git a/frontend/src/routes/__layout.svelte b/frontend/src/routes/__layout.svelte index 01fef88..5c0d494 100644 --- a/frontend/src/routes/__layout.svelte +++ b/frontend/src/routes/__layout.svelte @@ -29,6 +29,7 @@ + dark = !dark}/> diff --git a/frontend/src/routes/connect-four.svelte b/frontend/src/routes/connect-four.svelte new file mode 100644 index 0000000..5b3dc33 --- /dev/null +++ b/frontend/src/routes/connect-four.svelte @@ -0,0 +1,323 @@ + + + + Connect Four + + + + + +

Connect Four

+

+ This is a connect four bot, with custom board and game options using various algorithms. +

+ + mm: minimax + mm+d: minimax with depth + +
+ + + + +
+
+ + + + +
+
+ + + + +
+ +
+
+
+ + +

Board

+ {#if loaded && board.length !== 0} + { + const r = getLowestEmptyRow(board, w, event.detail.col); + board[r*w + event.detail.col] = human; + setTimeout(completeTurn); + }} + /> + {:else} +

{loaded ? status : "Loading..."}

+ {/if} +
+
+
+ + +

Scores

+

+ View scores for each potential move here. + Cells are numbered going left-to-right, top-to-bottom. + The table headers show the cell numbers. +

+ + +
+ + + + +
+ {#if showPreviousScores || showCurrentScores} + i < w ) + .map((_, c) => { + return { + key: c.toString(), + value: (c + 1).toString() + }; + }) + ]} + rows={[ + ...(showPreviousScores ? + scores.slice(0, -1).map(scoreToRows(showScoresFor)) : [] + ), + ...(showCurrentScores && scores.length !== 0 ? + [scoreToRows(showScoresFor, "Now")(scores.slice(-1)[0], scores.length)] : [] + ) + ]} + /> + {/if} +
+
+
+ + diff --git a/frontend/src/routes/o-x.svelte b/frontend/src/routes/o-x.svelte index d9cbdaf..dcbafb8 100644 --- a/frontend/src/routes/o-x.svelte +++ b/frontend/src/routes/o-x.svelte @@ -13,15 +13,11 @@ } from "carbon-components-svelte"; import wasmInit, { count_empty, find_winner, get_score, predict } from "@game-algorithms/o-x-rust"; import OXBoard from "$lib/components/o-x/OXBoard.svelte"; - import OXNoneIcon from "$lib/components/o-x/OXNoneIcon.svelte"; import OXOIcon from "$lib/components/o-x/OXOIcon.svelte"; import OXXIcon from "$lib/components/o-x/OXXIcon.svelte"; import { onMount } from "svelte"; - let board: number[] = []; - for (let i = 0;i < 9;i++) { - board.push(0); - } + let board = Array(9).fill(0); let turn = -1; let playAs = "X"; @@ -94,7 +90,7 @@ bot: number, algorithm: string ): [number | null, number | null][] { - return board.flat().map((v, i) => { + return board.map((v, i) => { if (v === 0) { const humanCopy = board.slice(); humanCopy[i] = human; @@ -139,7 +135,7 @@ function scoreToRows(player: string, moveOverride?: string) { return (score: [number | null, number | null][], i: number) => { const rowScores: Record = {}; - for (let cell = 0;cell < 9;cell++) { + for (let cell = 0;cell < score.length;cell++) { const s = score[cell][player === "human" ? 0 : 1]; rowScores[cell.toString()] = s === null ? "-" : toFixedOrLess(s, 3); } @@ -186,11 +182,8 @@