Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --TangentAutomaton V. 1.4
- --Notes:
- --Code is now 100% all mine
- --Removed my custom matrix, reducing memory usage by a factor of math.huge()
- --Shortened a ton of long functions
- --Removed all un-needed variables and functions
- --Added some sexy line numbering
- --Revised a few comments
- --Re-arranged the order that data is asked for at the beginning
- --Fixed the fickle way that some rules got cut off
- --Tweaked the seed providing functions just a bit
- --The rule table is now either "1" or "0" instead of "true" or "false"
- cellon="#"
- celloff="_"
- seeddata={} --Put your seed information here!
- states = {}
- rulegiven={}
- output={}
- 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 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= --The rule lookup table, the heart of this script.
- {
- {1,1,1},
- {1,1,0},
- {1,0,1},
- {1,0,0},
- {0,1,1},
- {0,1,0},
- {0,0,1},
- {0,0,0}
- }
- 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".
- rulecounter=1 --Starts a sync counter.
- for i=1,#rulegiven do --This will turn the binary into bollean logic.
- if rulegiven[i]==1 then --If the bit is 1...
- rule[rulecounter]=ruletable[i] --...set it to true (rather pointless, since I could combine this with the next part of the program).
- rulecounter=rulecounter+1
- end
- end
- end
- function grabseed() --This function asks for an input, and stores it to "state"
- seedgiven={} --I always store "binary" data in a table.
- print("Please enter a starting seed in binary!")
- print("Note: This seed will be placed in the center of the algorithm.")
- inputseed=io.read() --Asks for the seed.
- for i=1,string.len(inputseed) do --This loop chops the user input up, and stuffs it into my table.
- seedgiven[i]=string.sub(inputseed,i,i)
- end
- for i=1,#seedgiven do --Appearently, it's a string, and I need numbers.
- if seedgiven[i]=="1" then --If the string is a "1"...
- seedgiven[i]=1 --...make it into a number 1...
- else
- seedgiven[i]=0 --...else it's a 0.
- end
- end
- bitshift=((width-#seedgiven)/2) --To find how much the data should be shifted, the lenght of the given seed is subtracted from the width, then divided by 2.
- bitshift=math.floor(bitshift) --If it's an odd number, it gets rounded down (an even number is always created).
- j=bitshift --To keep the two numbers synced.
- for i=1,#seedgiven do --This stores the given string into the "states" table.
- states[j]=seedgiven[i] --Mirrors the value in "seedgiven" to "states".
- j=j+1 --To keep j and i in sync.
- end
- end
- function compute(row) --Here's where all the computation happens.
- io.write(row..":".."\t") --This adds line numbering.
- for i=1,#states do --For as many cells as there are in "states"...
- if states[i]==1 then io.write(cellon) else io.write(celloff) end --...if it's "1" then write an "on" state, else write an "off" state.
- end
- io.write("\n") --Writes a new line.
- current_gen = {} --This table stores the current information to get computed.
- for i = 1, #states do --This goes along the rows for as many starting states there are.
- current_gen[i] = 0 --Sets up every cell in this table as "0".
- for j = 1, #rule do --It computes for as many rules it is given.
- if (states[i - 1] == rule[j][1] and states[i] == rule[j][2] and states[i + 1] == rule[j][3]) then --If it complies with the rules...
- current_gen[i] = 1 --...set the current cell to an "on" state.
- end
- end
- end
- states = current_gen --changes "states" to reflect the changes made.
- end
- print("Please take a look at the top of this script to configure it!")
- function interface() --The user interface function.
- print("Please enter the width to run for.")
- width=io.read() --Gets the width from the user.
- height = width / 2 --In an obtuse isosceles triangle, the height is usually half of the width (The trangles generated are usually 45 degrees).
- for i = 1, width+1 do states[i] = 0 end --Sets up the "states" table, which is where the computations begin.
- states[(width / 2)+1] = 1 --The default seed is a "1" in the center of the table.
- print("Please enter a rule in decimal.")
- grabrule() --Asks for the rule from the user.
- print("Would you like to input a seed?(y/n)")
- input=io.read() --Gets the user's choice.
- if input=="y" then --If the choice is "y", then...
- grabseed() --...go to the custom seed input function.
- else
- print("Would you like to use a randomly generated seed?(y/n)")
- input=io.read() --Gets the user's choice.
- if input=="y" then --If the choice is "y", then...
- for i=1,#states do --...for as many cells as there are in "states"...
- states[i]=math.random(0,1) --...set the cell to a random number of either "1" or "0".
- end
- else --If the user's decision is not one of the above options, "states" is not changed at all...
- print("Going with standard seed...") --...and goes with the default configuration set at the beginning.
- end
- end
- end
- interface() --Runs the above function.
- for i = 1, height do --This runs the computation function for the desired height.
- compute(i)
- end
Add Comment
Please, Sign In to add comment