PHP Classes

PHP Chess Game Bundle: Manage a chess game in a Symfony application

Recommend this page to a friend!
  Info   View files Documentation   View files View files (32)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2022-06-02 (16 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 20 All time: 10,628 This week: 96Up
Version License PHP version Categories
chess-bundle 1.0Custom (specified...5PHP 5, Libraries, Games
Description Author

This package can manage a chess game in a Symfony application.

It comes with provider classes that implement the moves of a chess game.

The package classes were developed to integrate into a Symfony framework application that can render a chess game board on a Web page and process moves of the chess players.

Picture of Massimiliano Arione
  Performance   Level  
Name: Massimiliano Arione <contact>
Classes: 2 packages by
Country: Italy Italy
Innovation award
Innovation award
Nominee: 1x

Details

ChessBundle

Installation

Run composer require p-chess/chess-bundle

Configuration

Create a service that extends PChess\ChessBundle\HtmlOutput and implements required methods. You probably want to inject Symfony's router service here, and use it to return required URLs. Note that each route can be provided with an identifier for your game.

Create a configuration file, and use a content like the following:

# config/packages/chess.yaml
chess:
    output_service: App\YourOutputService

This is an example of how routes can be defined (using an "id" parameter as identifier):

# config/routes.yaml
move_start:
    path: /{id}/move/{from}
    methods: GET
    controller: ... # your controller action

move_cancel:
    path: /{id}
    methods: GET
    controller: ... # your controller action

move_promotion:
    path: /{id}/promote/{from}/{to}
    methods: GET
    controller: ... # your controller action

move_end:
    path: /{id}/move/{from}/{to}/{promotion}
    methods: GET
    controller: ... # your controller action
    defaults:
        promotion: ~

Usage

You can inject a service implementing \PChess\ChessBundle\ChessProviderInterface in your controller, then implement different actions, using provided \PChess\Chess\Chess object.

In your template, you can use Twig function chess_render(chess) to render the board. If you need to pass an identifier, use chess_render(chess, identifier) instead.

The main service you can use is \PChess\ChessBundle\SessionChessProvider. This service allows you to keep chess games in session, providing following methods:

  • `getChess($identifier, $fen)` to get main `\PChess\Chess\Chess` instance (as provided by interface)
  • `restart($identifier)` to restart the game
  • `save($identifier)` to save the game in session
  • `reverse($identifier)` to switch sides
  • `getAllowedMoves($chess, $from)` to get a list of currently allowed moves (optionally limited to `$from` square)

Using $identifier is not mandatory.

Styling

You can use provided _board.scss file to style the board:

@import '~@p-chess/chess-bundle/scss/board';

Don't forget to update your frontend files, using npm or yarn.

The final result should be something like this:

<img src="https://user-images.githubusercontent.com/179866/114995898-92cf1b80-9e9e-11eb-8e99-75a60bbba6bd.png" alt="">

Persisting a Chess object

You can easily save a Chess object into Doctrine (or other kind of mapping libraries), using two fields/properties: fen, and history. The first one is a simple string. The second one can be a simple_array (for Doctrine), where you should put the result of Mover::getHistoryStrings() method. When retrieving an object, you should use fen and the result of Mover::getHistoryEntries() to build back your Chess object.

  Files folder image Files  
File Role Description
Files folder imageassets (1 file, 2 directories)
Files folder imageconfig (1 file)
Files folder imagesrc (6 files, 2 directories)
Files folder imagetests (7 files, 2 directories)
Accessible without login Plain text file .php-cs-fixer.php Example Example script
Accessible without login Plain text file buddy.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpstan-baseline.neon Data Auxiliary data
Accessible without login Plain text file phpstan.neon Data Auxiliary data
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  assets  
File Role Description
Files folder imageimg (1 file)
Files folder imagescss (1 file)
  Accessible without login Plain text file package.json Data Auxiliary data

  Files folder image Files  /  assets  /  img  
File Role Description
  Accessible without login Image file pieces.png Data Auxiliary data

  Files folder image Files  /  assets  /  scss  
File Role Description
  Accessible without login Plain text file _board.scss Data Auxiliary data

  Files folder image Files  /  config  
File Role Description
  Accessible without login Plain text file services.xml Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
Files folder imageDependencyInjection (3 files)
Files folder imageTwig (1 file)
  Plain text file ChessBundle.php Class Class source
  Plain text file ChessProviderInterface.php Class Class source
  Plain text file HtmlOutput.php Class Class source
  Plain text file Mover.php Class Class source
  Plain text file SessionChessProvider.php Class Class source
  Plain text file SimpleChessProvider.php Class Class source

  Files folder image Files  /  src  /  DependencyInjection  
File Role Description
  Plain text file ChessExtension.php Class Class source
  Plain text file CompilerPass.php Class Class source
  Plain text file Configuration.php Class Class source

  Files folder image Files  /  src  /  Twig  
File Role Description
  Plain text file ChessExtension.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageDependencyInjection (2 files)
Files folder imageTwig (1 file)
  Plain text file BypassFinalHook.php Class Class source
  Plain text file ChessBundleTest.php Class Class source
  Plain text file HtmlOutputStub.php Class Class source
  Plain text file HtmlOutputTest.php Class Class source
  Plain text file MoverTest.php Class Class source
  Plain text file SessionChessProviderTest.php Class Class source
  Plain text file SimpleChessProviderTest.php Class Class source

  Files folder image Files  /  tests  /  DependencyInjection  
File Role Description
  Plain text file ChessExtensionTest.php Class Class source
  Plain text file CompilerPassTest.php Class Class source

  Files folder image Files  /  tests  /  Twig  
File Role Description
  Plain text file ChessExtensionTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:20
This week:0
All time:10,628
This week:96Up
For more information send a message to info at phpclasses dot org.