Skip to content

Commit 134d1a1

Browse files
committed
wip client side ko rule
1 parent c217180 commit 134d1a1

File tree

4 files changed

+111
-10
lines changed

4 files changed

+111
-10
lines changed

src/main/client/src/model/PointList.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ export class PointList {
77
static LO = 0xffff
88
static HI = 0xffff0000
99

10+
static EMPTY = {
11+
size: () => 0,
12+
forEach: () => undefined,
13+
isEmpty: () => true,
14+
}
15+
1016
static empty() {
11-
return {
12-
size: 0,
13-
forEach: () => {},
14-
}
17+
return PointList.EMPTY
1518
}
1619

1720
constructor(dim) {

src/main/client/src/model/ko.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import {
2+
updateBoard,
3+
} from "./base.js"
4+
import {
5+
BLACK,
6+
WHITE,
7+
} from "../util.js"
8+
9+
class Direction {
10+
constructor(inc_x, inc_y) {
11+
this.inc_x = inc_x
12+
this.inc_y = inc_y
13+
}
14+
15+
isOpposite(other) {
16+
return this.inc_x + other.inc_x === 0 && this.inc_y + other.inc_y === 0
17+
}
18+
19+
moveX(x) {
20+
return x + this.inc_x
21+
}
22+
23+
moveY(y) {
24+
return y + this.inc_y
25+
}
26+
}
27+
28+
const NORTH = new Direction(0, -1)
29+
const SOUTH = new Direction(0, 1)
30+
const WEST = new Direction(-1, 0)
31+
const EAST = new Direction(1, 0)
32+
const NONE = new Direction(0, 0)
33+
34+
const NOT_FORBIDDEN = [-1, -1]
35+
36+
export function getForbidden(board, updated, move) {
37+
if (!board || !board.length) {
38+
return NOT_FORBIDDEN
39+
}
40+
let {x, y, color, dead} = move
41+
if (dead.size() !== 1) {
42+
return NOT_FORBIDDEN
43+
}
44+
let direction = getKillDirection(x, y, board, updated)
45+
if (direction === NONE) {
46+
return NOT_FORBIDDEN
47+
}
48+
let xx = direction.moveX(x)
49+
let yy = direction.moveY(y)
50+
let oppositeColor = color ^ (WHITE | BLACK)
51+
let [simulationDead, simulation] = updateBoard(updated, {xx, yy, color: oppositeColor})
52+
if (simulationDead.size() !== 1) {
53+
return NOT_FORBIDDEN
54+
}
55+
let simulationDirection = getKillDirection(xx, yy, board, simulation)
56+
return simulationDirection.isOpposite(direction) ? [xx, yy] : NOT_FORBIDDEN
57+
}
58+
59+
// if a stone was removed next to [xx, yy], get its relative location
60+
function getKillDirection(xx, yy, board, updated) {
61+
let max = board.length - 1
62+
let min_x = Math.max(xx - 1, 0)
63+
let max_x = Math.min(xx + 1, max)
64+
let min_y = Math.max(yy - 1, 0)
65+
let max_y = Math.min(yy + 1, max)
66+
for (let y = min_y; y <= max_y; y++) {
67+
for (let x = min_x; x <= max_x; x++) {
68+
if (x === xx && y === yy) {
69+
continue
70+
}
71+
if (board[y][x] !== updated[y][x]) {
72+
return directionFrom(xx, yy, x, y)
73+
}
74+
}
75+
}
76+
return Direction.NONE
77+
}
78+
79+
function directionFrom(source_x, source_y, target_x, target_y) {
80+
if (target_x > source_x) {
81+
return EAST
82+
}
83+
if (target_x < source_x) {
84+
return WEST
85+
}
86+
if (target_y > source_y) {
87+
return SOUTH
88+
}
89+
if (target_y < source_y) {
90+
return NORTH
91+
}
92+
return NONE
93+
}

src/main/client/src/store.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import {
1717
import {
1818
updateBoard,
1919
} from "./model/base.js"
20+
import {
21+
getForbidden,
22+
} from "./model/ko.js"
2023

2124
export const useAuthStore = create((set) => ({
2225
auth: {
@@ -80,12 +83,14 @@ export const useGameStore = create((set, get) => ({
8083
return
8184
}
8285
let [dead, updated] = updateBoard(get().baseBoard, move)
83-
state.moves.push({...move, dead})
86+
let hydratedMove = {...move, dead}
87+
state.moves.push(hydratedMove)
8488
state.baseBoard = updated
8589
state.gameState.board = rehydrate(updated)
8690
state.gameState.currentColor = get().gameState.currentColor ^ (BLACK | WHITE)
8791
state.gameState.currentPlayer = get().gameState.currentPlayer === get().black.name ? get().white.name : get().black.name
88-
state.gameState.forbidden = move.forbidden
92+
//state.gameState.forbidden = move.forbidden
93+
state.gameState.forbidden = getForbidden(get().baseBoard, updated, hydratedMove)
8994
}))
9095
},
9196
setGameState: (game) => {

src/main/java/com/bernd/game/Direction.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ public static Direction from(int source_x, int source_y, int target_x, int targe
3535
return NONE;
3636
}
3737

38-
public int moveX(int xx) {
39-
return xx + inc_x;
38+
public int moveX(int x) {
39+
return x + inc_x;
4040
}
4141

42-
public int moveY(int yy) {
43-
return yy + inc_y;
42+
public int moveY(int y) {
43+
return y + inc_y;
4444
}
4545
}

0 commit comments

Comments
 (0)