Skip to content

Commit

Permalink
add back adapter class, change types, test case
Browse files Browse the repository at this point in the history
  • Loading branch information
merowin committed May 12, 2024
1 parent f7671e1 commit 2837120
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 89 deletions.
22 changes: 22 additions & 0 deletions packages/shared/src/lib/__tests__/board.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Color } from "../../variants/badukWithAbstractBoard";
import {
BoardPattern,
createBoard,
createGraph,
} from "../abstractBoard/boardFactory";
import { Intersection } from "../abstractBoard/intersection";

test("create rhombitrihexagonal board", () => {
const intersections = createBoard(
{
type: BoardPattern.Polygonal,
size: 2,
},
Intersection,
);
const graph = createGraph(intersections, Color.EMPTY);

// Remark: This test case is not independent of implementation
// Also see PolygonalBoardHelper lines 4 - 28
expect(graph.neighbors(0)).toEqual([1, 5, 6, 17]);
});
5 changes: 1 addition & 4 deletions packages/shared/src/lib/abstractBoard/boardFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,7 @@ export function createGraph<TIntersection extends Intersection, TColor>(
intersections.indexOf(neighbour),
),
);
const graph = new Graph<TColor>(adjacencyMatrix);
intersections.forEach((intersection) =>
graph.set(intersection.id, startColor),
);
const graph = new Graph<TColor>(adjacencyMatrix).fill(startColor);
return graph;
}

