` ````
<body style=background-color:#333;overflow:hidden;margin:0>
<canvas id=a style=display:block;width:100vw;height:100vh>
<script>d=document;b=d.body;c=a.getContext('2d')</script>
```

` ````
/*
Game of Wolves, Sheeps and Grass
================================
1) a blank cell which is adjacent to grass will be replaced by grass
2) a grass cell which is adjacent to a sheep will be replaced by a sheep
3) a sheep will be replaced by a wolf if a wolf is adjacent to it.
4) if a sheep has no grass adjacent to it, it will be replaced by a blank cell
5) if a wolf has no sheep adjacent to it, it also will be replaced by a blank cell
6) if a wolf has a shepherd adjacent to it, it will be replaced by a blank cell
7) In else cases, no replacement takes place.
*) Chances are, that the wolf/sheep isn't hungry, or the grass doesn't grow
*/
```

` ````
// Configuration: pick values between 0 and 1
grassGrowthRate=0.2
sheepHungriness=0.5
wolfHungriness =0.5
//------
R=Math.random
w=a.width=2*1920
h=a.height=2*1024
W=w/64|0
H=h/64|0
for(M=[],m=W*H;m--;)M[m]=R()*4|0
M[(R()*H|0)*W+R()*W|0]=4
m=function(x,y){return y<0||x<0||x>=W||y>=H?0:M[y*W+x]}
N=function(x,y,z){
return (m((x-1),(y-1))===z?1:0)+(m(x,(y-1))===z?1:0)
+(m((x+1),(y-1))===z?1:0)+(m((x-1),y)===z?1:0)
+(m((x+1),y)===z?1:0)+(m((x-1),(y+1))===z?1:0)
+(m(x,(y+1))===z?1:0)+(m((x+1),(y+1))===z?1:0)
}
P=function(x,y){
if(m(x,y)==0&&N(x,y,1)&&R()<grassGrowthRate)return 1 // (1)
if(m(x,y)==1&&N(x,y,2)>0&&R()<sheepHungriness)return 2 // (2)
if(m(x,y)==2){ // (3) && (4)
if(N(x,y,1)==0)return 0
if(N(x,y,3)>0&&R()<wolfHungriness)return 3
}
if(m(x,y)==3)if(N(x,y,2)==0||N(x,y,4)>0)return 0 // (5) && (6)
return m(x,y) // (7)
}
E=function(u,f,i,j,k){
for(k=j=u.length;j--;)
i=new Image(),
i.src='https://assets-cdn.github.com/images/icons/emoji/unicode/1f4'+u[j]+'.png',
i.onload=function(){if(!--k)f(u)},
u[j]=i
}
E([11,'3a',68],function(I,i,k,x,y){
G=document.createElement('canvas')
G.width=G.height=64
g=G.getContext('2d')
g.fillStyle='rgba(0,255,0,0.02)'
I.splice(0,0,new Image(64,64))
I[0].src=G.toDataURL()
for(i=5e3;i--;)g.fillRect(R()*64,R()*64,4,4)
I.splice(1,0,new Image(64,64))
I[1].src=G.toDataURL()
function L(){
c.fillStyle='rgba(51,51,51,0.9)'
c.fillRect(0,0,w,h)
for(y=H;y--;)
for(x=W;x--;)
c.drawImage(I[m(x,y)],x*64,y*64)
window.setTimeout(function(){
O=[]
for(y=H;y--;)
for(x=W;x--;)
O[y*W+x]=P(x,y)
M=O
requestAnimationFrame(L)
},100)
}
k=2,I[0].onload=I[1].onload=function(){if(!--k)L()}
})
```

999px

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Loading
..................

Alt
Opt
Find & Replace

Also see: Tab Triggers