Implementation notes Eye-changing quine. ---------------------------------------------------------------------- 0. Concept I was so impressed by the turn of events in Durarara that I decided to make an ASCII art for Sonohara Anri, the other main character. I thought the most fitting function for this situation would be a program that reproduces itself, similar to how Saika is able to replicate itself in the minds of hundreds of people. But it wouldn't be any fun if I just made a straightforward quine, since I made quite a few of those already. So this time around, the output programs will be slightly different from the original in that their eyes are different. ---------------------------------------------------------------------- 1. Layout data The first step to writing a quine is to layout the different pieces of data. For this program, the different pieces are: - A "decoder" for outputting the source code. - The decoder encoded as a string. - Formatting data. The decoder outputs the following: 1. Some fixed header string. 2. Encoded decoder. 3. Formatting data. 4. Decoded decoder data. The formatting is stored using some run-length encoding scheme, similar to quite a few other programs I have written in the past. Basically: - ! = newline - * = glasses frames - # .. ) = eyes - , .. [ = space - ] .. } = non-space Encoding of the decoder is a straightforward XOR 24. To avoid conflicts with whitespace and escape characters, this means characters like "3", "8" and "g" can't be used in the decoder code, which is easily worked around with a bit of arithmetic. ---------------------------------------------------------------------- 2. Fill in code Once the data layout has been decided, the final step is to write the code. Yes there are only two steps in writing programs like this, it's trivial once you wrapped your minds the self-referential and overlapping nature of the code. Having said that, some techniques will make the two steps slightly easier. - Write some proof-of-concept code first to verify that the data layout and the decoder works. This can be found under v1 and v2 subdirectories. - Use scripts to automate generation of the encoded and decoded portions of the source. test.sh and the Makefile takes care of this bit. - Write the program backwards: format the last line first, and gradually work toward the middle where the encoded and decoded sources will meet. This can be seen in image.txt: I wrote the second half of the program by hand, and the first half is generated automatically. After the generated output has been verified to work, some manual tweaking with the eyes are done to make sure that the "original" anri.c will be different from all her children. This program did not take that long to write relative to some other C programs I wrote in the past, probably because I can now reduce the while process to just 2 steps :) ---------------------------------------------------------------------- 3. Finally... The author wrote in the afterwords for volume 1: "I wonder how things would go when the main character has nothing above her neck". I can tell you how headless main characters are -- they make terrible ASCII arts. Fortunately, the other characters are also very cool in different ways. Among the female characters, I liked Anri the best. It's either the sword or the glasses.