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

ur-game

v1.3.0

Published

Royal Game of Ur implementation using the rules from the British Museum / Irving Finkel

Downloads

4

Readme

npm version

The Royal Game of Ur

This module implements the logic of the Royal Game of Ur using the rules from the British Museum / Irving Finkel.

Installation

npm install ur-game

Usage

const Ur = require('ur-game');

// Ur(stones?: number, dice?: number, player?: string)
const game = new Ur();
// default: stones = 7, dice = 4, player = Ur.WHITE

let state = game.getState();
// possible state:
// state.currentPlayer -> 'w'
// state.diceResult -> 3
// state.possibleMoves -> { '0': 3 }

// takeTurn(player: string, move?: string | number)
game.takeTurn(Ur.WHITE, 0);  // or game.takeTurn('w', 0);
// returns false on invalid input
// returns new state otherwise
// move is allowed to be undefined should there be no possible move

// voidTurn(player: string)
game.voidTurn(Ur.BLACK);
// returns false when not the current player is input, new state otherwise
// useful for ending turns after a certain amount of time

Alternatively you can keep track of the state yourself.

const Ur = require('ur-game');

// startGame(stones?: number, dice?: number, player?: string)
let state = Ur.startGame(7, 4, Ur.BLACK);
// default: stones = 7, dice = 4, player = Ur.WHITE

// possible state:
// state.currentPlayer -> 'b'
// state.diceResult -> 2
// state.possibleMoves -> { '0': 2 }

// takeTurn(state: Object, player: string, move?: string | number)
state = Ur.takeTurn(state, Ur.BLACK, 0);  // or Ur.takeTurn(state, 'b', 0)
// returns false on invalid input, new state otherwise
// move is allowed to be undefined should there be no possible move

// voidTurn(state: Object, player: string)
state = Ur.voidTurn(state, Ur.WHITE);
// returns false when not the current player is input, new state otherwise
// useful for ending turns after a certain amount of time

State

state.currentPlayer  
// either 'w' or 'b'
// (undefined after game has been finished)

state.dice
// array of individual dice
// [ 0, 1, 1, 0 ]
// (undefined after game has been finished)

state.diceResult
// result of the dice roll
// (undefined after game has been finished)

state.board
// representation of the board as a list of objects
// index 0 and 15 aren't actual fields on the board 
// they represent the starting and finishing point respectively
// [ 
//     { w: 7, b: 7 },
//     { w: 0, b: 0 },
//     { w: 0, b: 0 },
//     { w: 0, b: 0 },
//     { w: 0, b: 0 },
//     { w: 0, b: 0 },
//     { w: 0, b: 0 },
//     { w: 0, b: 0 },
//     { w: 0, b: 0 },
//     { w: 0, b: 0 },
//     { w: 0, b: 0 },
//     { w: 0, b: 0 },
//     { w: 0, b: 0 },
//     { w: 0, b: 0 },
//     { w: 0, b: 0 },
//     { w: 0, b: 0 } 
// ] 

state.possibleMoves
// an object containing all legal moves based on the result of dice roll
// the key of each key value pair is the starting point of the move
// the value is the field the player would land on
// { '0': 2 }
// (undefined after game has been finished)

state.winner
// either 'w', 'b' or undefined

Pretty State

Ur.prettifyState(state); returns a prettified version of the state.
Ur.uglifyState(state); converts a prettified state back to its original representation.