Merge branch 'random-starting-pattern'

This commit is contained in:
Marty Oehme 2018-07-07 20:47:37 +02:00
commit 2f96712b60
2 changed files with 49 additions and 21 deletions

View file

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

View file

@ -1,8 +1,14 @@
import 'dart:html' as html; import 'dart:html' as html;
import 'dart:math' as math;
import 'package:rules_of_living/src/Cell.dart'; import 'package:rules_of_living/src/Cell.dart';
import 'package:rules_of_living/src/Rule.dart'; import 'package:rules_of_living/src/Rule.dart';
enum Pattern {
SpaceShip,
Blinker
}
class Grid { class Grid {
final int w; final int w;
final int h; final int h;
@ -16,28 +22,48 @@ class Grid {
this.map = new List() { this.map = new List() {
map.addAll(_buildGrid(w, h)); 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
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);
print("Grid creation finished"); print("Grid creation finished");
} }
void setState(int x, int y, bool state) { 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);
switch (pattern) {
// Two blocks, offset
// ##
// ##
case Pattern.Blinker:
setCellState(cx, cy, true);
setCellState(cx+1, cy, true);
setCellState(cx, cy+1, true);
setCellState(cx+1, cy+1, 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 + 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 < amount ?? rng.nextInt(20); i++) {
setCellState(cx + dispersal ?? rng.nextInt(10), cy + dispersal ?? 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; if (y < map.length && x < map[y].length) map[y][x].state = state;
} }
@ -70,8 +96,9 @@ class Grid {
// GIVES RULES FOR CONWAY GAME OF LIFE BY DEFAULT S23/B3 // GIVES RULES FOR CONWAY GAME OF LIFE BY DEFAULT S23/B3
Cell cell = new Cell(); Cell cell = new Cell();
// cell.surviveRules.add(twoTrue); // cell.surviveRules.add(twoTrue);
cell.surviveRules.add(coagSurvive); cell.surviveRules.add(threeTrue);
cell.birthRules.add(coagBirth); cell.surviveRules.add(twoTrue);
cell.birthRules.add(threeTrue);
grid[y][x] = cell; grid[y][x] = cell;
} }