英文:
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('e2-e4', { 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 'chess.js'
const game = new Chess();
game.move('e2-e4');
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 -->
<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>
<!-- 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('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.
*/
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('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
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 -->
<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>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论