letrules=0;//thisisthecounterfordifferentsetsofrulesletcnt=0;//counterfordeadalivearrayinloopsbelowletn;letdArray=[];//arrayforeachcellfordead/alivestatusletr=20;//radiussize-changethisfordifferentlooks(thisdoesn't change based on canvas size)functionsetup(){createCanvas(windowWidth,windowHeight);//canvassizeseettowindowco=floor(width/(2*r));ro=corandomDead();//thissetsarandomdead/aliveforeachcellbackground(20);//almostblacknoStroke();//nolinesframeRate(5)//slowframeratetoseeprogressionofautomata}functiondraw(){background(20);translate(width/2,height/2);s=0.28;if(width>1000){s=0.2;//scalefactortoadjustforlargerscreens}scale(s,s)//setscalev=12for(letm=0;m<v;m++){//looptocreateseparatepointsonthestarpush();//translationsandrotationsbetweenpushandpoponlyapplybetweenpush/poprotate(m*TWO_PI/v)//rotatethecanvastocreateeachpointcnt=0;//thisisthecounterforthealive/deadarraytoloopthroughfor(letj=0;j<=co;j++){for(leti=ro;i>=0;i--){x=r*j*1.5;y=-r*12+r*sqrt(3)*i+r*j/1.15;if(dArray[cnt]===0){//changefillfordeadnoFill();noStroke();}if(dArray[cnt]===1){//changefill/strokeforalivewithagradientbasedonrow/columnfill(j*4+i*5,200+i*5,255-j*2,200-m*7);stroke(j*4+i*5,200+i*5,255-j*2,200-m*7);}circle(x,y,r*1.4);//placecircleinthehexgridfill(20);//setfillbacktobackgroundcnt=cnt+1;}}pop();//endpush/poptransformations}switchDead()//nextroundofalive/deadcaculatiions}functionrandomDead(){//initializesrandomdead/alivestatusofcellsn=(co+1)*(ro+1);//nisthesizeofthegriddArray=[]//dead-alivearrayisclearedfor(leti=0;i<n;i++){//loopthroughthearrayandfillwithrandomdead/alivetmp=random(-1,1);//basedead/aliveonpositive/negativenumbersif(tmp<=0){dArray[i]=1;}if(tmp>0){dArray[i]=0;}}}functionswitchDead(){//loopsthroughandchangesdead/alivestatusbasesonneighborscnt1=0for(letj=0;j<=co;j++){for(leti=ro;i>=0;i--){if(j!=0&&i!=0&&i!=ro&&j!=co){//ifacellisnotinthecornerofthegridsumd=dArray[cnt1+co+1]+dArray[cnt1+co+2]+dArray[cnt1-co-1]+dArray[cnt1-co-2]+dArray[cnt1+1]+dArray[cnt1-1];}if(i===0&&j!=0&&j!=co){//forleftsideofarray(butnotacorner)sumd=dArray[cnt1+co+1]+dArray[cnt1-co-1]+dArray[cnt1-co-2]+dArray[cnt1-1];//findthesumofneighborsthatarealive}if(i===ro&&j!=0&&j!=co){//forrightsideofarray(butnotacorner)sumd=dArray[cnt1+co+1]+dArray[cnt1-co-1]+dArray[cnt1+co+2]+dArray[cnt1+1];//findthesumofneighborsthatarealive}if(j===co&&i!=0&&i!=ro){//fortopsideofarray(butnotacorner)sumd=dArray[cnt1-co-1]+dArray[cnt1-co-2]+dArray[cnt1+1]+dArray[cnt1-1];//findthesumofneighborsthatarealive}if(j===0&&i!=0&&i!=co){//forbottomsideofarray(butnotacorner)sumd=dArray[cnt1+co+1]+dArray[cnt1+co+2]+dArray[cnt1+1]+dArray[cnt1-1];//findthesumofneighborsthatarealive}if(i===0&&j===0){//forcornerlowerleftsumd=dArray[cnt1+co+1]+dArray[cnt1+1];//findthesumofneighborsthatarealive}if(i===0&&j===co){//forcornerupperleftsumd=dArray[cnt1-1]+dArray[cnt1-co-1]+dArray[cnt1-co-2];//findthesumofneighborsthatarealive}if(i===ro&&j===0){//forcornerlowerrightsumd=dArray[cnt1+1]+dArray[cnt1+co+1]+dArray[cnt1+co+2];}if(i===ro&&j===co){//forcornerupperrightsumd=dArray[cnt1+1]+dArray[cnt1-co-1];}if(rules===0){if(dArray[cnt1]===1&&(sumd===6||sumd===5||sumd===1||sumd===0)){dArray[cnt1]=0;}elseif(dArray[cnt1]===0&&(sumd===4)){dArray[cnt1]=1;}}if(rules===1){if(dArray[cnt1]===1&&(sumd===6||sumd===5||sumd===2||sumd===1||sumd===0)){dArray[cnt1]=0;}elseif(dArray[cnt1]===0&&(sumd===3||sumd===4||sumd===5)){dArray[cnt1]=1;}}if(rules===2){if(dArray[cnt1]===1&&(sumd===6||sumd===5)){dArray[cnt1]=0;}elseif(dArray[cnt1]===0&&(sumd===3)){dArray[cnt1]=1;}}if(rules===3){if(dArray[cnt1]===1&&(sumd===1||sumd===2||sumd===3)){dArray[cnt1]=0;}elseif(dArray[cnt1]===0&&(sumd===3||sumd===4)){dArray[cnt1]=1;}}if(rules===4){if(dArray[cnt1]===1&&(sumd===3||sumd===4||sumd===5||sumd===6)){dArray[cnt1]=0;}elseif(dArray[cnt1]===0&&(sumd===0||sumd===1||sumd===2)){dArray[cnt1]=1;}}if(rules===5){if(dArray[cnt1]===1&&(sumd===2||sumd===3||sumd===4||sumd===5||sumd===6)){dArray[cnt1]=0;}elseif(dArray[cnt1]===0&&(sumd===0||sumd===1||sumd===2)){dArray[cnt1]=1;}}if(rules===6){if(dArray[cnt1]===1&&(sumd===4||sumd===5||sumd===6)){dArray[cnt1]=0;}elseif(dArray[cnt1]===0&&(sumd===3||sumd===4)){dArray[cnt1]=1;}}cnt1=cnt1+1}}stroke(255);noFill()push()//changescaleforrulecounteronlybetweenpush/popscale(3,3)//scalechangefor(leti=0;i<=6;i++){circle(-width/2+20,-height/2+30*i+5,20)//rulecircles}fill(100)circle(-width/2+20,-height/2+30*rules+5,20)//currentrulepop()}functionmousePressed(){//randomizedeadaliveagainwhenthemouseisclickedrandomDead();}functionkeyPressed(){//usearrowkeystochangetherulesif(keyCode===32||keyCode===40){rules=rules+1;if(rules>6){rules=0;}randomDead();}if(keyCode===38){//moveupwithuparrowrules=rules-1;if(rules<0){rules=6;}randomDead();}}