a_star = (start, board) => {
const boardWidth = board[0].length
const goal = board.length * boardWidth - 1
let openSet = [start]
const cameFrom = []
const gScore = []
gScore[start] = 0
const fScore = []
fScore[start] = h_score(start, board)
let i = 0
while(openSet.length > 0 && i < 100) {
i += 1
console.log({openSet})
const current = openSet.reduce((prev, curr) => fScore[curr] < fScore[prev] ? curr : prev, start)
if (current == goal) return reconstructPath(cameFrom, current)
openSet = openSet.filter(c => c != current)
const neighbors = []
const currentX = current % boardWidth
const currentY = Math.floor(current / board.length)
if (currentX < boardWidth - 1) neighbors.push([current + 1, 1])
if (currentY < board.length - 1) neighbors.push([current + boardWidth, 1])
if (neighbors.length == 2 && board[currentY][currentX] == 1) neighbors.push([current+boardWidth+1, 0])
for (let [neighbor, weight] of neighbors) {
const potentialG = gScore[current] + weight
let existingG = gScore[neighbor]
if (existingG == undefined) existingG = 9999
if (potentialG < existingG) {
cameFrom[neighbor] = current
gScore[neighbor] = potentialG
fScore[neighbor] = potentialG + h_score(neighbor, board)
if (!openSet.includes(neighbor)) openSet.push(neighbor)
}
}
}
}