Move Ordering in Javascript Chess Engine chess.js

i am by far a javascript specialist but i am trying to learn it a little.

For the move ordering in my chess engine (i follow a tutorial), i would like to check for captures first.
In the chess.js library the capture moves are listed with the flag ‘c’.

game.moves({ flags: 'c', verbose: true })

so im trying to make a shopping list with first the capture moves, and after that, all the rest moves.
i tried it like this:

var children = [                                                                    
    [game.ugly_moves({ flags: 'c', verbose: true })],
    [game.ugly_moves({ verbose: true })]
];

like this:

var children = [game.ugly_moves({ flags: 'c', verbose: true })] + [game.ugly_moves({ verbose: true })];

But nothing seems to work.

My code can be seen at http://users.telenet.be/gianc/chessai/js/main.js at line 153

other info: https://github.com/jhlywa/chess.js/blob/master/README.md

Any input is greatly appreciated!

TIA

70 thoughts on “Move Ordering in Javascript Chess Engine chess.js”

  1. Based on the documentation at https://github.com/jhlywa/chess.js/blob/master/README.md#moves-options-, the flags are not passed into moves(), but rather, are returned from moves() as an attribute associated with each legal move. Therefore, to get a list of all captures, you will need to apply a filter on the resulting array of all legal moves. Something along the following lines…

    let allLegalMoves = game.moves( { verbose: true } );
    let captures = allLegalMoves.filter( move => move.flags.includes( 'c' ) );
    

    Note the use of indexOf() as there can be multiple flags per move.

    EDIT: As a follow up to my original answer, another option is to assign values to the flags, and create a new attribute associated with each move called, say, importance. This new attribute will be constructed by assigning values to the critical flags and summing the flags to get an importance ranking for each move, and then sorting the moves in descending order.

    For example, let’s say that promotions (‘p’ flag) are most important followed by captures (‘c’ flag) followed by anything else. Let’s then assign, say, the value of 16 for each ‘p’ flag, and 8 for each ‘c’ flag. Note that in this case that a promotion that also captures will have a sum of 24 putting this move at the top of the list(!).

    The code will appear as follows…

    let allLegalMoves = game.moves( { verbose: true } );
    
    for ( let move of allLegalMoves ) {
      move.importance = 0
        + move.flags.includes( 'p' ) ? 16 : 0 +
        + move.flags.includes( 'c' ) ? 8 : 0;   
    }
    
    allLegalMoves.sort( ( a, b ) => b.importance - a.importance );
    

    And of course there’s nothing prohibiting you from including other factors to feed into importance to aid in ranking the moves…

    Reply

Leave a Comment