项目作者: shubhendusaurabh

项目描述 :
A Javascript draughts/checkers library for move generation/validation, piece placement/movement, and draw detection
高级语言: JavaScript
项目地址: git://github.com/shubhendusaurabh/draughts.js.git
创建时间: 2015-12-16T12:15:36Z
项目社区:https://github.com/shubhendusaurabh/draughts.js

开源协议:Mozilla Public License 2.0

下载


A Javascript draughts or checkers library for move generation/validation, piece placement/movement, and draw detection.

Inspired from chess.js

draughts.js

draughts.js is a Javascript draughts library that is used for draughts move
generation/validation, piece placement/movement, and game status
detection - basically everything but the AI.

draughts.js is in development.

Installation

Download the draughts.js file & include it in your html page.

  1. <script src="/js/draughts.js"></script>

Example Code

The code below plays a complete game of draughts … randomly.

  1. var draughts = new Draughts();
  2. while (!draughts.game_over()) {
  3. var moves = draughts.moves();
  4. var move = moves[Math.floor(Math.random() * moves.length)];
  5. draughts.move(move);
  6. }
  7. console.log(draughts.pdn());

Sites Using draughts.js

Need a user interface? Try draughtsboardJS library.

API

Constructor: draughts([ fen ])

The draughts() constructor takes a optional parameter which specifies the board configuration
in Forsyth-Edwards Notation.

  1. // board defaults to the starting position when called with no parameters
  2. var draughts = new Draughts();
  3. // pass in a FEN string to load a particular position
  4. var draughts = new Draughts('W:W31-50:B1-20');

.ascii()

Returns a string containing an ASCII diagram of the current position.

  1. var draughts = new Draughts();
  2. // make some moves
  3. draughts.move('35-31');
  4. draughts.move('19-23');
  5. draughts.move('32-27');
  6. draughts.ascii();
  7. // +------------------------------+
  8. // | b b b b b |
  9. // | b b b b b |
  10. // | b b b b b |
  11. // | b b b 0 b |
  12. // | 0 0 b 0 0 |
  13. // | 0 w 0 0 w |
  14. // | w 0 w w 0 |
  15. // | w w w w w |
  16. // | w w w w w |
  17. // | w w w w w |
  18. // +------------------------------+

.clear()

Clears the board.

  1. draughts.clear();
  2. draughts.fen();
  3. // -> 'W:B:W' <- empty board

.fen()

Returns the FEN string for the current position.

  1. var draughts = new Draughts();
  2. // make some moves
  3. draughts.move('35-31');
  4. draughts.move('19-23');
  5. draughts.move('32-27');
  6. draughts.fen();
  7. // -> "B:W27,30,31,33,34,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50:B1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,20,23"

.game_over()

Returns true if the game has ended via no moves left, or no pieces rule. Otherwise, returns false.

  1. var draughts = new Draughts();
  2. draughts.game_over();
  3. // -> false

.get(square)

Returns the piece on the square:

  1. draughts.clear();
  2. draughts.put('b', '23') // put a black man on a5
  3. draughts.get('23');
  4. // -> { 'b' },
  5. draughts.get('50');
  6. // -> null

.history([ options ])

Returns a list containing the moves of the current game. Options is an optional
parameter which may contain a ‘verbose’ flag. See .moves() for a description of the
verbose move fields.

  1. var draughts = new Draughts();
  2. draughts.move('35-31');
  3. draughts.move('19-23');
  4. draughts.move('32-27');
  5. draughts.history();
  6. // -> ["35-30", "19-23", "32-27"]
  7. draughts.history({ verbose: true });
  8. // -> [{ color: 'w', from: 'e2', to: 'e4', flags: 'b', piece: 'p', san: 'e4' },
  9. // { color: 'b', from: 'e7', to: 'e5', flags: 'b', piece: 'p', san: 'e5' },
  10. // { color: 'w', from: 'f2', to: 'f4', flags: 'b', piece: 'p', san: 'f4' },
  11. // { color: 'b', from: 'e5', to: 'f4', flags: 'c', piece: 'p', captured: 'p', san: 'exf4' }]

