CIT
591 Assignment 2: HammurabiFall 2004, David Matuszek |

- To give you practice with simple arithmetic and control structures.
- To give you a more interesting program than the last one.

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:

Each person needs at least 20 bushels of grain per year to surviveEach person can farm at most 10 acres of landIt takes 2 bushels of grain to farm an acre of landThe market price for land fluctuates yearly

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

- 100 people
- 2800 bushels of grain in storage
- 1000 acres of land
- Land value is 19 bushels/acre

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

The above summary represents the initial state, at theO 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.

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

Ask the player for:

How many acres of land to buyHow many acres of land to sell

- If the player is buying land, you can skip this question
How much grain to feed to the peopleHow many acres to plant with seedFor 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:

Do these computations,

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) + 1How 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.
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.

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) {`

...do something...}

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?");`

http://www.nicholson.com/rhn/basic/classic_programs/hanab.bas is a BASIC version.

http://www.crazyloon.com/games/hamurabi.htm
is a somewhat easier-to-read JavaScript version (use `View -> Source`

).

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.