Skip to content

Commit

Permalink
wip client side ko rule
Browse files Browse the repository at this point in the history
  • Loading branch information
h908714124 committed Aug 4, 2024
1 parent c217180 commit 134d1a1
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 10 deletions.
11 changes: 7 additions & 4 deletions src/main/client/src/model/PointList.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ export class PointList {
static LO = 0xffff
static HI = 0xffff0000

static EMPTY = {
size: () => 0,
forEach: () => undefined,
isEmpty: () => true,
}

static empty() {
return {
size: 0,
forEach: () => {},
}
return PointList.EMPTY
}

constructor(dim) {
Expand Down
93 changes: 93 additions & 0 deletions src/main/client/src/model/ko.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import {
updateBoard,
} from "./base.js"
import {
BLACK,
WHITE,
} from "../util.js"

class Direction {
constructor(inc_x, inc_y) {
this.inc_x = inc_x
this.inc_y = inc_y
}

isOpposite(other) {
return this.inc_x + other.inc_x === 0 && this.inc_y + other.inc_y === 0
}

moveX(x) {
return x + this.inc_x
}

moveY(y) {
return y + this.inc_y
}
}

const NORTH = new Direction(0, -1)
const SOUTH = new Direction(0, 1)
const WEST = new Direction(-1, 0)
const EAST = new Direction(1, 0)
const NONE = new Direction(0, 0)

const NOT_FORBIDDEN = [-1, -1]

export function getForbidden(board, updated, move) {
if (!board || !board.length) {
return NOT_FORBIDDEN
}
let {x, y, color, dead} = move
if (dead.size() !== 1) {
return NOT_FORBIDDEN
}
let direction = getKillDirection(x, y, board, updated)
if (direction === NONE) {
return NOT_FORBIDDEN
}
let xx = direction.moveX(x)
let yy = direction.moveY(y)
let oppositeColor = color ^ (WHITE | BLACK)
let [simulationDead, simulation] = updateBoard(updated, {xx, yy, color: oppositeColor})
if (simulationDead.size() !== 1) {
return NOT_FORBIDDEN
}
let simulationDirection = getKillDirection(xx, yy, board, simulation)
return simulationDirection.isOpposite(direction) ? [xx, yy] : NOT_FORBIDDEN
}

// if a stone was removed next to [xx, yy], get its relative location
function getKillDirection(xx, yy, board, updated) {
let max = board.length - 1
let min_x = Math.max(xx - 1, 0)
let max_x = Math.min(xx + 1, max)
let min_y = Math.max(yy - 1, 0)
let max_y = Math.min(yy + 1, max)
for (let y = min_y; y <= max_y; y++) {
for (let x = min_x; x <= max_x; x++) {
if (x === xx && y === yy) {
continue
}
if (board[y][x] !== updated[y][x]) {
return directionFrom(xx, yy, x, y)
}
}
}
return Direction.NONE
}

function directionFrom(source_x, source_y, target_x, target_y) {
if (target_x > source_x) {
return EAST
}
if (target_x < source_x) {
return WEST
}
if (target_y > source_y) {
return SOUTH
}
if (target_y < source_y) {
return NORTH
}
return NONE
}
9 changes: 7 additions & 2 deletions src/main/client/src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import {
import {
updateBoard,
} from "./model/base.js"
import {
getForbidden,
} from "./model/ko.js"

export const useAuthStore = create((set) => ({
auth: {
Expand Down Expand Up @@ -80,12 +83,14 @@ export const useGameStore = create((set, get) => ({
return
}
let [dead, updated] = updateBoard(get().baseBoard, move)
state.moves.push({...move, dead})
let hydratedMove = {...move, dead}
state.moves.push(hydratedMove)
state.baseBoard = updated
state.gameState.board = rehydrate(updated)
state.gameState.currentColor = get().gameState.currentColor ^ (BLACK | WHITE)
state.gameState.currentPlayer = get().gameState.currentPlayer === get().black.name ? get().white.name : get().black.name
state.gameState.forbidden = move.forbidden
//state.gameState.forbidden = move.forbidden
state.gameState.forbidden = getForbidden(get().baseBoard, updated, hydratedMove)
}))
},
setGameState: (game) => {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/bernd/game/Direction.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public static Direction from(int source_x, int source_y, int target_x, int targe
return NONE;
}

public int moveX(int xx) {
return xx + inc_x;
public int moveX(int x) {
return x + inc_x;
}

public int moveY(int yy) {
return yy + inc_y;
public int moveY(int y) {
return y + inc_y;
}
}

0 comments on commit 134d1a1

Please sign in to comment.