Skip to content
This repository has been archived by the owner on Aug 27, 2021. It is now read-only.

Commit

Permalink
messing around with ant
Browse files Browse the repository at this point in the history
  • Loading branch information
leeandher committed Dec 6, 2018
1 parent dc6b0d5 commit 13ba006
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 133 deletions.
118 changes: 6 additions & 112 deletions src/App.js
@@ -1,23 +1,10 @@
import React from "react";

import { Layout, Menu, Icon } from "antd";

import GameBoard from "./GameBoard";
import ControlPanel from "./ControlPanel";

const { Header, Content, Footer, Sider } = Layout;
const { SubMenu } = Menu;

class App extends React.Component {
state = {
size: {
type: "medium",
height: 30,
width: 50
},
speed: 100,
generation: 0,
collapsed: false,
isPlaying: false,
boardData: []
};
Expand Down Expand Up @@ -123,105 +110,12 @@ class App extends React.Component {

render() {
return (
<React.Fragment>
<Header>
<h1 style={{ color: "white" }}>Conway Game of Life</h1>
</Header>
<Layout>
<Sider
collapsible
collapsed={this.state.collapsed}
onCollapse={collapsed => this.setState({ collapsed })}
>
<Menu theme="dark">
<Menu.Item>
<Icon type={this.state.isPlaying ? "pause" : "caret-right"} />
<span>{this.state.isPlaying ? "Pause" : "Play"}</span>
</Menu.Item>
<Menu.Item>
<Icon type="step-forward" />
<span>Increment</span>
</Menu.Item>
<Menu.Item>
<Icon type="close" />
<span>Clear</span>
</Menu.Item>
<Menu.Item>
<Icon type="redo" />
<span>Randomize</span>
</Menu.Item>
<Menu.Divider />
<SubMenu
title={
<React.Fragment>
<Icon type="forward" />
<span>Speed</span>
</React.Fragment>
}
>
<Menu.Item>Slow</Menu.Item>
<Menu.Item>Medium</Menu.Item>
<Menu.Item>Fast</Menu.Item>
</SubMenu>

<SubMenu
title={
<React.Fragment>
<Icon type="pic-center" />
<span>Size</span>
</React.Fragment>
}
>
<Menu.Item>Slow</Menu.Item>
<Menu.Item>Medium</Menu.Item>
<Menu.Item>Fast</Menu.Item>
</SubMenu>

<SubMenu
title={
<React.Fragment>
<Icon type="star" />
<span>Styling</span>
</React.Fragment>
}
>
<Menu.Item>Slow</Menu.Item>
<Menu.Item>Medium</Menu.Item>
<Menu.Item>Fast</Menu.Item>
</SubMenu>

<SubMenu
title={
<React.Fragment>
<Icon type="build" />
<span>Patterns</span>
</React.Fragment>
}
>
<Menu.Item>Slow</Menu.Item>
<Menu.Item>Medium</Menu.Item>
<Menu.Item>Fast</Menu.Item>
</SubMenu>

<Menu.Item />
<Menu.Item />
<Menu.Item />
</Menu>
</Sider>
<Layout>
<Content>
<h1>Generation: {this.state.generation}</h1>
<GameBoard
height={this.state.size.height}
width={this.state.size.width}
boardData={this.state.boardData}
spawnCell={this.spawnCell}
/>
<ControlPanel size={this.state.size.type} resize={this.resize} />
</Content>
</Layout>
</Layout>
</React.Fragment>
<GameBoard
height={this.props.height}
width={this.props.width}
boardData={this.state.boardData}
spawnCell={this.spawnCell}
/>
);
}
}
Expand Down
19 changes: 0 additions & 19 deletions src/ControlPanel.js

This file was deleted.

224 changes: 224 additions & 0 deletions src/Page.js
@@ -0,0 +1,224 @@
import React from "react";

import { Layout, Menu, Icon } from "antd";

import GameBoard from "./GameBoard";

const { Content, Sider } = Layout;
const { SubMenu } = Menu;

class Page extends React.Component {
state = {
size: {
type: "medium",
height: 30,
width: 50
},
speed: 100,
generation: 0,
collapsed: true,
isPlaying: false,
boardData: []
};

randomizeBoard = (
height = this.state.size.height,
width = this.state.size.width
) => {
// Generate the random board
const newBoard = [];
for (let i = 0; i < height; i++) {
const rowData = [];
for (let j = 0; j < width; j++) {
rowData.push(Math.round(Math.random()));
}
newBoard.push(rowData);
}
this.setState({ boardData: newBoard });
};

stepBoard = () => {
// Create a copy of the existing board
const currentBoard = this.state.boardData;
const newBoard = currentBoard.map(row => [...row]);

currentBoard.forEach((row, i) => {
row.forEach((cell, j) => {
// Get the values of the surrounding 8 cells
const neighbours = [
i !== 0 ? currentBoard[i - 1][j - 1] || 0 : 0, // Top left
i !== 0 ? currentBoard[i - 1][j] || 0 : 0, // Above
i !== 0 ? currentBoard[i - 1][j + 1] || 0 : 0, // Top right
row[j - 1] || 0, // Left
row[j + 1] || 0, // Right
i !== this.state.size.height - 1
? currentBoard[i + 1][j - 1] || 0
: 0, // Bottom left
i !== this.state.size.height - 1 ? currentBoard[i + 1][j] || 0 : 0, // Below
i !== this.state.size.height - 1 ? currentBoard[i + 1][j + 1] || 0 : 0 // Bottom right
];

// Ignore the 'old'/'new' alive values and sum them as ones
const neighbourValue = neighbours.reduce(
(total, val) => total + Boolean(val),
0
);

// Now take a look at the existing cell
switch (cell) {
// If alive...
case 2:
case 1:
newBoard[i][j] = neighbourValue <= 1 || neighbourValue >= 4 ? 0 : 2;
break;

//If dead...
case 0:
default:
newBoard[i][j] = neighbourValue === 3 ? 1 : 0;
break;
}
});
});

this.setState({
boardData: newBoard,
generation: this.state.generation + 1
});
};

spawnCell = (row, col) => {
const newBoard = this.state.boardData.map(row => [...row]);
newBoard[row][col] = newBoard[row][col] > 0 ? 0 : 1;
this.setState({ boardData: newBoard });
};

resize = e => {
const type = e.target.value;
const newSize = {};
switch (type) {
case "large":
newSize.height = 50;
newSize.width = 60;
break;
case "medium":
default:
newSize.height = 30;
newSize.width = 40;
break;
case "small":
newSize.height = 20;
newSize.width = 30;
}
newSize.type = type;
this.randomizeBoard(newSize.height, newSize.width);
this.setState({ size: newSize });
};

componentDidMount() {
this.randomizeBoard();
// setInterval(() => this.stepBoard(), this.state.speed);
}

render() {
return (
<React.Fragment>
<Layout>
<Sider
collapsible
collapsed={this.state.collapsed}
onCollapse={collapsed => this.setState({ collapsed })}
>
<Menu theme="dark">
<Menu.Item>
<Icon type={this.state.isPlaying ? "pause" : "caret-right"} />
<span>{this.state.isPlaying ? "Pause" : "Play"}</span>
</Menu.Item>
<Menu.Item>
<Icon type="step-forward" />
<span>Increment</span>
</Menu.Item>
<Menu.Item>
<Icon type="close" />
<span>Clear</span>
</Menu.Item>
<Menu.Item>
<Icon type="redo" />
<span>Randomize</span>
</Menu.Item>
<Menu.Divider />
<SubMenu
title={
<React.Fragment>
<Icon type="forward" />
<span>Speed</span>
</React.Fragment>
}
>
<Menu.Item>Slow</Menu.Item>
<Menu.Item>Medium</Menu.Item>
<Menu.Item>Fast</Menu.Item>
</SubMenu>

<SubMenu
title={
<React.Fragment>
<Icon type="pic-center" />
<span>Size</span>
</React.Fragment>
}
>
<Menu.Item>Slow</Menu.Item>
<Menu.Item>Medium</Menu.Item>
<Menu.Item>Fast</Menu.Item>
</SubMenu>

<SubMenu
title={
<React.Fragment>
<Icon type="star" />
<span>Styling</span>
</React.Fragment>
}
>
<Menu.Item>Slow</Menu.Item>
<Menu.Item>Medium</Menu.Item>
<Menu.Item>Fast</Menu.Item>
</SubMenu>

<SubMenu
title={
<React.Fragment>
<Icon type="build" />
<span>Patterns</span>
</React.Fragment>
}
>
<Menu.Item>Slow</Menu.Item>
<Menu.Item>Medium</Menu.Item>
<Menu.Item>Fast</Menu.Item>
</SubMenu>

<Menu.Item />
<Menu.Item />
<Menu.Item />
</Menu>
</Sider>
<Layout>
<Content>
<h1>Generation: {this.state.generation}</h1>
<GameBoard
height={this.state.size.height}
width={this.state.size.width}
boardData={this.state.boardData}
spawnCell={this.spawnCell}
/>
</Content>
</Layout>
</Layout>
</React.Fragment>
);
}
}

export default Page;
4 changes: 2 additions & 2 deletions src/index.js
Expand Up @@ -2,10 +2,10 @@ import React from "react";
import ReactDOM from "react-dom";
import "./styles/css/index.css";
import "antd/dist/antd.css";
import App from "./App";
import Page from "./Page";
import * as serviceWorker from "./serviceWorker";

ReactDOM.render(<App />, document.getElementById("root"));
ReactDOM.render(<Page />, document.getElementById("root"));

// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
Expand Down

0 comments on commit 13ba006

Please sign in to comment.