Skip to content

Instantly share code, notes, and snippets.

@nathan-sixnines
Created August 29, 2017 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathan-sixnines/cb260bc8252304153d561b0aa1c0ed9a to your computer and use it in GitHub Desktop.
Save nathan-sixnines/cb260bc8252304153d561b0aa1c0ed9a to your computer and use it in GitHub Desktop.
Germ (Go + Life)
<!DOCTYPE html>
<html>
<body>
<div id="container">
<canvas id="myCanvas" width= 950%; height= 950%; style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>
</div>
<script>
/*
Copyright 2017 Nathan Epstein
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var cellSize = 50;
var boardLen = 9;
var canvasLeft = canvas.offsetLeft;
var canvasTop = canvas.offsetTop;
var turn = 0;
var board1 = new Array(boardLen);
for (var i = 0; i < boardLen; i++) {
board1[i] = new Array(boardLen);
board1[i].fill(0);
}
var board2 = new Array(boardLen);
for (var i = 0; i < boardLen; i++) {
board2[i] = new Array(boardLen);
board2[i].fill(0);
}
var board1next = new Array(boardLen);
for (var i = 0; i < boardLen; i++) {
board1next[i] = new Array(boardLen);
board1next[i].fill(0);
}
var board2next = new Array(boardLen);
for (var i = 0; i < boardLen; i++) {
board2next[i] = new Array(boardLen);
board2next[i].fill(0);
}
var board1Spore = new Array(boardLen);
for (var i = 0; i < boardLen; i++) {
board1Spore[i] = new Array(boardLen);
board1Spore[i].fill(0);
}
var board2Spore = new Array(boardLen);
for (var i = 0; i < boardLen; i++) {
board2Spore[i] = new Array(boardLen);
board2Spore[i].fill(0);
}
var board1nextSpore = new Array(boardLen);
for (var i = 0; i < boardLen; i++) {
board1nextSpore[i] = new Array(boardLen);
board1nextSpore[i].fill(0);
}
var board2nextSpore = new Array(boardLen);
for (var i = 0; i < boardLen; i++) {
board2nextSpore[i] = new Array(boardLen);
board2nextSpore[i].fill(0);
}
function mod(n, m) {
return ((n % m) + m) % m;
}
function select(X, Y, turn){
console.log(X*10+Y);
console.log(X);
console.log(board1[X]);
if(turn == 0 && board1[X][Y] == 0 && board1[X][Y] == 0 && board1Spore[X][Y] == 0 && board2Spore[X][Y] == 0){
console.log("setting board1spore to 1");
board1Spore[X][Y] = 1;
return true;
}
else if(turn == 1 && board1[X][Y] == 0 && board1[X][Y] == 0 && board1Spore[X][Y] == 0 && board2Spore[X][Y] == 0){
console.log("setting board2spore to 1");
board2Spore[X][Y] = 1;
return true;
}
else if(turn == 0 && board1Spore[X][Y] == 1){
board1[X][Y] = 1;
board1Spore[X][Y] = 0;
console.log("setting board1 to 1");
return true;
}
else if(turn == 1 && board2Spore[X][Y] == 1){
console.log("setting board2 to 1");
board2[X][Y] = 1;
board2Spore[X][Y] = 0;
return true;
}
return false;
}
function setPix(x, y, r, g, b){
var xPix = x * cellSize;
var yPix = y * cellSize;
ctx.fillStyle = 'rgb(' + r + ',' + g + ',' + b + ')';
ctx.fillRect(xPix, yPix, cellSize, cellSize);
}
function drawBoard1(){
ctx.fillStyle="#FFFFFF";
ctx.fillRect(0,0,canvas.width,canvas.height);
var halfCell = cellSize/2
for(var i = 0; i < boardLen; i++){
ctx.beginPath()
ctx.moveTo(cellSize * i + halfCell, halfCell)
ctx.lineTo(cellSize * i + halfCell, cellSize*boardLen - halfCell)
ctx.stroke()
ctx.beginPath()
ctx.moveTo( halfCell, cellSize * i + halfCell,)
ctx.lineTo( cellSize*boardLen - halfCell, cellSize * i + halfCell,)
ctx.stroke()
}
//for(var i = 0; i < (boardLen-1) * (boardLen-1); i++){
/*
var x1 = cellSize * (i%(boardLen-1)) + halfCell + 5
var x2 = cellSize * (i%(boardLen-1)) + cellSize - 3
var y1 = cellSize * Math.floor(i/(boardLen-1)) + halfCell + 5
var y2 = cellSize * Math.floor(i/(boardLen-1)) + cellSize - 3
var x3 = cellSize * (i%(boardLen-1)) + cellSize + 3
var x4 = cellSize * (i%(boardLen-1)) + cellSize + halfCell - 5
var y3 = cellSize * Math.floor(i/(boardLen-1)) + cellSize + 3
var y4 = cellSize * Math.floor(i/(boardLen-1)) + cellSize + halfCell - 5
ctx.beginPath()
ctx.moveTo(x1,y1)
ctx.lineTo(x2,y2)
ctx.stroke()
ctx.beginPath()
ctx.moveTo(x3,y3)
ctx.lineTo(x4,y4)
ctx.stroke()
ctx.beginPath()
ctx.moveTo(x1,y4)
ctx.lineTo(x2,y3)
ctx.stroke()
ctx.beginPath()
ctx.moveTo(x3,y2)
ctx.lineTo(x4,y1)
ctx.stroke()*/
//var x1 = cellSize * (i%(boardLen-1)) + halfCell + 6
//var x2 = cellSize * (i%(boardLen-1)) + halfCell + cellSize - 6
//var y1 = cellSize * Math.floor(i/(boardLen-1)) + halfCell + 6
//var y2 = cellSize * Math.floor(i/(boardLen-1)) + halfCell + cellSize - 6
//ctx.beginPath()
//ctx.moveTo(x1,y2)
//ctx.lineTo(x2,y1)
//ctx.stroke()
//}
if(boardLen == 9){
//drawCircle(3, 3, 6)
drawCircle(5, 5, 6)
//drawCircle(3, 7, 6)
//drawCircle(7, 3, 6)
//drawCircle(7, 7, 6)
}
if(boardLen == 13){
drawCircle(4, 4, 6)
drawCircle(7, 7, 6)
drawCircle(4, 10, 6)
drawCircle(10, 4, 6)
drawCircle(10, 10, 6)
}
if(boardLen == 19){
drawCircle(4, 4, 6)
drawCircle(4, 10, 6)
drawCircle(4, 16, 6)
drawCircle(10, 4, 6)
drawCircle(10, 10, 6)
drawCircle(10, 16, 6)
drawCircle(16, 4, 6)
drawCircle(16, 10, 6)
drawCircle(16, 16, 6)
}
//ctx.beginPath();
//ctx.moveTo(0,0);
//ctx.lineTo(300,150);
//ctx.stroke();
}
function drawCircle(xCo,yCo, radius){
x = xCo * cellSize - cellSize/2
y = yCo * cellSize - cellSize/2
ctx.beginPath();
ctx.arc(x, y, radius, 0, 2 * Math.PI);
ctx.fillStyle = 'black';
ctx.fill();
ctx.stroke();
}
function drawSpore(xCo,yCo, color){
x = xCo * cellSize + cellSize/2
y = yCo * cellSize + cellSize/2
ctx.beginPath();
ctx.arc(x, y, 15, 0, 2 * Math.PI);
ctx.fillStyle = color;
ctx.lineWidth = 3;
ctx.fill();
ctx.stroke();
ctx.lineWidth = 2;
ctx.beginPath()
ctx.moveTo(x+10,y+10)
ctx.lineTo(x-10,y-10)
ctx.stroke()
ctx.beginPath()
ctx.moveTo(x+15,y)
ctx.lineTo(x,y-15)
ctx.stroke()
ctx.beginPath()
ctx.moveTo(x+14,y+6)
ctx.lineTo(x-6,y-14)
ctx.stroke()
ctx.beginPath()
ctx.moveTo(x,y+15)
ctx.lineTo(x-15,y)
ctx.stroke()
ctx.beginPath()
ctx.moveTo(x+6,y+14)
ctx.lineTo(x-14,y-6)
ctx.stroke()
ctx.lineWidth = 1;
}
function drawCell2(xCo,yCo, color){
x = xCo * cellSize + cellSize/2
y = yCo * cellSize + cellSize/2
ctx.beginPath();
ctx.arc(x, y, 20, 0, 2 * Math.PI);
ctx.fillStyle = color;
ctx.lineWidth = 3;
ctx.fill();
ctx.stroke();
ctx.strokeStyle = 'white';
ctx.lineWidth = 2;
ctx.beginPath()
ctx.moveTo(x+14,y+14)
ctx.lineTo(x-14,y-14)
ctx.stroke()
ctx.beginPath()
ctx.moveTo(x+20,y)
ctx.lineTo(x,y-20)
ctx.stroke()
ctx.beginPath()
ctx.moveTo(x+19,y+8)
ctx.lineTo(x-8,y-19)
ctx.stroke()
ctx.beginPath()
ctx.moveTo(x,y+20)
ctx.lineTo(x-20,y)
ctx.stroke()
ctx.beginPath()
ctx.moveTo(x+8,y+19)
ctx.lineTo(x-19,y-8)
ctx.stroke()
ctx.strokeStyle = 'black';
ctx.beginPath();
ctx.arc(x, y, 20, 0, 2 * Math.PI);
ctx.fillStyle = ctx.fillStyle = "rgba(0, 0, 200, 0)"
ctx.lineWidth = 3;
ctx.fill();
ctx.stroke();
ctx.lineWidth = 1;
}
function drawCell(xCo,yCo, color){
x = xCo * cellSize + cellSize/2
y = yCo * cellSize + cellSize/2
ctx.beginPath();
ctx.arc(x, y, 20, 0, 2 * Math.PI);
ctx.fillStyle = color;
ctx.lineWidth = .01;
ctx.fill();
ctx.stroke();
//x = xCo * cellSize + cellSize/2 - 7
//y = yCo * cellSize + cellSize/2 - 13
//ctx.lineWidth = 0.1;
//ctx.beginPath();
//ctx.arc(x, y, 4, 0, 2 * Math.PI);
//ctx.fillStyle = "white";
//ctx.fill();
//ctx.stroke();
ctx.lineWidth = 1;
}
function drawBoard2(){
for(var i = 0; i< boardLen; i++){
for(var j = 0; j < boardLen; j++){
if(board1[i][j] == 1){
drawCell(i,j,"#FF6000")
//setPix(i,j,255,100,0);
}
else if(board2[i][j] == 1){
drawCell(i,j,"#0060FF")
//setPix(i,j,0,100,255);
}
else if(board1Spore[i][j] == 1){
drawSpore(i,j,"#FF6000")
//setPix(i,j,200,60,0);
}
else if(board2Spore[i][j] == 1){
drawSpore(i,j,"#0060FF")
//setPix(i,j,0,60,200);
}
//else{
// setPix(i,j,255,255,255);
//}
}
}
}
function countNeibs(X, Y, board){
//console.log(X *boardLen + Y)
//console.log(Y)
/* torus neibs
return board[mod(X+1,boardLen)][ Y ] +
board[mod(X+1,boardLen)][mod(Y+1,boardLen)] +
board[ X ][mod(Y+1,boardLen)] +
board[mod(X-1,boardLen)][mod(Y+1,boardLen)] +
board[mod(X-1,boardLen)][ Y ] +
board[mod(X-1,boardLen)][mod(Y-1,boardLen)] +
board[ X ][mod(Y-1,boardLen)] +
board[mod(X+1,boardLen)][mod(Y-1,boardLen)]
*/
if(X > 0 && X < boardLen-1 && Y > 0 && Y < boardLen-1){
return board[X+1][ Y] +
board[X+1][Y+1] +
board[X ][Y+1] +
board[X-1][Y+1] +
board[X-1][Y ] +
board[X-1][Y-1] +
board[X ][Y-1] +
board[X+1][Y-1]
}
sum = 0
if(X == 0 && Y == 0){
return board[X+1][ Y] +
board[X+1][Y+1] +
board[X ][Y+1]
}
if(X == 0 && Y == boardLen-1){
return board[X+1][ Y] +
board[X+1][Y-1] +
board[X ][Y-1]
}
if(X == boardLen-1 && Y == 0){
return board[X-1][ Y] +
board[X-1][Y+1] +
board[X ][Y+1]
}
if(X == boardLen-1 && Y == boardLen-1){
return board[X-1][ Y] +
board[X-1][Y-1] +
board[X ][Y-1]
}
if(X == 0){
return board[X+1][ Y] +
board[X+1][Y+1] +
board[X ][Y+1] +
board[X ][Y-1] +
board[X+1][Y-1]
}
if(Y == 0){
return board[X+1][ Y] +
board[X+1][Y+1] +
board[X ][Y+1] +
board[X-1][Y+1] +
board[X-1][Y ]
}
if(X == boardLen-1){
return board[X ][Y+1] +
board[X-1][Y+1] +
board[X-1][Y ] +
board[X-1][Y-1] +
board[X ][Y-1]
}
if(Y == boardLen-1){
return board[X+1][ Y] +
board[X-1][Y ] +
board[X-1][Y-1] +
board[X ][Y-1] +
board[X+1][Y-1]
}
}
function deepCopy(array){
var copy = new Array(boardLen);
for (var i = 0; i < boardLen; i++) {
copy[i] = array[i].slice();
}
return copy;
}
function stepBoard(){
var sum1 = 0;
var sum2 = 0;
console.log("total neib:");
//for(var i2 = 0; i2 < boardLen; i2++){
//for(var j = 0; j < 10; j++){
// console.log(board1[i2])
//}
//}
for(var i = 0; i< boardLen; i++){
for(var j = 0; j < boardLen; j++){
var active = false;
var self1 = board1[i][j];
var self2 = board2[i][j];
var self1Spore = board1Spore[i][j];
var self2Spore = board2Spore[i][j];
var selfSpore = self1Spore + self2Spore;
var self = self1 + self2 + selfSpore;
var neib1 = countNeibs(i, j, board1) + countNeibs(i,j, board1Spore);
var neib2 = countNeibs(i, j, board2) + countNeibs(i,j, board2Spore);
var allActive = countNeibs(i,j,board1) + countNeibs(i,j,board2);
var total = neib1 + neib2;
//console.log(countNeibs(i,j,board1));
//console.log(countNeibs(i,j,board2));
//if(i == 0 && j == 3){
//}
// handle spores
if(selfSpore == 1){
console.log("SELFSPORE I: " + i.toString() + " j: " + j.toString());
if(allActive == 0){
board1nextSpore[i][j] = board1Spore[i][j];
board2nextSpore[i][j] = board2Spore[i][j];
}
else{
board1nextSpore[i][j] = 0;
board2nextSpore[i][j] = 0;
}
}
if(allActive > 0 && selfSpore == 1){
//console.log("how did this happen allactive:" + allActive.toString());
//console.log(countNeibs(i,j,board1));
//console.log(countNeibs(i,j,board2));
active = true;
}
else if( selfSpore == 0 ){
active = true;
}
//console.log(active);
//console.log(allActive);
//console.log("self: " + self.toString() + " selfSpore: " + selfSpore.toString() + " allactive: " + allActive.toString() + " active: " + active.toString());
if(total == 3 && allActive > 0 && active){
if(neib1 > neib2){
board1next[i][j] = 1;
board2next[i][j] = 0;
board1nextSpore[i][j] = 0;
board2nextSpore[i][j] = 0;
sum1 += 1;
}
else{
board2next[i][j] = 1;
board1next[i][j] = 0;
board1nextSpore[i][j] = 0;
board2nextSpore[i][j] = 0;
sum2 += 1;
}
//if(i == 1 && j == 0){
// console.log("306");
// for(var i2 = 0; i2 < boardLen; i2++){
// console.log(board1[i2]);
// }
//}
}
else if(total == 2 && (self == 1 || selfSpore == 1) && active){
if(neib1 == neib2){
if(self == 1 && selfSpore == 0){
board2next[i][j] = board2[i][j];
board1next[i][j] = board1[i][j];
board1nextSpore[i][j] = 0;
board2nextSpore[i][j] = 0;
}
else{
board2next[i][j] = board2Spore[i][j];
board1next[i][j] = board1Spore[i][j];
board1nextSpore[i][j] = 0;
board2nextSpore[i][j] = 0;
}
sum1 += self1;
sum2 += self2;
}
else if(neib1 == 2){
board1next[i][j] = 1;
board2next[i][j] = 0;
board1nextSpore[i][j] = 0;
board2nextSpore[i][j] = 0;
sum1 += 1;
}
else if(neib2 == 2){
board2next[i][j] = 1;
board1next[i][j] = 0;
board1nextSpore[i][j] = 0;
board2nextSpore[i][j] = 0;
sum2 += 1;
}
//if(i == 1 && j == 0){
// console.log("233");
// for(var i2 = 0; i2 < boardLen; i2++){
// console.log(board1[i2]);
// }
//}
}
else if (active){
board2next[i][j] = 0;
board1next[i][j] = 0;
board1nextSpore[i][j] = 0;
board2nextSpore[i][j] = 0;
//if(i == 1 && j == 0){
// console.log("233");
// for(var i2 = 0; i2 < boardLen; i2++){
// console.log(board1next[i2]);
// }
//}
}
}
}
board1 = deepCopy(board1next);
board2 = deepCopy(board2next);
board1Spore = deepCopy(board1nextSpore);
board2Spore = deepCopy(board2nextSpore);
console.log("~~~~");
return [sum1, sum2]
}
function runAuto() {
var time = 0;
var id = setInterval(step, 50);
function step() {
if (time == 100) {
clearInterval(id);
drawBoard();
}
else {
sums = stepBoard();
if(sums[0] == 0 || sums[1] == 0){
clearInterval(id);
}
drawBoard();
}
}
}
document.getElementById("container").addEventListener('click', function(event) {
var pixX = event.pageX - canvasLeft;
var pixY = event.pageY - canvasTop;
var X2 = Math.floor(pixX / cellSize);
var Y2 = Math.floor(pixY / cellSize);
if(X2 >= 0 && X2 < boardLen && Y2 >= 0 && Y2 < boardLen){
for(var i2 = 0; i2 < boardLen; i2++){
//for(var j = 0; j < 10; j++){
console.log(board1[i2])
//}
}
if(select(X2, Y2,(turn % 2))){
for(var i2 = 0; i2 < boardLen; i2++){
//for(var j = 0; j < 10; j++){
console.log(board1[i2])
//}
}
turn++;
stepBoard();
}
}
else{
stepBoard();
}
drawBoard1();
drawBoard2();
//drawBoard1();
//console.log(countNeibs(X2, Y2, board2));
});
drawBoard1();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment