diff --git a/examples/index.html b/examples/index.html index 3fa2717..478b1ff 100644 --- a/examples/index.html +++ b/examples/index.html @@ -6,13 +6,13 @@ A Star Puzzle Solver - Example 01 +
-
@@ -32,6 +32,12 @@
+
+ + + + +
@@ -44,14 +50,6 @@
- -
-
- - -
-
-
@@ -60,6 +58,8 @@ + + \ No newline at end of file diff --git a/examples/index.js b/examples/index.js index 37541cd..7a39d19 100644 --- a/examples/index.js +++ b/examples/index.js @@ -1,5 +1,5 @@ $(document).ready(function () { - const initialState = [4, 5, 8, 0, 1, 6, 7, 2, 3]; + const initialState = [1, 8, 2, 0, 4, 3, 7, 6, 5]; renderTDList(initialState); $("#btnRandomize").on("click", function () { @@ -25,56 +25,44 @@ function renderTDList(stateList) { function getActualState() { const list = $("td.td-puzzle"); - let state = [[], [], []]; const stateList = []; for (let td of list) { - stateList.push($(td).text()); + stateList.push(Number($(td).text())); } - for (let i in stateList) { - if (i <= 2) { - state[0].push(Number(stateList[i])); - } else if (i <= 5) { - state[1].push(Number(stateList[i])); - } else { - state[2].push(Number(stateList[i])); - } - } - return state; + return stateList; } function getSolution() { - const aStar = AStarPuzzleSolver; const state = getActualState(); - console.log(state); try { - const solution = aStar.solvePuzzle(state); - showResults(solution); - viewSolution(solution); + const result = AStarPuzzleSolver.solvePuzzle(state); + + showResults(result); + viewSolution(result); } catch (err) { - alert( - "O estado inicial é solucionável! Por favor, gere outro estado e tente novamente." - ); + alert(err); } } -function showResults(solution) { +function showResults(result) { const markup = `
  • Problema resolvido!
  • -
  • Custo final: ${solution.evaluationFunctionValue.g}
  • -
  • Nós expandidos: ?
  • -
  • Início da fronteira: ?
  • +
  • Custo do caminho: ${result.pathCost}
  • +
  • Nós expandidos: ${result.expandedNodes}
  • +
  • Iterações: ${result.iterations}
  • `; $("#results").html(markup); } -async function viewSolution(solution) { +async function viewSolution(result) { // const markup = ` // `; - let states = getListOfStates(solution, []); - awaitsToRender(states); + const states = result.solution.map((s) => s.state); + const operations = result.solution.map((s) => s.operation); + awaitsToRender(states, operations); // $("#solution").html(markup); } @@ -83,14 +71,19 @@ function timer(ms) { return new Promise((res) => setTimeout(res, ms)); } -async function awaitsToRender(states) { - for (let s of states) { - console.log(s); +async function awaitsToRender(states, operations) { + for (let i in states) { await timer(1200); - renderTDList(transformStateInArray(s)); + renderTDList(states[i]); + showOperation(operations[i]); } } +function showOperation(op) { + $(".operation-item").attr("hidden", ""); + $(`#MOVE_${op}`).removeAttr("hidden"); +} + function transformStateInArray(state) { let array = []; for (let l in state) {