.in_draw()

Returns true or false if the game is drawn (50-move rule or insufficient material).

  1. var draughts = new Draughts('4k3/4P3/4K3/8/8/8/8/8 b - - 0 78');
  2. draughts.in_draw();
  3. // -> true

.in_threefold_repetition()

TODO

.header()

Allows header information to be added to pdn output. Any number of key/value
pairs can be passed to .header().

  1. draughts.header('White', 'Shubhendu');
  2. draughts.header('Black', 'Saurabh');
  3. // or
  4. draughts.header('White', 'Shubhendu', 'Black', 'Saurabh', 'Date', '2016-??-??');

Calling .header() without any arguments returns the header information as an object.

  1. draughts.header();
  2. // -> { White: 'Shubhendu', Black: 'Saurabh', Date: '2016-??-??' }

.load(fen)

The board is cleared and the FEN string is loaded. Returns true if position was
successfully loaded, otherwise false.

  1. var draughts = new Draughts();
  2. draughts.load('W:W31-50:B1-20');
  3. // -> true
  4. draughts.load('W:W31-55:B1-20');
  5. // -> false, bad piece X

.load_pdn(pdn, [ options ])

Load the moves of a game stored in
Portable Draughts Notation.
Options is a optional parameter that contains a ‘newline_char’ which is a
string representation of a RegExp (and should not be pre-escaped) and defaults
to ‘\r?\n’). Returns true if the pdn was parsed successfully, otherwise false.

  1. var draughts = new Draughts();
  2. pdn = ['[Event "18th Computer Olympiad, 10x10 Draughts"]',
  3. '[Site "Leiden, NLD"]',
  4. '[Date "2015.07.04"]',
  5. '[Round "1"]',
  6. '[White "Scan"]',
  7. '[Black "Moby Dam (Jun 30 2015)"]',
  8. '[Result "1-0"]',
  9. '',
  10. '1. 34-30 19-23 2. 30-25 20-24 3. 33-29 24x33 4. 39x19 14x23 5. 40-34 9-14 6.',
  11. '44-39 14-20 7. 25x14 10x19 8. 50-44 5-10 9. 38-33 10-14 10. 42-38 23-28 11.',
  12. '32x23 18x40 12. 45x34 12-18 13. 35-30 7-12 14. 37-32 18-23 15. 44-40 12-18 16.',
  13. '41-37 1-7 17. 46-41 7-12 18. 47-42 23-28 19. 33x22 17x28 20. 32x23 19x28 21.',
  14. '38-32 13-19 22. 32x23 19x28 23. 43-38 8-13 24. 38-32 13-19 25. 32x23 19x28 26.',
  15. '42-38 11-17 27. 31-27 6-11 28. 38-33 18-23 29. 33x22 17x28 30. 37-31 11-17 31.',
  16. '27-22 28-32 32. 22x11 16x7 33. 48-42 12-18 34. 31-26 2-8 35. 36-31 7-12 36.',
  17. '40-35 15-20 37. 49-43 23-28 38. 34-29 12-17 39. 30-24 20-25 40. 42-38 25-30',
  18. '41. 38x27 30x19 42. 41-36 28-32 43. 27x38 19-23 44. 31-27 23x34 45. 39x30 8-12',
  19. '46. 30-24 3-8 47. 43-39 18-23 48. 27-21 23-28 49. 36-31 8-13 50. 35-30 14-19',
  20. '51. 21-16 12-18 52. 31-27 19-23 53. 38-33 28-32 54. 27x38 18-22 55. 24-20',
  21. '22-27 56. 30-24 17-21 57. 26x17 27-31 58. 17-12 31-36 59. 12-7 36-41 60. 38-32',
  22. '13-18 61. 7-2 23-29 62. 2-13 29x27 63. 13x47 4-10 64. 24-19 10-15 65. 47-24',
  23. '1-0'];
  24. draughts.load_pdn(pdn.join('\n'));
  25. // -> true
  26. draughts.fen()
  27. // -> "B:W27,30,31,33,34,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50:B1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,20,23"
  28. draughts.ascii()
  29. // +--------------------------+
  30. // | b b b b b |
  31. // | b b b b b |
  32. // | b b b b b |
  33. // | b b b 0 b |
  34. // | 0 0 b 0 0 |
  35. // | 0 w 0 0 w |
  36. // | w 0 w w 0 |
  37. // | w w w w w |
  38. // | w w w w w |
  39. // | w w w w w |
  40. // +--------------------------+

