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
2
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
createNewGame()
validBoard()
selectPieceToMove()
moveToPossible()
checkRedwinner()
checkBlackWinner()
checkValidAmount()
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