Forgot your password?
typodupeerror
Books Graphics Programming Games

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

Posted by timothy
from the when-factors-align dept.
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 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 Anonymous Coward on Saturday December 01, 2012 @02:53PM (#42155639)

    That....is.....a 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 daremonai (859175) on Saturday December 01, 2012 @03:00PM (#42155719)
    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 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 tepples (727027) <{tepples} {at} {gmail.com}> on Saturday December 01, 2012 @03:08PM (#42155801) Homepage Journal

    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 [slashdot.org].

  • 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]

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

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

  • Re:No loop? (Score:5, Informative)

    by Zero__Kelvin (151819) on Saturday December 01, 2012 @03:40PM (#42156069) Homepage

    "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 [wikipedia.org], or use your Google-Fu to verify it thousands of other ways.

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

  • by Anonymous Coward on Saturday December 01, 2012 @05:10PM (#42156653)
    This guy needs to be modded insightful.
  • by Anonymous Coward on Saturday December 01, 2012 @05:23PM (#42156731)

    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.

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

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

    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 [wikipedia.org] 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 [wikipedia.org] 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

  • by Zero__Kelvin (151819) on Saturday December 01, 2012 @06:48PM (#42157265) Homepage
    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.
  • by Dan East (318230) on Saturday December 01, 2012 @07:02PM (#42157365) Homepage 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 narcc (412956) on Saturday December 01, 2012 @07:09PM (#42157421) Journal

    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 [reddit.com]

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

  • by Dan East (318230) on Saturday December 01, 2012 @07:26PM (#42157527) Homepage Journal

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

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

Take care of the luxuries and the necessities will take care of themselves. -- Lazarus Long

Working...