Add reset button and glider and pulsar on ctrl and shift click
This commit is contained in:
parent
f68977a557
commit
922a7d3160
104
src/lib.rs
104
src/lib.rs
@ -165,6 +165,110 @@ impl Universe {
|
||||
let idx = self.get_index(row, col);
|
||||
self.cells[idx].toggle();
|
||||
}
|
||||
|
||||
pub fn kill_all(&mut self) {
|
||||
self.cells.iter_mut().for_each(|cell| *cell = Cell::Dead);
|
||||
}
|
||||
|
||||
pub fn glider(&mut self, row: u32, col: u32) {
|
||||
let glider_grid = [
|
||||
Cell::Dead,
|
||||
Cell::Dead,
|
||||
Cell::Alive,
|
||||
Cell::Alive,
|
||||
Cell::Dead,
|
||||
Cell::Alive,
|
||||
Cell::Dead,
|
||||
Cell::Alive,
|
||||
Cell::Alive,
|
||||
];
|
||||
let glider_positions = [
|
||||
(row - 1, col - 1),
|
||||
(row, col - 1),
|
||||
(row + 1, col - 1),
|
||||
(row - 1, col),
|
||||
(row, col - 1),
|
||||
(row + 1, col),
|
||||
(row - 1, col + 1),
|
||||
(row, col + 1),
|
||||
(row + 1, col + 1),
|
||||
];
|
||||
glider_grid
|
||||
.iter()
|
||||
.zip(glider_positions)
|
||||
.for_each(|(state, (row, col))| {
|
||||
let idx = self.get_index(row, col);
|
||||
self.cells[idx] = *state;
|
||||
});
|
||||
}
|
||||
|
||||
pub fn pulsar(&mut self, row: u32, col: u32) {
|
||||
let pulsar_grid = [
|
||||
Cell::Alive,
|
||||
Cell::Alive,
|
||||
Cell::Alive,
|
||||
Cell::Dead,
|
||||
Cell::Dead,
|
||||
Cell::Dead,
|
||||
Cell::Alive,
|
||||
Cell::Alive,
|
||||
Cell::Alive,
|
||||
Cell::Alive,
|
||||
Cell::Dead,
|
||||
Cell::Alive,
|
||||
Cell::Dead,
|
||||
Cell::Dead,
|
||||
Cell::Dead,
|
||||
Cell::Alive,
|
||||
Cell::Dead,
|
||||
Cell::Alive,
|
||||
Cell::Alive,
|
||||
Cell::Alive,
|
||||
Cell::Alive,
|
||||
Cell::Dead,
|
||||
Cell::Dead,
|
||||
Cell::Dead,
|
||||
Cell::Alive,
|
||||
Cell::Alive,
|
||||
Cell::Alive,
|
||||
];
|
||||
let pulsar_positions = [
|
||||
(row - 4, col - 1),
|
||||
(row - 3, col - 1),
|
||||
(row - 2, col - 1),
|
||||
(row - 1, col - 1),
|
||||
(row, col - 1),
|
||||
(row + 1, col - 1),
|
||||
(row + 2, col - 1),
|
||||
(row + 3, col - 1),
|
||||
(row + 4, col - 1),
|
||||
(row - 4, col),
|
||||
(row - 3, col),
|
||||
(row - 2, col),
|
||||
(row - 1, col),
|
||||
(row, col),
|
||||
(row + 1, col),
|
||||
(row + 2, col),
|
||||
(row + 3, col),
|
||||
(row + 4, col),
|
||||
(row - 4, col + 1),
|
||||
(row - 3, col + 1),
|
||||
(row - 2, col + 1),
|
||||
(row - 1, col + 1),
|
||||
(row, col + 1),
|
||||
(row + 1, col + 1),
|
||||
(row + 2, col + 1),
|
||||
(row + 3, col + 1),
|
||||
(row + 4, col + 1),
|
||||
];
|
||||
pulsar_grid
|
||||
.iter()
|
||||
.zip(pulsar_positions)
|
||||
.for_each(|(state, (row, col))| {
|
||||
let idx = self.get_index(row, col);
|
||||
self.cells[idx] = *state;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl Universe {
|
||||
|
@ -23,6 +23,7 @@
|
||||
<body>
|
||||
<canvas id="game-of-life-canvas"></canvas>
|
||||
<button id="play-pause"></button>
|
||||
<button id="reset">Reset</button>
|
||||
<script src="./bootstrap.js"></script>
|
||||
</body>
|
||||
|
||||
|
12
www/index.js
12
www/index.js
@ -28,7 +28,13 @@ canvas.addEventListener("mousedown", event => {
|
||||
const row = Math.min(Math.floor(canvasTop / (CELL_SIZE + 1)), height - 1);
|
||||
const col = Math.min(Math.floor(canvasLeft / (CELL_SIZE + 1)), width - 1);
|
||||
|
||||
if (event.ctrlKey) {
|
||||
universe.glider(row, col);
|
||||
} else if (event.shiftKey) {
|
||||
universe.pulsar(row, col);
|
||||
} else {
|
||||
universe.toggle_cell(row, col);
|
||||
}
|
||||
|
||||
drawGrid();
|
||||
drawCells();
|
||||
@ -38,6 +44,12 @@ const ctx = canvas.getContext('2d');
|
||||
|
||||
const playPauseButton = document.getElementById("play-pause");
|
||||
|
||||
const reset = document.getElementById("reset");
|
||||
|
||||
reset.addEventListener("click", event => {
|
||||
universe.kill_all();
|
||||
});
|
||||
|
||||
const drawGrid = () => {
|
||||
ctx.beginPath();
|
||||
ctx.strokeStyle = GRID_COLOR;
|
||||
|
Loading…
Reference in New Issue
Block a user