Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Hammurabi

4.00/5 (5 votes)
19 Feb 2016CPOL3 min read 1   369  
A C# port of the classic Hammurabi game

Introduction

Michael Birken's Star Trek article rekindled my interest in classic BASIC games.

As a kid, I spent many hours typing in programs from magazines such as Antic, Analog, and Compute!, as well as from various books such as David Ahl's BASIC Computer Games (1978) and More BASIC Computer Games (1979). After typing in a program and getting it to successfully run, the next step would be to make changes, modify code, add features, etc.

Hammurabi was originally written as 'The Sumer Game' (1969) in FOCAL on a DEC PDP-8 computer, by Richard Merrill. Sometime later, it was ported to PDP-8 BASIC. David Ahl ported this version of Hammurabi to BASIC, and added the 10-year performance assessment.

Back in the day, I also played Atari's Kingdom game program which came on cassette. After looking at the BASIC listing for Hammurabi, Atari's Kingdom seems to be a direct port of Hammurabi, modified to use the Atari screen capabilities.

Porting

I started out with an Atari 400 home computer back in 1982. At the time, I sometimes found it a challenge to get generic BASIC programs such as the programs in BASIC Computer Games and More BASIC Computer Games to work correctly. Atari BASIC handled STRING arrays differently than other 8-bit machines of that era. Atari BASIC was also missing FNA(), FNB(), FNC() types of statements which had to be converted to subroutines. Fortunately, Hammurabi did not require any of these changes.

As Michael Birken's article points out, it is fairly easy to port BASIC programs to C#. To Birkenize a BASIC listing, follow these steps:

For example:

VB
551 REM *** STARVE ENOUGH FOR IMPEACHMENT?
552 D=P-C:IF D>.45*P THEN 560
553 P1=((Z-1)*P1+D*100/P)/Z
555 P=C:D1=D1+D:GOTO 215

becomes:

C#
_551: ; // REM *** STARVE ENOUGH FOR IMPEACHMENT?
_552: D = P - C; if (D > .45 * P) goto _560;
_553: P1 = ((Z - 1) * P1 + D * 100 / P) / Z;
_555: P = C; D1 = D1 + D; goto _215;

For example:

VB
229 PRINT "A HORRIBLE PLAGUE STRUCK!  HALF THE PEOPLE DIED."
230 PRINT "POPULATION IS NOW"P 
232 PRINT "THE CITY NOW OWNS"A"ACRES."
235 PRINT "YOU HARVESTED"Y"BUSHELS PER ACRE."
250 PRINT "RATS ATE"E"BUSHELS."
260 PRINT "YOU NOW HAVE"S"BUSHELS IN STORE.":PRINT

becomes:

C#
_229: Console.WriteLine("A HORRIBLE PLAGUE STRUCK!  HALF THE PEOPLE DIED.");
_230: Console.WriteLine("POPULATION IS NOW " + P.ToString());
_232: Console.WriteLine("THE CITY NOW OWNS " + A.ToString() + " ACRES.");
_235: Console.WriteLine("YOU HARVESTED " + Y.ToString() + " BUSHELS PER ACRE.");
_250: Console.WriteLine("RATS ATE " + E.ToString() + " BUSHELS.");
_260: Console.WriteLine("YOU NOW HAVE " + S.ToString() + 
                        " BUSHELS IN STORE."); Console.WriteLine();
  • Change BASIC line numbers to C# labels:
  • Change BASIC GOSUB statements to method calls.
  • Change BASIC PRINT statements to Console.Write[Line] method calls.
  • As C# uses zero-based indexing and BASIC uses one-based indexing, for array structures, either modify the data to add an unused zeroth element datum, or modify the code to subtract one from the existing indexing logic.
  • Review remaining statements; modify code, variables, and logic to compile in C#.

Playing

In the game of Hammurabi, you direct the administrator of Sumeria, Hammurabi, how to manage the city. At the start of the game, Sumeria initially has 1,000 acres of land, a population of 100 people, and 3,000 bushels of grain in storage.

You may buy and sell land with your neighboring city-states for bushels of grain. The price of land will vary between 17 and 26 bushels per acre. You also must use grain to feed your people and as seed to plant the next year's crop.

You will quickly find that a certain number of people can only tend a certain amount of land, and that people starve if they are not fed enough. You also have the unexpected to contend with such as a plague, rats destroying stored grain, and variable harvests.

You will also find that managing just the few resources in this game is not a trivial job over a period, of say, ten years. The crisis of population density rears its head very rapidly.

References

History

  • 19th February, 2016: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)