Java Basics
Programming: Hammurabi Game - Soldiers
Add soldiers to this working Kingdom class. You don't need to be concerned with the user interface class (Hammurabi) for this problem. You need to make the following modifications to the Kingdom class.
- Keep track of how many soldiers the Kingdom has.
- Write a "getter" method so the user interface can find out how many there are.
- Update the toString() method so that it reports the number of soldiers.
- Add an extra parameter to the
simulateOneYear
method to indicate how much grain should be used to feed soldiers. - Soldiers eat twice as much as peasants.
- Soldiers don't do any farming (ie, no changes to farming computation).
- For every 10 soldiers, you get an extra acre of land each year because of border disputes.
Make changes to this source code
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
// Purpose: Represents a "kingdom". // Author : Fred Swartz 12 Dec 2005 class Kingdom { //========================================================== constants private final static int MIN_GRAIN_TO_SURVIVE = 20; private final static int LAND_FARMABLE_PER_PERSON = 15; private final static int SEED_REQUIRED_PER_ACRE = 2; //================================================= instance variables private int myGrain = 3000; // Bushels of grain in storage. private int myArea = 1000; // Area of kingdom in acres. private int myYear = 0; // Years since founding of kingdom. private int myHarvest = 0; // Last harvest in bushels. private int myPopulation = 100; // Number of peasants. //=========================================================== getters public int getPopulation() { return myPopulation; } public int getGrain() { return myGrain; } public int getArea() { return myArea; } public int getYear() { return myYear; } public int getRequiredFood() { return myPopulation * MIN_GRAIN_TO_SURVIVE; } //=========================================================== toString public String toString() { return "Kingdom status at year " + myYear + ", last harvest = " + myHarvest + ", peasants = " + myPopulation + ", total grain = " + myGrain; } //==================================================== simulateOneYear public void simulateOneYear(int food, int seed) { //... Reduce grain by amount used for food and seed. myGrain = myGrain - food - seed; //... Compute new population that food can support. int neededFood = myPopulation * MIN_GRAIN_TO_SURVIVE; myPopulation = myPopulation * food / neededFood; //... The amount of land we can plant is the smallest of // (1) the area we have, // (2) the amount we have enough seed for // (3) the amount that people can plant. int seedableArea = seed / SEED_REQUIRED_PER_ACRE; int humanWorkableArea = myPopulation * LAND_FARMABLE_PER_PERSON; int potentiallyPlantable = Math.min(seedableArea, humanWorkableArea); int acresPlanted = Math.min(myArea, potentiallyPlantable); //... Compute random yield because of weather in range 2 to 6 int yieldPerAcre = 2 + (int)(5 * Math.random()); //... Update harvest so it can be reported. myHarvest = yieldPerAcre * acresPlanted; //... Another year has passed myYear++; //... Update the harvest from this years crops. myGrain += myHarvest; } } |