[Tutor] ascii art

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Sat, 14 Sep 2002 00:20:32 -0700 (PDT)


On Sun, 8 Sep 2002, Gil Tucker [ateliermobile] wrote:

>                      I am trying to make a program that can generate
> random ascii art forms or designs. I have tried a few but they dont
> work.Any tip or tips. Or warnings?

Hi Gil,

Can you show us some of the programs you've tried writing?  By not
working, do you mean that you're just getting uninteresting pictures, or
are there syntax errors?


Steven Wolfram's book, "A new Kind of Science", has many examples of
simple programs that produce random-but-pretty pictures.  For example:

    http://www.wolframscience.com/preview/nks_pages/?NKS0032.gif

shows a neat triangle picture sort of thing.  *grin*

Here's a Python program that generates this image:


######
"""A small ascii-art program in Python.

Danny Yoo (dyoo@hkn.eecs.berkeley.edu)

See:

    http://www.wolframscience.com/preview/nks_pages/?NKS0032.gif

and Wolfram's "A New Kind of Science" for more information about this
fascinating topic.

"""

import sys


"""Wolfram's automaton uses eight rules for generating the next row of
output.  Let's take a look at them."""
RULE_100 = { (1, 1, 1) : 0,
             (1, 1, 0) : 1,
             (1, 0, 1) : 1,
             (1, 0, 0) : 0,
             (0, 1, 1) : 1,
             (0, 1, 0) : 1,
             (0, 0, 1) : 1,
             (0, 0, 0) : 0 }


def drawRow(row):
    for character in row:
        if character: sys.stdout.write('O')
        else: sys.stdout.write('.')
    sys.stdout.write('\n')


STARTING_ROW = [0]*2 + [0]*70 + [1] + [0]*2

def applyRuleOnRow(rule, row):
    new_row = [0]
    for i in range(1, len(row)-1):
        triple = (row[i-1], row[i], row[i+1])
        new_row.append(rule[triple])
    new_row.append(0)
    return new_row


if __name__ == '__main__':
    row = STARTING_ROW
    for i in range(70):
        drawRow(row)
        row = applyRuleOnRow(RULE_100, row)
######



I hope this sparks some interest!  Good luck to you!