.move(move)

Attempts to make a move on the board, returning a move object if the move was
legal, otherwise null.

  1. var draughts = new Draughts();
  2. draughts.move('e4')
  3. // -> {flags: "n", from: 35, jumps: Array[0], piece: "w", piecesTaken: undefined, takes: Array[0], to: 30}

.moves([ options ])

Returns a list of legals moves from the current position. The function takes an optional parameter which controls the single-square move generation and verbosity.

  1. var draughts = new Draughts();
  2. draughts.moves();
  3. // -> {from: 16, jumps: Array[0], piecesTaken: undefined, takes: Array[0], to: 21}

.getLegalMoves(squareNum)

Returns a list of legals moves from the given square.

  1. var draughts = new Draughts();
  2. draughts.getLegalMoves();
  3. // -> [
  4. {
  5. "jumps": [],
  6. "takes": [],
  7. "from": 32,
  8. "to": 28
  9. },
  10. {
  11. "jumps": [],
  12. "takes": [],
  13. "from": 32,
  14. "to": 27
  15. }
  16. ]

.pdn([ options ])

Returns the game in pdn format. Options is an optional parameter which may include
max width and/or a newline character settings.

  1. var draughts = new Draughts();
  2. draughts.header('White', 'Plunky', 'Black', 'Plinkie');
  3. draughts.move('35-31');
  4. draughts.move('19-23');
  5. draughts.move('32-27');
  6. draughts.pdn({ max_width: 5, newline_char: '<br />' });
  7. // -> [SetUp "1"]<br />[FEN "W:W31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50:B1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20"]<br /><br />1. 35-30

.put(piece, square)

Place a piece on square where piece is an object with the form
{ piece: …, square: … }. Returns true if piece was successfully placed,
otherwise the board remains unchanged and false is returned. put() will fail
when passed an invalid piece or square.

  1. draughts.clear();
  2. draughts.put({ piece: 'w', square: 35}) // put a black pawn on a5
  3. // -> true

.remove(square)

Remove and return the piece on square.

  1. draughts.remove('35');
  2. // -> { type: 'p', color: 'b' },

.reset()

Reset the board to the initial starting position.

.turn()

Returns the current side to move.

  1. draughts.load('W:W31-50:B1-20')
  2. draughts.turn()
  3. // -> 'w'

.undo()

Takeback the last half-move, returning a move object if successful, otherwise null.

  1. var draughts = new Draughts();
  2. draughts.fen();
  3. // -> 'W:W31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50:B1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20'
  4. draughts.move({from: 35, to: 30});
  5. draughts.fen();
  6. // -> 'B:W30,31,32,33,34,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50:B1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20'
  7. draughts.undo();
  8. // -> { flags: "n", from: 35, jumps: Array[0], piece: "w", piecesTaken: undefined, takes: Array[0], to: 30 }
  9. draughts.fen();
  10. // -> 'W:W31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50:B1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20'
  11. draughts.undo();
  12. // -> null

.validate_fen(fen):

Returns a validation object specifying validity or the errors found within the
FEN string.

  1. draughts.validate_fen('');
  2. // -> { error: { code: 0, message: "no errors"}, error_number: 0, valid: true' }
  3. draughts.validate_fen('4r3/8/X12XPk/1p6/pP2p1R1/P1B5/2P2K2/3r4 w - - 1 45');
  4. // -> {error: {code: 1, message: "fen position is not a string"}, valid: false }

Maintainers

BUGS

  • Piece promotion may be buggy.

TODO

  • Investigate the use of piece lists (this may shave a few cycles off
    generate_moves() and attacked()).
  • Refactor API to use camelCase - yuck.
  • Add more robust FEN validation.