Expand Down
29 changes: 26 additions & 3 deletions packages/shared/src/variants/baduk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ import { getGroup, getOuterBorder } from "../lib/group_utils";
import { SuperKoDetector } from "../lib/ko_detector";
import { AbstractGame } from "../abstract_game";
import {
BoardConfig,
BoardPattern,
createBoard,
createGraph,
} from "../lib/abstractBoard/boardFactory";
import { Intersection } from "../lib/abstractBoard/intersection";
import { GraphWrapper } from "../lib/graph";
import {
GridBadukConfig,
LegacyBadukConfig,
NewBadukConfig,
NewGridBadukConfig,
isGridBadukConfig,
isLegacyBadukConfig,
mapToNewConfig,
Expand Down Expand Up @@ -51,7 +50,7 @@ export class Baduk extends AbstractGame<NewBadukConfig, BadukState> {
/** after game ends, this is black points - white points */
public numeric_result?: number;

constructor(config?: LegacyBadukConfig | BadukConfig) {
constructor(config?: BadukConfig) {
super(isLegacyBadukConfig(config) ? mapToNewConfig(config) : config);

if (isGridBadukConfig(this.config)) {
Expand Down Expand Up @@ -247,3 +246,27 @@ export function groupHasLiberties(
function count_color<T>(value: T) {
return (total: number, color: T) => total + (color === value ? 1 : 0);
}

export class GridBaduk extends Baduk {
// ! isn't typesafe, but we know board will be assigned in super()
declare board: Grid<Color>;
protected declare score_board?: Grid<Color>;
declare config: NewGridBadukConfig;
constructor(config?: BadukConfig) {
if (config && !isGridBadukConfig(config)) {
throw "GridBaduk requires a GridBadukConfig";
}
super(config);
}

override defaultConfig(): NewGridBadukConfig {
return {
komi: 6.5,
board: {
type: BoardPattern.Grid,
width: 19,
height: 19,
},
};
}
}
12 changes: 6 additions & 6 deletions packages/shared/src/variants/baduk_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ export type NewBadukConfig = {
board: BoardConfig;
};

export type GridBadukConfig =
| LegacyBadukConfig
| {
komi: number;
board: GridBoardConfig;
};
export type NewGridBadukConfig = {
komi: number;
board: GridBoardConfig;
};

export type GridBadukConfig = LegacyBadukConfig | NewGridBadukConfig;

export function isGridBadukConfig(
config: BadukConfig,
Expand Down
39 changes: 7 additions & 32 deletions packages/shared/src/variants/drift.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,18 @@
import { GridBoardConfig } from "../lib/abstractBoard/boardFactory";
import { Coordinate } from "../lib/coordinate";
import { Grid } from "../lib/grid";
import { getGroup } from "../lib/group_utils";
import { Baduk, Color, groupHasLiberties } from "./baduk";
import { LegacyBadukConfig, NewBadukConfig } from "./baduk_utils";
import { Color, GridBaduk, groupHasLiberties } from "./baduk";
import { NewGridBadukConfig } from "./baduk_utils";

export type DriftGoConfig = { komi: number; board: GridBoardConfig } & {
export type DriftGoConfig = NewGridBadukConfig & {
yShift: number;
xShift: number;
};

export type LegacyDriftGoConfig = LegacyBadukConfig & {
yShift: number;
xShift: number;
};

function isDriftGoConfig(config: object): config is DriftGoConfig {
return (
"komi" in config &&
"board" in config &&
"xShift" in config &&
"yShift" in config
);
}

export class DriftGo extends Baduk {
private typedConfig: DriftGoConfig;
export class DriftGo extends GridBaduk {
declare config: DriftGoConfig;
declare board: Grid<Color>;

constructor(config?: DriftGoConfig | LegacyDriftGoConfig) {
super(config);
if (!isDriftGoConfig(this.config)) {
throw Error(
`Drift accepts only grid board config. Received config: ${JSON.stringify(config)}`,
);
}
this.typedConfig = this.config ?? this.defaultConfig();
}

defaultConfig(): DriftGoConfig {
return {
komi: 6.5,
Expand Down Expand Up @@ -81,8 +56,8 @@ export class DriftGo extends Baduk {

private shift(coord: Coordinate): Coordinate {
return new Coordinate(
(coord.x - this.typedConfig.xShift) % this.board.width,
(coord.y - this.typedConfig.yShift) % this.board.height,
(coord.x - this.config.xShift) % this.board.width,
(coord.y - this.config.yShift) % this.board.height,
);
}

Expand Down
18 changes: 2 additions & 16 deletions packages/shared/src/variants/keima.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
import { Coordinate } from "../lib/coordinate";
import { Baduk, BadukState } from "./baduk";
import {
GridBadukConfig,
LegacyBadukConfig,
isGridBadukConfig,
} from "./baduk_utils";
import { BadukState, GridBaduk } from "./baduk";

export interface KeimaState extends BadukState {
keima?: string;
}

export class Keima extends Baduk {
export class Keima extends GridBaduk {
private move_number = 0;

constructor(config?: GridBadukConfig | LegacyBadukConfig) {
super(config);
if (!isGridBadukConfig(this.config)) {
throw Error(
`Keima accepts only grid board config. Received config: ${JSON.stringify(config)}`,
);
}
}

playMove(player: number, move: string): void {
super.playMove(player, move);
this.increment_next_to_play();
Expand Down
17 changes: 4 additions & 13 deletions packages/shared/src/variants/pyramid.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
import { Baduk, Color } from "./baduk";
import { Color, GridBaduk } from "./baduk";
import { Grid } from "../lib/grid";
import {
GridBadukConfig,
LegacyBadukConfig,
isGridBadukConfig,
} from "./baduk_utils";
import { GridBadukConfig } from "./baduk_utils";

export class PyramidGo extends Baduk {
export class PyramidGo extends GridBaduk {
private weights: Grid<number>;
declare board: Grid<Color>;
declare score_board?: Grid<Color>;

constructor(config?: GridBadukConfig | LegacyBadukConfig) {
constructor(config?: GridBadukConfig) {
super(config);
if (!isGridBadukConfig(this.config)) {
throw Error(
`Pyramid accepts only grid board config. Received config: ${JSON.stringify(config)}`,
);
}

// Note: config may be undefined, but this.config is
// defined after the AbstractGame constructor is called.
Expand Down
18 changes: 5 additions & 13 deletions packages/shared/src/variants/quantum.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { AbstractGame } from "../abstract_game";
import { BoardPattern } from "../lib/abstractBoard/boardFactory";
import { Coordinate, CoordinateLike } from "../lib/coordinate";
import { Grid } from "../lib/grid";
import { Baduk, BadukBoard, BadukConfig, Color } from "./baduk";
import {
GridBadukConfig,
LegacyBadukConfig,
isGridBadukConfig,
NewBadukConfig,
NewGridBadukConfig,
isLegacyBadukConfig,
mapToNewConfig,
} from "./baduk_utils";
Expand Down Expand Up @@ -47,18 +45,12 @@ class BadukHelper {
}
}

export class QuantumGo extends AbstractGame<BadukConfig, QuantumGoState> {
export class QuantumGo extends AbstractGame<NewBadukConfig, QuantumGoState> {
subgames: BadukHelper[] = [];
quantum_stones: Coordinate[] = [];

constructor(config: BadukConfig | LegacyBadukConfig) {
constructor(config: BadukConfig) {
super(isLegacyBadukConfig(config) ? mapToNewConfig(config) : config);

if (config && !isGridBadukConfig(this.config)) {
throw Error(
`Drift accepty only grid board config. Received config: ${JSON.stringify(config)}`,
);
}
}

playMove(player: number, move: string): void {
Expand Down Expand Up @@ -201,7 +193,7 @@ export class QuantumGo extends AbstractGame<BadukConfig, QuantumGoState> {
numPlayers(): number {
return 2;
}
defaultConfig(): GridBadukConfig {
defaultConfig(): NewGridBadukConfig {
return {
board: { type: BoardPattern.Grid, width: 9, height: 9 },
komi: 7.5,
Expand Down
2 changes: 1 addition & 1 deletion packages/vue-client/src/components/boards/BadukBoard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const props = defineProps<{
}>();
const width = computed(() => getWidthAndHeight(props.config).width);
const height = computed(() => getWidthAndHeight(props.config).width);
const height = computed(() => getWidthAndHeight(props.config).height);
const positions = computed(positionsGetter(width, height));
function colorToClassString(color: Color): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
BadukConfig,
BadukState,
GridBadukConfig,
NewBadukConfig,
isGridBadukConfig,
} from "@ogfcommunity/variants-shared";
import { computed } from "vue";
Expand All @@ -29,7 +30,7 @@ const isGridBoard = computed(() => isGridBadukConfig(props.config));
<BadukGraphBoard
v-if="!isGridBoard"
:gamestate="$props.gamestate"
:config="$props.config"
:config="$props.config as NewBadukConfig"
@move="move"
/>
<BadukBoard
Expand Down

0 comments on commit 2837120

Please sign in to comment.