Skip to content

Commit 84d0dbe

Browse files
committed
finishing
1 parent b386549 commit 84d0dbe

File tree

15 files changed

+718
-1391
lines changed

15 files changed

+718
-1391
lines changed

package-lock.json

Lines changed: 285 additions & 1188 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
"dependencies": {
66
"@react-spring/three": "^9.5.1",
77
"@react-three/drei": "9.14.3",
8-
"@react-three/fiber": "^8.2.1",
98
"@splinetool/react-spline": "^2.2.1",
109
"@testing-library/jest-dom": "^5.16.4",
1110
"@testing-library/react": "^13.3.0",
@@ -16,13 +15,15 @@
1615
"@types/react-dom": "^18.0.6",
1716
"@types/three": "0.141.0",
1817
"csstype": "^3.1.0",
19-
"leva": "^0.9.29",
2018
"postcss-cli": "^10.0.0",
2119
"react": "^18.2.0",
2220
"react-dom": "^18.2.0",
2321
"react-router": "^6.3.0",
2422
"react-router-dom": "^6.3.0",
25-
"react-spring": "8.0.27"
23+
"react-spring": "8.0.27",
24+
"react-three-fiber": "4.0.12",
25+
"three": "0.112.1",
26+
"zustand": "^4.0.0-rc.1"
2627
},
2728
"devDependencies": {
2829
"autoprefixer": "^9.8.8",

src/components/Algorithms/Maze/Directions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var directions = new Array();
1+
const directions = new Array();
22
directions.push({x: 0, y: 1});
33
directions.push({x: 1, y: 0});
44
directions.push({x: 0, y: -1});

src/components/Algorithms/Maze/Generation/BinaryTreeCreation.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function BinaryTreeCreation(board) {
3232
// const testNeighbor = _GetNeighbors(board, -6, -6);
3333
// if (testNeighbor) {
3434
// testNeighbor.forEach(neighbor => {
35-
// console.log('Neighbor: ', neighbor);
35+
// // console.log('Neighbor: ', neighbor);
3636
// neighbor.type = PATH_TYPE;
3737
// });
3838
// }
@@ -55,15 +55,5 @@ export function BinaryTreeCreation(board) {
5555
}
5656
}
5757

58-
let [setG, setS] = [false, false];
59-
// while (!setG || !setS) {
60-
// let node = board[w];
61-
//
62-
// if (node.type === '_wall_') {
63-
// continue;
64-
// }
65-
//
66-
// }
67-
return board;
6858
}
6959

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,58 @@
1-
import {directions} from '../Directions';
2-
import {_FindCellVisitState, _FindCellTypeState, _FindCell} from '../Tools';
3-
// import exit from 'exit';
1+
import {directions} from '../Directions';
2+
import {_FindCellVisitState, _FindCellTypeState, _FindCell, _BoardReset, _GetNeighbors} from '../Tools';
3+
import exit from 'exit';
4+
import {PATH_TYPE, DEFAULT_TYPE, FLOOR_TYPE, WALL_TYPE, START_TYPE, GOAL_TYPE} from '../../../../Utility/Colors';
45

56

