Skip to content

Commit

Permalink
Merge Baduk and abstractAlternatingOnGrid
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminpjones committed Aug 9, 2023
1 parent 52842f6 commit 40a7258
Show file tree
Hide file tree
Showing 16 changed files with 92 additions and 156 deletions.
1 change: 0 additions & 1 deletion packages/shared/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from "./variants/baduk";
export * from "./lib/abstractAlternatingOnGrid";
export * from "./variants/phantom";
export * from "./variants/capture";
export * from "./variants/parallel";
Expand Down

This file was deleted.

1 change: 0 additions & 1 deletion packages/shared/src/lib/abstractAlternatingOnGrid/index.ts

This file was deleted.

3 changes: 1 addition & 2 deletions packages/shared/src/variants/__tests__/baduk.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Baduk } from "../baduk";
import { Color } from "../../lib/abstractAlternatingOnGrid";
import { Baduk, Color } from "../baduk";
import { KoError } from "../../lib/ko_detector";

const B = Color.BLACK;
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/src/variants/__tests__/capture.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Capture } from "../capture";
import { Color } from "../../lib/abstractAlternatingOnGrid";
import { Color } from "../baduk";

test("White wins by capture", () => {
const game = new Capture({ width: 2, height: 2, komi: 0.5 });
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/src/variants/__tests__/keima.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Keima } from "../keima";
import { Color } from "../../lib/abstractAlternatingOnGrid";
import { Color } from "../baduk";

const B = Color.BLACK;
const _ = Color.EMPTY;
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/src/variants/__tests__/one_color.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Color } from "../../lib/abstractAlternatingOnGrid/abstractAlternatingOnGrid";
import { Color } from "../baduk";
import { OneColorGo } from "../one_color";

const B = Color.BLACK;
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/src/variants/__tests__/phantom.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Phantom } from "../phantom";
import { Color } from "../../lib/abstractAlternatingOnGrid";
import { Color } from "../baduk";

