@@ -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
183193GameOfLife . defaultProps = {
184194 generation : 0 ,
185195 start : false ,
186196 border : 'hard' ,
197+ cellWidth : 20 ,
187198}
188199
189200export default GameOfLife ;
0 commit comments