CIT 591 Assignment 2: Hammurabi
Fall 2004, David Matuszek

Purposes of this assignment:

General idea of the assignment:

Hammurabi is a very old computer game. Your job is to bring it into the 21st century by writing it in Java.

Here are the standard instructions for the game:

Congratulations, you are the newest ruler of ancient Samaria, elected for a ten year term of office. Your duties are to dispense food, direct farming, and buy and sell land as needed to support your people. Watch out for rat infestiations and the plague! Grain is the general currency, measured in bushels. The following will help you in your decisions:

Rule wisely and you will be showered with appreciation at the end of your term. Rule poorly and you will be kicked out of office!


Initially, you will start out with

Each year, print out a summary similar to the following:

O great Hammurabi!
You are in year 1 of your ten year rule.
In the previous year 0 people starved to death.
In the previous year 5 people entered the kingdom.
The population is now 100.
We harvested 3000 bushels at 3 bushels per acre.
Rats destroyed 200 bushels, leaving 2800 bushels in storage.
The city owns 1000 acres of land.
Land is currently worth 19 bushels per acre.
The above summary represents the initial state, at the beginning of the first year--that is, when you first take office, and before you do any of the computations below). So, for example, the previous year (under a different ruler) must have started with 95 people; none starved, and 5 entered the kingdom, so as you enter office you rule 100 people.

Here's what you need to do each year, for 10 years:

Ask the player for:

For each question, you need to test whether the answer is possible (you have enough grain, you have enough people to do the planting), and keep asking until you get a possible value. (The jargon term for this is "sanity checks.")

Then you need to determine:

If there is a plague
Each year, there is a 15% chance of a horrible plague. When this happens, half your people die. Print a message if this happens.
How many people starved
Each person needs 20 bushels of grain to survive. If you feed them more than this, they are happy, but the grain is still gone. If more than 45% of the people starve, you are immediately thrown out of office (print an appropriate message) and the game ends.
How many people came to the city
Nobody will come to the city if people are starving. If everyone is well fed, compute how many people come to the city as: (20 * number of acres you have + amount of grain you have in storage) / (100 * population) + 1.
How good the harvest is
Choose a random number between 1 and 5. Each acre that was planted with seed will yield this many bushels of grain. (Example: if you planted 50 acres, and your number is 3, you harvest 150 bushels of grain).
If you have a problem with rats
There is a 40% chance that you will have a rat infestation. When this happens, rats will eat somewhere between 1/10 and 3/10 of your grain. Print a message when this happens.
How much land costs (for next year)
The price of land is random, and ranges from 17 to 23 bushels per acre. The player will need this information in order to buy or sell land.
Do these computations, in this order, for each of ten years.

At the end, you should print out a final summary, an tell the player how good a job he/she did. I'll leave the details up to you, but the usual evaluation is based on how many people starved, and how many acres per person you end up with.

All the required arithmetic in this program should be integer. You do not need floating-point numbers.

I suggest that you first get the simplest possible version of the program running and tested out (that is, leave out things like rats, plagues, and immigration), then add these things one at a time to a working program, testing as you go.

Things you need to know:

Random numbers

To get a random number generator, you first need to import java.util.Random (or java.util.*). Then, you can create a random number generator with a statement such as:

static Random rand = new Random();

To get a new random number in the range 0..n-1, where n is an int, call rand.nextInt(n). You can use this call in statements such as

myNewNumber = rand.nextInt(5);
if (rand.nextInt(100) < 15) { ... }

To do something that happens p percent of the time, use

if (rand.nextInt(100) < p) { something... }

Getting input

You will need to import javax.swing.JOptionPane (or javax.swing.*).

To get a number from the player, add this method to your program:

    static int getNumber(String message) {
        try {
            String input = JOptionPane.showInputDialog(message);
            return new Integer(input).intValue();
        catch (Exception e) {
            return getNumber("That's not a number! Try again!");

Here is an example of how you can use this method:

sell = getNumber("How many acres shall you sell?");

References: is a BASIC version. is a somewhat easier-to-read JavaScript version (use View -> Source).

Due date:

Thursday, Sept. 30, before midnight. Turn in your zipped assignment via Blackboard -- see Instructions for Using Zip Files and Blackboard for instructions. Your program should be ready to run under BlueJ, so include the files that BlueJ generates, along with your .java and .class files. Be sure to Submit File, not Add File! Programs submitted any other way will not be accepted.

Just turn in one program for the two of you, please; whoever turns it in, please put your partner's name in the Blackboard comments field.