## HAMURABI.BAS

### July 27, 2010

The game strategy implemented below is to always use exactly enough grain to feed all the people, selling land as needed to achieve that objective, and to always plant as many acres as possible; but don’t sell more land than necessary, as the rats will have less to eat if your silos are nearly empty. In the last round, buy enough land to assure at least ten acres per person; it is better to err on the side of too much land rather than too little, as the incoming steward will be able to sell land if needed.

`> (hamurabi)`

HAMURABI

CREATIVE COMPUTING MORRISTOWN, NEW JERSEY

```
```
TRY YOUR HAND AT GOVERNING ANCIENT SUMERIA

FOR A TEN-YEAR TERM OF OFFICE.

HAMURABI: I BEG TO REPORT TO YOU,

IN YEAR 1, 0 PEOPLE STARVED, 5 CAME TO THE CITY,

POPULATION IS NOW 100

THE CITY OWNS 1000 ACRES.

YOU HARVESTED 3 BUSHELS PER ACRE.

RATS ATE 200 BUSHELS.

YOU NOW HAVE 2800 BUSHELS IN STORE.

LAND IS TRADING AT 17 BUSHELS PER ACRE.

HOW MANY ACRES DO YOU WISH TO BUY? 0

HOW MANY ACRES DO YOU WISH TO SELL? 0

HOW MANY BUSHELS DO YOU WISH TO FEED YOUR PEOPLE? 2000

HOW MANY ACRES DO YOU WISH TO PLANT WITH SEED? 1000

HAMURABI: I BEG TO REPORT TO YOU,

IN YEAR 2, 0 PEOPLE STARVED, 3 CAME TO THE CITY,

POPULATION IS NOW 103

THE CITY OWNS 1000 ACRES.

YOU HARVESTED 4 BUSHELS PER ACRE.

RATS ATE 0 BUSHELS.

YOU NOW HAVE 4300 BUSHELS IN STORE.

LAND IS TRADING AT 26 BUSHELS PER ACRE.

HOW MANY ACRES DO YOU WISH TO BUY? 0

HOW MANY ACRES DO YOU WISH TO SELL? 0

HOW MANY BUSHELS DO YOU WISH TO FEED YOUR PEOPLE? 2060

HOW MANY ACRES DO YOU WISH TO PLANT WITH SEED? 1000

HAMURABI: I BEG TO REPORT TO YOU,

IN YEAR 3, 0 PEOPLE STARVED, 3 CAME TO THE CITY,

POPULATION IS NOW 106

THE CITY OWNS 1000 ACRES.

YOU HARVESTED 2 BUSHELS PER ACRE.

RATS ATE 0 BUSHELS.

YOU NOW HAVE 3740 BUSHELS IN STORE.

LAND IS TRADING AT 25 BUSHELS PER ACRE.

HOW MANY ACRES DO YOU WISH TO BUY? 0

HOW MANY ACRES DO YOU WISH TO SELL? 0

HOW MANY BUSHELS DO YOU WISH TO FEED YOUR PEOPLE? 2120

HOW MANY ACRES DO YOU WISH TO PLANT WITH SEED? 1000

HAMURABI: I BEG TO REPORT TO YOU,

IN YEAR 4, 0 PEOPLE STARVED, 3 CAME TO THE CITY,

A HORRIBLE PLAGUE STRUCK! HALF THE PEOPLE DIED.

POPULATION IS NOW 54

THE CITY OWNS 1000 ACRES.

YOU HARVESTED 2 BUSHELS PER ACRE.

RATS ATE 0 BUSHELS.

YOU NOW HAVE 3120 BUSHELS IN STORE.

LAND IS TRADING AT 25 BUSHELS PER ACRE.

HOW MANY ACRES DO YOU WISH TO BUY? 0

HOW MANY ACRES DO YOU WISH TO SELL? 0

HOW MANY BUSHELS DO YOU WISH TO FEED YOUR PEOPLE? 1080

HOW MANY ACRES DO YOU WISH TO PLANT WITH SEED? 1000

BUT YOU HAVE ONLY 54 PEOPLE TO TEND THE FIELDS. NOW THEN,

HOW MANY ACRES DO YOU WISH TO PLANT WITH SEED? 540

HAMURABI: I BEG TO REPORT TO YOU,

IN YEAR 5, 0 PEOPLE STARVED, 9 CAME TO THE CITY,

POPULATION IS NOW 63

THE CITY OWNS 1000 ACRES.

YOU HARVESTED 1 BUSHELS PER ACRE.

RATS ATE 0 BUSHELS.

YOU NOW HAVE 2310 BUSHELS IN STORE.

LAND IS TRADING AT 20 BUSHELS PER ACRE.

HOW MANY ACRES DO YOU WISH TO BUY? 0

HOW MANY ACRES DO YOU WISH TO SELL? 0

HOW MANY BUSHELS DO YOU WISH TO FEED YOUR PEOPLE? 1260

HOW MANY ACRES DO YOU WISH TO PLANT WITH SEED? 630

HAMURABI: I BEG TO REPORT TO YOU,

IN YEAR 6, 0 PEOPLE STARVED, 4 CAME TO THE CITY,

POPULATION IS NOW 67

THE CITY OWNS 1000 ACRES.

YOU HARVESTED 2 BUSHELS PER ACRE.

RATS ATE 0 BUSHELS.

YOU NOW HAVE 1995 BUSHELS IN STORE.

LAND IS TRADING AT 25 BUSHELS PER ACRE.

HOW MANY ACRES DO YOU WISH TO BUY? 0

HOW MANY ACRES DO YOU WISH TO SELL? 0

HOW MANY BUSHELS DO YOU WISH TO FEED YOUR PEOPLE? 1340

HOW MANY ACRES DO YOU WISH TO PLANT WITH SEED? 670

HAMURABI: I BEG TO REPORT TO YOU,

IN YEAR 7, 0 PEOPLE STARVED, 8 CAME TO THE CITY,

POPULATION IS NOW 75

THE CITY OWNS 1000 ACRES.

YOU HARVESTED 5 BUSHELS PER ACRE.

RATS ATE 0 BUSHELS.

YOU NOW HAVE 3670 BUSHELS IN STORE.

LAND IS TRADING AT 21 BUSHELS PER ACRE.

HOW MANY ACRES DO YOU WISH TO BUY? 0

HOW MANY ACRES DO YOU WISH TO SELL? 0

HOW MANY BUSHELS DO YOU WISH TO FEED YOUR PEOPLE? 1500

HOW MANY ACRES DO YOU WISH TO PLANT WITH SEED? 750

HAMURABI: I BEG TO REPORT TO YOU,

IN YEAR 8, 0 PEOPLE STARVED, 10 CAME TO THE CITY,

A HORRIBLE PLAGUE STRUCK! HALF THE PEOPLE DIED.

POPULATION IS NOW 42

THE CITY OWNS 1000 ACRES.

YOU HARVESTED 4 BUSHELS PER ACRE.

RATS ATE 897 BUSHELS.

YOU NOW HAVE 3898 BUSHELS IN STORE.

LAND IS TRADING AT 17 BUSHELS PER ACRE.

HOW MANY ACRES DO YOU WISH TO BUY? 0

HOW MANY ACRES DO YOU WISH TO SELL? 0

HOW MANY BUSHELS DO YOU WISH TO FEED YOUR PEOPLE? 840

HOW MANY ACRES DO YOU WISH TO PLANT WITH SEED? 420

HAMURABI: I BEG TO REPORT TO YOU,

IN YEAR 9, 0 PEOPLE STARVED, 6 CAME TO THE CITY,

POPULATION IS NOW 48

THE CITY OWNS 1000 ACRES.

YOU HARVESTED 5 BUSHELS PER ACRE.

RATS ATE 1424 BUSHELS.

YOU NOW HAVE 3524 BUSHELS IN STORE.

LAND IS TRADING AT 25 BUSHELS PER ACRE.

HOW MANY ACRES DO YOU WISH TO BUY? 0

HOW MANY ACRES DO YOU WISH TO SELL? 0

HOW MANY BUSHELS DO YOU WISH TO FEED YOUR PEOPLE? 960

HOW MANY ACRES DO YOU WISH TO PLANT WITH SEED? 480

HAMURABI: I BEG TO REPORT TO YOU,

IN YEAR 10, 0 PEOPLE STARVED, 5 CAME TO THE CITY,

A HORRIBLE PLAGUE STRUCK! HALF THE PEOPLE DIED.

POPULATION IS NOW 26

THE CITY OWNS 1000 ACRES.

YOU HARVESTED 3 BUSHELS PER ACRE.

RATS ATE 581 BUSHELS.

YOU NOW HAVE 3183 BUSHELS IN STORE.

LAND IS TRADING AT 24 BUSHELS PER ACRE.

HOW MANY ACRES DO YOU WISH TO BUY? 0

HOW MANY ACRES DO YOU WISH TO SELL? 0

HOW MANY BUSHELS DO YOU WISH TO FEED YOUR PEOPLE? 520

HOW MANY ACRES DO YOU WISH TO PLANT WITH SEED? 260

HAMURABI: I BEG TO REPORT TO YOU,

IN YEAR 11, 0 PEOPLE STARVED, 18 CAME TO THE CITY,

POPULATION IS NOW 44

THE CITY OWNS 1000 ACRES.

YOU HARVESTED 1 BUSHELS PER ACRE.

RATS ATE 633 BUSHELS.

YOU NOW HAVE 2160 BUSHELS IN STORE.

IN YOUR 10-YEAR TERM OF OFFICE, 0 PERCENT OF THE

POPULATION STARVED PER YEAR ON AVERAGE, I.E., A TOTAL OF

0 PEOPLE DIED!!

YOU STARTED WITH 10 ACRES PER PERSON AND ENDED WITH 22 ACRES PER PERSON.

A FANTASTIC PEFORMANCE!!! CHARLEMAGNE, DISRAELI, AND

JEFFERSON COMBINED COULD NOT HAVE DONE BETTER

`SO LONG FOR NOW.`

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