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[ 0, 0, 0, 0, 1, 1, 0, 1,-1, 0,-1,-1, 1, 1, 1, 1,-1,-1,-1],
[ 0, 0, 1, 1,-1, 1, 1,-1, 0,-1,-1, 1, 1, 1, 1, 0, 1,-1, 1],
[ 0, 0, 0, 1,-1,-1,-1,-1,-1, 0,-1,-1, 1, 1, 0, 1, 1, 1, 0]
])
let move = board.putChess([9, 4], 1)
The board arrangement is represented by an array of arrays. Each of those subarrays represent one row, all containing the same number of integers. -1
denotes a white stone, 1
a black stone, and 0
represents an empty vertex.
[[ 0, 0, 1, 0,-1,-1, 1, 0, 0],
[ 1, 0, 1,-1,-1, 1, 1, 1, 0],
[ 0, 0, 1,-1, 0, 1,-1,-1, 0],
[ 1, 1, 1,-1,-1,-1, 1,-1, 0],
[ 1,-1, 1, 1,-1, 1, 1, 1, 0],
[-1,-1,-1,-1,-1, 1, 0, 0, 0],
[ 0,-1,-1, 0,-1, 1, 1, 1, 1],
[ 0, 0, 0, 0, 0,-1,-1,-1, 1],
[ 0, 0, 0, 0, 0, 0, 0,-1, 0]]
Board positions are represented by a vertex, i.e. an array of the form [x, y]
where x
and y
are non-negative integers, zero-based coordinates. [0, 0]
denotes the upper left position of the board.
signMap
<BoardArr>
(optional) - Default:[]
<BoardArr>
- The underlying sign map of the board.
vertex
<Vertex>
Returns the sign at the given vertex
.
vertex
<Vertex>
sign
<Integer>
- One of-1
,0
, or1
Sets the sign at the given vertex
. No validity checks will be made. This function mutates the board and returns this
to enable chaining.
vertex
<Vertex>
Returns a boolean whether the given vertex
is valid or can be found on the board.
sign
<Integer>
- One of-1
,0
, or1
vertex
<Vertex>
Returns a new board instance that represents the board state after the player who corresponds to the given sign
makes a move at vertex
. The capture count will also be updated correctly. If board
is valid then the new returned board instance will also be valid. This function will not mutate board
. If sign
is 0
or vertex
not valid, this function will be equivalent to clone()
.