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

checkerslibrary

v1.0.3

Published

<a href='https://travis-ci.org/KimjManansala/Checkers-Library/'><img src='https://travis-ci.org/KimjManansala/Checkers-Library.svg?branch=master' alt="build status"></a> <a href="https://badge.fury.io/js/checkerslibrary"><img src="https://badge.fury.io/js

Downloads

3

Readme

Checkers Library

This is a project to implement the game logic for the classic game Checkers

Development Setup

Install, then from this directory:

# install node_modules
npm install

# run the test suite
npm test

API Documentation

The Checkers Library will have functions displayed below

board Array

The Checkers board is represented as a 8x8 Array of Arrays (rows / columns). Each element in the Array is either null, empty, black,blackking, red, redking, or possible,`.

createNewGame()

This method returns a new game array. A square with an invalid placement is represented with null, all piece are represented with their corresponding piece color, and all empty valid piece is represented with empty. So a new Board looks like this in JSON

[
  ["red", null, "red", null, "red", null, "red", null],
  [null, "red", null, "red", null, "red", null, "red"],
  ["red", null, "red", null, "red", null, "red", null],
  [null, "empty", null, "empty", null, "empty", null, "empty"],
  ["empty", null, "empty", null, "empty", null, "empty", null],
  [null, "black", null, "black", null, "black", null, "black"],
  ["black", null, "black", null, "black", null, "black", null],
  [null, "black", null, "black", null, "black", null, "black"]
]

validBoard()

This method returns a boolean. It will check if the current board is valid

  • If board does not have 32 null pieces:
    • false
  • If board length is not 8:
    • false
  • If any board row length is not 8:
    • false
  • If all checks pass:
    • true

selectPieceToMove()

This method will return a new array with possible places a selected piece can go. The method takes in arguments current board, player turns, either 'black' or 'red', row of piece to move, and column of piece to move

const firstMove = [
    ["red", null, "red", null, "red", null, "red", null],
    [null, "red", null, "red", null, "red", null, "red"],
    ["red", null, "red", null, "red", null, "red", null],
    [null, "empty", null, "empty", null, "empty", null, "empty"],
    ["empty", null, "empty", null, "empty", null, "empty", null],
    [null, "black", null, "black", null, "black", null, "black"],
    ["black", null, "black", null, "black", null, "black", null],
    [null, "black", null, "black", null, "black", null, "black"]
  ]
selectPieceToMove(firstMove, 'black', 5,1)

Will return

[
  ["red", null, "red", null, "red", null, "red", null],
  [null, "red", null, "red", null, "red", null, "red"],
  ["red", null, "red", null, "red", null, "red", null],
  [null, "empty", null, "empty", null, "empty", null, "empty"],
  ["possible", null, "possible", null, "empty", null, "empty", null],
  [null, "blackmoving", null, "black", null, "black", null, "black"],
  ["black", null, "black", null, "black", null, "black", null],
  [null, "black", null, "black", null, "black", null, "black"]
]

Another Example

const blackPieceWillCapture = [
    ["red", null, "red", null, "red", null, "red", null],
    [null, "red", null, "red", null, "red", null, "red"],
    ["red", null, "red", null, "empty", null, "red", null],
    [null, "empty", null, "red", null, "empty", null, "empty"],
    ["empty", null, "black", null, "empty", null, "black", null],
    [null, "empty", null, "black", null, "black", null, "empty"],
    ["black", null, "black", null, "black", null, "black", null],
    [null, "black", null, "black", null, "black", null, "black"]
  ]
selectPieceToMove(blackPieceWillCapture, 'black', 4,2)

Will Return

[
  ["red", null, "red", null, "red", null, "red", null],
  [null, "red", null, "red", null, "red", null, "red"],
  ["red", null, "red", null, "possible", null, "red", null],
  [null, "possible", null, "red", null, "empty", null, "empty"],
  ["empty", null, "blackmoving", null, "empty", null, "black", null],
  [null, "empty", null, "black", null, "black", null, "empty"],
  ["black", null, "black", null, "black", null, "black", null],
  [null, "black", null, "black", null, "black", null, "black"]
]

moveToPossible()

This method will return a new array with the moved piece to the 'possible' location selected. The method takes in arguments current board, row of 'possible' piece selected, and column of 'possible' piece selected

const boardWithPossible = [
    ["red", null, "red", null, "red", null, "red", null],
    [null, "red", null, "red", null, "red", null, "red"],
    ["red", null, "red", null, "red", null, "red", null],
    [null, "empty", null, "empty", null, "empty", null, "empty"],
    ["possible", null, "possible", null, "empty", null, "empty", null],
    [null, "blackmoving", null, "black", null, "black", null, "black"],
    ["black", null, "black", null, "black", null, "black", null],
    [null, "black", null, "black", null, "black", null, "black"]
  ]
  moveToPossible(boardWithPossible, 4,0)

Will return

[
  ["red", null, "red", null, "red", null, "red", null],
  [null, "red", null, "red", null, "red", null, "red"],
  ["red", null, "red", null, "red", null, "red", null],
  [null, "empty", null, "empty", null, "empty", null, "empty"],
  ["black", null, "empty", null, "empty", null, "empty", null],
  [null, "empty", null, "black", null, "black", null, "black"],
  ["black", null, "black", null, "black", null, "black", null],
  [null, "black", null, "black", null, "black", null, "black"]
]

Another Example

const boardWithPossible = [
    ["red", null, "red", null, "red", null, "red", null],
    [null, "red", null, "red", null, "red", null, "red"],
    ["red", null, "red", null, "possible", null, "red", null],
    [null, "possible", null, "red", null, "empty", null, "empty"],
    ["empty", null, "blackmoving", null, "empty", null, "black", null],
    [null, "empty", null, "black", null, "black", null, "empty"],
    ["black", null, "black", null, "black", null, "black", null],
    [null, "black", null, "black", null, "black", null, "black"]
  ]
  moveToPossible(boardWithPossible, 2,4)

Will return

[
  ["red", null, "red", null, "red", null, "red", null],
  [null, "red", null, "red", null, "red", null, "red"],
  ["red", null, "red", null, "black", null, "red", null],
  [null, "empty", null, "empty", null, "empty", null, "empty"],
  ["empty", null, "empty", null, "empty", null, "black", null],
  [null, "empty", null, "black", null, "black", null, "empty"],
  ["black", null, "black", null, "black", null, "black", null],
  [null, "black", null, "black", null, "black", null, "black"]
]

checkRedWinner()

This method returns a boolean. It will check if red is the winner

const redWinningBoard=
    [
  ["empty", null, "empty", null, "empty", null, "empty", null],
  [null, "empty", null, "empty", null, "empty", null, "empty"],
  ["empty", null, "empty", null, "empty", null, "red", null],
  [null, "empty", null, "empty", null, "empty", null, "empty"],
  ["empty", null, "empty", null, "redking", null, "empty", null],
  [null, "empty", null, "empty", null, "empty", null, "empty"],
  ["empty", null, "empty", null, "empty", null, "empty", null],
  [null, "redking", null, "empty", null, "empty", null, "empty"]
]

console.log(checkRedWinner(redWinningBoard)) //true

checkBlackWinner()

This method returns a boolean. It will check if black is the winner

const blackWinningBoard=
    [
  ["empty", null, "empty", null, "empty", null, "empty", null],
  [null, "empty", null, "empty", null, "empty", null, "empty"],
  ["empty", null, "empty", null, "black", null, "empty", null],
  [null, "empty", null, "empty", null, "empty", null, "empty"],
  ["empty", null, "blackking", null, "empty", null, "empty", null],
  [null, "empty", null, "empty", null, "empty", null, "blackking"],
  ["empty", null, "empty", null, "empty", null, "empty", null],
  [null, "empty", null, "empty", null, "empty", null, "empty"]
]

console.log(checkBlackWinner(blackWinningBoard)) //true

checkValidAmount()

This method returns a boolean. It will check if there is no more than 13 of red pieces and black piece

const normalBoard=
    [
  ["redking", null, "black", null, "black", null, "empty", null],
  [null, "empty", null, "empty", null, "empty", null, "empty"],
  ["empty", null, "blackking", null, "empty", null, "empty", null],
  [null, "empty", null, "empty", null, "empty", null, "empty"],
  ["empty", null, "empty", null, "empty", null, "empty", null],
  [null, "empty", null, "empty", null, "empty", null, "empty"],
  ["empty", null, "empty", null, "empty", null, "empty", null],
  [null, "red", null, "red", null, "red", null, "empty"]
]
console.log(checkValidAmount(normalBoard)) //true

License

ISC License