test("Play a game", () => {
const game = new Phantom({ width: 4, height: 2, komi: 0.5 });
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/src/variants/__tests__/tetris.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TetrisGo } from "../tetris";
import { Color } from "../../lib/abstractAlternatingOnGrid";
import { Color } from "../baduk";

test("Play a game", () => {
const game = new TetrisGo({ width: 4, height: 2, komi: 0.5 });
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/src/variants/__tests__/thue_morse.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ThueMorse } from "../thue_morse";
import { Color } from "../../lib/abstractAlternatingOnGrid";
import { Color } from "../baduk";

const B = Color.BLACK;
const _ = Color.EMPTY;
Expand Down
96 changes: 77 additions & 19 deletions packages/shared/src/variants/baduk.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,105 @@
import {
AbstractAlternatingOnGrid,
AbstractAlternatingOnGridConfig,
AbstractAlternatingOnGridState,
Color,
} from "../lib/abstractAlternatingOnGrid";
import { Coordinate, CoordinateLike } from "../lib/coordinate";
import { Grid } from "../lib/grid";
import { getGroup, getOuterBorder } from "../lib/group_utils";
import { SuperKoDetector } from "../lib/ko_detector";
import { AbstractGame } from "../abstract_game";

export interface BadukConfig extends AbstractAlternatingOnGridConfig {
export enum Color {
EMPTY = 0,
BLACK = 1,
WHITE = 2,
}

export interface BadukConfig {
width: number;
height: number;
komi: number;
}

export interface BadukState extends AbstractAlternatingOnGridState {
export interface BadukState {
board: Color[][];
next_to_play: 0 | 1;
captures: { 0: number; 1: number };
last_move: string;
score_board?: Color[][];
}

export type BadukMove = { 0: string } | { 1: string };

export class Baduk extends AbstractAlternatingOnGrid<BadukConfig, BadukState> {
export class Baduk extends AbstractGame<BadukConfig, BadukState> {
protected captures = { 0: 0, 1: 0 };
private ko_detector = new SuperKoDetector();
protected score_board?: Grid<Color>;
protected board: Grid<Color>;
protected next_to_play: 0 | 1 = 0;
protected last_move = "";

constructor(config?: BadukConfig) {
super(config);
this.board = new Grid<Color>(this.config.width, this.config.height).fill(
Color.EMPTY,
);
}

override exportState(): BadukState {
return {
...super.exportState(),
...(this.score_board && { score_board: this.score_board.to2DArray() }),
board: this.board.to2DArray(),
next_to_play: this.next_to_play,
last_move: this.last_move,
captures: { 0: this.captures[0], 1: this.captures[1] },
...(this.score_board && { score_board: this.score_board.to2DArray() }),
};
}

protected override playMoveInternal(move: Coordinate): void {
super.playMoveInternal(move);
override nextToPlay(): number[] {
return [this.next_to_play];
}

override playMove(player: number, move: string): void {
if (player != this.next_to_play) {
throw Error(`It's not player ${player}'s turn!`);
}

if (move === "resign") {
this.phase = "gameover";
this.result = this.next_to_play === 0 ? "W+R" : "B+R";
return;
}

if (move != "pass") {
const decoded_move = Coordinate.fromSgfRepr(move);
const { x, y } = decoded_move;
const color = this.board.at(decoded_move);
if (color === undefined) {
throw Error(
`Move out of bounds. (move: ${decoded_move}, board dimensions: ${this.config.width}x${this.config.height}`,
);
}
if (color !== Color.EMPTY) {
throw Error(
`Cannot place a stone on top of an existing stone. (${color} at (${x}, ${y}))`,
);
}

this.playMoveInternal(decoded_move);
this.postValidateMove(decoded_move);
this.prepareForNextMove(move);
} else {
this.prepareForNextMove(move);
}
}

override numPlayers(): number {
return 2;
}

override specialMoves() {
return { pass: "Pass", resign: "Resign" };
}

private playMoveInternal(move: Coordinate): void {
this.board.set(move, this.next_to_play === 0 ? Color.BLACK : Color.WHITE);

const opponent_color = this.next_to_play === 0 ? Color.WHITE : Color.BLACK;
this.board.neighbors(move).forEach((pos) => {
const neighbor_color = this.board.at(pos);
Expand All @@ -54,7 +114,7 @@ export class Baduk extends AbstractAlternatingOnGrid<BadukConfig, BadukState> {
});
}

protected override postValidateMove(move: Coordinate): void {
protected postValidateMove(move: Coordinate): void {
// Detect suicide
if (!groupHasLiberties(getGroup(move, this.board), this.board)) {
throw Error("Move is suicidal!");
Expand All @@ -67,14 +127,12 @@ export class Baduk extends AbstractAlternatingOnGrid<BadukConfig, BadukState> {
});
}

protected override prepareForNextMove(
move: string,
decoded_move?: Coordinate,
): void {
private prepareForNextMove(move: string): void {
if (move == "pass" && this.last_move === "pass") {
this.finalizeScore();
} else {
super.prepareForNextMove(move, decoded_move);
this.next_to_play = this.next_to_play === 0 ? 1 : 0;
this.last_move = move;
}
}

Expand Down
6 changes: 4 additions & 2 deletions packages/shared/src/variants/freeze.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Color } from "../lib/abstractAlternatingOnGrid";
import { Coordinate, CoordinateLike } from "../lib/coordinate";
import { Grid } from "../lib/grid";
import { getGroup, getOuterBorder } from "../lib/group_utils";
import { Baduk, BadukState } from "./baduk";
import { Baduk, BadukState, Color } from "./baduk";

export interface FreezeGoState extends BadukState {
frozen: boolean;
Expand All @@ -15,6 +14,9 @@ export class FreezeGo extends Baduk {
const captures_before = this.captures[player as 0 | 1];
super.playMove(player, move);
const captures_after = this.captures[player as 0 | 1];

console.log("before, after", captures_before, captures_after);

if (this.frozen && captures_before !== captures_after) {
throw new Error("Cannot capture after opponent ataris");
}
Expand Down
3 changes: 1 addition & 2 deletions packages/shared/src/variants/one_color.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Baduk, BadukState } from "./baduk";
import { Color } from "../lib/abstractAlternatingOnGrid";
import { Baduk, BadukState, Color } from "./baduk";

export class OneColorGo extends Baduk {
exportState(): BadukState {
Expand Down
3 changes: 1 addition & 2 deletions packages/shared/src/variants/phantom.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Baduk, BadukState } from "./baduk";
import { Color } from "../lib/abstractAlternatingOnGrid";
import { Baduk, BadukState, Color } from "./baduk";
import { Grid } from "../lib/grid";

export class Phantom extends Baduk {
Expand Down
3 changes: 1 addition & 2 deletions packages/shared/src/variants/pyramid.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Baduk, BadukConfig } from "./baduk";
import { Baduk, BadukConfig, Color } from "./baduk";
import { Grid } from "../lib/grid";
import { Color } from "../lib/abstractAlternatingOnGrid";

export class PyramidGo extends Baduk {
private weights: Grid<number>;
Expand Down
3 changes: 1 addition & 2 deletions packages/shared/src/variants/tetris.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Color } from "../lib/abstractAlternatingOnGrid";
import { Baduk } from "./baduk";
import { Baduk, Color } from "./baduk";
import { Coordinate, CoordinateLike } from "../lib/coordinate";
import { Grid } from "../lib/grid";

Expand Down

0 comments on commit 40a7258

Please sign in to comment.