Chess.js How to access the various fields in .moves({ verbose: true})

58 Views Asked by At

I am an experienced Delphi programmer but totally new to JavaScript/TypeScript and I don't understand the structure of what is returned by the chess.js function .moves({ verbose: true}) and the appropriate JS/TS syntax to retrieve the data from the fields of the list of possible moves. For example, the following simple code lists 50 possible moves from the position given by the FEN string.

import { Chess } from "chess.js"
const analysisBoard = new Chess("r1bq1rk1/ppppbppp/4p3/4P3/5Q2/2NB1NP1/PP1P3P/R1B1K2R w KQ - 1 11")
console.log(analysisBoard.moves({ verbose: true}))

Move 26 in the list is both a capture and a check [san: "Bxh7+"]

26: (10) {color: "w", piece: "b", from: "d3",...}
color: "w"
piece: "b"
from: "d3"
to: "h7"
san: "Bxh7+"
flags: "c"
lan: "d3h7"
before: "r1bq1rk1/ppppbppp/4p3/4P3/5Q2/2NB1NP1/PP1P3P/R1B1K2R w KQ - 1 11"
after: "r1bq1rk1/ppppbppB/4p3/4P3/5Q2/2N2NP1/PP1P3P/R1B1K2R b KQ - 0 11"
captured: "p"

Would someone who is familiar with the structure and the appropriate JS/TS syntax please be so kind as to tell me how I would loop through the available legal moves (I understand how to use a "for" loop, finding any that are checks (i.e., contain the character "+") and/or are captures (either they contain the character "x", or flags: contains "c", or the "captured:" field is present) and then be able to access the data from some of the fields of these checks and/or captures, for example, "piece:", "from:", "san:" and "after:"? I understand how to work with strings and extract the things I need when I have the appropriate string inside a string variable, but I am stuck on finding and extracting the strings from the .moves structure. Perhaps the structure is an object holding key:value pairs and I need to use .map, but if that is the case I am unsure exactly how to use it in this case.

I thought that the structure might be just a list/array of strings. However, syntax like let moveStr=analysisBoard.moves[26], isn't getting me anywhere. When I try console.log(moveStr), it is undefined.

1

There are 1 best solutions below

0
vmishka On

I guessed that the structure is a list/array of objects. This website: https://www.freecodecamp.org/news/javascript-array-of-objects-tutorial-how-to-create-update-and-loop-through-objects-using-js-array-methods/ gave me some sample code that worked, e.g.,

import { Chess } from "chess.js"
const analysisBoard = new 
Chess("r1bq1rk1/ppppbppp/4p3/4P3/5Q2/2NB1NP1/PP1P3P/R1B1K2R w KQ - 1 11")
let possibleMoves = analysisBoard.moves({ verbose: true})
let movesThatAreChecks = possibleMoves.filter(possibleMoves => possibleMoves.san.includes("+"))
console.log(possibleMoves)
console.log(analysisBoard.ascii())
console.log(movesThatAreChecks)
console.log(movesThatAreChecks[0].after)
console.log(movesThatAreChecks[1].after)
let movesThatAreCaptures = possibleMoves.filter(possibleMoves => possibleMoves.san.includes("x"))
console.log(movesThatAreCaptures)