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

huangapple go评论165阅读模式
英文:

chess.js - unable to make a move using the permissive parser

问题

只返回翻译好的部分:

"我只能使用标准代数记法和对象表示法进行移动。宽松解析器不起作用。我尝试使用 { 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'); // 或者 game.move({ from: 'e2', to: 'e4' });
game.fen();

返回 rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1
顺便说一下,'e4' 看起来比 'e2-e4' 对我来说更宽松..."

英文:

I can only make moves using the SAN and object notations. The permissive parser doesn't work. I tried using the { permissive: true } option but it doesn't seem to do anything. Did I miss something?

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

Returns 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();

Returns rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1.
And by the way, 'e4' looks more permissive than 'e2-e4' to me...

答案1

得分: 2

短回答:在浏览器环境中,添加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 } = {}
)

但在由CNDjs提供的源代码(用于浏览器的版本)中,我们找到了:

move: function (move, options) {
    // 允许用户指定sloppy移动解析器,以解决Fritz和Chessbase中的过度歧义问题
    var sloppy =
        typeof options !== 'undefined' && 'sloppy' in options
        ? options.sloppy
        : false
}

请注意,第一个版本提供了一个strict选项,而第二个版本提供了一个sloppy选项。默认值相反。

Node.js模块随着时间的推移发生了变化,将此选项的默认值从严格更改为sloppy,从而将sloppy选项替换为strict选项。请参阅GitHub项目的一些旧分支,这些分支仍具有先前的行为。

解决方案

因此,在浏览器环境中,您应该提供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>
英文:

Short answer: In a browser context, add the sloppy option:

game.move(&#39;e2-e4&#39;, { sloppy: true })

Why

At the time of writing, the latest version of chess.js is v0.13.4 and there is (still) a difference between the npm module and the browser version.

Node.js

When using your code in Node.js, the first snippet performs the move:

import { Chess } from &#39;chess.js&#39;

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

This has the expected, correct output:

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

See it run on repl.it

Browser

In a browser, we can reproduce the issue when including this module script:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;script type=&quot;module&quot;&gt;
    import { Chess } from &#39;https://cdnjs.cloudflare.com/ajax/libs/chess.js/0.13.4/chess.min.js&#39;
    
    const game = new Chess();
    console.log(game.move(&#39;e2-e4&#39;)); // null!
    console.log(game.fen());
&lt;/script&gt; 

<!-- end snippet -->

The Difference

The difference can be seen in the source code:

In the source code on GitHub we find this function definition (heading):

  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(&#39;Nxb7&#39;)       &lt;- argument is a case-sensitive SAN string
     *
     * .move({ from: &#39;h7&#39;, &lt;- argument is a move object
     *         to :&#39;h8&#39;,
     *         promotion: &#39;q&#39; })
     *
     *
     * An optional strict argument may be supplied to tell chess.js to
     * strictly follow the SAN specification.
     */

But in the source code provided by CNDjs (for browsers) we find:

move: function (move, options) {
      /* The move function can be called with in the following parameters:
       *
       * .move(&#39;Nxb7&#39;)      &lt;- where &#39;move&#39; is a case-sensitive SAN string
       *
       * .move({ from: &#39;h7&#39;, &lt;- where the &#39;move&#39; is a move object (additional
       *         to :&#39;h8&#39;,      fields are ignored)
       *         promotion: &#39;q&#39;,
       *      })
       */

      // allow the user to specify the sloppy move parser to work around over
      // disambiguation bugs in Fritz and Chessbase
      var sloppy =
        typeof options !== &#39;undefined&#39; &amp;&amp; &#39;sloppy&#39; in options
          ? options.sloppy
          : false

Note that the first one foresees a strict option, while the second provides a sloppy option. The default is the opposite.

The node.js module evolved over time, changing the default for this option from strict to sloppy, thereby replacing the sloppy option with the strict option. See some of the stale branches of the GitHub project which still have the previous behaviour.

Solution

So in a browser context you should provide the sloppy option:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;script type=&quot;module&quot;&gt;
    import { Chess } from &#39;https://cdnjs.cloudflare.com/ajax/libs/chess.js/0.13.4/chess.min.js&#39;
    
    const options = { sloppy: true };
    const game = new Chess();
    console.log(game.move(&#39;e2-e4&#39;, options));
    console.log(game.fen());
&lt;/script&gt; 

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年8月5日 16:47:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76840841.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定