Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9b9e245

Browse files
Added A* search algorith
1 parent f3e66bb commit 9b9e245

File tree

5 files changed

+75
-10
lines changed

5 files changed

+75
-10
lines changed

‎AI_search.html‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,8 @@
8181

8282
<script type="text/javascript" src="search_algorithms/BreadthFirstSearch.js"></script>
8383
<script type="text/javascript" src="search_algorithms/DeapthFirstSearch.js"></script>
84-
84+
<scripttype="text/javascript" src="search_algorithms/AStarSearch.js"></script>
8585

8686
<script type="text/javascript" src="Main.js"></script>
87-
8887
</body>
8988
</html>

‎Main.js‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let MAZE = undefined
88

99
// getting canvas from HTML
1010
const canvas = document.getElementById('canvas')
11-
canvas.width = document.getElementById('canvas_div').offsetWidth - 1
11+
canvas.width = document.getElementById('canvas_div').offsetWidth - 1
1212
canvas.height = document.getElementById('canvas_div').offsetHeight - 1
1313
console.log('Window size - ', 'width:', canvas.width, 'height:', canvas.height);
1414
const ctx = canvas.getContext('2d')
@@ -34,12 +34,12 @@ async function maze_button_click() {
3434
case 'Recursive Division':
3535
recursive_division(MAZE)
3636
break;
37-
}
38-
37+
}
38+
3939
// add start and finish tiles
40-
const tiles = MAZE.get_all_tiles()
41-
const start_tile = MAZE.switch_tile_type(tiles.at(0), TileTypeEnumeration.START)
42-
const fisih_tile = MAZE.switch_tile_type(tiles.at(-1), TileTypeEnumeration.FINISH)
40+
const path_tiles = MAZE.get_all_tiles().filter(x=>MAZE.get_tile(x).get_type()===TileTypeEnumeration.PATH)
41+
const start_tile = MAZE.switch_tile_type(path_tiles.at(0), TileTypeEnumeration.START)
42+
const fisih_tile = MAZE.switch_tile_type(path_tiles.at(-1), TileTypeEnumeration.FINISH)
4343
MAZE.stage_changes([start_tile, fisih_tile])
4444

4545
// draw maze
@@ -69,6 +69,7 @@ async function search_button_click() {
6969
breadth_first_search(MAZE)
7070
break;
7171
case 'A* Search':
72+
a_star_search(MAZE)
7273
break;
7374
}
7475
// draw maze

‎search_algorithms/AStarSearch.js‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
/**
3+
* Performs A* search algorithm on a given maze to find the shortest path between the start and finish tiles.
4+
* @param {TileGrid} maze - The maze object to search.
5+
* @returns {void} - Does not return anything, but modifies the maze object by changing the type of its tiles.
6+
*/
7+
function a_star_search(maze) {
8+
console.log('Starting of A star search');
9+
10+
// getting start tile
11+
const all_tiles = maze.get_all_tiles()
12+
const [start_id, start] = maze.get_start_tile()
13+
14+
// getting finish tile
15+
const finish_id = all_tiles.filter(x => maze.get_tile(x).get_type() === TileTypeEnumeration.FINISH)
16+
const finish = maze.get_tile(finish_id)
17+
18+
let queue = [{
19+
id: start_id,
20+
tile: start,
21+
node_path: [],
22+
cost: start.center.get_manhattan_distance(finish.center)
23+
}]
24+
25+
let selected_node
26+
while (queue.length != 0) {
27+
// sort nodes by least cost (distance to finish)
28+
queue = queue.sort((a, b) => a.cost - b.cost)
29+
30+
// pop node with least cost
31+
selected_node = queue.shift(1)
32+
33+
// is selected node is finish exit out of loop
34+
if (selected_node.tile.get_type() === TileTypeEnumeration.FINISH)
35+
break
36+
37+
// for every neighbour node if its PATH or FINISH add them to queue
38+
maze.get_neighbour_ids(selected_node.id).forEach(node_id => {
39+
node_tile = maze.get_tile(node_id)
40+
if (node_tile.get_type() === TileTypeEnumeration.PATH || node_tile.get_type() === TileTypeEnumeration.FINISH) {
41+
queue.push({
42+
id: node_id,
43+
tile: node_tile,
44+
node_path: [node_id, ...selected_node.node_path],
45+
cost: selected_node.cost + node_tile.center.get_manhattan_distance(finish.center)
46+
})
47+
}
48+
});
49+
50+
// draw changes
51+
if (selected_node.id != start_id)
52+
maze.stage_changes(maze.switch_tile_type(selected_node.id, TileTypeEnumeration.VISITED))
53+
}
54+
55+
// backtracing part
56+
if (selected_node.id != finish_id)
57+
return
58+
59+
selected_node.node_path.shift(1)
60+
selected_node.node_path.forEach(element => {
61+
maze.stage_changes(maze.switch_tile_type(element, TileTypeEnumeration.CHOOSEN_PATH))
62+
});
63+
64+
console.log('A star search finished');
65+
}

‎search_algorithms/BreadthFirstSearch.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Performs a breadth-first search on a given maze
3-
* @param {Maze} maze - The maze object on which to perform the search
3+
* @param {TileGrid} maze - The maze object on which to perform the search
44
* @returns {void} - Does not return anything, but modifies the maze object by changing the type of its tiles.
55
*/
66
function breadth_first_search(maze) {

‎search_algorithms/DeapthFirstSearch.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Performs a depth-first search to find a path from the start tile to the finish tile in the given maze.
3-
* @param {Maze} maze - The maze to search.
3+
* @param {TileGrid} maze - The maze to search.
44
* @returns {void} - Does not return anything, but modifies the maze object by changing the type of its tiles.
55
*/
66
function deapth_first_search(maze) {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /