Skip to content

Commit c9765de

Browse files
committed
added turing machine and 15 puzzle skeleton codes
1 parent 81fb799 commit c9765de

21 files changed

Lines changed: 1739 additions & 49 deletions

File tree

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ So far there are 6 segments
1717
- N Queen
1818
- Convex Hull
1919
- Binary Search Game
20+
- Turing Machine
2021

2122
I have implemented a total of `14 algorithms` so far. And will try to add more later.
2223

@@ -36,6 +37,7 @@ I have implemented a total of `14 algorithms` so far. And will try to add more l
3637
- N Queen Backtracking
3738
- Graham Scan for Convex Hull
3839
- Binary Search
40+
- Turing Machine
3941

4042

4143

@@ -55,3 +57,10 @@ npm run build
5557
npm run deploy
5658
```
5759

60+
61+
62+
### Inspiration for next sections
63+
64+
- [University of San Francisco Site](https://www.cs.usfca.edu/~galles/visualization/Algorithms.html)
65+
-[algorithm-visualizer](https://github.com/algorithm-visualizer) / [algorithm-visualizer](https://github.com/algorithm-visualizer/algorithm-visualizer)
66+

package-lock.json

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

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@
1010
"@testing-library/react": "^11.1.0",
1111
"@testing-library/user-event": "^12.1.10",
1212
"bootstrap": "^4.5.3",
13+
"firebase": "^9.6.7",
1314
"fontsource-roboto": "^3.0.3",
15+
"lodash": "^4.17.21",
16+
"query-string": "^7.1.1",
1417
"react": "^17.0.1",
1518
"react-dom": "^17.0.1",
1619
"react-flip-move": "^3.0.4",
1720
"react-github-btn": "^1.2.0",
21+
"react-mt-svg-lines": "^0.9.1",
22+
"react-router": "^6.2.1",
1823
"react-router-dom": "^5.2.0",
1924
"react-scripts": "4.0.0",
2025
"react-spring": "^8.0.27",

src/15puzzleComponents/navbar.jsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import React, {Component} from 'react';
2+
import {Link} from "react-router-dom";
3+
4+
class Navbar extends Component {
5+
render() {
6+
return (
7+
<nav className="navbar navbar-expand-lg navbar-dark bg-dark">
8+
<span className="navbar-brand">15 Puzzle Game</span>
9+
<button className="navbar-toggler" type="button" data-toggle="collapse"
10+
data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent"
11+
aria-expanded="false" aria-label="Toggle navigation">
12+
<span className="navbar-toggler-icon"></span>
13+
</button>
14+
<div className="collapse navbar-collapse" id="navbarSupportedContent" >
15+
<Link to={"/"}>
16+
<span style={{color:"white"}}>
17+
Home
18+
</span>
19+
</Link>
20+
</div>
21+
22+
</nav>
23+
);
24+
}
25+
}
26+
27+
export default Navbar;

src/15puzzleComponents/puzzle.jsx

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import React, {Component} from 'react';
2+
import Navbar from "./navbar";
3+
import FlipMove from 'react-flip-move';
4+
import SvgLines from 'react-mt-svg-lines';
5+
import '../helpers/array_helpers';
6+
import './style.css';
7+
import {times} from 'lodash';
8+
9+
const FLIP_DURATION = 750;
10+
11+
class Puzzle extends Component {
12+
constructor() {
13+
super();
14+
this.state = {
15+
squares: times(16, i => ({
16+
value: i
17+
})),
18+
};
19+
}
20+
21+
balsal = async () => {
22+
for (let i = 0; i < 15; i++) {
23+
this.setState({
24+
squares: this.state.squares.slice().swap(i, i + 1)
25+
});
26+
await sleep(500);
27+
}
28+
29+
}
30+
31+
render() {
32+
let classNames;
33+
return (
34+
<div style={{backgroundColor: "#57407c", height: '100'}}
35+
className={'full-height'}
36+
>
37+
<Navbar/>
38+
<center className={'justify-content-around '}
39+
style={{textAlign: "Center"}}>
40+
<div style={{textAlign: "center", height: "440px", width: "440px", margin: 'auto'}}
41+
className={"m-5"}>
42+
<FlipMove
43+
duration={FLIP_DURATION}
44+
easing="cubic-bezier(.12,.36,.14,1.2)"
45+
>
46+
{this.state.squares.map((stt) =>
47+
<div key={stt.value}
48+
className={stt.value === 0 ? "square " : stt.value % 2 === 0 ? 'square shadow correct pt-1' : 'square shadow painted pt-1'}
49+
>
50+
{stt.value === 0 ? "" : stt.value}
51+
</div>
52+
)}
53+
<br/>
54+
</FlipMove>
55+
<button className={"btn btn-secondary"} onClick={this.balsal}>Animate</button>
56+
</div>
57+
58+
{/*<FlipMove*/}
59+
{/* duration={FLIP_DURATION}*/}
60+
{/* easing="cubic-bezier(.12,.36,.14,1.2)" >*/}
61+
{/* <line key={5464} style={{strokeWidth: "3px", stroke: '#999'}} x2={299} y2={186}*/}
62+
{/* x1={(this.state.squares[0].value + 1) * 50}*/}
63+
{/* y1={(this.state.squares[0].value + 1) * 50}></line>*/}
64+
{/* <div key={this.state.squares[0].value}*/}
65+
{/* className={'square shadow correct pt-1'}>*/}
66+
{/* {this.state.squares[0].value}*/}
67+
{/* </div>*/}
68+
{/* <div key={this.state.squares[1].value}*/}
69+
{/* className={'square shadow correct pt-1'}>*/}
70+
{/* {this.state.squares[1].value}*/}
71+
{/* </div>*/}
72+
{/*</FlipMove>*/}
73+
{/*/!*<SvgLines animate={true} duration={1000}>*!/*/}
74+
{/*/!* <svg viewBox="0 0 960 500">*!/*/}
75+
{/*/!* <line key={5464} style={{strokeWidth: "3px", stroke: '#999'}} x2={299} y2={186}*!/*/}
76+
{/*/!* x1={(this.state.squares[0].value + 1) * 50}*!/*/}
77+
{/*/!* y1={(this.state.squares[0].value + 1) * 50}></line>*!/*/}
78+
{/*/!* </svg>*!/*/}
79+
{/*/!*</SvgLines>*!/*/}
80+
</center>
81+
82+
</div>
83+
);
84+
}
85+
}
86+
87+
function sleep(ms) {
88+
return new Promise(resolve => setTimeout(resolve, ms));
89+
}
90+
91+
export default Puzzle;

src/15puzzleComponents/style.css

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
.square{
2+
height: 100px;
3+
width: 100px;
4+
margin: 5px;
5+
float: left;
6+
border-radius: 10px;
7+
font-size: 60px;
8+
font-weight: 700;
9+
color: white;
10+
font-family: "Pacifico", cursive;
11+
}
12+
13+
.painted{
14+
background-color: rgb(106, 198, 184)
15+
}
16+
17+
.correct{
18+
background-color: #e88a45;
19+
}
20+
21+
.full-height {
22+
height: calc(100vh);
23+
}

src/App.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import React, {Component} from 'react';
2+
import './TrialPathFinder/firebaseConfig';
3+
24
import {HashRouter as Router, Switch, Route} from 'react-router-dom';
35
import Pathfinder from "./pathfinderComponents/pathfinder";
46
import Home from "./homeComponents/home";
@@ -8,12 +10,20 @@ import Queen from "./queenComponents/queen";
810
import ConvexHull from "./convexHullComponents/convexHull";
911
import BinarySearch from "./binarySearchComponent/binarySearch";
1012
import RecursiveSort from "./recursiveSortComponents/recursiveSort";
13+
import Puzzle from "./15puzzleComponents/puzzle";
14+
import TrialPath from "./TrialPathFinder/pathfinder";
15+
import TrialPathEdit from "./TrialPathFinderEdit/pathfinder";
16+
import TuringMachine from "./Turing Machine/turingMachine";
17+
18+
19+
1120
class App extends Component {
21+
1222
constructor() {
1323
super();
1424
}
1525
componentDidMount() {
16-
console.log(window.innerHeight," ",window.innerWidth);
26+
// console.log(window.innerHeight," ",window.innerWidth);
1727
}
1828

1929
render() {
@@ -27,7 +37,12 @@ class App extends Component {
2737
<Route path='/convexhull' component={ConvexHull}/>
2838
<Route path='/binarysearch' component={BinarySearch}/>
2939
<Route path='/recursivesort' component={RecursiveSort}/>
40+
<Route path='/turing' component={TuringMachine}/>
41+
<Route path='/15puzzle' component={Puzzle}/>
42+
{/*<Route path='/trial' component={TrialPath}/>*/}
43+
{/*<Route path='/trialedit' component={TrialPathEdit}/>*/}
3044
<Route path='/' component={Home}/>
45+
3146
</Switch>
3247
</Router>
3348
);

src/Turing Machine/cell.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.turing-cell{
2+
height:50px;
3+
width:50px;
4+
background-color: white;
5+
outline:1px solid rgb(175, 216, 248);
6+
display: inline-block;
7+
font-size: 25px;
8+
text-align: center;
9+
color: black;
10+
/*border-radius: 5px 20px;*/
11+
}

src/Turing Machine/cell.jsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React, {Component} from 'react';
2+
import './cell.css';
3+
class Cell extends Component {
4+
render() {
5+
return (
6+
<div className={'turing-cell'}>
7+
{this.props.val}
8+
</div>
9+
);
10+
}
11+
}
12+
13+
export default Cell;

src/Turing Machine/menu.jsx

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import React, {Component} from 'react';
2+
import DiscreteSlider from "./slider";
3+
import SimpleSelect from "./simpleSelect";
4+
5+
6+
class Menu extends Component {
7+
render() {
8+
return (
9+
<nav className="nav alert-dark">
10+
11+
12+
<DiscreteSlider
13+
default={50}
14+
min={10}
15+
max={100}
16+
step={1}
17+
title="Speed"
18+
onCountChange={this.props.onSpeedChange}
19+
disable={false}
20+
/>
21+
<SimpleSelect
22+
pos={0}
23+
onAlgoChanged={this.props.onAlgoChanged}
24+
/>
25+
26+
<button
27+
className='btn btn-warning btn-lg m-2'
28+
// onClick={this.props.onViusalize}
29+
disabled={this.props.disable}
30+
>Reset</button>
31+
32+
<button
33+
className='btn btn-warning btn-lg '
34+
onClick={this.props.visualize}
35+
disabled={this.props.disable}
36+
>Visualize</button>
37+
38+
</nav>
39+
);
40+
}
41+
isClickable = () =>
42+
{
43+
if (this.props.disable) {
44+
return {cursor: "not-allowed"};
45+
} else {
46+
return {};
47+
}
48+
}
49+
}
50+
51+
52+
export default Menu;

0 commit comments

Comments
 (0)