Skip to content

Commit

Permalink
DONE:
Browse files Browse the repository at this point in the history
- Added psudeocode section to the UI with the aim of providing pseudocode for the search algorithm selected by the user.
- Learnt an import React.js concept about "ligting up state".

TODO:
- Simply add the text for each algorithm's pseudocode where appropriate.
  • Loading branch information
np57 committed Jun 18, 2022
1 parent 8999264 commit 5d3a8be
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 30 deletions.
20 changes: 7 additions & 13 deletions src/components/MazeSolver.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
.gridKey {
display: table;
margin: 10px auto 0;
display: flex;
margin: 10px auto;
justify-content: center;
pointer-events: none;
}

@media (max-width: 1250px) {
Expand Down Expand Up @@ -86,30 +88,22 @@
.keyIcon {
display: inline-block;
vertical-align: middle;
margin-left: 10px;
}

.keyLabel {
display: inline-block;
margin-left: 5px;
vertical-align: center;
}

.grid {
font-size: 0;
pointer-events: unset;

position: absolute;
left: 50%;
margin-right: -50%;
transform: translate(-50%, 2%);
text-align: center;
}

.grid-visualizing {
font-size: 0;
pointer-events: none;

position: absolute;
left: 50%;
margin-right: -50%;
transform: translate(-50%, 2%);
text-align: center;
}
19 changes: 18 additions & 1 deletion src/components/MazeSolver.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "./MazeSolver.css";
// Import components for creating the MazeSolver application.
import Node from "./Node/Node";
import NavBar from "./NavBar/NavBar";
import Pseudocode from "./Pseudocode/Pseudocode";

// Import search algorithms for solving mazes.
import {randomWalk} from "../search_algorithms/randomWalk"
Expand Down Expand Up @@ -46,11 +47,20 @@ class MazeSolver extends Component {
numColumns: initNumCols,
speed: 10,
mazeSpeed: 10,
algorithm: "Visualize Algorithm",
};


// UTILITY METHODS:

updateAlgorithm = (newAlgorithm) => {

this.setState({
algorithm: newAlgorithm
})

}

updateDimensions = () => {

this.setState({
Expand Down Expand Up @@ -638,6 +648,9 @@ class MazeSolver extends Component {
clearPath={this.clearPath.bind(this)}
updateSpeed={this.updateSpeed.bind(this)}

algorithm={this.state.algorithm}
updateAlgorithm={this.updateAlgorithm.bind(this)}

/>

{/* The key for the grid. */}
Expand Down Expand Up @@ -779,7 +792,11 @@ class MazeSolver extends Component {

</div>

{/* TODO: Add a pseudocode for selected algorithm component. */}
{/* Pseudocode for selected algorithm component. */}
<Pseudocode
algorithm={this.state.algorithm}
updateAlgorithm={this.updateAlgorithm.bind(this)}
/>

</React.Fragment>

Expand Down
32 changes: 16 additions & 16 deletions src/components/NavBar/NavBar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class NavBar extends Component {
// ATTRIBUTES (i.e., State):

state = {
algorithm: "Visualize Algorithm", // Algorithm for pathfinding.
maze: "Generate Maze", // Algorithm for maze generation.
pathState: false, // State of found path.
mazeState: false, // State of generated maze.
Expand All @@ -22,19 +21,20 @@ class NavBar extends Component {

if (this.props.visualizingAlgorithm) return;

if (selection === this.state.algorithm ||
this.state.algorithm === "Visualize Algorithm" || this.state.algorithm === "Select an Algorithm!") {
if (selection === this.props.algorithm ||
this.props.algorithm === "Visualize Algorithm" ||
this.props.algorithm === "Select an Algorithm!") {

this.setState({algorithm: selection});
this.props.updateAlgorithm(selection);

} else if (this.state.pathState) {

this.clearPath();
this.setState({algorithm: selection});
this.props.updateAlgorithm(selection);

} else {

this.setState({algorithm: selection});
this.props.updateAlgorithm(selection);

}

Expand Down Expand Up @@ -70,21 +70,21 @@ class NavBar extends Component {
return;
}

if (this.state.algorithm === "Visualize Algorithm" || this.state.algorithm === "Select an Algorithm!") {
if (this.props.algorithm === "Visualize Algorithm" || this.props.algorithm === "Select an Algorithm!") {

this.setState({algorithm: "Select an Algorithm!"});
this.props.updateAlgorithm("Select an Algorithm!");

} else {

this.setState({pathState: true});

if (this.state.algorithm === "Visualize Dijkstra") this.props.visualizeDijkstra();
else if (this.state.algorithm === "Visualize A*") this.props.visualizeAStar();
else if (this.state.algorithm === "Visualize Greedy BFS") this.props.visualizeGreedyBFS();
else if (this.state.algorithm === "Visualize Bidirectional Greedy") this.props.visualizeBidirectionalGreedySearch();
else if (this.state.algorithm === "Visualize Breadth First Search") this.props.visualizeBFS();
else if (this.state.algorithm === "Visualize Depth First Search") this.props.visualizeDFS();
else if (this.state.algorithm === "Visualize Random Walk") this.props.visualizeRandomWalk();
if (this.props.algorithm === "Visualize Dijkstra") this.props.visualizeDijkstra();
else if (this.props.algorithm === "Visualize A*") this.props.visualizeAStar();
else if (this.props.algorithm === "Visualize Greedy BFS") this.props.visualizeGreedyBFS();
else if (this.props.algorithm === "Visualize Bidirectional Greedy") this.props.visualizeBidirectionalGreedySearch();
else if (this.props.algorithm === "Visualize Breadth First Search") this.props.visualizeBFS();
else if (this.props.algorithm === "Visualize Depth First Search") this.props.visualizeDFS();
else if (this.props.algorithm === "Visualize Random Walk") this.props.visualizeRandomWalk();

}

Expand Down Expand Up @@ -246,7 +246,7 @@ class NavBar extends Component {

<button type="button" className="btn btn-success"
onClick={() => this.visualizeAlgorithm()}>
{this.state.algorithm}
{this.props.algorithm}
</button>

</li>
Expand Down
5 changes: 5 additions & 0 deletions src/components/Pseudocode/Pseudocode.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.pseudocode {
margin-top: 10px;
margin-left: 20px;
text-align: left;
}
107 changes: 107 additions & 0 deletions src/components/Pseudocode/Pseudocode.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import React, { Component } from "react";
import "./Pseudocode.css";

class Pseudocode extends Component {


// ATTRIBUTES (i.e., State):

state = {
selectedAlgorithm: null
}


// METHODS:

// TODO: Add actual pseudocode text for each search algorithm.

pseudocodeDijkstra = () => {

return (
"Dijkstra's Algorithm"
);

}

pseudocodeAStar = () => {

return (
"A* Algorithm"
);

}

pseudocodeGreedyBFS = () => {

return (
"Greedy Best First Algorithm"
);

}

pseudocodeBidirGreedy = () => {

return (
"Bidirectional Greedy Algorithm"
);

}

pseudocodeBFS = () => {

return (
"Breadth First Search Algorithm"
);

}

pseudocodeDFS = () => {

return (
"Depth First Search Algorithm"
);

}

pseudocodeRandomWalk = () => {

return (
"Random Walk Algorithm"
);

}

handlePseudocode = (algorithm) => {

switch (algorithm) {
case "Dijkstra": return this.pseudocodeDijkstra();
case "A*": return this.pseudocodeAStar();
case "Greedy BFS": return this.pseudocodeGreedyBFS();
case "Bidirectional Greedy": return this.pseudocodeBidirGreedy();
case "Breadth First Search": return this.pseudocodeBFS();
case "Depth First Search": return this.pseudocodeDFS();
case "Random Walk": return this.pseudocodeRandomWalk();
}

}

render() {

const pseudoAlg =
(this.props.algorithm === "Select an Algorithm!" || this.props.algorithm === "Visualize Algorithm") ?
"" :
this.props.algorithm.replace("Visualize ", "")

return (
<div className="pseudocode">
<h4> Search Algorithm Pseudocode {(pseudoAlg === "") ? "" : " - " + pseudoAlg}</h4>
<pre>{this.handlePseudocode(pseudoAlg)}</pre>
</div>
);

}


}

export default Pseudocode;

0 comments on commit 5d3a8be

Please sign in to comment.