6-
export function RecursiveGenerate(board, kill) {
7-
let copyBoard = board;
87

98

10-
}
9+
export function RecursiveBacktrackCreation(board, start, end) {
10+
_BoardReset(board);
11+
const numPoints = board.length;
12+
const prev = null;
13+
for (var i = 0; i < numPoints; i++) {
14+
let node = board[i];
15+
// Needed to be percise to avoid floating point errors....
16+
let direction = Math.floor(Math.random() * 4);
17+
// console.log(node);
18+
// console.log(direction);
19+
// console.log(directions[direction].x);
20+
// console.log(board[i].x + directions[direction].x);
21+
let xcord = node.x + directions[direction].x;
22+
let ycord = node.y + directions[direction].y;
23+
// console.log(xcord, ycord);
24+
// console.log(node.x + directions[direction].x, node.y + directions[direction].y);
25+
const cell = _FindCell(board, xcord, ycord);
26+
// console.log(cell);
27+
// exit();
28+
if (cell) {
29+
// exit();
30+
31+
// const testNeighbor = _GetNeighbors(board, -6, -6);
32+
// if (testNeighbor) {
33+
// testNeighbor.forEach(neighbor => {
34+
// // console.log('Neighbor: ', neighbor);
35+
// neighbor.type = PATH_TYPE;
36+
// });
37+
// }
38+
// return;
39+
40+
41+
if (!cell.visited) {
42+
// console.log(cell);
43+
cell.visited = true;
44+
cell.type = '_floor_';
45+
}
46+
}
47+
}
48+
49+
for (var x = 0; x < numPoints; x++) {
50+
let node = board[x];
51+
52+
if (!node.visited) {
53+
node.type = '_wall_';
54+
}
55+
}
56+
57+
}
58+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import {_GetNeighbors} from '../Tools';
2+
3+
4+
5+
6+
export default function BfsSolution(grid) {
7+
const visited = new Set();
8+
const queue = [];
9+
const result = [];
10+
const start = grid[0][0];
11+
queue.push(start);
12+
visited.add(start);
13+
while (queue.length) {
14+
const current = queue.shift();
15+
result.push(current);
16+
const neighbors = _GetNeighbors(grid, current);
17+
neighbors.forEach(neighbor => {
18+
if (!visited.has(neighbor)) {
19+
queue.push(neighbor);
20+
visited.add(neighbor);
21+
}
22+
});
23+
}
24+
return result;
25+
}
Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,41 @@
1-
import {directions} from '../Directions';
2-
import {_FindCellVisitState, _FindCellTypeState, _FindCell} from '../Tools';
3-
import {useState, useEffect} from 'react';
1+
import {directions} from '../Directions';
2+
import {_FindCellVisitState, _FindCellTypeState, _FindCell, _GetNeighbors, _GetNeighborsUnvisited} from '../Tools';
3+
import {useState, useEffect} from 'react';
44
// import exit from 'exit';
55

66

77

