From 9816778d4bdf0cda7ed63bbd01f7f933f1e4b339 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Sat, 7 Jul 2018 20:35:04 +0200 Subject: [PATCH] Allow Defining x and y for Patterns --- lib/src/App.dart | 1 + lib/src/Grid.dart | 54 ++++++++++++++++------------------------------- 2 files changed, 19 insertions(+), 36 deletions(-) diff --git a/lib/src/App.dart b/lib/src/App.dart index a571dba..64bfde9 100644 --- a/lib/src/App.dart +++ b/lib/src/App.dart @@ -25,6 +25,7 @@ class App { App(this.canvas) { _elapsed.start(); + grid.startingPattern(); } void reset () { diff --git a/lib/src/Grid.dart b/lib/src/Grid.dart index 72152d0..f188ae9 100644 --- a/lib/src/Grid.dart +++ b/lib/src/Grid.dart @@ -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; }