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 71e28da

Browse files
Added reseting and redrawing maze when search is called
1 parent 3bab58f commit 71e28da

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

‎Main.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ async function search_button_click() {
5454
// disable all buttons
5555
Array.from(document.getElementsByTagName("button")).forEach(b => b.disabled = true)
5656

57+
// clear all previous searches
58+
console.log('Clearing all previous searches');
59+
MAZE.switch_all_tiles_with_type(TileTypeEnumeration.CHOOSEN_PATH, TileTypeEnumeration.PATH)
60+
MAZE.switch_all_tiles_with_type(TileTypeEnumeration.VISITED, TileTypeEnumeration.PATH)
61+
MAZE.redraw_maze()
62+
5763
const selected = document.getElementById("search_alg_selection").value
5864
switch (selected) {
5965
case 'Depth First Search':
@@ -70,4 +76,4 @@ async function search_button_click() {
7076

7177
// enable all buttons
7278
Array.from(document.getElementsByTagName("button")).forEach(b => b.disabled = false)
73-
}
79+
}

‎search_algorithms/DeapthFirstSearch.js‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,10 @@ function deapth_first_search(maze) {
2929
* @returns {Tile[]|undefined} An array of tiles representing the path to the finish tile, or undefined if no path was found.
3030
*/
3131
function recursive_search(maze, tile) {
32-
33-
console.log('Entering Loop:', tile);
3432
// get possible paths
3533
const neighbours = maze.get_neighbour_ids(tile.id).map(edge_tile => {
36-
return {
37-
id: edge_tile,
38-
type: maze.get_tile(edge_tile).get_type()
39-
}
34+
return {id: edge_tile,
35+
type: maze.get_tile(edge_tile).get_type()}
4036
}).filter(x => x.type === TileTypeEnumeration.FINISH || x.type === TileTypeEnumeration.PATH)
4137

4238
if (neighbours.length == 0)

‎utility_calsses/TileGridClass.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,28 @@ class TileGrid {
212212

213213
this.staged_changes = [];
214214
}
215+
216+
/**
217+
* Resets the background and redraws grid on canvas.
218+
*/
219+
redraw_maze() {
220+
// drawing black background on canvas
221+
this.canvas_context.fillStyle = 'black'
222+
this.canvas_context.fillRect(0, 0, canvas.width, canvas.height)
223+
224+
// redraw maze
225+
Array.from(this.tile_grid.values()).forEach(tile => tile.draw(this.canvas_context))
226+
}
227+
228+
/**
229+
* Switches the type of all tiles in the maze that have the old type to the new type.
230+
* @param {TileTypeEnumeration} old_type - The old tile type to replace.
231+
* @param {TileTypeEnumeration} new_type - The new tile type to set.
232+
*/
233+
switch_all_tiles_with_type(old_type, new_type) {
234+
this.tile_grid.forEach((tile, tile_id) => {
235+
if (tile.get_type() === old_type)
236+
this.switch_tile_type(tile_id, new_type)
237+
})
238+
}
215239
}

0 commit comments

Comments
(0)

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