HAMURABI.BAS

July 27, 2010

Back in the 1970s, David Ahl wrote a new game program each month for Creative Computing magazine. Those were the days of all-caps teletypes (if you were rich you could get a new-fangled “glass teletype”) and punched paper tapes (it was fun to play with the confetti they made). MS-BASIC permitted twenty-six single-letter variable names; later they also allowed a single letter followed by a single digit. There were no user-defined functions and no recursion. GOTO was common, resulting in a phenomenon called “spaghetti code.” There was good news, however: it was acceptable programming practice to GOTO the middle of a FOR loop and run the code there, as long as you jumped back out of the loop before the corresponding NEXT — try to do that in your favorite functional language!

One of Ahl’s most memorable games was Hamurabi, in which the player took the role of the administrator of the ancient city of Sumeria, managing the grain and land resources of the city and trying to keep the residents from starvation. It is typical of the genre, with simple numeric input and scrolling text output. Here is a description and sample game, and the original BASIC source code is reproduced on the next page. By my count, there are fourteen lines that are unreachable except by an IF…THEN, GOSUB or GOTO, forty-three lines that redirect control flow away from the line below, and four instances (line 555 to 215, bypassing line 210, 453 and 479 to 440, bypassing 430, 441 to 511, bypassing 510, and 880 and 885 to 565, bypassing 560) of jumping into the middle of a block of code; that’s a fine bowl of spaghetti, considering the entire program is only 120 lines. Variable P represents the current population, S is the number of bushels in stores, and A is the number of acres of farmland owned by the city, but other variables are used inconsistently — for instance D sometimes represents the number of deaths in the current year, but other times it represents the current input value, and other times Q is used to represent the current input value.

Your task is to reimplement HAMURABI.BAS in a more modern computer language. Don’t peek at the solution unless you want to deprive yourself of the sheer joy of working out the spaghetti code and figuring out what the variables really stand for. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

About these ads

Pages: 1 2 3 4

12 Responses to “HAMURABI.BAS”

  1. TravisH82 said

    Here’s my rendition in Java. I’m really new to it so your thoughts on how I can improve my code are appreciated

  2. TravisH82 said

    oh… Sorry.. here’s the link: http://pastebin.com/j32JaSEw

  3. alexander said

    One more rendition, in lua: http://codepad.org/pc8FSfnQ

    It seems that average size of population in optimal game should be somewhere between 380 and 390 (if we remove restriction on the game duration).

  4. alexander said

    Hm… How INT and RND are work in the basic? Is INT(5*RND(1)) uniformy distributed in {0, 1, .., 4}?

  5. TravisH82 said

    Wish you were allowed to edit your posts here.. or maybe you can and I don’t know how.. anyways. I think I figured out how to embed the code in the comments here…

  6. Rörd said

    > Hm… How INT and RND are work in the basic? Is INT(5*RND(1)) uniformy distributed in {0, 1, .., 4}?

    IIRC yes. INT will truncate a floating point number (cut off everything behind the point), and RND returns a floating point number between 0 (including) and 1 (excluding).
    (The argument to RND will be used as new seed, when negative; create a new seed, when zero; and use the existing seed, when positive.)

  7. Rörd said

    So here’s my reimplementation in Common Lisp: http://lisp.pastebin.com/wnUkj52V

    I started with using tagbody/go for the control flow, but I had replaced all uses of these before I arrived at a working version.

  8. Rörd said

    Sorry, there was a problem with the paste. The URL is now: http://lisp.pastebin.com/r1gL4zYT

  9. John said

    I wrote a version in Factor and blogged about it:

    http://re-factor.blogspot.com/2010/08/hamurabi.html

    The implementation can be seen here:

    http://paste.factorcode.org/paste?id=1833

  10. [...] HAMURABI.BAS « Programming Praxis Your task is to reimplement HAMURABI.BAS in a more modern computer language. Don’t peek at the solution unless you want to deprive yourself of the sheer joy of working out the spaghetti code and figuring out what the variables really stand for. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below. (tags: dev compsci programming todo) [...]

  11. gold said

    http://wiki.tcl.tk/26775,

    Loaded an etcl version of Hamurabi with
    either a small console demo version or
    a larger etcl canvas version.
    I’m calling it Game kingdom of Strategy
    gold

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 631 other followers

%d bloggers like this: