Skip to content

Commit 620657a

Browse files
committed
Props for sizes
1 parent d847cba commit 620657a

File tree

4 files changed

+27
-10
lines changed

4 files changed

+27
-10
lines changed

src/Cell.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
11
import React, { Component } from 'react';
22
import classNames from 'classnames';
33
import PropTypes from 'prop-types';
4+
import { timingSafeEqual } from 'crypto';
45

56
class Cell extends Component {
67
render() {
78
const cellClass = classNames({
89
'Cell': true,
910
'populated': this.props.populated
1011
});
12+
const cellStyle = {
13+
width: this.props.width,
14+
height: this.props.height || this.props.width,
15+
}
1116
return (
12-
<div className={cellClass} onClick={this.props.onClick}>
17+
<div className={cellClass} onClick={this.props.onClick} style={cellStyle}>
1318
</div>
1419
);
1520
}
1621
}
1722

1823
Cell.propTypes = {
1924
populated: PropTypes.bool,
25+
width: PropTypes.number.isRequired,
26+
height: PropTypes.number,
2027
};
2128

2229
Cell.defaultProps = {
23-
populated: false
30+
populated: false,
2431
};
2532

2633
export default Cell;

src/GameOfLife.css

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
.Cell {
99
display:inline-block;
10-
width:20px;
11-
height:20px;
1210
box-sizing:border-box;
1311
border:1px solid black;
1412
border-right:none;

src/GameOfLife.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class GameOfLife extends Component {
2525
return populatedCells.includes(cellIndex);
2626
}
2727

28-
getAdjacentCellIndices(x, y) {
28+
getAdjacentCellCoordinates(x, y) {
2929
let left = x - 1;
3030
let right = x + 1;
3131
if (this.state.border === 'marquee') {
@@ -59,13 +59,22 @@ class GameOfLife extends Component {
5959

6060
getPopulatedCellsAfter(populatedCells) {
6161
let newPopulatedCells = [];
62+
// TODO: Make this more efficient. The smallest
63+
// number that it could possibly be be is the
64+
// smallest in the pop. cells minus (width + 1)
65+
// and the biggest is the biggest in pop. cells
66+
// plus (width + 1). But need to take marquee borders
67+
// into account. Could do a for (let x of candidates(populatedCells))
68+
// to make a shortlist. Or could go through each populated cell,
69+
// go through each neighbour, then add it to the "checked it"
70+
// list - this is definitely more efficient
6271
for (let x = 0; x < this.state.width; x++) {
6372
for (let y = 0; y < this.state.height; y++) {
6473
const currentCellIndex = x + (y * this.state.width);
6574
let numberOfPopulatedNeighbours = 0;
6675
// let cellRight = x + 1 >= this.state.width ? 0 : x + 1;
6776
// let cellBelow = y + 1 >= this.state.height ? 0 : y + 1;
68-
const { top, right, bottom, left } = this.getAdjacentCellIndices(x, y);
77+
const { top, right, bottom, left } = this.getAdjacentCellCoordinates(x, y);
6978
let xOptions = x === right ? [left, x] : [left, x, right];
7079
let yOptions = y === bottom ? [top, y] : [top, y, bottom];
7180

@@ -144,7 +153,7 @@ class GameOfLife extends Component {
144153
const worldSize = this.state.width * this.state.height;
145154
let cells = [];
146155
for (let i = 0; i < worldSize; i++) {
147-
const props = { populated: false };
156+
const props = { populated: false, width: this.props.cellWidth };
148157
if (this.state.populatedCells.includes(i)) {
149158
props.populated = true;
150159
}
@@ -155,7 +164,7 @@ class GameOfLife extends Component {
155164

156165
const style = {
157166
display: 'block',
158-
width: this.state.width * 20,
167+
width: this.state.width * this.props.cellWidth,
159168
};
160169

161170
return (
@@ -178,12 +187,14 @@ GameOfLife.propTypes = {
178187
start: PropTypes.bool,
179188
gridBehaviour: PropTypes.bool,
180189
border: PropTypes.oneOf(['hard', 'marquee']),
190+
cellWidth: PropTypes.number,
181191
}
182192

183193
GameOfLife.defaultProps = {
184194
generation: 0,
185195
start: false,
186196
border: 'hard',
197+
cellWidth: 20,
187198
}
188199

189200
export default GameOfLife;

src/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import * as serviceWorker from './serviceWorker';
66

77
const gliderPopulation = [[1, 0], [2, 1], [0, 2], [1, 2], [2, 2],]
88
ReactDOM.render(<GameOfLife
9-
width={10}
10-
height={10}
9+
width={20}
10+
height={20}
1111
startingPopulation={gliderPopulation}
1212
start={true}
1313
border='marquee'
14+
cellWidth={10}
1415
/>, document.getElementById('root'));
1516

1617
// If you want your app to work offline and load faster, you can change

0 commit comments

Comments
 (0)