Skip to content

Commit b6710ea

Browse files
committed
main component controlling interactions with user
1 parent 356426c commit b6710ea

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

src/App.css

Whitespace-only changes.

src/App.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import React from 'react'
2+
import './App.css';
3+
import Grid from "./Grid"
4+
import {dijkstra} from "./dijkstra"
5+
import {STARTX,START,ENDX,END, ROWS,COLS, VISITED} from "./const"
6+
7+
function getRandomInt(min, max) {
8+
min = Math.ceil(min);
9+
max = Math.floor(max);
10+
return Math.floor(Math.random() * (max - min)) + min;
11+
}
12+
13+
14+
class App extends React.Component {
15+
constructor(props){
16+
super(props);
17+
this.state = {
18+
grid: [],
19+
visited: [],
20+
animate: false,
21+
algorithm: null
22+
}
23+
this.frame = this.frame.bind(this)
24+
}
25+
26+
findPath = () => {
27+
this.setState({animate:true})
28+
}
29+
changeAlgo = () => {
30+
this.setState({algorithm: dijkstra})
31+
console.log('change')
32+
}
33+
34+
// componentDidUpdate() {
35+
// if(this.state.animate){
36+
// this.start()
37+
// }
38+
// }
39+
componentDidUpdate(){
40+
if(this.state.animate) this.start()
41+
}
42+
43+
start() {
44+
const grid = new Array(COLS*ROWS)//[COLS*ROWS];
45+
grid[STARTX] = START
46+
grid[ENDX] = END
47+
48+
const visited = this.state.algorithm(grid)
49+
//console.log(visited)
50+
grid[STARTX] = START
51+
grid[ENDX] = END
52+
53+
this.setState({
54+
grid: grid,
55+
visited: visited,
56+
animate: false,
57+
}, () => {
58+
this.frame();
59+
})
60+
}
61+
62+
frame() {
63+
const {grid,visited} = this.state;
64+
if(visited.length === 0) return;
65+
const nextVisited = visited.shift();
66+
grid[nextVisited] = VISITED
67+
68+
this.setState({
69+
grid
70+
}, () => {
71+
setTimeout(this.frame, 1)
72+
})
73+
}
74+
75+
render() {
76+
return (
77+
<div className="App">
78+
<select onChange = {this.changeAlgo} id="algo">
79+
<option>dijkstra</option>
80+
<option>astar</option>
81+
</select>
82+
<button onClick={this.findPath}></button>
83+
<Grid grid={this.state.grid}/>
84+
</div>
85+
);
86+
}
87+
}
88+
89+
export default App;

0 commit comments

Comments
 (0)