Skip to content

Commit

Permalink
menu fix and improved minesweeper
Browse files Browse the repository at this point in the history
  • Loading branch information
padraigfl committed Sep 9, 2019
1 parent e875f78 commit 85d97b7
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 32 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"nanoid": "^2.0.1",
"node-sass": "^4.11.0",
"packard-belle": "github:padraigfl/packard-belle#0.3.3",
"pb-minesweeper": "1.0.1",
"pb-minesweeper": "1.0.3",
"react": "^16.8.3",
"react-dom": "^16.8.3",
"react-rnd": "^9.1.2",
Expand Down
64 changes: 49 additions & 15 deletions src/components/Minesweeper/Minesweeper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,39 @@ import { WindowProgram } from "packard-belle";
import Minesweeper95 from "pb-minesweeper";

import { minesweeper16 } from "../../icons";
import buildMenu from "../../helpers/menuBuilder";
import { helpOptions } from "../../helpers/menuBuilder";

import Window from "../tools/Window";

import "./_styles.scss";

const difficulty = {
beginner: {
gridSize: [9, 9],
mines: 11
},
intermediate: {
gridSize: [15, 15],
mines: 30
},
expert: {
gridSize: [30, 15],
mines: 40
}
};

class Minesweeper extends Component {
static defaultProps = {
data: {}
};
state = {
gameId: Date.now()
gameId: Date.now(),
difficulty: "beginner"
};

updateDifficulty = difficulty => () =>
this.setState({ difficulty, gameId: Date.now() });

render() {
const { props, state } = this;
return (
Expand All @@ -29,16 +48,28 @@ class Minesweeper extends Component {
{ text: "needs 100% width height" },
{ text: "overflow control" }
]}
menuOptions={buildMenu({
...props,
fileOptions: [
{
title: "Restart Game",
onClick: () => this.setState({ gameId: Date.now() })
}
],
multiInstance: true
})}
menuOptions={[
{
title: "File",
options: [
{
title: "New",
onClick: () => this.setState({ gameId: Date.now() })
},
[
...Object.keys(difficulty).map(level => ({
title: level[0].toUpperCase() + level.slice(1),
onClick: this.updateDifficulty(level),
className:
this.state.difficulty === level ? "checked" : undefined
})),
{ title: "Custom", isDisabled: true }
],
{ title: "Close", onClick: () => props.onClose(props) }
]
},
helpOptions(props)
]}
className={cx("Minesweeper", props.className, {
"Minesweeper--wrap": state.wrap,
"Window--blocked": state.saveScreen
Expand All @@ -47,12 +78,15 @@ class Minesweeper extends Component {
Component={WindowProgram}
minHeight={150}
minWidth={150}
initialHeight={200}
initialWidth={200}
initialHeight={150}
initialWidth={150}
resizable={false}
onMaximize={null}
>
<Minesweeper95 key={this.state.gameId} />
<Minesweeper95
key={this.state.gameId}
{...difficulty[this.state.difficulty]}
/>
</Window>
</>
);
Expand Down
28 changes: 15 additions & 13 deletions src/helpers/menuBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ const buildCustomOptions = rows =>
[]
);

export const helpOptions = props => ({
title: "Help",
options: [
[{ title: "Help Topics", isDisabled: true }],
{ title: `About ${props.title}`, isDisabled: true }
]
});

export const buildMenu = (props, customOptions = {}) => {
const fileOptions = props.fileOptions || [];
const onClose = [{ title: "Close", onClick: () => props.onClose(props) }];
Expand All @@ -26,15 +34,15 @@ export const buildMenu = (props, customOptions = {}) => {
: [];
const multiInstance = props.multiInstance
? [
{
title: "Open",
isDisabled: !props.onOpenSearch,
onClick: props.onOpenSearch
},
{
title: "New",
onClick: () => props.onOpen(props, { new: true }),
isDisabled: props.singleInstance
},
{
title: "Open...",
isDisabled: !props.onOpenSearch,
onClick: props.onOpenSearch
}
]
: [];
Expand All @@ -45,16 +53,10 @@ export const buildMenu = (props, customOptions = {}) => {
return [
{
title: "File",
options: [...multiInstance, ...saveOptions, ...fileOptions, ...onClose]
options: [...multiInstance, saveOptions, ...fileOptions, onClose]
},
...customElements,
{
title: "Help",
options: [
[{ title: "Help Topics", isDisabled: true }],
{ title: `About ${props.title}`, isDisabled: true }
]
}
helpOptions(props)
];
};

Expand Down

0 comments on commit 85d97b7

Please sign in to comment.