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.
Here’s my rendition in Java. I’m really new to it so your thoughts on how I can improve my code are appreciated
oh… Sorry.. here’s the link: http://pastebin.com/j32JaSEw
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).
Hm… How INT and RND are work in the basic? Is INT(5*RND(1)) uniformy distributed in {0, 1, .., 4}?
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…
> 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.)
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.
Sorry, there was a problem with the paste. The URL is now: http://lisp.pastebin.com/r1gL4zYT
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
[…] 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) […]
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
Here’s my solution in ruby:
http://pastie.org/681968
Wow, this is an old thread. I was thinking about the early days of home computing, and remembered playing Hamurabi in school and a web search brought me here. TravisH82’s java code ( http://pastebin.com/j32JaSEw) is not bad, but never resets death back to zero. I noticed that if 29 people starved, even if I fed everybody in the subsequent turn I still had 29 deaths again.
This corrects the problem:
There also seems to be a problem with calculating sowable land in turn 1. I can start with 2800 bushels, buy 10 acres 18 each, give 2000 bushels in food and still sow 1010 acres when I ought to have been limited to 620.
OK, I understand now… each bushel suffices to sow two acres of land.
I worked it out on Visual BaSICK. Now I only need to translate ancient english words to civilised languade. All that MORISTOWNs, GOVERNINGs, ACRESsss…