# selfavoid.py

Below is the syntax highlighted version of selfavoid.py from §1.4 Arrays.

#-----------------------------------------------------------------------
# selfavoid.py
#-----------------------------------------------------------------------

import stdio
import stdarray
import sys
import random

# Accept integers n and trialCount as command-line arguments. Do
# trialCount random self-avoiding walks in an n-by-n lattice.
# Write to standard output the percentage of dead ends encountered.

n      = int(sys.argv[1])
trials = int(sys.argv[2])

for t in range(trials):

# Create an n-by-n array, with all elements set to False.
a = stdarray.create2D(n, n, False)

x = n//2
y = n//2
while (x > 0) and (x < n-1) and (y > 0) and (y < n-1):
# Check for dead end and make a random move.
a[x][y] = True
if a[x-1][y] and a[x+1][y] and a[x][y-1] and a[x][y+1]:
break
r = random.randrange(1, 5)
if   (r == 1) and (not a[x+1][y]):
x += 1
elif (r == 2) and (not a[x-1][y]):
x -= 1
elif (r == 3) and (not a[x][y+1]):
y += 1
elif (r == 4) and (not a[x][y-1]):
y -= 1

#-----------------------------------------------------------------------

# python selfavoid.py 5 100

# python selfavoid.py 20 100

# python selfavoid.py 40 100

# python selfavoid.py 80 100

# python selfavoid.py 5 1000