88
/*
99
* Recursive backtracking algorithm to solve a solvable maze.
1010
*/
11-
export default function RecursiveSolver({board, start, end, current}) {
12-
const [maze, setMaze] = useState(board);
13-
const [mazeSize, setMazeSize] = useState(null);
14-
const [mazeSolved, setMazeSolved] = useState(false);
15-
const [mazeSolving, setMazeSolving] = useState(false);
16-
const [mazeSolvingError, setMazeSolvingError] = useState(false);
17-
18-
19-
export function solve() {
20-
21-
}
22-
23-
24-
function useTargetLayoutHook({board, layoutType}) {
25-
26-
useEffect(() => {
27-
for (let i = 0; i < board.length; ++i) {
28-
board[i].sourceX = board[i].x || 0;
29-
board[i].sourceY = board[i].y || 0;
30-
board[i].sourceZ = board[i].z || 0;
31-
board[i].sourceType = board[i].type;
11+
export default function RecursiveBacktrackSolution(board, start, end) {
12+
const [visited, setVisited] = useState([]);
13+
const [path, setPath] = useState([]);
14+
const [current, setCurrent] = useState(start);
15+
const [endFound, setEndFound] = useState(false);
16+
17+
useEffect(() => {
18+
if (endFound) {
19+
return;
20+
}
21+
22+
const next = _GetNeighborsUnvisited(board, current);
23+
if (next) {
24+
setVisited([...visited, next]);
25+
setCurrent(next);
26+
} else {
27+
setPath([...path, current]);
28+
setCurrent(path[path.length - 1]);
29+
if (current === end) {
30+
setEndFound(true);
3231
}
33-
}, [board, layoutType]);
34-
35-
useLayoutHook({board, layoutType});
36-
37-
useEffect(() => {
38-
for (let i = 0; i < board.length; ++i) {
39-
board[i].targetX = board[i].x;
40-
board[i].targetY = board[i].y;
41-
board[i].targetZ = board[i].z;
42-
board[i].targetType = board[i].type;
43-
}
44-
}, [board, layoutType]);
45-
46-
47-
}
32+
}
33+
}, [endFound, board, current, end, path, visited]);
34+
35+
return {
36+
visited,
37+
path,
38+
current,
39+
endFound,
40+
};
4841
}

src/components/Algorithms/Maze/Tools.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ export function _FindCell(board, x, y) {
4141
return null;
4242
};
4343

44+
export function _GetNeighborsUnvisited(board, cell) {
45+
let neighbors = [];
46+
for (let i = 0; i < directions.length; i++) {
47+
let neighbor = _FindCell(board, cell.x + directions[i].x, cell.y + directions[i].y);
48+
if (neighbor !== null && neighbor.visited === false && neighbor.type !== WALL_TYPE) {
49+
neighbors.push(neighbor);
50+
}
51+
}
52+
return neighbors;
53+
}
54+
4455

4556
export function _GetNeighbors(board, x, y) {
4657
let neighbors = [];
@@ -49,7 +60,9 @@ export function _GetNeighbors(board, x, y) {
4960
let ycoord = y + directions[i].y;
5061
let neighbor = _FindCell(board, xcoord, ycoord);
5162
if (neighbor) {
52-
neighbors.push(neighbor);
63+
if (neighbor.type !== WALL_TYPE) {
64+
neighbors.push(neighbor);
65+
}
5366
}
5467
}
5568
return neighbors;

src/components/BoardCanvas/Board.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
import * as React from 'react';
22
// import {useFrame /*useFrame*/} from '@react-three/fiber';
3-
import {Canvas} from '@react-three/fiber';
3+
import {Canvas} from 'react-three-fiber';
44
import Controls from './OrbitControls';
55
import {useImperativeHandle, useRef, forwardRef} from 'react';
6-
import {TrackballControls} from '@react-three/fiber';
6+
import {TrackballControls} from 'three/examples/jsm/controls/TrackballControls';
77
import OrbitControls from './OrbitControls';
8-
98
import Cells from './Cells';
109

1110

1211

1312

14-
export const Board = ({board, layoutType, selectedPoint, onSelectPoint, mazeType}, ref) => {
13+
export const Board = ({board, solving, algorithm, layoutType, selectedPoint, onSelectPoint, mazeType}, ref) => {
1514
const controlsRef = useRef(OrbitControls);
1615

1716

@@ -25,10 +24,10 @@ export const Board = ({board, layoutType, selectedPoint, onSelectPoint, mazeType
2524
return (
2625
<Canvas>
2726
<Controls ref = {controlsRef} selectedPoint = {selectedPoint}/>
28-
{/* <mesh> /!* BorderBox *!/ */}
29-
{/* <boxBufferGeometry attach = "geometry" args = {[80, 80, -10]}/> */}
30-
{/* <meshBasicMaterial attach = "material" color = "black"/> */}
31-
{/* </mesh> */}
27+
<mesh> {/* BorderBox */}
28+
<boxBufferGeometry attach = "geometry" args = {[80, 80, -10]}/>
29+
<meshBasicMaterial attach = "material" color = "black"/>
30+
</mesh>
3231

3332

3433
<ambientLight color = "white" intensity = {0.1}/>
@@ -39,10 +38,11 @@ export const Board = ({board, layoutType, selectedPoint, onSelectPoint, mazeType
3938
intensity = {1.0}
4039
/>
4140
<Cells
42-
4341
board = {board}
4442
layoutType = {layoutType}
4543
mazeType = {mazeType}
44+
solving={solving}
45+
algorithm={algorithm}
4646
selectedPoint = {selectedPoint}
4747
onSelectPoint = {onSelectPoint}
4848
/>

0 commit comments

Comments
 (0)