From 964ba69c2e4e459cfc3a1046f559b49ab5002535 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Sat, 7 Jul 2018 20:02:13 +0200 Subject: [PATCH 1/3] Add Grid Starting Patterns can be common forms and random --- lib/src/Grid.dart | 61 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 8 deletions(-) diff --git a/lib/src/Grid.dart b/lib/src/Grid.dart index b0d7e9f..72152d0 100644 --- a/lib/src/Grid.dart +++ b/lib/src/Grid.dart @@ -1,8 +1,14 @@ import 'dart:html' as html; +import 'dart:math' as math; import 'package:rules_of_living/src/Cell.dart'; import 'package:rules_of_living/src/Rule.dart'; +enum Pattern { + SpaceShip, + Blinker +} + class Grid { final int w; final int h; @@ -28,16 +34,54 @@ class Grid { // map[8][8].state = true; // SPACESHIP - setState(1 + 5, 0 + 5, true); - setState(2 + 5, 1 + 5, true); - setState(2 + 5, 2 + 5, true); - setState(1 + 5, 2 + 5, true); - setState(0 + 5, 2 + 5, true); +// 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 setState(int x, int y, bool state) { + void startingPattern([Pattern pattern]) { + math.Random rng = new math.Random(); + var x = rng.nextInt(w~/3) + (w~/3); + var 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(x+2, y+2, true); + setCellState(x+3, y+2, true); + setCellState(x+2, y+3, true); + setCellState(x+3, y+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); + break; + default: + for (var i = 0; i < rng.nextInt(20); i++) { + setCellState(x + rng.nextInt(10), y + 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; } @@ -70,8 +114,9 @@ class Grid { // GIVES RULES FOR CONWAY GAME OF LIFE BY DEFAULT S23/B3 Cell cell = new Cell(); // cell.surviveRules.add(twoTrue); - cell.surviveRules.add(coagSurvive); - cell.birthRules.add(coagBirth); + cell.surviveRules.add(threeTrue); + cell.surviveRules.add(twoTrue); + cell.birthRules.add(threeTrue); grid[y][x] = cell; } From 9816778d4bdf0cda7ed63bbd01f7f933f1e4b339 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Sat, 7 Jul 2018 20:35:04 +0200 Subject: [PATCH 2/3] 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; } From 4b35dbe5fc689c0401c0f846b062649cb5ac84fb Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Sat, 7 Jul 2018 20:47:23 +0200 Subject: [PATCH 3/3] Add Parameters for Dispersal and Amount of Cells --- lib/src/Grid.dart | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/src/Grid.dart b/lib/src/Grid.dart index f188ae9..5649c6b 100644 --- a/lib/src/Grid.dart +++ b/lib/src/Grid.dart @@ -25,7 +25,7 @@ class Grid { print("Grid creation finished"); } - void startingPattern({Pattern pattern, int x, int y}) { + void startingPattern({Pattern pattern, int x, int y, int amount, int dispersal}) { math.Random rng = new math.Random(); int cx = x ?? rng.nextInt(w~/3) + (w~/3); int cy = y ?? rng.nextInt(h~/3) + (h~/3); @@ -56,8 +56,8 @@ class Grid { setCellState(0 + cx, 2 + cy, true); break; default: - for (var i = 0; i < rng.nextInt(20); i++) { - setCellState(cx + rng.nextInt(10), cy + rng.nextInt(10), true); + for (var i = 0; i < amount ?? rng.nextInt(20); i++) { + setCellState(cx + dispersal ?? rng.nextInt(10), cy + dispersal ?? rng.nextInt(10), true); } break; }