-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
308574e
commit e69966c
Showing
5 changed files
with
259 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
import { wait } from "../helper/Timmer"; | ||
|
||
type Node = { | ||
id: string, | ||
prevNode: string|null, | ||
gn: number, | ||
hn: number, | ||
fn: number | ||
} | ||
|
||
export class Astar { | ||
path:any = []; | ||
path_b:any = []; | ||
track: {[key: string]: any} = {}; | ||
visited:string[] = []; | ||
|
||
public async search(graph:any, hurestic:any, start: any, goal: any) { | ||
let queue: Node[] = []; | ||
|
||
queue.push({ | ||
id: start, | ||
prevNode: null, | ||
gn: 0, | ||
hn: this.calculate_hn(goal, start), | ||
fn: this.calculate_hn(goal, start) + 0 | ||
}); | ||
|
||
while (queue.length > 0) { | ||
var node = queue.shift(); | ||
|
||
document.getElementById(node!.id)!.firstElementChild!.classList.remove('boom-visited'); | ||
document.getElementById(node!.id)!.firstElementChild!.classList.add('marker'); | ||
await wait(15); | ||
document.getElementById(node!.id)!.firstElementChild!.classList.remove('marker'); | ||
document.getElementById(node!.id)!.firstElementChild!.classList.add('visited'); | ||
|
||
if (node!.id === goal) { | ||
console.log(hurestic); | ||
console.log(this.visited); | ||
console.log(this.track) | ||
console.log(node); | ||
|
||
this.solve(node!, false) | ||
this.drawPath() | ||
break; | ||
} | ||
|
||
for (var neighbour of graph[node!.id]) { | ||
if (!this.visited.includes(node!.id) && !this.visited.includes(neighbour) && !document.getElementById(neighbour)!.firstElementChild!.classList.contains('wall')){ | ||
let new_gn = node!.gn + this.getWeight(neighbour); | ||
|
||
let existingNode = this.hasNode(queue, neighbour); | ||
|
||
console.log(existingNode); | ||
if(existingNode!=null) { | ||
existingNode.prevNode = node!.id, | ||
existingNode.gn = new_gn, | ||
existingNode.hn = this.calculate_hn(goal, neighbour) | ||
} else { | ||
queue.push({ | ||
id: neighbour, | ||
prevNode: node!.id, | ||
gn: new_gn, | ||
hn: this.calculate_hn(goal, neighbour), | ||
fn: this.calculate_hn(goal, neighbour) + new_gn | ||
}); | ||
} | ||
|
||
this.track[node!.id] = node!.prevNode; | ||
} | ||
} | ||
this.visited.push(node!.id); | ||
|
||
queue.sort((a,b) => a.fn - b.fn) | ||
} | ||
} | ||
|
||
|
||
public async search_bomb(graph:any, hurestic:any, start: any, goal: any, bomb:any) { | ||
let queue: Node[] = []; | ||
|
||
queue.push({ | ||
id: start, | ||
prevNode: null, | ||
gn: 0, | ||
hn: this.calculate_hn(bomb, start), | ||
fn: this.calculate_hn(bomb, start) + 0 | ||
}); | ||
|
||
while (queue.length > 0) { | ||
var node = queue.shift(); | ||
|
||
document.getElementById(node!.id)!.firstElementChild!.classList.add('marker'); | ||
await wait(15) | ||
document.getElementById(node!.id)!.firstElementChild!.classList.remove('marker'); | ||
document.getElementById(node!.id)!.firstElementChild!.classList.add('boom-visited'); | ||
|
||
if (node!.id === bomb) { | ||
console.log(hurestic); | ||
console.log(this.visited); | ||
console.log(this.track) | ||
console.log(node); | ||
|
||
this.solve(node!, true) | ||
this.visited = [] | ||
this.track = {} | ||
|
||
this.search(graph,hurestic, bomb, goal); | ||
break; | ||
} | ||
|
||
for (var neighbour of graph[node!.id]) { | ||
if (!this.visited.includes(node!.id) && !this.visited.includes(neighbour) && !document.getElementById(neighbour)!.firstElementChild!.classList.contains('wall')){ | ||
let new_gn = node!.gn + this.getWeight(neighbour); | ||
|
||
let existingNode = this.hasNode(queue, neighbour); | ||
|
||
console.log(existingNode); | ||
if(existingNode!=null) { | ||
existingNode.prevNode = node!.id, | ||
existingNode.gn = new_gn, | ||
existingNode.hn = this.calculate_hn(bomb, neighbour) | ||
} else { | ||
queue.push({ | ||
id: neighbour, | ||
prevNode: node!.id, | ||
gn: new_gn, | ||
hn: this.calculate_hn(bomb, neighbour), | ||
fn: this.calculate_hn(bomb, neighbour) + new_gn | ||
}); | ||
} | ||
|
||
this.track[node!.id] = node!.prevNode; | ||
} | ||
} | ||
this.visited.push(node!.id); | ||
|
||
queue.sort((a,b) => a.fn - b.fn) | ||
} | ||
} | ||
|
||
getWeight(id:any){ | ||
return parseInt(document.getElementById(id)!.getAttribute('weight')!) | ||
} | ||
|
||
public async drawPath(){ | ||
for(var item of this.path_b){ | ||
document.getElementById(item)!.firstElementChild!.classList.remove('visited'); | ||
document.getElementById(item)!.firstElementChild!.classList.remove('boom-visited'); | ||
document.getElementById(item)!.firstElementChild!.classList.remove('path'); | ||
document.getElementById(item)!.firstElementChild!.classList.add('path-marker'); | ||
await wait(15); | ||
document.getElementById(item)!.firstElementChild!.classList.remove('path-marker'); | ||
document.getElementById(item)!.firstElementChild!.classList.add('path'); | ||
await wait(15); | ||
} | ||
await wait(15); | ||
for(var item of this.path){ | ||
document.getElementById(item)!.firstElementChild!.classList.remove('visited'); | ||
document.getElementById(item)!.firstElementChild!.classList.remove('boom-visited'); | ||
document.getElementById(item)!.firstElementChild!.classList.remove('path'); | ||
document.getElementById(item)!.firstElementChild!.classList.add('path-marker'); | ||
await wait(15); | ||
document.getElementById(item)!.firstElementChild!.classList.remove('path-marker'); | ||
document.getElementById(item)!.firstElementChild!.classList.add('path'); | ||
await wait(15); | ||
} | ||
} | ||
|
||
solve(goal:Node, isBomb:boolean) { | ||
let arr = []; | ||
arr.push(goal.id); | ||
|
||
let ele = goal.prevNode; | ||
arr.push(ele) | ||
while (this.track[ele!] != null) { | ||
console.log(arr); | ||
|
||
arr.push(this.track[ele!]); | ||
ele = this.track[ele!]; | ||
} | ||
|
||
if(!isBomb) { | ||
this.path = arr.reverse() | ||
console.log(this.path); | ||
} else { | ||
this.path_b = arr.reverse(); | ||
console.log(this.path_b); | ||
} | ||
} | ||
|
||
hasNode(queue: Node[], node: string) { | ||
for(let n of queue) { | ||
if (n.id == node) { | ||
return n; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
calculate_hn(id1: string, id2: string) { | ||
var a = id1.replace('[', '').replace(']', '').split(','); | ||
var b = id2.replace('[', '').replace(']', '').split(','); | ||
return (Math.abs(parseInt(b[0]) - parseInt(a[0]))) + Math.abs(parseInt(b[1]) - parseInt(a[1])); | ||
} | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,52 @@ | ||
export class Grid{ | ||
mapGrid(row:number, col:number){ | ||
let graph:any = {}; | ||
mapGrid(row:number, col:number): {[key: string]:string[]} { | ||
let graph:{[key: string]:string[]} = {}; | ||
let i =0; | ||
while(i < row){ | ||
for(var j = 0; j<col; j++){ | ||
var node = `[${i},${j}]`; | ||
var neighbor = []; | ||
var topEle = document.getElementById(`[${i-1},${j}]`); | ||
var rightEle = document.getElementById(`[${i},${j+1}]`); | ||
var leftEle = document.getElementById(`[${i},${j-1}]`); | ||
var bottomEle = document.getElementById(`[${i+1},${j}]`); | ||
var neighbour = []; | ||
var topElement = document.getElementById(`[${i-1},${j}]`); | ||
var rightElement = document.getElementById(`[${i},${j+1}]`); | ||
var leftElement = document.getElementById(`[${i},${j-1}]`); | ||
var bottomElement = document.getElementById(`[${i+1},${j}]`); | ||
|
||
if(topEle != null){ | ||
neighbor.push(`[${i-1},${j}]`); | ||
if(topElement != null){ | ||
neighbour.push(`[${i-1},${j}]`); | ||
} | ||
|
||
if(rightEle != null){ | ||
neighbor.push(`[${i},${j+1}]`); | ||
if(rightElement != null){ | ||
neighbour.push(`[${i},${j+1}]`); | ||
} | ||
|
||
if(bottomEle != null){ | ||
neighbor.push(`[${i+1},${j}]`); | ||
if(bottomElement != null){ | ||
neighbour.push(`[${i+1},${j}]`); | ||
} | ||
|
||
|
||
if(leftEle != null){ | ||
neighbor.push(`[${i},${j-1}]`); | ||
if(leftElement != null){ | ||
neighbour.push(`[${i},${j-1}]`); | ||
} | ||
|
||
graph[node] = neighbor | ||
graph[node] = neighbour | ||
} | ||
i++; | ||
} | ||
return graph; | ||
} | ||
|
||
} | ||
|
||
mapHurestic(goal: string, row: number, col: number) { | ||
let hurestic: {[key: string]: number} = {}; | ||
let cordinate = goal.replace('[', '').replace(']', '').split(','); | ||
let goal_row:number = parseInt(cordinate[0]); | ||
let goal_col = parseInt(cordinate[1]); | ||
|
||
for (let i = 0; i < row; i++) { | ||
for (let j = 0; j < col; j++) { | ||
hurestic[`[${i},${j}]`] = Math.abs(goal_row - i) + Math.abs(goal_col - j); | ||
} | ||
} | ||
|
||
return hurestic; | ||
} | ||
} |