Skip to content

Commit

Permalink
kings are 1.2x, not 2x. Fixed readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
netanel-haber committed May 27, 2023
1 parent 664839d commit d5c3cd0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# checkers

[Play](https://netanel-haber.github.io/checkers/) vanilla TS, html and css pvp checkers started a trillion years ago, before I could tell a div from a span.
[Play](https://netanel-haber.github.io/checkers/) vanilla TS, html and css checkers started a trillion years ago, before I could tell a div from a span.
Now with Vite.

**Features:**
Expand Down
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="PVP vanilla checkers, author: Netanel Haber">
<meta name="description" content="vanilla JS checkers, author: Netanel Haber">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PVP vanilla checkers</title>
<title>vanilla JS checkers</title>
<link rel="preload" as="image" href="/red.webp" />
<link rel="preload" as="image" href="/black.webp" />
<link rel="preload" as="image" href="/red-king.webp" />
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "checkers",
"version": "1.0.0",
"description": "pure js, html and css web based pvp checkers from a trillion years ago. [Play](https://netanel-haber.github.io/checkers/).",
"description": "pure js, html and css web based checkers from a trillion years ago. [Play](https://netanel-haber.github.io/checkers/).",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down Expand Up @@ -32,4 +32,4 @@
"pre-commit": [
"lint"
]
}
}
43 changes: 14 additions & 29 deletions src/ai/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,31 @@ import { type StateControllers } from "../types";

import { forEachCell, gridValToColor } from "../utils";

const valueToScore = [1, 1.2, 1, 1.2, 0] as const;

export const calculateScore = ({ grid, turn }: BoardState) => {
let score = 0;
forEachCell((r, c) => {
const cell = grid[r][c];
const color = gridValToColor[cell];
if (!color) return;
let value = 0;
switch (cell) {
case 0:
case 2:
value = 1;
break;
case 1:
case 3:
value = 2;
}
const value = valueToScore[cell];
score += color === turn ? value : -value;
});
return score;
};

const DEPTH = 5;
const ODD_DEPTH = 5;
if (ODD_DEPTH % 2 === 0) {
throw new Error("Depth must be odd");
}

type BestMove = {
score: number;
move: {
finalRow: number;
finalColumn: number;
startRow: number;
startColumn: number;
};
const defaultBest = {
score: 0,
move: { finalColumn: 0, finalRow: 0, startColumn: 0, startRow: 0 },
};

function bestMove(state: BoardState) {
function bestScore(state: BoardState, depth = DEPTH) {
function bestScore(state: BoardState, depth = ODD_DEPTH) {
if (!depth) {
return calculateScore(state);
}
Expand All @@ -57,10 +47,7 @@ function bestMove(state: BoardState) {
}
return max;
}
let best: BestMove = {
score: 0,
move: { finalColumn: 0, finalRow: 0, startColumn: 0, startRow: 0 },
};
let best = defaultBest;
for (const [cell, potentialMoves] of state.getAllLegalMovesForColor()) {
for (const { updates, finalCell } of potentialMoves) {
const score = bestScore(state.updatedGrid(updates).updateCurrentTurn());
Expand All @@ -77,7 +64,7 @@ function bestMove(state: BoardState) {
}
}
}
return best;
return best.move;
}

export const doAiMove = (
Expand All @@ -87,9 +74,7 @@ export const doAiMove = (
if (state.turn !== COMPUTER) return;
setTimeout(() => {
const t0 = performance.now();
const {
move: { finalRow, finalColumn, startRow, startColumn },
} = bestMove(state);
const { finalRow, finalColumn, startRow, startColumn } = bestMove(state);
const elapsed = performance.now() - t0;
const maxiumum400 = Math.max(250 - elapsed, 0);
setTimeout(() => {
Expand Down

0 comments on commit d5c3cd0

Please sign in to comment.