Code Monkey home page Code Monkey logo

py-chess-api's Introduction

Chess API

Requirements

Sketching a simple API for the game of Chess able to:

  • enable the user to move pieces on the chessboard
  • check whether the move is legal
  • deal with possible illegal moves
  • check whether a piece is taken

Focus on the interfaces to model the system and the implementation for the rules associated to one of the pieces and for the chessboard.

Implementation

Software is designed on 3 key classes: Game, Chessboard and Piece.

  • Game handles all the features related to game setup and mechanics and answers to the requests from the API.
  • Chessboard handle the status of the chessboard. Place all the piaces on their initial position and expose methods to move them around.
  • Piece knows its position, its team and its status. Each subclass (Paws, Rook, Knight, Bishop, King and Queen) has a custom implementation of the method that evaluate if a move is legal.

Endpoints

The API expose 3 endpoint:

POST /move

Moves a piece in a give position (if the move is legal). Takes 2 arguments:

  • piece the label of the piece to move
  • target the location where to move the piece (with standard notation)
curl -d "piece=WP1&target=A6" -X POST http://127.0.0.1:5000/move

Illegal moves aren't executed and an error message is returned containing details about why the move is illegal.

GET /is-legal

Checks if a move is legal or not. Takes 2 arguments:

  • piece the label of the piece to move
  • target the location where to move the piece (with standard notation)

If the move is not legal, details about why the move is illegal are returned from the API.

curl -X GET http://127.0.0.1:5000/is-legal?piece=WP1&target=A6

GET /is-taken

Checks if a piece is taken. Takes 1 argument:

  • piece the label of the piece to move
curl -X GET http://127.0.0.1:5000/is-taken?piece=BK1

POST /restart

Restart the game by resetting the chessboard and the places

GET /

Draw the status of the chessboard with icons of pieces and their acronyms

chessboard

Pieces are described by a label:

  • W or B for the team
  • K, Q, B, N, R and P (N is for kNight) for the pieces
  • 1 to 8 to identify different pieces of the same type.

Examples:

  • White King: WK1
  • Black Queen: BQ1
  • Last White Pawn: WP8
  • Second Black Knight: BN2

Position use the standard notation with files (columns, from A to H) and ranks (rows, from 1 to 8). Chessboard space goes from A1 to H8.

Development

The project is written using Python (target version is 2.7) and uses Flask to expose the APIs and pytest is used for testing.

To install required library use:

pip install -r requirements.txt

To run a development server with live reload use:

FLASK_DEBUG=1 FLASK_APP=api.py python -m flask run

To run tests use

python -m pytest tests

References

py-chess-api's People

Contributors

zenkay avatar

Stargazers

Dheeraj Chhabra avatar Josh Shipton avatar MillionthOdin16 avatar  avatar Kenny ケニー avatar

Watchers

 avatar

py-chess-api's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.