Repository navigation
Expand file tree
/
Copy pathApp.tsx
More file actions
118 lines (100 loc) · 2.88 KB
/
Copy pathApp.tsx
File metadata and controls
118 lines (100 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { findShortestPath } from 'Algorithm';
import React from 'react'
import styled from 'styled-components'
import 'App.css'
import { CellState, Grid, Row, Value } from 'types'
import { Cell } from './Cell'
import { Row as RowComponent } from './Row'
const NUMBER_OF_ROWS = 20
const NUMBER_OF_COLUMNS = 20
const GridWrapper = styled.div`
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
`
const ButtonWrapper = styled.div`
display: flex;
flex-direction: row;
`
const Header = styled.h3`
margin: 20px 0;
color: white;
text-align: center;
`
const Button = styled.div`
background-color: #00fc69a4;
color: white;
margin: 30px 10px;
height: 20px;
width: 20px;
display: flex;
justify-content: center;
align-items: center;
border-radius: 100%;
font-weight: bold;
padding: 25px;
cursor: pointer;
:hover {
background-color: #00fc699b;
box-shadow: 0 0 1px #000 inset;
}
`
const ResetButton = styled(Button)`
background-color:rgb(255, 45, 85);
:hover {
background-color: rgba(255, 45, 83, 0.842);
box-shadow: 0 0 1px #000 inset;
}
`
const getRandomPoint = () => ({
x: Math.floor(Math.random() * NUMBER_OF_COLUMNS),
y: Math.floor(Math.random() * NUMBER_OF_ROWS),
})
const isPoint = (x, y, point) => point.x === x && point.y === y
function App() {
// const a = [
// [0, 0, 0],
// [0, 0, 0],
// ]
const [grid, setGrid] = React.useState<Grid>([])
const initializeGrid = React.useCallback(() => {
const startingPoint = getRandomPoint()
const destinationPoint = getRandomPoint()
let _grid: Grid = []
for (let x = 0; x < NUMBER_OF_ROWS; x++) {
let row: Row = []
for (let y = 0; y < NUMBER_OF_COLUMNS; y++) {
let value: Value = isPoint(x, y, startingPoint)
? CellState.INITIAL
: isPoint(x, y, destinationPoint)
? CellState.DESTINATION
: CellState.UNTOUCHED
row.push(value)
}
_grid.push(row)
}
setGrid(_grid)
}, [])
// NOTE: initialize the grid
React.useEffect(initializeGrid, [initializeGrid])
const findShortestPathHandler = React.useCallback(() => { void findShortestPath(grid) }, [grid])
return (
<>
<Header>Dijkstra's algorithm for finding the shortest path</Header>
<GridWrapper>
{grid.map((row, rowIndex) => (
<RowComponent key={`row-${rowIndex}`}>
{row.map((cell, columnIndex) => <Cell key={`cell-row${rowIndex}column${columnIndex}`} id={`cell-row${rowIndex}column${columnIndex}`} state={cell} />)}
</RowComponent>
))}
<ButtonWrapper>
<Button onClick={findShortestPathHandler}>GO!</Button>
{/* eslint-disable-next-line no-restricted-globals */}
<ResetButton onClick={() => {location.reload()}}>RESET</ResetButton>
</ButtonWrapper>
</GridWrapper>
</>
);
}
export default App;