Skip to content

Commit a82a2fa

Browse files
committed
Section 22: Determining Movement Direction
1 parent 3924261 commit a82a2fa

2 files changed

Lines changed: 20 additions & 10 deletions

File tree

22-Javascript-with-the-Canvas-API/02-Maze copy/index.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ World.add(world, walls)
3535
const shuffle = (arr) => {
3636
let counter = arr.length
3737

38-
while(counter > 0 ) {
38+
while (counter > 0) {
3939
const index = Math.floor(Math.random() * counter)
4040

4141
counter--
@@ -77,21 +77,29 @@ const stepThroughCell = (row, column) => {
7777

7878
// Assemble randomly-ordered list of neighbors
7979
const neighbors = shuffle([
80-
[row - 1, column],
81-
[row, column + 1],
82-
[row + 1, column],
83-
[row, column - 1],
80+
[row - 1, column, 'up'],
81+
[row, column + 1, 'right'],
82+
[row + 1, column, 'down'],
83+
[row, column - 1, 'left'],
8484
])
85-
console.log(neighbors);
85+
console.log(neighbors)
8686

8787
// For each neighbor....
88+
for (let neighbor of neighbors) {
89+
const [nextRow, nextColumn] = neighbor
8890

89-
// See if that neighbor is out of bounds
91+
// See if that neighbor is out of bounds
92+
if (nextRow < 0 || nextRow >= cells || nextColumn < 0 || nextColumn >= ce) {
93+
continue
94+
}
9095

91-
// If we have visited that neighbor, continue to next neighbor
92-
93-
// Remove a wall from either horizontals or verticals
96+
// If we have visited that neighbor, continue to next neighbor
97+
if (grid[nextRow][nextColumn]) {
98+
continue
99+
}
94100

101+
// Remove a wall from either horizontals or verticals
102+
}
95103
// Visit that next cell
96104
}
97105

22-Javascript-with-the-Canvas-API/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,5 @@ console.log(grid)
6464
## 12. 16 Neighbor Coordinates
6565

6666
## 13. 17 Shuffling Neighbor Pairs
67+
68+
## 14. 18 Determining Movement Direction

0 commit comments

Comments
 (0)