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.

19 of 438 comments (clear)

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

  2. Without the use of a loop? by Anonymous Coward · · Score: 3, Informative

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

  3. 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
  4. Re:Some minor deficiencies by daremonai · · Score: 1, Informative

    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.

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

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

  7. Bah why write the code... by ducomputergeek · · Score: 3, Informative
    --
    "The problem with socialism is eventually you run out of other people's money" - Thatcher.
  8. Re:Without the use of a loop!? by Zero__Kelvin · · Score: 3, Informative

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

    --
    Guns don't kill people; Physics kills people! - John Lithgow as Dick Solomon on Third Rock From The Sun
  9. A meditation by LihTox · · Score: 3, Informative

    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.

  10. 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
  11. Re:Without the use of a loop!? by NFN_NLN · · Score: 3, Informative

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

  12. Re:Without the use of a loop!? by Anonymous Coward · · Score: 0, Informative

    This guy needs to be modded insightful.

  13. Re:Without the use of a loop!? by Anonymous Coward · · Score: 3, Informative

    The difference between a maze and a labyrinth is that the goal of a maze is to get from the entrance to the exit. The goal of a labyrinth on the other hand is to get to the center. If there is no entrance and there is no exit, it isn't a maze by strict definition, but a set of paths.

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

  15. An explanation of the code by Anonymous Coward · · Score: 2, Informative

    If you need help with the code, here's a simple explanation:

    The RND(1) function will return a floating point value between 0 and 1 (technically, this is 0 to 0.999... but let's just call it 0 to 1). So the part of the code that says 205.5 + RND(1) simply returns a value somewhere between 205.5 and 206.5. Assuming the RND(1) function is a good random number generator, that means half of the numbers will be between 205.5 and 205.9999..., and half will be between 206.0 and 206.5.

    The CHR$() function pulls a single character from the Commodore 64's PETSCII table. The address is always an integeter, so if you ask for CHR$(205.999), that's the same as CHR$(205). Similarly, asking for CHR$(206.5) is the same as CHR$(206).

    In the PETSCII table, character 205 is a single backslash, and character 206 is a single forward slash. So what the code is doing is very simple: it simply prints either a forward slash or a backslash, and keeps doing it because it loops back to itself.

    This same code won't work on Linux or Windows/DOS because in the ASCII table, the forward slash is 057, and the backslash is 134. For ASCII systems, you'd need to (basically) flip a coin and print "\" if heads, and "/" if tails.

    And that's all it does. There's no magic. No guarantee it is even a solvable maze. It just looks pretty.

    -jh

  16. Re:Without the use of a loop!? by Zero__Kelvin · · Score: 3, Informative

    Actually, you almost understood the implications. "One line of code" is in fact meaningless. It is one of the many reasons why using LOCs as a metric when assessing the quality and productivity of a codebase and its team is not a particularly useful approach.

    --
    Guns don't kill people; Physics kills people! - John Lithgow as Dick Solomon on Third Rock From The Sun
  17. Re:Without the use of a loop!? by Dan+East · · Score: 3, Informative

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

    --
    Better known as 318230.
  18. 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.

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