Java: Programming: Hammurabi II - Two Player Version
Extend the basic Hammurabi project
This is based on the Hammurabi I program, extending it to two players with a couple of small enhancements. This is intended to be a small assignment.
Start with the solution to the one-player version
You can start this problem either with your solution to Hammurabi I, or use the solution at .
Separate files. Each class is in a separate source file. This is typical, but multiple classes can be in the same source file. If they are in the same source file, there must be only one public class, which will be the basis for the source file name. One class per file is the best strategy.
The Hammurabi class
This class is used to run the simulation. It contains the main method and a utility method to display the results. It creates one Kingdom object which it happens to call samaria, but you might want to change that name.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
// File : hammurabi0/Hammurabi.java // Purpose: Starting point for working on the Hammurabi program. // Author : Fred Swartz // Date : 17 Apr 2005 import javax.swing.*; public class Hammurabi { //========================================================= main public static void main(String[] args) { //... Initialization [TODO: set initial population] Kingdom samaria = new Kingdom(); // Create a new samaria.setGrain(3000); //... Run the simulation for 10 years or until everyone starves. // TODO: while (samaria.getYear() <= 5 /* && samaria.getPopulation() > 0 */) { displayStatus(samaria); //TODO: Ask the Exalted Ruler how much to feed the people. int food = 0; // This is in place of asking for input. String plantStr = JOptionPane.showInputDialog(null, "How much of the remaining " + (samaria.getGrain()-food) + " bushels should we plant?"); int seeds = Integer.parseInt(plantStr); //... Update the food and population of this kingdom. samaria.simulateOneYear(food, seeds); } //... Show final state. displayStatus(samaria); } //==================================================== displayStatus // Shows the amount of grain and population for // and Kingdom. TODO: Add population output. private static void displayStatus(Kingdom country) { JOptionPane.showMessageDialog(null, "Year = " + country.getYear() + "\nGrain = " + country.getGrain()); } } |
The Kingdom class
This class represents a "kingdom". There are two portions to a class:
the data and the methods. Currently, the only data (instance variables)
in this class are the year and the amount of grain in storage.
The population must be added. This also has some methods - some "getters"
and "setters" to get and set the instance variables.
The simulateOneYear
method, which takes parameters which
tell how much grain to use to plant then crop, and how much to feed
the population. Getter and setter methods have to be added to
work with the population, and the simulateOneYear
method
must be extended to update the population.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
// File : hammurabi0/Kingdom.java // Purpose: Represents a "kingdom" // This version uses only year and grain. // TODO: Add population value and simulation. // Author : Fred Swartz // Date : 17 Apr 2005 class Kingdom { //==================================================== Constants public static int SEED_REQUIRED_PER_ACRE = 2; //=========================================== Instance variables. private int myGrain; private int myYear = 1; //===================================================== setGrain public void setGrain(int grain) { myGrain = grain; } //===================================================== getGrain public int getGrain() { return myGrain; } //====================================================== getYear public int getYear() { return myYear; } //============================================== simulateOneYear public void simulateOneYear(int food, int seed) { myYear++; //TODO: Need to calculate new population.based on food. //... Reduce grain stockpile by amount used for food and seed myGrain = myGrain - food - seed; //... Calculate new harvest // 1. How many acres can be planted with seed. // 2. The yield per acre is random (2-6) // 3. Harvest is number area planted * yield. int acresPlanted = seed / SEED_REQUIRED_PER_ACRE; int yieldPerAcre = 2 + (int)(5 * Math.random()); int harvest = yieldPerAcre * acresPlanted; myGrain += harvest; // New amount of grain in storage. } } |