Allow Defining x and y for Patterns

This commit is contained in:
Marty Oehme 2018-07-07 20:35:04 +02:00
parent 964ba69c2e
commit 9816778d4b
2 changed files with 19 additions and 36 deletions

View File

@ -25,6 +25,7 @@ class App {
App(this.canvas) {
_elapsed.start();
grid.startingPattern();
}
void reset () {

View File

@ -22,65 +22,47 @@ class Grid {
this.map = new List() {
map.addAll(_buildGrid(w, h));
// BLINKER
// map[5][5].state = true;
// map[5][6].state = true;
// map[6][5].state = true;
// map[6][6].state = true;
//
// map[7][7].state = true;
// map[7][8].state = true;
// map[8][7].state = true;
// map[8][8].state = true;
// SPACESHIP
// setCellState(1 + 5, 0 + 5, true);
// setCellState(2 + 5, 1 + 5, true);
// setCellState(2 + 5, 2 + 5, true);
// setCellState(1 + 5, 2 + 5, true);
// setCellState(0 + 5, 2 + 5, true);
startingPattern();
print("Grid creation finished");
}
void startingPattern([Pattern pattern]) {
void startingPattern({Pattern pattern, int x, int y}) {
math.Random rng = new math.Random();
var x = rng.nextInt(w~/3) + (w~/3);
var y = rng.nextInt(h~/3) + (h~/3);
int cx = x ?? rng.nextInt(w~/3) + (w~/3);
int cy = y ?? rng.nextInt(h~/3) + (h~/3);
switch (pattern) {
// Two blocks, offset
// ##
// ##
case Pattern.Blinker:
setCellState(x, y, true);
setCellState(x+1, y, true);
setCellState(x, y+1, true);
setCellState(x+1, y+1, true);
setCellState(cx, cy, true);
setCellState(cx+1, cy, true);
setCellState(cx, cy+1, true);
setCellState(cx+1, cy+1, true);
setCellState(x+2, y+2, true);
setCellState(x+3, y+2, true);
setCellState(x+2, y+3, true);
setCellState(x+3, y+3, true);
setCellState(cx+2, cy+2, true);
setCellState(cx+3, cy+2, true);
setCellState(cx+2, cy+3, true);
setCellState(cx+3, cy+3, true);
break;
// A 'gliding' Spaceship
// #
// #
// ###
case Pattern.SpaceShip:
setCellState(1 + x, 0 + y, true);
setCellState(2 + x, 1 + y, true);
setCellState(2 + x, 2 + y, true);
setCellState(1 + x, 2 + y, true);
setCellState(0 + x, 2 + y, true);
setCellState(1 + cx, 0 + cy, true);
setCellState(2 + cx, 1 + cy, true);
setCellState(2 + cx, 2 + cy, true);
setCellState(1 + cx, 2 + cy, true);
setCellState(0 + cx, 2 + cy, true);
break;
default:
for (var i = 0; i < rng.nextInt(20); i++) {
setCellState(x + rng.nextInt(10), y + rng.nextInt(10), true);
setCellState(cx + rng.nextInt(10), cy + rng.nextInt(10), true);
}
break;
}
}
void setCellState(int x, int y, bool state) {
if (y < map.length && x < map[y].length) map[y][x].state = state;
}