Published
Edited
Nov 4, 2019
1 fork
5 stars
Insert cell
Insert cell
draw = {
let agents = initAgents();
let scores = initScores();
function update() {
scores = gBuildScores(agents, 2);
agents = gPickWinners2(agents, scores, neighborSlots);
gShow(agents);
return gShow.getCanvas();
}
while (true) {
yield Promises.delay(5, update());
}
}
Insert cell
initAgents = function(){
let agents = new Array(640).fill(0).map(() => new Array(640).fill(0));
for (let i = 0; i < 640; i++) {
for (let j = 0; j < 640; j++) {
agents[i][j] = Math.floor(Math.random() * 3);
}
}
return agents;
}
Insert cell
initScores = function(){
let scores = new Array(640).fill(0).map(() => new Array(640).fill(0));
return scores;
}
Insert cell
gBuildScores = gpu.createKernel(function(agents, r){
var currentState = agents[this.thread.y][this.thread.x];
var winState = (currentState + 1) % 3;
var nextState = currentState;
var score = 0;
var radius = Math.floor(r);
for (var i = -radius; i <= radius; i++) {
var p = (this.thread.y + i + 640) % 640;
for (var j = -radius; j <= radius; j++) {
var q = (this.thread.x + j + 640) % 640;

if (agents[p][q] == winState) {
score +=0;
} else if (agents[p][q] == currentState) {
score +=1;
} else {
score +=2;
}
}
}
return score;
}).setOutput([640, 640]);
Insert cell
neighborSlots = {
let slotList = [];
slotList.push([-1,-1]);
slotList.push([-1,0]);
slotList.push([-1,1]);
slotList.push([0,1]);
slotList.push([1,1]);
slotList.push([1,0]);
slotList.push([1,-1]);
slotList.push([0,-1]);
return slotList;
}
Insert cell
gPickWinners2 = gpu.createKernel(function(agents, scores, nSlots){
var maxScore = 0;
var topMove = agents[this.thread.y][this.thread.x];
let startIndex = Math.floor(Math.random() * 8);
for (let i = 0; i < 8; i++) {
let index = (startIndex + i) % 8;
var p = (this.thread.y + nSlots[index][1] + 640) % 640;
var q = (this.thread.x + nSlots[index][0] + 640) % 640;

if (scores[p][q] > maxScore) {
topMove = agents[p][q];
maxScore = scores[p][q];
}
}
return topMove;
}).setOutput([640, 640]);
Insert cell
gShow = gpu.createKernel(function(agents){
this.color(
agents[this.thread.y][agents.thread.x] == 0 ? 1 : 0,
agents[this.thread.y][agents.thread.x] == 1 ? 1 : 0,
agents[this.thread.y][agents.thread.x] == 2 ? 1 : 0,
1
);
}).setOutput([640, 640]).setGraphical(true);
Insert cell
Insert cell
gPickWinners1 = gpu.createKernel(function(agents, scores){
var maxScore = 0;
var topMove = agents[this.thread.y][this.thread.x];
for (var i = -1; i <= 1; i++) {
var p = (this.thread.y + i + 500) % 500;
for (var j = -1; j <= 1; j++) {
var q = (this.thread.x + j + 500) % 500;
if (scores[p][q] > maxScore) {
topMove = agents[p][q];
maxScore = scores[p][q];
}
}
}
return topMove;
}).setOutput([640, 640]);
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more