-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGameOfLife.js
More file actions
executable file
·200 lines (181 loc) · 5.88 KB
/
GameOfLife.js
File metadata and controls
executable file
·200 lines (181 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import Cell from './Cell';
import './GameOfLife.css';
class GameOfLife extends Component {
constructor(props) {
super(props);
this.state = {
width: props.width,
height: props.height,
generation: props.generation,
started: props.start,
worldSize: props.width * props.height,
populatedCells: (props.startingPopulation || []).map((cell) => {
return cell[0] + (cell[1] * props.width);
}),
border: this.props.border,
};
}
cellIsPopulated(populatedCells, x, y) {
let cellIndex = x + (y * this.state.width);
return populatedCells.includes(cellIndex);
}
getAdjacentCellCoordinates(x, y) {
let left = x - 1;
let right = x + 1;
if (this.state.border === 'marquee') {
if (left < 0) {
left = this.state.width - 1;
}
if (right >= this.state.width) {
right = 0;
}
} else {
right = Math.min(this.state.width - 1, x + 1);
}
let top = y - 1;
let bottom = y + 1;
if (this.state.border === 'marquee') {
if (bottom >= this.state.height) {
bottom = 0;
}
if (top < 0) {
top = this.state.height - 1;
}
} else {
bottom = Math.min(this.state.height - 1, y + 1);
}
return {
top, right, bottom, left,
};
}
getPopulatedCellsAfter(populatedCells) {
let newPopulatedCells = [];
// TODO: Make this more efficient. The smallest
// number that it could possibly be be is the
// smallest in the pop. cells minus (width + 1)
// and the biggest is the biggest in pop. cells
// plus (width + 1). But need to take marquee borders
// into account. Could do a for (let x of candidates(populatedCells))
// to make a shortlist. Or could go through each populated cell,
// go through each neighbour, then add it to the "checked it"
// list - this is definitely more efficient
for (let x = 0; x < this.state.width; x++) {
for (let y = 0; y < this.state.height; y++) {
const currentCellIndex = x + (y * this.state.width);
let numberOfPopulatedNeighbours = 0;
// let cellRight = x + 1 >= this.state.width ? 0 : x + 1;
// let cellBelow = y + 1 >= this.state.height ? 0 : y + 1;
const { top, right, bottom, left } = this.getAdjacentCellCoordinates(x, y);
let xOptions = x === right ? [left, x] : [left, x, right];
let yOptions = y === bottom ? [top, y] : [top, y, bottom];
for (let nx of xOptions) {
for (let ny of yOptions) {
let cellIndex = nx + (ny * this.state.width);
let populated = populatedCells.includes(cellIndex);
if ((nx !== x || ny !== y) && populated) {
++numberOfPopulatedNeighbours;
}
}
}
const currentCellPopulated = populatedCells.includes(currentCellIndex);
if (numberOfPopulatedNeighbours === 3) {
newPopulatedCells.push(currentCellIndex);
} else if (numberOfPopulatedNeighbours === 2 && currentCellPopulated) {
newPopulatedCells.push(currentCellIndex);
}
}
}
return newPopulatedCells;
}
componentDidMount() {
if (this.state.started) {
setTimeout(this.setState({ generation: this.state.generation + 1 }), 1000);
}
}
componentDidUpdate(prevProps, prevState) {
if (this.props.generation !== prevProps.generation) {
this.setState({ generation: this.props.generation });
}
if (this.props.started !== prevProps.started) {
this.setState({ started: this.props.started });
}
if (this.state.started === true && !prevState.started) {
setTimeout(() => {
this.setState({ generation: this.state.generation + 1 })
}, 100);
}
if (this.state.generation !== prevState.generation) {
let populatedCellsToProgress = this.state.populatedCells;
for (let gen = prevState.generation + 1; gen <= this.state.generation; gen++) {
populatedCellsToProgress = this.getPopulatedCellsAfter(populatedCellsToProgress)
this.setState({ populatedCells: populatedCellsToProgress });
}
if (this.state.started) {
setTimeout(() => {
this.setState({ generation: this.state.generation + 1 })
}, 100);
}
}
}
toggleCell(cellIndexValue) {
if (this.state.populatedCells.includes(cellIndexValue)) {
this.setState({
populatedCells: this.state.populatedCells.filter(value => value !== cellIndexValue),
});
} else {
this.setState({
populatedCells: [...this.state.populatedCells, cellIndexValue],
});
}
}
toggleGame() {
this.setState({
started: !this.state.started,
});
}
render() {
const worldSize = this.state.width * this.state.height;
let cells = [];
for (let i = 0; i < worldSize; i++) {
const props = { populated: false, width: this.props.cellWidth };
if (this.state.populatedCells.includes(i)) {
props.populated = true;
}
cells.push(<Cell key={"cell-" + i} onClick={() => {
this.toggleCell(i);
}} {...props} />);
}
const style = {
display: 'block',
width: this.state.width * this.props.cellWidth,
};
return (
<div>
<div className="GameOfLife" style={style}>
{cells}
</div>
<div className="ControlPanel">
<button onClick={this.toggleGame.bind(this)}>{this.state.started ? 'Pause' : 'Start'}</button>
</div>
</div>
);
}
}
GameOfLife.propTypes = {
width: PropTypes.number,
height: PropTypes.number,
generation: PropTypes.number,
start: PropTypes.bool,
gridBehaviour: PropTypes.bool,
border: PropTypes.oneOf(['hard', 'marquee']),
cellWidth: PropTypes.number,
}
GameOfLife.defaultProps = {
generation: 0,
start: false,
border: 'hard',
cellWidth: 20,
}
export default GameOfLife;