refactor adjacentJump

experiments
Peter Babič 3 years ago
parent fd360726d7
commit 9feb28eaf0
  1. 37
      src/App.svelte

@ -62,42 +62,43 @@
jumps.addEdge(9, 14) jumps.addEdge(9, 14)
jumps.addEdge(11, 14) jumps.addEdge(11, 14)
const change = clicked => { const change = currentlyClicked => {
const indexOfPicked = state.indexOf(pickedPole) const previouslyPicked = state.indexOf(pickedPole)
const emptyHoles = [...state.keys()].filter(i => state[i] == emptyHole) const emptyHoles = [...state.keys()].filter(i => state[i] == emptyHole)
const possibleJumps = jumps.adjacencyList[clicked].filter(jump => const possibleJumps = jumps.adjacencyList[
emptyHoles.includes(jump) currentlyClicked
) ].filter(jump => emptyHoles.includes(jump))
if ( if (
state[clicked] == standingPole && state[currentlyClicked] == standingPole &&
indexOfPicked == -1 && previouslyPicked == -1 &&
possibleJumps.length > 0 possibleJumps.length > 0
) { ) {
state[clicked] = pickedPole state[currentlyClicked] = pickedPole
return return
} }
const possibleMove = jumps.adjacencyList[clicked].filter( const adjacentJump = jumps.adjacencyList[currentlyClicked].indexOf(
jump => jump == indexOfPicked previouslyPicked
) )
if (state[clicked] == emptyHole && possibleMove.length == 1) { if (state[currentlyClicked] == emptyHole && adjacentJump != -1) {
const indexOfMoved = possibleMove[0] const jumpedOver = adjacents.adjacencyList[
const jumpedOver = adjacents.adjacencyList[clicked].filter(common => currentlyClicked
adjacents.adjacencyList[indexOfMoved].includes(common) ].filter(common =>
adjacents.adjacencyList[previouslyPicked].includes(common)
) )
state[clicked] = standingPole state[currentlyClicked] = standingPole
state[indexOfMoved] = emptyHole state[previouslyPicked] = emptyHole
state[jumpedOver] = emptyHole state[jumpedOver] = emptyHole
return return
} }
if (state[clicked] == pickedPole) { if (state[currentlyClicked] == pickedPole) {
state[clicked] = standingPole state[currentlyClicked] = standingPole
} }
} }
</script> </script>

Loading…
Cancel
Save