Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --One Dimensional Cellular Automaton
- --Adapted from "dominic"'s Lua CA written to use LOVE.
- --Original version can be found here: http://blog.signalsondisplay.com/?p=60
- --The only thing I borrowed from his version is the algorithm computer.
- -----------------------------------------------------------------------------
- --Tangent Automaton V. 1.0
- width = 128 --Set your desired width here!
- height = width / 2
- t=true
- f=false
- debug=false --Do you want debugging messages?
- states = {}
- rulegiven={}
- mt={}
- for i = 1, width do states[i] = 0 end
- states[width / 2] = 1
- output={}
- ruletable= --The rule lookup table.
- {
- {t,t,t},
- {t,t,f},
- {t,f,t},
- {t,f,f},
- {f,t,t},
- {f,t,f},
- {f,f,t},
- {f,f,f}
- }
- function dectobin(input)
- conversion={1,2,4,8,16,32,64,128} --I'll make this into 2^n eventually.
- output={0,0,0,0,0,0,0,0} --The output table, keeping it like this for now.
- i=8 --Starting number to decrement from
- for h=1,8 do --It's 8 bit!
- input=input-conversion[i] --Grabs the number to convert, and subtracts it from the lookup table.
- if debug then print(input) end
- if input <0 then --If it's less than 0 (negative)...
- input=input+conversion[i] --...undo that subtraction.
- else --If it isn't less than 0 (greater than or equal to)...
- output[h]=1 --...set the output bit high (change "h" to "i" to swap endianess).
- end
- i=i-1 --The simple decrementer
- end
- end
- ruletable= --My second rule lookup table. I have no idea why I have two.
- {
- {true,true,true},
- {true,true,false},
- {true,false,true},
- {true,false,false},
- {false,true,true},
- {false,true,false},
- {false,false,true},
- {false,false,false}
- }
- rule={} --Sets up the actual table the algorithm looks at.
- function grabrule() --This function decodes the given rule.
- userinput=io.read() --Gets the user's input.
- dectobin(userinput) --Sends the input to get decoded into binary.
- rulegiven=output --Takes that binary number and sticks it into "rulegiven".
- if debug then print(#rulegiven) end
- --print(string.len(userinput)) --I might re-implement this later. For now, You'll have to use the decimal system.
- --for i=1,string.len(userinput) do
- -- rulegiven[i]=string.sub(userinput,i,i)
- --end
- if #rulegiven~=8 then --Rather pointless warning since an 8-bit number is ALWAYS created...
- print("You must enter an 8-bit number!")
- end
- if debug then --more debugging
- for j=1,#rulegiven do
- io.write(rulegiven[j].." ")
- end
- io.write("\n")
- end
- for k=1,#rulegiven do --This will turn the binary into bollean logic.
- if rulegiven[k]==1 then --If the bit is 1...
- rulegiven[k]=true --...set it to true (rather pointless, since I could combine this with the next part of the program).
- else
- rulegiven[k]=false --This is rather pointless, since it's set to "false" by default.
- end
- end
- rulecounter=1 --A temporary number that helps with keeping the rule table from skipping ahead.
- for i=1,#rulegiven do --For as many rules as there are given...
- if rulegiven[i]then --...if the state of the rule is true...
- rule[rulecounter]=ruletable[i] --...look at the lookup table, and stick that value into "rule".
- rulecounter=rulecounter+1 --Decrements the counter for the "rule" table.
- end
- end
- end
- function compute_next_gen(index) --Here's where all the computation happens.
- current_gen = {} --This table stores the current information to get computed.
- for i = 1, #states do --This just stores the data from the current bit into the p, c, and n registers.
- p = states[i - 1] == 1
- c = states[i] == 1
- n = states[i + 1] == 1
- current_gen[i] = 0 --Once that's done, it sets the gen back to 0 to do the actual computation.
- for j = 1, #rule do --It computes for as many rules it is given.
- if (p == rule[j][1] and c == rule[j][2] and n == rule[j][3]) then --If it complies with the rules...
- current_gen[i] = 1 --...set the current cell to an "on" state.
- mt[index][i]="#" --Stores it in my matrix.
- end
- end
- end
- states = current_gen
- end
- grabrule()
- for i=1,height do --This loop sets up my matrix with a spacing character.
- mt[i] = {}
- for j=1,width do
- mt[i][j] = "_"
- end
- end
- if debug then print("This automaton uses "..#rule.." rules.") end --Disregard this, it's for debugging.
- if debug then
- for a=1,#rule do
- for b=1,3 do
- if rule[a][b] then
- io.write("true")
- else
- io.write("false")
- end
- end
- io.write("\n")
- end
- end
- for i = 1, height do --This runs the computation function for the desired height.
- compute_next_gen(i)
- end
- for a=1,height do --This loop actually prints out my matrix, displaying the pretty pattern!
- for b=1,width do
- io.write(mt[a][b])
- end
- io.write("\n")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement