Skip to content

Instantly share code, notes, and snippets.

@7c6f434c
Created November 12, 2014 06:38
Show Gist options
  • Save 7c6f434c/a27eba62e10b451dcd99 to your computer and use it in GitHub Desktop.
Save 7c6f434c/a27eba62e10b451dcd99 to your computer and use it in GitHub Desktop.
A convertor from a ternary «3 line of 8+1+8 characters» to Golly rule table
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> </title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<script>
function inc_arr(arr){
var i;
for(i=0;(i<8) && (arr[i]==1); i+=1){}
if(i==8) return 0;
arr[i]+=1;
for(i-=1;i>=0;i-=1){
arr[i]=-1;
}
return 1;
}
function convert_rules ()
{
var src,i,j,dst,name;
var src = document.getElementById("src").value;
src = src.split(/[\r\n]+/)
for (i in src) {
src[i] = src[i].split("");
}
name = document.getElementById("rule_name").value;
dst="";
dst+="@RULE "+name+"\n";
dst+="@COLORS\n0 0 0 0\n1 32 64 255\n2 255 64 32\n";
dst+="@TABLE\n";
dst+="#Golly rule-table format.\n";
dst+="#Each rule: C,N,NE,E,SE,S,SW,W,NW,C'\n";
dst+="#Default for transitions not listed: no change\n";
dst+="n_states:3\nneighborhood:Moore\nsymmetries:rotate8reflect\n";
for(j=0;j<3;j+=1){
var inputarr=[];
for(i=0;i<8;i+=1){
inputarr[i]=-1;
}
var flag=1;
while(flag){
var res,line;
var recode=[1,0,2];
flag=inc_arr(inputarr);
res = 8;
for (i in inputarr){
res+=inputarr[i];
}
res=src[j][res];
res=["-","0","+"].indexOf(res);
line="";
line+=recode[j];
for(i in inputarr){
line+=","+recode[inputarr[i]+1];
}
line+=","+recode[res];
dst+=line+"\n"
}
}
document.getElementById("dst").value = dst;
}
</script>
</head>
<body>
Name: <input id="rule_name" /><br/>
<textarea id="src" rows=10 cols=70></textarea>
<br/>
<a href="javascript:convert_rules();">Convert</a>
<br/>
<textarea id="dst" rows=10 cols=70></textarea>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment