Slashdot Mirror


How Does a Single Line of BASIC Make an Intricate Maze?

JameskPratt writes "This Slate article talks about a single line of code — 10 PRINT CHR$ (205.5 + RND (1)); : GOTO 10 — and how it manages to create a complicated maze without the use of a loop, variables and without very complicated syntax." Now that amazing snippet of code is the basis of a book, and the book is freely downloadable.

37 of 438 comments (clear)

  1. Without the use of a loop!? by Anonymous Coward · · Score: 5, Insightful

    What is
    10 something: GOTO 10
    if not an (endless) loop?

    1. Re:Without the use of a loop!? by Nutria · · Score: 4, Informative

      That's exactly what I thought... Maybe JameskPratt isn't a very good programmer.

      --
      "I don't know, therefore Aliens" Wafflebox1
    2. Re:Without the use of a loop!? by Anonymous Coward · · Score: 5, Informative

      No shit, and it is not a labyrinth either. It is just randomly printing forward slashes and backlashes.

    3. Re:Without the use of a loop!? by gl4ss · · Score: 4, Insightful

      No shit, and it is not a labyrinth either. It is just randomly printing forward slashes and backlashes.

      "an intricate combination of paths or passages in which it is difficult to find one's way or to reach the exit."

      it is that. it's just not very amazing at all if you describe it as printing \ and / randomly.

      --
      world was created 5 seconds before this post as it is.
    4. Re:Without the use of a loop!? by shitzu · · Score: 4, Insightful

      And most importantly - its not a particularly amazing piece of code. I am not a programmer, but know how to write basic stuff in a few languages - and i do not find a TWO LINE LOOP that fills the screen with a choice between two characters that make up something that LOOKS LIKE A MAZE, but is not necessarily navigable not a least bit amazing.

    5. Re: Without the use of a loop!? by Anonymous Coward · · Score: 4, Funny

      Pratt by name . . .

    6. Re:Without the use of a loop!? by erroneus · · Score: 4, Interesting

      I think there is a bit of a story in the fact that while in function, it is extremely simple though in result/appearance it creates what most perceive to be a complex maze of passages. The code puts out random positive space objects while the mind sees a single, complex negative space.

      It sort of reminds me of similar little tricks used to generate landscapes and other such things... mandelbrot comes to mind.

    7. Re:Without the use of a loop!? by stokessd · · Score: 5, Funny

      The code puts out random positive space objects while the mind sees a single, complex negative space.

      Sadly, that's the way I'm seeing slashdot these days...

    8. Re:Without the use of a loop!? by Black+Parrot · · Score: 5, Funny

      Maybe he's trolling to see who will admit that they know BASIC.

      --
      Sheesh, evil *and* a jerk. -- Jade
    9. Re:Without the use of a loop!? by swillden · · Score: 5, Insightful

      It sort of reminds me of similar little tricks used to generate landscapes and other such things... mandelbrot comes to mind.

      Except that the Mandelbrot set, for example, really is much, much more interesting. It actually has a great deal of sophisticated structure, that's highly chaotic (in both typical and mathematical senses of the word) but not random at all. Not at all comparable to this example, whose output has no real structure at all, but just exploits the tendency of the human brain to find patterns whether or not they exist.

      --
      Note to ACs: I usually delete AC replies without reading them. If you want to talk to me, log in.
    10. Re:Without the use of a loop!? by MightyMartian · · Score: 4, Funny

      Much like a televangelist or politician then.

      --
      The world's burning. Moped Jesus spotted on I50. Details at 11.
    11. Re:Without the use of a loop!? by Samantha+Wright · · Score: 5, Insightful

      It's called a display hack. They're at least as old as the oscilloscope, and have always been a mainstay of the demoscene. The book provides little actual relevant history about the context of this snippet in that regard, only noting that the snippet itself derives from the C64 User's Manual.

      Somewhat dismayingly, the bulk of the text ponders on criticism that presumes an intentional, carefully-planned artist:

      10 PRINT picks up on aspects of "Mouse in the Maze." Its output is a regular arrangement of "walls" in a grid—akin to the display of that earlier program and similar to the arrangement of the stereotypical laboratory maze. "Mouse in the Maze" does not present the compelling creation of an inspired Daedalus, but a behaviorist experiment. This maze is a challenge to intelligence—not, however, a romantic, riddling intelligence, but a classically conditioned, animal kind. It also brings in the idea of the scientist, who may be indifferent to the struggles of the creatures lost in the maze.

      This manner of thinking, now put on display nakedly in the context of something completely mathematical and involving no relevant human imagination, can plainly be seen to be philosophically inconsistent. The author has said that a very simple natural phenomenon is influenced by a complex work of art (specifically a TX-0 game from twenty or so years earlier), which indicates a profound metaphysical error.

      Certainly it is worthwhile to talk about chaotic functions (like the R pentamino in Conway's Game of Life, in addition to the display hacks already mentioned) but attempting to critique them as if they were part of the artistic canon is intellectually dishonest.

      --
      Bio questions? Ask me to start a Q&A journal. Computer analogies available for most topics!
    12. Re:Without the use of a loop!? by The+Moof · · Score: 4, Insightful

      If you want to go that route, every program written in C can be condensed down to 1 line. Or any language where whitespace isn't significant.

    13. Re:Without the use of a loop!? by egcagrac0 · · Score: 4, Informative
      #include
      main() { ten: printf("%c", (rand()%2)?47:92); goto ten; }

      The preprocessor include directive is on a separate line, but that's really not part of the program.

    14. Re:Without the use of a loop!? by narcc · · Score: 5, Interesting

      Indeed, it isn't exactly rocket science -- zillions of kids under 10 picked up the basics of BASIC from type-in programs in kids books and magazines back in the 80's.

      What bugs me most is that instead of doing the obvious (making a binary tree maze) it's some weird artifact of how the / and \ combine on-screen that makes something that vaguely resembles a maze -- full of loops (no big deal) large winding sections without any junctions (bad), and isolations (terrible!).

      Just for fun:

      IBM PC users! You can modify the C64 program in the summary to both run on your micro and produce a binary tree maze with this simple change: PRINT CHR$(220 + INT(RND(1)*2) );

      You won't be able to get the same effect with alternating forward- and back-slashes with something like PRINT CHR$(47 + INT(RND(1)*2)*45); as they don't connect at all -- neither on the same line nor between lines.

    15. Re:Without the use of a loop!? by Anonymous Coward · · Score: 5, Funny

      The code puts out random positive space objects while the mind sees a single, complex negative space.

      I don't even see the random positive space objects anymore. I just see blonde, brunette, redhead...

    16. Re:Without the use of a loop!? by Dan+East · · Score: 5, Interesting

      A simple addition makes the TI-99/4A version look visually just like the C64's. That is to simply define the forward and backward slash characters to look more the C64's and span the whole area of the character's bitmap.


      10 CALL CHAR(47, "C0E070381C0E0703")
      20 CALL CHAR(92, "03070E1C3870E0C0")
      30 PRINT CHR$(INT(RND+.5)*45+47);
      40 GOTO 30

      Finally, if we're going to go to the trouble of defining character images, then we might as well use contiguous character codes so we don't need the extra math. We could use the C64's exact values, however the TI's character set only has 128 characters. So we'll use values 100 less than the C64 version. Also, the TI rounds floating values to integers, whereas the C64 simply truncates them. So we don't need to add .5 to the random value.


      10 CALL CHAR(105, "C0E070381C0E0703")
      20 CALL CHAR(106, "03070E1C3870E0C0")
      30 PRINT CHR$(105+RND);
      40 GOTO 30

      --
      Better known as 318230.
    17. Re:Without the use of a loop!? by Dan+East · · Score: 5, Informative

      And finally, here's a screenshot in case anyone actually cares what the TI version looks like.

      http://dexsoft.com/slashdot/ti_screenshot.png

      --
      Better known as 318230.
    18. Re:Without the use of a loop!? by narcc · · Score: 5, Interesting

      A binary tree maze algorithm will generate a "perfect maze" (no loops, isolations, and only a single path between any two cells), though you need a long hallway along two sides (depending on which edges you use for walls).

      Mazes with square cells share edges, so by assuming some rules about the outside walls of a maze, you can represent each cell in a maze with only two bits. You need only store, for example, just the south and west walls of each cell.

      To make a binary tree maze, first assume an outer wall and a long hall (no south walls) in the first column, and along the bottom row (no west walls). For all remaining cells, randomly add a west or south wall. You don't need any information about adjacent cells -- just flip a coin and draw a wall for each remaining cell. This will produce a "perfect maze". It's pretty cool.

      The code I posed will generate a binary tree maze, though it won't show the two long halls. (In this case, the program uses west and south walls, so the halls will be the in the first column and the along the bottom row.)

      The lameness filter doesn't want me to show you an example. Still, it's pretty easy to make a nice binary tree maze generator yourself. Give it a try and you'll see how it works.

      For fun, you can make four binary tree mazes and arrange them so that you have two long halls (one East to West, the other North to South) intersecting at the center of the maze, just by choosing which pair of walls to randomly generate in each quadrant. It makes a much more interesting looking maze without adding much complexity (just figure out which quadrant the current cell is in) and retaining all of the properties of a perfect maze.

      I hope that helps.

    19. Re:Without the use of a loop!? by narcc · · Score: 4, Interesting

      Try something like this. (Keeping with the BASIC theme)

      10 CLS
      20 W = 10
      30 H = 10
      40 RANDOMIZE TIMER
      50 FOR I = 1 TO W: PRINT " __"; : NEXT I
      60 PRINT ""
      70 FOR I = 1 TO H
      80 PRINT "| ";
      90 FOR J = 1 TO W - 1
      100 IF INT(RND(1) * 2) = 0 THEN PRINT " __"; ELSE PRINT "| ";
      110 NEXT J
      120 PRINT "|"
      130 NEXT I
      140 PRINT "|";
      150 FOR I = 1 TO W - 1: PRINT "__ "; : NEXT I
      160 PRINT "__|"

      Lines 20 and 30 specify the width and height of the maze
      Lines 50 and 60 draw the north outer wall of the maze
      Lines 70 - 130 draw the maze by randomly drawing either a west or south wall.
      Line 80 draws the first cell in a row, which won't have a south wall, to make a long empty hall
      Line 120 draws the east-most outer wall at the end of each row
      Lines 140 - 160 draw the last row, just a long empty hall.

      You'll see that the maze has no loops or isolations. Every cell is reachable from every other cell by a single path.

      Hope that helps. Happy maze making!

  2. Sad day for slashdot... by Anonymous Coward · · Score: 5, Insightful

    ...when the summary does not know what a loop is.

    1. Re:Sad day for slashdot... by Zero__Kelvin · · Score: 5, Funny

      Worse yet is the fact that the editors don't either.

      --
      Guns don't kill people; Physics kills people! - John Lithgow as Dick Solomon on Third Rock From The Sun
    2. Re:Sad day for slashdot... by Trepidity · · Score: 4, Interesting

      Also, it can't string together a single grammatically correct sentence. Complete failure on both technical and English levels!

      The book is, however, quite interesting (just go straight to the open-access PDF and skip the mediocre Slate article).

  3. No loop? by chthon · · Score: 5, Insightful

    No editors with programming experience perhaps.

    The basic definition of a loop is a GOTO to a previous address! All the rest is syntax and optimisation.

    1. Re:No loop? by Zero__Kelvin · · Score: 5, Informative

      "Everyone knows that : in BASIC starts a new line."

      Nobody knows that, because it is untrue. The : is a statement separator, not a line seperator. That 10 you see is the line number. Notice that there are no other line numbers. Make an error on either side of the colon and the interpreter will give you the exact same complaint: Syntax error in line 10. You can verify this by looking for the line " In BASIC it's used as a separator between the statements or instructions in a single line." on Wikipedia, or use your Google-Fu to verify it thousands of other ways.

      --
      Guns don't kill people; Physics kills people! - John Lithgow as Dick Solomon on Third Rock From The Sun
  4. 10 ... : GOTO 10 is a loop by Mneme · · Score: 5, Informative

    it manages to create a complicated maze with out the use of a loop, variables and without very complicate syntax

    It's very cool the way this code draws a maze, but there's obviously a loop there.

    (And it's “without” not “with out”, and “complicated” not “complicate”.)

    1. Re: 10 ... : GOTO 10 is a loop by Lobachevsky · · Score: 4, Interesting

      it's not a maze, it's a pattern of random forward and backward slashes, "/" and "\". There's no guarantee that a path exists anywhere near the top to anywhere near the bottom. In fact, because it's random, you'd be blocked off at some point.

  5. Some minor deficiencies by zmooc · · Score: 5, Insightful

    create a complicated maze with out the use of a loop
    1. This is not necessarily a maze. It's noise. At best.
    2. It's "without", not "with out"
    3. There is a loop

    --
    0x or or snor perron?!
  6. Enterprise Java Version by Mr.+Sketch · · Score: 5, Funny

    package enterprise;

    import java.io.IOException;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.io.Writer;
    import java.util.Collections;
    import java.util.Map.Entry;
    import java.util.Random;
    import java.util.SortedMap;
    import java.util.TreeMap;

    public class Maze {
    private final WallFactory<Double> wallFactory;
    private final EntropyGenerator entropyGenerator;

    public Maze( WallFactory<Double> wallFactory, EntropyGenerator entropyGenerator ) {
    this.wallFactory = wallFactory;
    this.entropyGenerator = entropyGenerator;
    }

    public void visit( MazeVisitor visitor ) throws MazeException {
    while( true ) {
    MazeWall wall = wallFactory.createMazeWall( entropyGenerator.getNewEntropyValue() );
    wall.visit( visitor );
    }
    }

    public interface MazeWall {
    /**
    * @param visitor
    * @throws IOException
    */
    void visit( MazeVisitor visitor ) throws MazeException;
    }

    public static class LeftDiagonalWall implements MazeWall {
    @Override
    public void visit( MazeVisitor visitor ) throws MazeException {
    visitor.visit( this );
    }
    }

    public static class RightDiagonalWall implements MazeWall {
    @Override
    public void visit( MazeVisitor visitor ) throws MazeException {
    visitor.visit( this );
    }
    }

    public interface MazeVisitor {
    void visit( LeftDiagonalWall leftDiagonalWall ) throws MazeException;

    void visit( RightDiagonalWall rightDiagonalWall ) throws MazeException;
    }

    public interface WallFactory<T> {
    /**
    * @param value
    * @return the MazeWall
    * @throws MazeException
    */
    MazeWall createMazeWall( T value ) throws MazeException;
    }

    public static class StrategyWallFactory<T> implements WallFactory<T> {
    private WallRepartitionStrategy<T> wallRepartitionStrategy;

    public StrategyWallFactory( WallRepartitionStrategy<T> wallRepartitionStrategy ) {
    this.wallRepartitionStrategy = wallRepartitionStrategy;
    }

    @Override
    public MazeWall createMazeWall( T value ) throws MazeException {
    Class<? extends MazeWall> wallClassForValue = wallRepartitionStrategy.getWallClassForValue( value );
    try {
    return wallClassForValue.newInstance();
    } catch( InstantiationException | IllegalAccessException e ) {
    throw new MazeException( "Cannot create MazeWall instance", e );
    }
    }

    1. Re:Enterprise Java Version by narcc · · Score: 5, Informative

      Plagiarizer. You stole this from reddit -- from the same post linked from Nick Montfort's blog.

      Enterprise Java Version:
      http://www.reddit.com/r/programming/comments/142jix/10_print_chr_2055_rnd_1_goto_10_how_a_single_line/c79elxn

      You shouldn't take credit for the work of others. That +5 funny is a filthy dirty lie.

  7. Perl analogue by Okian+Warrior · · Score: 5, Interesting

    Don't have a Commodore Basic interpreter? this Perl 1-liner will do the same thing:

    print ["/","\\"]->[rand(2)] while 1;

    It has no start or end point, and for two arbitrary points you can't guarantee that a path exists.

    ...but it definitely *looks* like a maze, so that's what it must be. The authors said so, after all...

  8. Really? by Multiplicity · · Score: 5, Insightful

    Frontpage slashdot story with a 10 GOTO 10 and saying it's not a loop?

    Dudes, just what the fuck. I ask you that.

  9. Python version by tepples · · Score: 5, Interesting
    The Python version is two non-comment lines:

    #!/usr/bin/env python
    # DataGlyphs simulator by Damian Yerrick
    from random import choice
    while True: print "".join(choice('/\\') for i in range(40))

  10. Reminded me of DataGlyphs by tepples · · Score: 4, Informative

    It's just randomly printing forward and backward slashes, which line up because of the font. It's nifty, but hardly amazing.

    And in fact, it appears Slashdot ran an article on this a decade ago when it was called DataGlyphs.

  11. Re:Multi-Fail by Chelloveck · · Score: 4, Interesting

    The article reads to me like a sophomore-level paper deconstructing some insignificant piece of drivel and claiming great insights into human nature.

    "What can this one line -- '10 PRINT,' to use the authors' shorthand -- teach us about software, and culture at large?"

    Damn! And that's just the review, I can't even imagine what the actual 294-page book must be like. Next up I expect a 500-page treatise on Vogon poetry.

    --
    Chelloveck
    I give up on debugging. From now on, SIGSEGV is a feature.
  12. Not even that by Immerman · · Score: 5, Insightful

    It certainly has the intricate path part down, but most people would take issue with a "maze" that lacks a beginning, end, or any guarantee that you can get from point A to B even if you consider obvious closed loops out of bounds.

    --
    --- Most topics have many sides worth arguing, allow me to take one opposite you.
  13. Re:A simple Linux port by Sam+H · · Score: 5, Insightful

    Sorry, what was I thinking? This is obviously more elegant:

    tr -dc '/\' </dev/urandom

    --
    God, root, what is difference ?