npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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
    Use Game.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 position

  • isPlacedFromXY (x: board2d.X, y: board2d.Y): boolean
    Whether the disk is placed in the passed position

  • placeDisk (pos: Pos, disk: Disk, callback: (e: Error | null, game: Game) => void): void
    Place a disk

    The 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 y

  • boardToString (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 board

    returns: 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 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.

  • 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 position

    returns: 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]