Code Monkey home page Code Monkey logo

chessmate's Introduction

A dead-simple chess validation gem for Rails

Coverage Status

About

ChessMate was built in around 2 months to be as easy to use as possible to quickly and easily bootstrap a chess game in Rails. The original idea came from a bootcamp exercise to build a working chess game, and so the code that was written there has been ported to ChessMate in order to benefit future programmers.

ChessMate is designed to be a backend tool to validate possible chess moves. Therefore, it is intentionally as agnostic to your frontend as possible to integrate into many different frameworks. Because it's designed to be flexible, it even supports custom game boards and can allow out-of-turn movement!

Click here to play with a Rails app running ChessMate! (Or check out the source code here.)

Usage

Simply add ChessMate to your Gemfile:

gem 'chessmate'

and then you can require it in a model, controller, etc.

require 'chessmate'

From there, you are free to utilize all the functions and features of ChessMate!

Building a new game and playing

game = ChessMate.new

Moving pieces works based on chess notation: for example, a common first move is e2 to e4. ChessMate accepts these squares as arguments to the move method, so you don't need to specify a piece type like in normal chess notation. To make this move in ChessMate:

game.move('e2', 'e4')

If a move is invalid, ChessMate.move will return false, and the board and game state will not update. You also do not need to specify for any "special" moves, like castling or en passant, as ChessMate can handle this for you.

Other functions you might call:

game.promote?(square)           # Check if a square is capable of pawn promotion
game.promote!(square, piece)    # Promote a promotable pawn, accepts 'rook'/'knight'/'bishop'/'queen'
game.in_check?                  # Determine if either color is currently in check
game.checkmate?(color)          # Determine if the game is over, accepts 'W'/'B'
game.draw?(color)               # Same as checkmate, for draw

Using these functions, it's quite simple to build a working chess game!

Custom games

With ChessMate, it's very easy to build your own custom games. Below is a list of all the defaults ChessMate uses to build a game board, but they can be modified and passed into the ChessMate.new method as named arguments in any order, and ChessMate will build a game around your custom params.

For instance, for a custom board, on turn 10, with the white king in check:

custom_board = [
          [nil, nil, nil, nil, 'BK', nil, nil, nil],
          [nil, nil, nil, nil, 'BQ', nil, nil, nil],
          [nil, nil, nil, nil, nil, nil, nil, nil],
          [nil, nil, nil, nil, nil, nil, nil, nil],
          [nil, nil, nil, nil, nil, nil, nil, nil],
          [nil, nil, nil, nil, nil, nil, nil, nil],
          ['WP', nil, nil, nil, nil, nil, nil, nil],
          [nil, nil, nil, nil, 'WK', nil, nil, nil]
        ]
turn = 10
in_check = { white: true, black: false }

game = ChessMate.new(board: custom_board, turn: turn, in_check: in_check)

ChessMate can build a game with those parameters!

Here is a list of all the defaults:

board: [
  %w[BR BN BB BQ BK BB BN BR],
  %w[BP BP BP BP BP BP BP BP],
  [nil, nil, nil, nil, nil, nil, nil, nil],
  [nil, nil, nil, nil, nil, nil, nil, nil],
  [nil, nil, nil, nil, nil, nil, nil, nil],
  [nil, nil, nil, nil, nil, nil, nil, nil],
  %w[WP WP WP WP WP WP WP WP],
  %w[WR WN WB WQ WK WB WN WR]
]

turn: 1

promotable: nil

en_passant: { white: nil, black: nil }

in_check: { white: false, black: false }

castling: {
  white: {
    kingside: true,
    queenside: true
  },
  black: {
    kingside: true,
    queenside: true
  }
}

Contributing

ChessMate is open to contributions! If you have a feature request, a bug, or a suggestion, please open an issue! Please note that a review is required and passing TravisCI build before your PR can be merged with master.

chessmate's People

Contributors

ty-porter avatar

Stargazers

 avatar

Watchers

 avatar

chessmate's Issues

Kings are able to move into check

Kings are able to move into check under any circumstances. Once they move into check, their next move correctly updates the @in_check instance variable to true for their color.

This is likely due to logic for this being stuck in the move method and should be updated to the update method if this is the case.

Black moves do not register as putting white in check

Black moves do not appear to register correct notation when putting white in check.

image
(From demo site)

Relevant code snippet for generating turn notiation:

  @game = ChessMate.new

  @game.move(ORIGIN, DESTINATION) # Repeated until white in check

  @game.turn

  # From above example...
  # => 'Qxf2+'

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.