Forum: Ruby Hamurabi (#223)

33117162fff8a9cf50544a604f60c045?d=identicon&s=25 Daniel X Moore (yahivin)
on 2009-11-01 00:24
(Received via mailing list)
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

The three rules of Ruby Quiz:

1.  Please do not post any solutions or spoiler discussion for this
quiz until 48 hours have elapsed from the time this message was
sent.

2.  Support Ruby Quiz by submitting ideas and responses
as often as you can.

3.  Enjoy!

Suggestion:  A [QUIZ] in the subject of emails about the problem
helps everyone on Ruby Talk follow the discussion.  Please reply to
the original quiz message, if you can.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

RSS Feed: http://rubyquiz.strd6.com/quizzes.rss

Suggestions?: http://rubyquiz.strd6.com/suggestions

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

## Hamurabi (#223)

Boo Rubyists,

This week's quiz was submitted by Martin DeMello[1]

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[2] 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!

[1]: http://zem.novylen.net
[2]: http://en.wikipedia.org/wiki/Hamurabi
259f23c3b129f07b0c496b9f0495f07e?d=identicon&s=25 James Edward Gray II (Guest)
on 2009-11-02 01:00
(Received via mailing list)
On Oct 31, 2009, at 6:23 PM, Daniel Moore wrote:

> The Wikipedia page[2] links to a couple of implementations in BASIC;
> pick one and reimplement it in Ruby.

Thanks for a very enjoyable quiz!

I wrote a super minimal BASIC interpreter to avoid translating the
code.  (Yes, I am aware that's insane.  I'll seek help.)

Ironically, I think supporting that victory FOR loop at the very end
was the hardest part.  I still have bugs in that.  :(

James Edward Gray II
2c672f42613d05ca8ba8d834800b1113?d=identicon&s=25 Seebs (Guest)
on 2009-11-02 01:10
(Received via mailing list)
On 2009-11-01, James Edward Gray II <james@graysoftinc.com> wrote:
> On Oct 31, 2009, at 6:23 PM, Daniel Moore wrote:
>> 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.

Oh, man!  I remember that.  There was one for the econ students at St.
Olaf loooong ago, I actually wrote my own from scratch once.  In mine,
there were also woolen mills (or, in an early version, wollen mills --
I speld ril gud as a kid), and the win condition was to have a given
amount of
land, plus enough bolts of fabric to make wedding dresses for your
population,
and enough people that you could have hundreds of happy families.  (The
question of whether wool was a good choice for a wedding dress was never
answered).

> I wrote a super minimal BASIC interpreter to avoid translating the
> code.  (Yes, I am aware that's insane.  I'll seek help.)

It's a good kind of crazy.

-s
Dddc63b63be3f9abeca7b0bebb383b80?d=identicon&s=25 Ehsanul Hoque (Guest)
on 2009-11-02 05:56
(Received via mailing list)
> I wrote a super minimal BASIC interpreter to avoid translating the
> code.  (Yes, I am aware that's insane.  I'll seek help.)

Hah, ingenious!

I really didn't want to go about translating that BASIC either, but
instead, I was just going to make my own version based on whatever I
just read about the game. Making a simple BASIC interpreter is so much
cooler though, it should be a ruby quiz in and of itself really (make it
a DSL for extra credit :P).

BTW, does anybody have any ideas for how a (fun) multiplayer version of
Hamurabi would look like? What I was thinking: Add in a model of free
markets to set price of land, so there would then be real trading
between players. Then a smart player could influence prices through
supply, to his opponents' detriment, if he could see the state of their
kingdoms. And then there could be all sorts of nice strategies and
counter-strategies in the struggle to be the best.  Also: quick (say 30
seconds or a minute at most), timed rounds, ending at round ten or so,
at which point the winner is announced, and the game is restarted.

What do you guys think of that? Sound like any fun? A problem might be
finding other players to actually play with live! In which case a
slower, non-timed version that emailed you when it was your turn might
be more appropriate (and less of a time sucker).

- Ehsan
Ae16cb4f6d78e485b04ce1e821592ae5?d=identicon&s=25 Martin DeMello (Guest)
on 2009-11-03 19:54
(Received via mailing list)
On Mon, Nov 2, 2009 at 5:29 AM, James Edward Gray II
<james@graysoftinc.com> wrote:
>
> I wrote a super minimal BASIC interpreter to avoid translating the code.
>  (Yes, I am aware that's insane.  I'll seek help.)

Wow, I thought translating the code would be fairly simple, but it's a
morass of gotos and one-letter variables. I think I'd have written the
interpreter a lot quicker :) But now my stubborn side has kicked in
and I'm going to finish the translation instead.

martin
Ae16cb4f6d78e485b04ce1e821592ae5?d=identicon&s=25 Martin DeMello (Guest)
on 2009-11-03 20:03
(Received via mailing list)
On Mon, Nov 2, 2009 at 10:25 AM, Ehsanul Hoque <ehsanul_g3@hotmail.com>
wrote:
>
> BTW, does anybody have any ideas for how a (fun) multiplayer version of Hamurabi would 
look like? What I was thinking: Add in a model of free markets to set price of land, so 
there would then be real trading between players. Then a smart player could influence 
prices through supply, to his opponents' detriment, if he could see the state of their 
kingdoms. And then there could be all sorts of nice strategies and counter-strategies in 
the struggle to be the best.  Also: quick (say 30 seconds or a minute at most), timed 
rounds, ending at round ten or so, at which point the winner is announced, and the game is 
restarted.
>
> What do you guys think of that? Sound like any fun? A problem might be finding other 
players to actually play with live! In which case a slower, non-timed version that emailed 
you when it was your turn might be more appropriate (and less of a time sucker).

You could also have disaffected people moving from city to city,
adjusting the populations upward and downward respectively.

martin
259f23c3b129f07b0c496b9f0495f07e?d=identicon&s=25 James Edward Gray II (Guest)
on 2009-11-03 20:12
(Received via mailing list)
On Nov 2, 2009, at 2:19 PM, Martin DeMello wrote:

> and I'm going to finish the translation instead.
Yeah, I'm am looking forward to seeing your translated version so I
can tell if my interpreter is even doing the right things.  :)

James Edward Gray II
55ef7cc6dd2ec3ec7dfda7caa721dbbb?d=identicon&s=25 William Manire (Guest)
on 2009-11-03 20:15
(Received via mailing list)
Great ideas.

I suggest a new disaster that could happen to a kingdom as well.

Public Healthcare

2009/11/3 Martin DeMello <martindemello@gmail.com>
Ae16cb4f6d78e485b04ce1e821592ae5?d=identicon&s=25 Martin DeMello (Guest)
on 2009-11-03 20:20
(Received via mailing list)
Attachment: hamurabi.rb (5 KB)
okay, that was long and painful, but i have emerged triumphant ... i
hope :)

http://pastie.org/681968 and attached

martin
259f23c3b129f07b0c496b9f0495f07e?d=identicon&s=25 James Edward Gray II (Guest)
on 2009-11-03 20:32
(Received via mailing list)
On Oct 31, 2009, at 6:23 PM, Daniel Moore wrote:

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

Here's my sort-of solution (via Pastie because it's longish):

   http://pastie.textmate.org/680908

I started cleaning it up a bit today, but ran out of time.  There's
still a lot more that could be done.

It also has a pretty big bug in it.  My simple stack solution for
GOTO, GOSUB, and FOR doesn't really work.  It looked like it worked
most of the way through the code because, GOTO's and GOSUB's only
appear on their own lines or at the end of a compound instruction.
The FOR loop at the end isn't like that though, and my interpreter
doesn't really run it 10 times because it cannot resume into the
middle of a compound instruction line.  Drat, foiled again.

I did have a ton of fun playing with this problem.  Thanks again to
the organizers for a great problem!

James Edward Gray II
55ef7cc6dd2ec3ec7dfda7caa721dbbb?d=identicon&s=25 William Manire (Guest)
on 2009-11-03 21:04
(Received via mailing list)
BASIC wasn't that bad...

it was horrible :)

Just out of curiosity, which BASIC interpreter was the BASIC script
you're
trying to port written for?

2009/11/2 Martin DeMello <martindemello@gmail.com>
Ae16cb4f6d78e485b04ce1e821592ae5?d=identicon&s=25 Martin DeMello (Guest)
on 2009-11-03 21:15
(Received via mailing list)
"8k MICROSOFT BASIC", apparently :) It's the one here:
http://www.dunnington.u-net.com/public/basicgames/HMRABI

martin
55ef7cc6dd2ec3ec7dfda7caa721dbbb?d=identicon&s=25 William Manire (Guest)
on 2009-11-03 21:37
(Received via mailing list)
Wow this looks old, almost like GWBasic.

I would suggest flow charting this thing first, then creating it in Ruby
from your chart rather than trying to translate it line for line.

2009/11/2 Martin DeMello <martindemello@gmail.com>
5a3696496b80613b55c3b7bc3b2d94bc?d=identicon&s=25 genericpenguin (Guest)
on 2009-11-03 23:52
(Received via mailing list)
Sorry I'm new to the list. Is it ok to post solutions at this point
(Nov 1 10:30am to now +48hrs)?
3b593e0746a5694a1d3f3cc533954645?d=identicon&s=25 David Southwell (Guest)
on 2009-11-03 23:54
(Received via mailing list)
> > And then there could be all sorts of nice strategies and
> > You could also have disaffected people moving from city to city,
> 2009/11/3 Martin DeMello <martindemello@gmail.com>
Existing disaster is when there is no public responsibility for
healthcare.
When the world is full of pharisees who pass by on the other side.

Human concern for others is something with a value beyond rubies!!

David
55ef7cc6dd2ec3ec7dfda7caa721dbbb?d=identicon&s=25 William Manire (Guest)
on 2009-11-04 00:24
(Received via mailing list)
ha ha ha, hard to argue against that point. Too bad our laws aren't
written
in Ruby.

2009/11/3 David Southwell <david@vizion2000.net>
259f23c3b129f07b0c496b9f0495f07e?d=identicon&s=25 James Edward Gray II (Guest)
on 2009-11-04 01:31
(Received via mailing list)
On Nov 2, 2009, at 6:37 PM, genericpenguin wrote:

> Sorry I'm new to the list. Is it ok to post solutions at this point
> (Nov 1 10:30am to now +48hrs)?

It sure is.  Post away.

And welcome to the list.

James Edward Gray II
5a3696496b80613b55c3b7bc3b2d94bc?d=identicon&s=25 genericpenguin (Guest)
on 2009-11-04 01:31
(Received via mailing list)
Well, here goes. I did it the bad way (no flowcharts or anything) and
it was pretty painful (meaning I regretted it later). I got it to the
point where it is semi-reasonable but it's not very um, Rubyish. I
ironed out all the showstopper bugs but I'm sure there's quite a few
more. In any case it works after a fashion (compared the output to a
running instance of http://www.calormen.com/applesoft/) and it doesn't
make my eyes bleed (no guarantees on yours).

http://dl.getdropbox.com/u/1826667/hamurabi.rb

I spent so much time on it that in the end I didn't clean up all the
puts'. Sorry!


On Nov 3, 11:50 am, James Edward Gray II <ja...@graysoftinc.com>
0ea7f61aec8fee539be0cf39b7bab77c?d=identicon&s=25 Benoit Daloze (Guest)
on 2009-11-04 05:00
(Received via mailing list)
Hi, I did the hard-way too, but no so sure I regret :)

In fact it was interesting how to transform jumps into methods.

So here is my pastie:
http://pastie.textmate.org/681199

Still bugged, because I couldn't understand so much the part with
planting
seeds. (and why using the variable dead at that moment?)

For the "for" loop at the end, what is it supposed to do? output
7.chr(bell)
?

2009/11/3 genericpenguin <sven.schott@gmail.com>
Ae16cb4f6d78e485b04ce1e821592ae5?d=identicon&s=25 Martin DeMello (Guest)
on 2009-11-04 09:09
(Received via mailing list)
On Wed, Nov 4, 2009 at 4:23 AM, David Southwell <david@vizion2000.net>
wrote:
>
> Existing disaster is when there is no public responsibility for healthcare.
> When the world is full of pharisees who pass by on the other side.
>
> Human concern for others is something with a value beyond rubies!!

Yeah, not having public healthcare could garner you a few extra
bushels of wheat per year, but increase your chances of plague and
decrease your immigration rates. There's a nice continuum between
HAMURABI and Civilisations :)

m.
5a3696496b80613b55c3b7bc3b2d94bc?d=identicon&s=25 Sven Schott (Guest)
on 2009-11-04 12:43
(Received via mailing list)
Cool. Caveat for the non-bleed users: ruby1.9 required (1.8.6 here).

I must admit, I didn't even try to understand certain design decisions.
I
basically followed flow and then cleaned up the code a little later. I
believe the for loop was to play the system alert seven times (ASCII
char
7). I think. Sound effects on a PDP-8 terminal. :)
Ae16cb4f6d78e485b04ce1e821592ae5?d=identicon&s=25 Martin DeMello (Guest)
on 2009-11-04 13:16
(Received via mailing list)
On Tue, Nov 3, 2009 at 1:48 PM, Benoit Daloze <eregontp@gmail.com>
wrote:
> So here is my pastie:
> http://pastie.textmate.org/681199
>
> Still bugged, because I couldn't understand so much the part with planting
> seeds. (and why using the variable dead at that moment?)

he reused some of the variables for two separate things. the seed
planting bit was "calculate a random yield per acre, and see how many
bushels you got after planting so many bushels".

martin
33117162fff8a9cf50544a604f60c045?d=identicon&s=25 Daniel X Moore (yahivin)
on 2010-01-29 06:03
(Received via mailing list)
Attachment: 223.tar.gz (10 KB)
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[1],
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[2] who suggested[3] it. Thank you everyone
for the great solutions and discussion!

Hamurabi (#223) - Solutions[4]

[1]: http://ruby-doc.org/core/classes/StringScanner.html
[2]: http://zem.novylen.net
[3]: http://rubyquiz.strd6.com/suggestions
[4]: http://rubyquiz.strd6.com/quizzes/223.tar.gz
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.