I'm trying to make a function that sort moves based on the evaluation of the board and it seems not efficient because when I concant listA and listB
some index have undefined value and I can't find the reason why so I asked this.
Here's my sort function:
function sortMoves(chess)
{
const listA = [], listB = chess.moves();
const scores = [];
const start = Date.now();
// calc best moves
const moves = listB;
for (const i in moves)
{
const state = chess.move(moves[i], {
promotion: 'q'
});
scores.push(evaluate(chess.board())); // Should I really set the score to the evaluation of the board?
chess.undo();
}
// sort move
// sort the first 6 moves
for (var i = 0; i < Math.min(6, listB.length); i++)
{
let maxEval = -Infinity;
let maxIndex = 0;
// loop through the scores arr
// and find the maxEval
for (var j = 0; j < scores.length; j++)
{
if (scores[j] > maxEval)
{
maxEval = scores[j];
maxIndex = j;
}
}
scores[maxIndex] = -Infinity;
// put the best score in listA
listA.push(moves[maxIndex]);
// remove it on listB
listB.splice(maxIndex, 1);
}
// in return, some values is undefined
return listA.concat(listB);
}
EDIT: I found the bug, when I do splice
on listB, it also affects the moves arr since I initialized it as moves = listB
so what I did is moves = [...listB]
But I don't think it worked because I when I tried moves = sortMoves(chess)
, it's slower than normal like moves = chess.moves()
So is there another problem in my sorting function?
EDIT2: It's actually the sortMoves()
it self is what it lags, I tried adding up all cut offs. When there's no sort moves. e4 depth3
takes 520 cutoffs
but with the sortMoves()
it takes 550 cutoffs
.
question from:
https://stackoverflow.com/questions/65840690/how-to-order-chess-moves-properly-based-on-the-evaluation-of-the-board 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…