implement posiiton swap

experiments
Peter Babič 3 years ago
parent dee5784bb9
commit 3b46f892ed
  1. 57
      src/App.svelte

@ -6,10 +6,10 @@
const numVertices = 15 const numVertices = 15
const adjacents = new Graph() const adjacents = new Graph()
const jumps = new Graph() const jumps = new Graph()
let emptys = [] let states = []
for (let i = 0; i < numVertices; i++) { for (let i = 0; i < numVertices; i++) {
emptys[i] = false states[i] = false
adjacents.addVertex(i) adjacents.addVertex(i)
jumps.addVertex(i) jumps.addVertex(i)
@ -18,8 +18,8 @@
} }
} }
emptys[0] = true states[0] = true
emptys[9] = true states[9] = true
adjacents.addEdge(0, 8) adjacents.addEdge(0, 8)
adjacents.addEdge(1, 7) adjacents.addEdge(1, 7)
@ -58,12 +58,33 @@
jumps.addEdge(11, 14) jumps.addEdge(11, 14)
const change = num => { const change = num => {
const indices = [...emptys.keys()].filter(i => emptys[i]) const indicesofHoles = [...states.keys()].filter(i => states[i])
const possibleJumps = jumps.adjacencyList[num].filter(hole =>
indicesofHoles.includes(hole)
)
if (states[num] == 0 && possibleJumps.length > 0) {
states[num] = 2
return
}
console.log( const indexOfPicked = states.indexOf(2)
num, const possibleMove = jumps.adjacencyList[num].filter(
jumps.adjacencyList[num].filter(v => indices.includes(v)) jump => jump == indexOfPicked
) )
if (states[num] == 1 && possibleMove.length == 1) {
const indexOfMoved = possibleMove[0]
states[num] = 0
states[indexOfMoved] = 1
return
}
if (states[num] == 2) {
states[num] = 0
}
} }
</script> </script>
@ -126,22 +147,36 @@
.circle { .circle {
width: 30px; width: 30px;
height: 30px; height: 30px;
background: red;
clip-path: circle(); clip-path: circle();
text-align: center; text-align: center;
} }
.red {
background-color: red;
}
.gray { .gray {
background-color: gray; background-color: gray;
} }
.green {
background-color: green;
}
.yellow {
background-color: yellow;
}
</style> </style>
<main> <main>
<div class="triangle"> <div class="triangle">
{#each emptys as _, i} {#each states as _, i}
<div <div
class="circle div{i}" class="circle div{i}"
class:gray={emptys[i]} class:red={states[i] == 0}
class:gray={states[i] == 1}
class:green={states[i] == 2}
class:yellow={states[i] == 3}
on:click={() => change(i)}> on:click={() => change(i)}>
{i} {i}
</div> </div>

Loading…
Cancel
Save