0.26.0 • Published 1 year ago

@loloof64/chessboard-component v0.26.0

Weekly downloads
33
License
MIT
Repository
github
Last release
1 year ago

Chess board component

A chess board web component

Component reference

Usage

  • Run npm init if needed
  • Install the package with npm i @loloof64/chessboard-component
  • load the script from node_modules/@loloof64/chessboard-component/dist (just load it, as it will define the web component loloof64-chessboard).
<loloof64-chessboard
    size="300"
    background="crimson"
    coordinates_color="yellow"
    white_cell_color="navajowhite"
    black_cell_color="peru"
    reversed="true"
    origin_cell_color="red"
    target_cell_color="green"
    dnd_cross_color="purple"
    white_player_human="true"
    black_player_human="false"
    move_highlight_color="red"
    last_move_visible="false"
    coordinates_visible="false"
></loloof64-chessboard>

Attributes

NamePurposeTypeDefault
sizeCommon size (width/height) of the board in pixelsnumber100.0
backgroundBackground color of the board outside zonestring#124589
coordinatesColorColor of the coordinates around the boardstringyellow
whiteCellColorBackground color of the white cells of the boardstringnavajowhite
blackCellColorBackground color of the black cells of the boardstringperu
reversedWhether black side is on top or notbooleanfalse
origin_cell_colorColor of the origin cell of the Drag and Dropstringcrimson
target_cell_colorColor of the current target cell of the Drag and DropstringForestGreen
dnd_cross_colorColor of the Drag and Drop cross indicatorstringDimGrey
promotion_dialog_titleTitle of the promotion selection dialogstringSelect the promotion piece
white_player_humanTrue if the white player is human, false for external (1)booleantrue
black_player_humanTrue if the black player is human, false for external (1)booleantrue
move_highlight_colorColor of the last move highlight arrowstringCadetBlue
coordinates_visibleTrue if the coordinates should be visible, false otherwisebooleantrue
last_move_visibleTrue if the last move arrow should be visible, false otherwisebooleantrue

(1) External player means that, instead of playing its move with interaction on the board, call the method playMove in order to commit its move. A simple use case would be to let an engine play.

A little word of caution

When chaining methods/events, you may experience some update issues. Indeed, often the cpu needs some time in order to update the component state. If you run into such issues, don't hesitate to use setTimeout with some milliseconds or hundreds of milliseconds

Methods

  • newGame(startPositionFen): Starts a new game with the given position in Forsyth-Edwards Notation. If the startPositionFen string is not given, will use the default chess start position.
  • isWhiteTurn() : Boolean. true if it is White turn, false otherwise.
  • getCurrentPosition(): String. Returns the current position in Forsyth-Edwards Notation.
  • playMove({startFile, startRank, endFile, endRank, promotion = 'q'}): Tries to play the given move on the component, only if the current player is defined as an external user. Returns a boolean, saying if the move has been commited or not. All coordinates, integers, start from 0 (file 0 = 'A', rank 0 = '1'). Valid promotion values are 'q', 'r', 'b' and 'n'.
  • playMoveSan(moveSan): Tries to play the given move SAN on the board, only if the current player is defined as an external user. Returns a boolean, saying if the move has been commited or not.
  • setPositionAndLastMove({positionFen, fromFileIndex, fromRankIndex, toFileIndex, toRankIndex}) : you can set up the position and last move arrow, if the game is not in progress (otherwise won't have any effect). Returns true if the position and last move could be set, false otherwise. If no parameter given or wrong values, then it will clear last move arrow and sets the board to the position before the first move. Particularly useful for history managers.
  • stop(): we stop the game.
  • gameIsInProgress(): returns a boolean telling us if the game is in progress, or no game is running.
  • gamePgn({whiteName, blackName}) : returns the game pgn as a string. Only sets white and black names if they are defined. Each missing field will be replaced by an empty string.

Events

  • checkmate : Informs that a checkmate has just happened on the board. The payload detail.whiteTurnBeforeMove of the event tells if the side that checkmated were White or Black.
  • stalemate : Informs that a stalemate has just happended on the board. No additional payload.
  • perpetual-draw: Informs that a 3-fold repetitions draw has just happened on the board. No additional payload.
  • missing-material-draw : Informs that a draw by missing material has just happened on the board. No additional payload.
  • fifty-moves-draw: Informs that a draw by the 50 moves rule has just happened on the board. No additional payload.
  • waiting-manual-move: Informs that the component has just landed in the 'waiting manual move' state. That means that the move for the current player has to be commited with a call to playMove() (and with a legal move of course, the only way to progress in the game). No additional payload, but you should call isWhiteTurn() and getCurrentPosition() as clues.
  • move-done: Informs that a move has happened on the board, whatever the way (human or external player). The payload detail.moveObject is an object with all the details : moveNumber, whiteTurn, moveFan, moveSan, fromFileIndex, fromRankIndex, toFileIndex, toRankIndex.

Developers

You can build with the command (in the terminal) $ yarn build from the root of the project. Result will be in the dist folder.

But don't forget first to install all dependencies, with NodeJS : yarn.

Credits

Pieces vectors definitions from CBurnett and found on Wikimedia commons.

Using ChessJS library, which is bundled in the produced script.

Acknowledgement

  • Wails creator and developpers, SvelteJs creator and developpers
  • A shot of code Youtube channel, for having posted this short tutorial video
  • Contributors to SvelteJs discord server.
0.26.0

1 year ago

0.25.0

3 years ago

0.23.1

3 years ago

0.23.0

3 years ago

0.24.0

3 years ago

0.22.0

3 years ago

0.19.22

4 years ago

0.19.24

4 years ago

0.19.26

4 years ago

0.19.28

4 years ago

0.19.8

4 years ago

0.20.0

4 years ago

0.19.20

4 years ago

0.19.4

4 years ago

0.19.6

4 years ago

0.21.0

4 years ago

0.19.12

4 years ago

0.19.14

4 years ago

0.19.16

4 years ago

0.19.18

4 years ago

0.19.10

4 years ago

0.19.2

4 years ago

0.19.0

4 years ago

0.18.2

4 years ago

0.18.4

4 years ago

0.18.6

4 years ago

0.18.0

4 years ago

0.16.4

5 years ago

0.16.0

5 years ago

0.16.2

5 years ago

0.15.10

5 years ago

0.15.6

5 years ago

0.15.8

5 years ago

0.15.4

5 years ago

0.15.2

5 years ago

0.15.0

5 years ago

0.14.2

5 years ago

0.14.4

5 years ago

0.12.0

5 years ago

0.13.0

5 years ago

0.14.0

5 years ago

0.8.8

5 years ago

0.8.10

5 years ago

0.10.0

5 years ago

0.10.2

5 years ago

0.8.4

5 years ago

0.8.6

5 years ago

0.8.0

5 years ago

0.6.2

5 years ago

0.8.2

5 years ago

0.7.0

5 years ago

0.6.0

5 years ago

0.5.0

5 years ago

0.4.2

5 years ago

0.3.0

5 years ago

0.2.0

5 years ago

0.3.2

5 years ago

0.4.0

5 years ago

0.1.0

5 years ago