xxxxxxxxxx
var sandpile;
var pauseButton;
var saveButton;
var paused = false;
var txt = []
function preLoad() {
// txt = loadStrings('myList.txt');
}
function setup() {
frameRate(100)
var canSize = 1000;
var size = 51;
// 35000 is close to 151
createCanvas(canSize, canSize);
sandpile = new Sandpile(size, canSize / size);
sandpile.build();
// pauseButton = createButton('pause');
// pauseButton.position(0, 0);
// pauseButton.mousePressed(pausePressed);
// saveButton = createButton("save");
// saveButton.position(0, 30);
// saveButton.mousePressed(savePressed);
}
function draw() {
if (!paused) {
sandpile.updateCycle()
}
}
function savePressed() {
print(sandpile.grid);
save(sandpile.grid, "Sandpile.txt");
}
function mousePressed() {
savePressed()
}
var shades = {
0: 150,
1: [200, 0, 200],
2: [125, 0, 125],
3: [50, 0, 50],
4: 0,
5: 0,
6: 0,
7: 0,
8: 0,
9: 0
}
function pausePressed() {
paused = !paused;
}
function savePressed() {
print(sandpile.grid);
save(sandpile.grid, "Sandpile.txt");
var bob = [[1,2,3,4],[5,6,7,8],[9,1,2,3]];
saveStrings(bob, "bob.txt");
}
function Sandpile(gridSize, squareSize) {
this.gridSize = gridSize;
this.squareSize = squareSize;
this.grid = [];
this.count = 0
this.needsUpdate = {};
this.spillOver = {};
this.startAt = 0;
this.isActive = false;
this.build = function() {
for (var i = 0; i < this.gridSize; i++) {
var row = [];
var nextRow = [];
for (var j = 0; j < this.gridSize; j++) {
row.push(0);
nextRow.push(0);
}
this.grid.push(row);
}
}
this.drawOnce = function() {
for (var i = 0; i < this.gridSize; i++) {
for (var j = 0; j < this.gridSize; j++) {
var val = this.grid[i][j];
fill(shades[val]);
noStroke();
// print("val " + val + ", i: " + i + ", j:" + j + ", size:" + this.squareSize + ", shade:" + shades[val]);
rect(i * this.squareSize, j * this.squareSize, (this.squareSize), this.squareSize);
}
}
}
this.redrawFromSet = function() {
if (Object.keys(this.needsUpdate).length == 0) {
this.feedMiddle();
} else {
for (key in this.needsUpdate) {
var coor = this.needsUpdate[key];
var i = coor[0];
var j = coor[1];
var val = this.grid[i][j];
fill(shades[val]);
noStroke();
// print("val " + val + ", i: " + i + ", j:" + j + ", size:" + this.squareSize + ", shade:" + shades[val]);
// rect(i * this.squareSize, j * this.squareSize, (this.squareSize), this.squareSize);
ellipse((i * this.squareSize) + (this.squareSize / 2), (j * this.squareSize) + (this.squareSize / 2), this.squareSize - 1);
if (val >= 4) {
this.spillOver[key] = coor;
}
delete this.needsUpdate[key];
}
}
}
this.handleSpillOver = function() {
for (key in this.spillOver) {
var coor = this.spillOver[key];
var i = coor[0];
var j = coor[1];
delete this.spillOver[key];
if (i > 0) {
this.grid[i - 1][j] += 1;
this.needsUpdate[[i - 1, j]] = [i - 1, j];
}
if (i < this.gridSize - 1) {
this.grid[i + 1][j] += 1;
this.needsUpdate[[i + 1, j]] = [i + 1, j];
}
if (j > 0) {
this.grid[i][j - 1] += 1;
this.needsUpdate[[i, j - 1]] = [i, j - 1];
}
if (j < this.gridSize - 1) {
this.grid[i][j + 1] += 1;
this.needsUpdate[[i, j + 1]] = [i, j + 1];
}
this.grid[i][j] -= 4;
this.needsUpdate[[i, j]] = [i, j];
}
}
this.feedMiddle = function() {
var midI = floor(this.gridSize / 2);
var midJ = floor(this.gridSize / 2);
this.grid[midI][midJ] += 1;
this.needsUpdate[[midI, midJ]] = [midI, midJ];
this.count += 1;
if (this.count > this.startAt) {
print("count" + this.count);
}
}
this.updateCycle = function() {
if (this.count >= this.startAt) {
this.redrawFromSet();
this.handleSpillOver();
}
if (this.count == this.startAt) {
this.drawOnce()
} else {
while (this.count <= this.startAt) {
this.redrawFromSet();
this.handleSpillOver();
}
}
}
}