Skip to content

Instantly share code, notes, and snippets.

@stevekinney
Created May 11, 2018 15:27
Show Gist options
  • Select an option

  • Save stevekinney/b24a056fea1fef189c2c7fdd809ca43d to your computer and use it in GitHub Desktop.

Select an option

Save stevekinney/b24a056fea1fef189c2c7fdd809ca43d to your computer and use it in GitHub Desktop.
class Board {
constructor(min = 1, max = 30) {
this.spaces = {};
for (let space = min; space <= max; space++) {
this.spaces[space] = space;
}
// Ladders
this.spaces[2] = 21;
this.spaces[4] = 7;
this.spaces[10] = 25;
this.spaces[19] = 28;
// Snakes
this.spaces[26] = 0;
this.spaces[20] = 8;
this.spaces[16] = 3;
this.spaces[18] = 6;
}
}
const findMoves = () => {
const board = new Board();
let currentSpace = 0;
let potentialNewSpace = 1;
let bestRoll = 1;
const diceRolls = [];
while (currentSpace < 30) {
for (let diceRoll = 1; diceRoll <= 6; diceRoll++) {
const potential = board.spaces[currentSpace + diceRoll];
if (potential > potentialNewSpace) {
potentialNewSpace = potential;
bestRoll = diceRoll;
}
}
diceRolls.push(bestRoll);
currentSpace = potentialNewSpace;
bestRoll = 1;
}
return diceRolls;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment