-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c217180
commit 134d1a1
Showing
4 changed files
with
111 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters