223 Hamurabi

Description

Boo Rubyists,

This week’s quiz was submitted by Martin DeMello

Back in the microcomputer age, there was a fascinating little game called Hamurabi (no, that isn’t a typo - there was an eight character limit). The game mechanics were pretty simple: you were the ruler of a kingdom, and every year you had to decide what proportion of your grain to allocate to planting, and what proportion to feeding the people. The game engine would then set up the formulae, toss in a few random numbers and simulate your crop and population growth. For all its simplicity, though, it was a lot of fun to play, and oddly compulsive.

The Wikipedia page http://en.wikipedia.org/wiki/Hamurabi links to a couple of implementations in BASIC; pick one and reimplement it in Ruby.

Extra credit: if someone can come up with a fun multiplayer version, I’ll be impressed :)

Have fun!

Summary

There were many great solutions to this week’s quiz. Most of the solutions took the route of translating the BASIC code into Ruby though James Edward Gray III actually built a BASIC interpreter.

James uses regex based parsing and makes use of StringScanner, which provides for lexical scanning operations on a String. BASIC is pretty amenable to parsing with regex because of it’s simple syntax. I’m not familiar enough with BASIC to know if a grammar would be required to provide a full implementation, but it may add simplicity in some situations of complex expressions. I ran into some glitches with my grain count getting extremely large and plagues happening every turn, but overall this was definitely the most adventurous attempt.

Benoit Daloze’s solution translated the original statement sections into Ruby methods. Each of these methods ended with a call to another method as the last expression in the method. This is to simulate either an implicit fallthrough or an explicit goto to another logical section of the code.

genericpenguin gives us another translation, though with a little bit more of the control flow abstracted into the run_iteration method.

Martin DeMello has the most Rubyish translation. Each logical section was translated into a method and the overall program flow was controlled by a main loop. The main loop can easily be extended to have the game run for a 20 year term, or even thousands (maybe even rivalling Civilization).

It is interesting to look back on these older computer games and the spectrum of complexity and see how even simple games can provide so much enjoyment. And to also note how even simple games can be tricky to implement. Speaking of tricky, there was some lively discussion about a multiplayer version. Some possible game mechanics include: a free market for buying and selling land, migrant populations, and the tradeoffs of public healthcare. Maybe even a cap and trade system for carbon emissions, who knows?

In playing many of these solutions I was doing pretty well, up until the point where everyone died. This quiz was a lot of fun, special thanks go to Martin DeMello who suggested it. Thank you everyone for the great solutions and discussion!

Hamurabi (#223) - Solutions


Saturday, October 31, 2009