Add Grid Starting Patterns

can be common forms and random
This commit is contained in:
Marty Oehme 2018-07-07 20:02:13 +02:00
parent f50f30453d
commit 964ba69c2e

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;
@ -28,16 +34,54 @@ class Grid {
// map[8][8].state = true; // map[8][8].state = true;
// SPACESHIP // SPACESHIP
setState(1 + 5, 0 + 5, true); // setCellState(1 + 5, 0 + 5, true);
setState(2 + 5, 1 + 5, true); // setCellState(2 + 5, 1 + 5, true);
setState(2 + 5, 2 + 5, true); // setCellState(2 + 5, 2 + 5, true);
setState(1 + 5, 2 + 5, true); // setCellState(1 + 5, 2 + 5, true);
setState(0 + 5, 2 + 5, true); // setCellState(0 + 5, 2 + 5, true);
startingPattern();
print("Grid creation finished"); 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; 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 // 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;
} }