Skip to content

Commit

Permalink
make scoring better
Browse files Browse the repository at this point in the history
  • Loading branch information
netanel-haber committed May 29, 2023
1 parent 7cc7830 commit 3005fc1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
14 changes: 3 additions & 11 deletions src/ai/ai.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,7 @@ r-------`),

it("calculates score correctly", () => {
const result = calculateScore(state2);
const regular = 12 * R;
const backRow = 3 * B;
const side = 2 * S;

const black = 3 * R;
const blackKing = 1 * KING;
const blackSide = 2 * S;
const blackCenter = 1 * C;
expect(result).toBe(
regular + backRow + side - black - blackKing - blackSide - blackCenter
);
const redScore = 12 * R + 0 * KING + 2 * S + 3 * B + 0 * C;
const blackScore = 3 * R + 1 * KING + 2 * S + 0 * B + 1 * C;
expect(result).toBe(redScore - blackScore);
});
22 changes: 13 additions & 9 deletions src/ai/bestScore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,29 @@ const valueToScore = [R, KING, R, KING, 0] as const;

/* eslint-disable prettier/prettier */
const bonuses = [
[0, B, 0, B, 0, B, 0, B],
[S, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, S],
[S, 0, C, 0, C, 0, 0, 0],
[0, 0, 0, C, 0, C, 0, S],
[S, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, S],
[B, 0, B, 0, B, 0, B, 0],
[0, B, 0, B, 0, B, 0, B],
[S, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, S],
[S, 0, C, 0, C, 0, 0, 0],
[0, 0, 0, C, 0, C, 0, S],
[S, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, S],
[B, 0, B, 0, B, 0, B, 0],
] as const;


/* eslint-enable prettier/prettier */

const calculateBonus = (
r: number,
c: number,
color: Color | undefined,
color: Color,
turn: Color
): number => {
const potentialBonus = bonuses[r][c];
/** Laziest way to do this. TODO: assert so this doesn't happen. */
if (potentialBonus === S)
return turn === color ? potentialBonus : -potentialBonus;
if (r <= 3 && color === Color.red)
return turn === Color.red ? potentialBonus : -potentialBonus;
if (r >= 4 && color === Color.black)
Expand Down
6 changes: 6 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,9 @@ export const computeGridFromString = (grid: string): Grid => {
row.map((_, cIndex) => Number(regularBoardSetup[rIndex].charAt(cIndex)))
);
};

export const assert = (condition: boolean, msg?: string): asserts condition => {
if (!condition) {
throw new Error(msg);
}
};

0 comments on commit 3005fc1

Please sign in to comment.