function playGame2(pos1, pos2) {
let p1Wins = 0;
let p2Wins = 0;
const play = (pos1, score1, pos2, score2, paths, isPlayer1) => {
if (score1 >= 21) {
p1Wins += paths;
return;
}
if (score2 >= 21) {
p2Wins += paths;
return;
}
for (const [dice, nPaths] of dicePaths) {
if (isPlayer1) {
const newPos = ((pos1 + dice - 1) % 10) + 1;
play(newPos, score1 + newPos, pos2, score2, paths * nPaths, !isPlayer1);
} else {
const newPos = ((pos2 + dice - 1) % 10) + 1;
play(pos1, score1, newPos, score2 + newPos, paths * nPaths, !isPlayer1);
}
}
};
play(pos1, 0, pos2, 0, 1, true);
return Math.max(p1Wins, p2Wins);
}