checkerslibrary

1.0.3 • Public • Published

Checkers Library

build status npm version

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

Readme

Keywords

none

Package Sidebar

Install

npm i checkerslibrary

Weekly Downloads

7

Version

1.0.3

License

ISC

Unpacked Size

55.6 kB

Total Files

6

Last publish

Collaborators

  • kimjmanansala