reversi-logic
v1.0.2
Published
Reversi game logic library. UI is not included.
Downloads
1
Readme
reversi
Reversi game logic library. UI is not included.
design concept
- Object is immutable
- The reference method returns the result as a return value
- Update method returns result in callback
install
npm
npm install reversi-logic
browser
in html
<script src="https://naosim.github.io/reversi-logic/reversi.js"></script>
import
import * as reversi from 'reversi-logic';
getting started
import * as reversi from 'reversi-logic';
var game = reversi.Game.init();
game.placeDisk({x: 4, y: 2}, reversi.Disk.light, (e, nextGame) => {
if(e) {
throw e;
}
game = nextGame;// update global value
var values = game.board.values; // as 2d array
/* values are
[
[null, null, null, null, null, null, null, null],
[null, null, null, null, null, null, null, null],
[null, null, null, null, 'l', null, null, null],
[null, null, null, 'l', 'l', null, null, null],
[null, null, null, 'd', 'l', null, null, null],
[null, null, null, null, null, null, null, null],
[null, null, null, null, null, null, null, null],
[null, null, null, null, null, null, null, null],
]
*/
// you can get each values
game.board.forEach((pos, disk) => {
console.log(pos.x, pos.y, disk)
})
})
sample
main classes
Game
Reversi game class
entry point
Constructors
- constructor (turn: Disk, board: Board, winner: Disk | null, _eventList: Event[]): Game
UseGame.init ()
instead of the constructor to start the game.
Properties
- board: Board
- turn: Disk
- winner: Disk | null
null if no win or loss has been decide - isGameOver: boolean
Whether the win or loss has been decided - score: Score
Methods
getTunableDiskPosList (pos: PosReadable, disk: Disk): Pos[]
getTunableDiskPosListFromXY (x: board2d.X, y: board2d.Y, disk: Disk): Pos[]
hasTurnableDiskPos (disk: Disk): boolean
Is there a place to put the disk?isPlaced (pos: PosReadable): boolean
Whether the disk is placed in the passed positionisPlacedFromXY (x: board2d.X, y: board2d.Y): boolean
Whether the disk is placed in the passed positionplaceDisk (pos: Pos, disk: Disk, callback: (e: Error | null, game: Game) => void): void
Place a diskThe result is returned in callback. Conditions that cause an error in callback
- The type of disk and the turn do not match
- The position where the disk cannot be placed is specified
placeDiskFromXY (x: board2d.X, y: board2d.Y, disk: Disk, callback: (e: Error | null, game: Game) => void): void
Place a disk with x and yboardToString (board: Board): string [static]
Convert board to string (for debugging)init (): Game [static]
Start the game
Board
Two-dimensional board
Constructors
- constructor (boardCore: BoardCore, skipCopy: SkipCopy): Board
Properties
values: (null | T)[][]
Two-dimensional array as raw data on the boardreturns: Return a copy. Updating the returned value does not affect the board.
xSize: number
ySize: number
Methods
copy (): Board
exists (pos: PosReadable): boolean
whether there is a piece at the specified positionreturns: Returns true if there is a piece. Returns false if there is no a piece or the position is outside of the board.
find (check: (pos: Pos, value: T | null) => boolean): ValueAndPos | null
findAll (check: (pos: Pos, value: T | null) => boolean): ValueAndPos[]
forEach (callback: (pos: Pos, value: T | null) => void): void
executes a provided function once for each positions on the board.getValue (pos: PosReadable): T | null | undefined
Get the piece at the specified positionreturns: Returns null if the specified position is empty. Returns undefined if outside the board.
getValueWithDirection (pos: PosReadable, direction: Direction): ValueAndPos | undefined
Get a piece that is one step ahead in the direction from position.returns: Returns true if there is a piece. Returns false if there is no a piece or the position is outside of the board.
getValueWithXY (x: X, y: Y): T | null | undefined
indexToPos (index: number): Pos
posToIndex (pos: PosReadable): number
put (pos: PosReadable, value: T | null): Board
Put pieces on the board (immutable)putWithXY (x: X, y: Y, value: T | null): Board
some (check: (pos: Pos, value: T | null) => boolean): boolean
Tests whether at least one piece in the board passes the test implemented by the provided check function. It returns a Boolean value.toMutable (): BoardMutable
empty (xSize: number, ySize: number): Board [static]