Chess pieces legal movements generation API with chess.js. The API was built with Redis cache provider, thus, as soon as the movement is created its result list is stored in cache, which speeds up the following requests.
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.
-
List chess pieces legal movements with:
- position on the board (square)
- piece type
- "p" for Pawn
- "n" for Knight
- "b" for Bishop
- "r" for Rook
- "q" for Queen
- "k" for King
- color (black or white)
- list of valid movements from the current position in algebraic notation
-
List pawn movements in a populated board:
- special movements, combos and capture
- pawn promotion
Download or clone the source code on a local folder.
git clone https://github.com/adroaldopagliari/chess-move-generation.git
Open the project folder and type (installing the dependencies):
npm install
or
yarn
Afterwards, run the command and executes the application on devlopment mode:
npm dev:server
or
yarn dev:server
Chess move generation API will be running on:
http://localhost:3333
List all the legal knight movements, given its current position (square), in algebraic notation.
POST /movements
http://localhost:3333/movements
Basic usage example:
post http://localhost:3333/movements
Other acceptable input parameters (piece and color):
- Piece representation:
-
- "p" for Pawn
-
- "n" for Knight
-
- "b" for Bishop
-
- "r" for Rook
-
- "q" for Queen
-
- "k" for King
Expected result set (json):
- id: Unique identifier (It's gonna be useful when rendering in a list).
- current_position: It represents the piece square on the board.
- piece: Selected piece on the board.
-
- "p" for Pawn
-
- "n" for Knight
-
- "b" for Bishop
-
- "r" for Rook
-
- "q" for Queen
-
- "k" for King
- color: White or Black.
- legal_moves: List containing all the valid moves for the selected piece in the current_position.
{
"id": "bcdfafc5-6e58-4a2f-861e-b66f11c29ffb",
"current_position": "E5",
"piece": "p",
"color": "w",
"legal_moves": [
"E6"
]
}
In order to run the tests, open the project folder and type:
npm test --coverage
or
yarn test --coverage
After running the test coverage, access the test report, go to the folder coverage/lcov-report and open up the index.html file on the browser to go through the details.
There're five tests to cover the GenerateMovementsService.ts:
- It should be able to list the knight movements, given the current position.
- It should be able to list other pieces movements.
- It should not be able to move a piece to an invalid position.
- It should be able to generate moves for black pieces.
- It should be able to generate moves for pawns about to get the last square.
Given the following input:
current_position: 'E2'
The expected result is:
['C3', 'D4', 'F4', 'G3', 'G1', 'C1']
In the project folder, type:
npm build
or
yarn build
A dist folder will be create with all the files needed to deploy the API.
- Fork the project.
- Create a new branch with your changes:
git checkout -b my-feature
- Save your changes and create a commit message telling what you did:
git commit -m" feature: My new feature "
- Submit your changes:
git push origin my-feature
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
v1.0.0
- Adroaldo Pagliari - Initial work - ChessMovement
This project is licensed under the MIT License - see the LICENSE.md file for details
- Hat tip to anyone whose code was used
- Meritt who gave me an opportunity to 'show the code'