chess.js - 无法使用宽容解析器进行移动

问题描述 投票:0回答:1

我只能使用 SAN 和对象符号进行移动。宽容解析器不起作用。我尝试使用

{ permissive: true }
选项,但它似乎没有做任何事情。我错过了什么吗?

const game = new Chess();
game.move('e2-e4');
game.fen();

退货

rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1

const game = new Chess();
game.move('e4'); // Or game.move({ from: 'e2', to: 'e4' });
game.fen();

返回

rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1
。 顺便说一句,“e4”对我来说看起来比“e2-e4”更宽容......

javascript chess
1个回答
0
投票

简短回答:在浏览器上下文中,添加

sloppy
选项:

game.move('e2-e4', { sloppy: true })

为什么

在撰写本文时,chess.js 的最新版本是 v0.13.4,npm 模块和浏览器版本之间(仍然)存在差异。

Node.js

在 Node.js 中使用代码时,第一个代码片段执行移动:

import { Chess } from 'chess.js'

const game = new Chess();
game.move('e2-e4');
console.log(game.fen());

这具有预期的正确输出:

rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1

查看它在 repl.it

上运行

浏览器

在浏览器中,我们可以在包含此模块脚本时重现该问题:

<script type="module">
    import { Chess } from 'https://cdnjs.cloudflare.com/ajax/libs/chess.js/0.13.4/chess.min.js'
    
    const game = new Chess();
    console.log(game.move('e2-e4')); // null!
    console.log(game.fen());
</script> 

差异

源码中可以看出区别:

GitHub 上的源代码中,我们找到这个函数定义(标题):

  move(
    move: string | { from: string; to: string; promotion?: string },
    { strict = false }: { strict?: boolean } = {}
  ) {
 {
    /*
     * The move function can be called with in the following parameters:
     *
     * .move('Nxb7')       <- argument is a case-sensitive SAN string
     *
     * .move({ from: 'h7', <- argument is a move object
     *         to :'h8',
     *         promotion: 'q' })
     *
     *
     * An optional strict argument may be supplied to tell chess.js to
     * strictly follow the SAN specification.
     */

但是在CNDjs提供的源代码中(对于浏览器)我们发现:

move: function (move, options) { /* The move function can be called with in the following parameters: * * .move('Nxb7') <- where 'move' is a case-sensitive SAN string * * .move({ from: 'h7', <- where the 'move' is a move object (additional * to :'h8', fields are ignored) * promotion: 'q', * }) */ // allow the user to specify the sloppy move parser to work around over // disambiguation bugs in Fritz and Chessbase var sloppy = typeof options !== 'undefined' && 'sloppy' in options ? options.sloppy : false
请注意,第一个预见了 

strict

 选项,而第二个则提供 
sloppy
 选项。默认是相反的。

解决方案

因此,在浏览器上下文中,您应该提供

sloppy

 选项:

<script type="module"> import { Chess } from 'https://cdnjs.cloudflare.com/ajax/libs/chess.js/0.13.4/chess.min.js' const options = { sloppy: true }; const game = new Chess(); console.log(game.move('e2-e4', options)); console.log(game.fen()); </script>

© www.soinside.com 2019 - 2024. All rights reserved.