Become a fan of Slashdot on Facebook

 



Forgot your password?
typodupeerror
×
Books Graphics Programming Games

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

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.
This discussion has been archived. No new comments can be posted.

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

Comments Filter:
  • by Anonymous Coward on Saturday December 01, 2012 @02:51PM (#42155619)

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

    • by Nutria ( 679911 ) on Saturday December 01, 2012 @02:54PM (#42155647)

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

      • by Anonymous Coward on Saturday December 01, 2012 @03:39PM (#42156061)

        Pratt by name . . .

      • by Black Parrot ( 19622 ) on Saturday December 01, 2012 @03:52PM (#42156177)

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

        • by Nutria ( 679911 )

          What programmer doesn't know/can't figure out rudimentary BASIC?

          • by narcc ( 412956 ) on Saturday December 01, 2012 @06:15PM (#42157057) Journal

            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.

    • [slick salesman voice] This is not your grandchild's looping mechanism! [/slick salesman voice]

    • print REPLY; goto PARENT;
    • by Anonymous Coward on Saturday December 01, 2012 @03:01PM (#42155737)

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

      • by gl4ss ( 559668 ) on Saturday December 01, 2012 @03:19PM (#42155887) Homepage Journal

        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.

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

          You are really going to break the author's heart if you keep that up.

          It's hard to look at this story and not feel that it's a base case in an inductive proof that refutes the existence of art.

          • by erroneus ( 253617 ) on Saturday December 01, 2012 @03:42PM (#42156093) Homepage

            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.

            • by stokessd ( 89903 ) on Saturday December 01, 2012 @03:50PM (#42156167) Homepage

              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...

            • by swillden ( 191260 ) <shawn-ds@willden.org> on Saturday December 01, 2012 @03:52PM (#42156181) Journal

              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.

              • by MightyMartian ( 840721 ) on Saturday December 01, 2012 @04:05PM (#42156281) Journal

                Much like a televangelist or politician then.

            • by Samantha Wright ( 1324923 ) on Saturday December 01, 2012 @04:10PM (#42156313) Homepage Journal

              It's called a display hack [wikipedia.org]. 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.

            • by Anonymous Coward on Saturday December 01, 2012 @06:23PM (#42157089)

              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...

        • Not even that (Score:5, Insightful)

          by Immerman ( 2627577 ) on Saturday December 01, 2012 @03:54PM (#42156189)

          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.

      • by NFN_NLN ( 633283 ) on Saturday December 01, 2012 @04:12PM (#42156329)

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

        I disagree, I think this finally proves the existence of god. ;)

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

      Well, in certain dialects of Forth, you'd probably be able to write it using tail recursion. And it would be probably slightly shorter. :-)

    • by Potor ( 658520 ) <farker1NO@SPAMgmail.com> on Saturday December 01, 2012 @03:21PM (#42155899) Journal
      this is one of the stupidest /. stories ever -- it is not one line of code, and it is a loop, as you and many others point out.
      • by shitzu ( 931108 ) on Saturday December 01, 2012 @03:29PM (#42155977)

        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.

        • by Dan East ( 318230 ) on Saturday December 01, 2012 @07:02PM (#42157365) Journal

          That's be cause you don't appreciate the context in which this code came to exist. Back in the early eighties, to be able to generate such visually impressive and complex looking imagery with so little code, was quite an amazing thing. I, for one, wish I would have known that bit of code back then as a ten year old. It certainly would have beaten my usual "10 PRINT "DAN WAS HERE!!! "; 20 GOTO 10" that I would type into the C64s, TI-99/4As, Atari 800s, and other computers on display at K-Mart and Sears.

          For the sake of completeness, here is a version that works with the syntax (and character set) of another home computer of the era, the TI-99/4A


          10 PRINT CHR$(INT(RND+.5)*45+47);
          20 GOTO 10

          The code is a little more complex because the forward and backward slash characters are not contiguous in the TI's character set (47 and 92). The result visually isn't as good because the TI's character glyphs are more spaced out than the C64's. However it does work - I tested it in the emulator (with standard TI BASIC, doesn't required Extended BASIC).

          • by Dan East ( 318230 ) on Saturday December 01, 2012 @07:17PM (#42157461) Journal

            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

      • by Zero__Kelvin ( 151819 ) on Saturday December 01, 2012 @03:34PM (#42156027) Homepage

        " it is not one line of code"

        Actualy, it absolutely is just one line of code. You are confusing the number of statements with the number of lines. In BASIC, as in many languages, you can have multiple statements and operation in a single line of code. Those statements do indeed however constitute an infinite loop, however.

        • by The Moof ( 859402 ) on Saturday December 01, 2012 @05:27PM (#42156755)
          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.
          • " every program written in C can be condensed down to 1 line"

            This would be true if it were not for the pre-processor. You cannot, for example #include multiple external files on the same line or have multiple #defines on the same line. As far as "going that route" goes, the only route I took was making factually correct statements to correct the factually erroneous ones repeated far too many times on this subject. This isn't open to interpretation, unless you happen to be a C interpreter* that is ;-)

            * B

          • by egcagrac0 ( 1410377 ) on Saturday December 01, 2012 @05:48PM (#42156883)
            #include <stdio.h>
            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.

    • Submitter was a moron. From the book: "For a one-line program that loops forever..." also "...it uses GOTO 10 to loop back..."
  • by Anonymous Coward on Saturday December 01, 2012 @02:51PM (#42155623)

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

  • No loop? (Score:5, Insightful)

    by chthon ( 580889 ) on Saturday December 01, 2012 @02:51PM (#42155625) Journal

    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.

    • Just missing BASIC experience...like I had in the 70s.

  • by Mneme ( 56118 ) on Saturday December 01, 2012 @02:52PM (#42155627)

    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”.)

    • by Lobachevsky ( 465666 ) on Saturday December 01, 2012 @03:08PM (#42155799)

      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.

      • There are some interesting mathematical problems. Like given a certain width and an endless number of rows, if you start in at an arbitrary 'square', what is the distribution of the number of other squares that you can reach? (with or without wrapping?) What if it is endless in two directions?
      • by narcc ( 412956 )

        In fact, because it's random, you'd be blocked off at some point.

        Nonsense. See: Binary tree mazes.

        Also, I recommend that you look at other maze generation algorithms (especially Eller's Algorithm) as you can easily create a random yet solvable maze where there is exactly one path between any two cells -- no loops or isolations. In the case of Eller's Algorithm, you can create a random maze of any length that has this property without keeping the entire maze in memory -- just a single line!

  • by Anonymous Coward on Saturday December 01, 2012 @02:53PM (#42155639)

    That....is.....a loop.......

    • Yes Captain (Score:2, Funny)

      by Anonymous Coward

      that's what our sensors are picking up.

  • by zmooc ( 33175 ) <{ten.coomz} {ta} {coomz}> on Saturday December 01, 2012 @02:54PM (#42155651) Homepage

    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

    • Re: (Score:2, Informative)

      by daremonai ( 859175 )
      The Slate article does make clear that this only works on something with the old Commodore 64 character set (PETSCII). And that it is a loop.It's not exactly a great article, but it does get these things right.
      • by Cyberax ( 705495 )
        It would work with any script that has '/' and '\' symbols separated by 1 code point.
      • by gl4ss ( 559668 )

        The Slate article does make clear that this only works on something with the old Commodore 64 character set (PETSCII). And that it is a loop.It's not exactly a great article, but it does get these things right.

        it would work on anything with the right type of font. if you want to try it on windows cmdline them from prefs select raster fonts and 8x8 pixel font.

        (yeah I tested.. with a javascript script of all things, ugh)

    • Comment removed based on user account deletion
  • by dcollins ( 135727 ) on Saturday December 01, 2012 @02:56PM (#42155675) Homepage

    Re: "it manages to create a complicated maze with out the use of a loop"

  • by Mr. Sketch ( 111112 ) <<moc.liamg> <ta> <hcteks.retsim>> on Saturday December 01, 2012 @02:57PM (#42155691)
    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 );
    }
    }
  • How does this not use a loop?

  • Perl analogue (Score:5, Interesting)

    by Okian Warrior ( 537106 ) on Saturday December 01, 2012 @03:01PM (#42155725) Homepage Journal

    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...

  • Really? (Score:5, Insightful)

    by Multiplicity ( 2498210 ) on Saturday December 01, 2012 @03:02PM (#42155741)

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

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

    • Re: (Score:2, Funny)

      by Anonymous Coward

      It's a step up from the plastic on Mars story a few days ago.

    • Re: (Score:3, Insightful)

      by Anonymous Coward

      What the fuck? I'll tell you what the fuck... Slashdot as it stands today is a sad, pale reflection of the Good Slashdot of Old.

      The focus isn't "News for Nerds" anymore, it's "Bash Apple Good... and oh, here's some code too."

  • 1) It is an endless loop created by the GOTO command.
    2) Is it a maze or does it just look like one? There is no indication that it actually creates a maze each and every time. It is more likely a pseudo random pattern that LOOKS like a maze.

    • by mysidia ( 191772 )

      Is it a maze or does it just look like one?

      It is a maze. It doesn't necessarily have exactly one solution (It probably has more than one). It doesn't necessarily have any solutions.

      The loop never seems to terminate, so it seems that the maze will keep getting larger until the program is manually aborted

  • Multi-Fail (Score:4, Interesting)

    by SuperCharlie ( 1068072 ) on Saturday December 01, 2012 @03:04PM (#42155767)
    This article fails in English and fails in the content. Booya.
    • Re:Multi-Fail (Score:5, Interesting)

      by Chelloveck ( 14643 ) on Saturday December 01, 2012 @03:22PM (#42155905)

      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.

  • /., in collaboration with the Erowid Vault. brings you "get high and watch this code run." Wow, it's so cool.
  • by JoeCommodore ( 567479 ) <larry@portcommodore.com> on Saturday December 01, 2012 @03:29PM (#42155971) Homepage

    I remember doing that in 1980something on the PET (maybe not that line, but the logic was the same). IIRC it was a type-in along with a bunch of other cheap BASIC graphics routines in a Creative Computing - later I'm sure it showed up in RUN or Transactor for the VIC/64.

    It was cool for maybe a couple hours... along with thoughts of what games could I use it with (that's how we developed game ideas old-school style, starting with cool little snippets like this.)

  • Watching the video it occurs to me that the interesting part is that if you randomly print out slashes or backslashes endlessly, you create an endless "maze". It works particularly well on the c64 because the printable graphics set includes a slash and backslash that have no spacing around the character. But you can do the same thing in a terminal like this:

    perl -e 'while (1) { print rand() > .5 ? "/" : "\\" }';

    Although depending on your font it won't look as compelling as the c64 version

  • by ducomputergeek ( 595742 ) on Saturday December 01, 2012 @03:32PM (#42156003)

    when there's a perl module for that:

    http://search.cpan.org/~jgamble/Games-Maze-1.08/lib/Games/Maze.pm [cpan.org]

  • A meditation (Score:4, Informative)

    by LihTox ( 754597 ) on Saturday December 01, 2012 @03:37PM (#42156045)

    From page 4 of the book:

    This book is unusual in its focus on a single line of code, an extremely concise BASIC program that is simply called 10 PRINT throughout. Studies of individual, unique works abound in the humanities. Roland Barthes’s S/Z, Samuel Beckett’s Proust, Rudolf Arnheim’s Genesis of a Painting: Picasso’s Guernica, Stuart Hall et al.’s Doing Cultural Studies: The Story of the Sony Walkman, and Michel Foucault’s Ceci n’est pas une pipe all exemplify the sort of close readings that deepen our understanding of cultural production, cultural phenomena, and the Western cultural tradition. While such literary texts, paintings, and consumer electronics may seem significantly more complex than a one-line BASIC program, undertaking a close study of 10 PRINT as a cultural artifact can be as fruitful as close readings of other telling cultural artifacts have been.

    In short, this is not a programming book, but it appears to be a book of cultural anthropology about programming. Or perhaps a meditation which starts with one simple starting point and branching out in many different directions. Criticizing the program "10 PRINT" as trivial rather misses the point, I should think.

  • by Sam H ( 3979 ) <sam@zoy.org> on Saturday December 01, 2012 @03:56PM (#42156197) Homepage

    strings /dev/urandom|tr -dc '/\'

  • Diablo (Score:4, Funny)

    by dmomo ( 256005 ) on Saturday December 01, 2012 @04:00PM (#42156231)

    Wow. Who would have known that the code for Diablo is so simple.

  • by hanz88 ( 2780929 ) on Saturday December 01, 2012 @05:19PM (#42156713)
    int main(){for(;;)putchar("\\/"[rand()%2]);}
  • by HuguesT ( 84078 ) on Saturday December 01, 2012 @05:21PM (#42156725)

    The book is worth reading, it retells many things from the beginning of personal computing, exploring subjects along the way. the 10 PRINT one-liner is the motivation but not the topic.

    Cheers.

Never test for an error condition you don't know how to handle. -- Steinbach

Working...