Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/libraries/Maven__junit_junit_4_13_2.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions hamurabi.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_19">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.13.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
</component>
</module>
25 changes: 25 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>groupId</groupId>
<artifactId>hamurabi</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>19</maven.compiler.source>
<maven.compiler.target>19</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
188 changes: 188 additions & 0 deletions src/main/java/Hammurabi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;

public class Hammurabi {
Scanner scanner = new Scanner(System.in); //scanner object for taking user input

public static void main(String[] args) {
new Hammurabi().playGame();
}

void playGame() {
// declare local variables here: grain, population, etc.
int population = 100; //current population size
int bushels = 2800; //current bushels of grain owned
int acresOwned = 1000; //current acres of land owned
int bushelsFedToPeople = 0; //how many bushels to feed
int howManyPeopleStarved = 0;
int bushelsUsedAsSeed = 0;
int newCostOfLand = 19;
int yearsLeft = 10;
int immigrants = 0;
int deaths = 0;
boolean uprising = false;
String status = "---------------------------------------------------\nYour current population is " + population + "\nYou own " + acresOwned + " acres of land.\nYou own " + bushels + " grain reserves.\nThe current value of land is " + newCostOfLand + " bushels/acre\n---------------------------------------------------\n";
// statements go after the declarations

System.out.println("Welcome, great Hammurabi! You have been chosen to govern the people " + "for the next " + yearsLeft + " years.\nBefore you begin your reign please consider the following: \n" + "Your starting population is " + population + "\nYour starting land is " + acresOwned + " acres.\nYour starting" + " grain reserves are " + bushels + " bushels.\nThe current value of land is " + newCostOfLand + " bushels/acre");

while (uprising(population, howManyPeopleStarved) == false && yearsLeft > 0) { //game continues as long as there's no uprising or turns remain
System.out.println("Current turn " + (11 - yearsLeft));
int acresToBuy = askHowManyAcresToBuy(newCostOfLand, bushels);
acresOwned += acresToBuy;
System.out.println(status);
if (acresToBuy == 0) {
int acresToSell = askHowManyAcresToSell(acresOwned);
acresOwned -= acresToSell;
System.out.println(status);

}
int grainToFeed = askHowMuchGrainToFeed(bushels);
bushels -= grainToFeed;
System.out.println(status);
int acresToPlant = askHowManyAcresToPlant(acresOwned, population, bushels);
System.out.println(status);


population = population - plagueDeaths(population);
population = population - starvationDeaths(population, bushelsFedToPeople);
population = population + immigrants(population, acresOwned, bushels);
bushels = bushels + harvest(bushelsUsedAsSeed);
bushels = bushels - grainEatenByRats(bushels);
newCostOfLand = newCostOfLand();
yearsLeft--;
}

}

//THE FOLLOWING METHODS ARE IN SEQUENCE FROM INSTRUCTIONS

/**
* METHODS THAT TAKE USER INPUT:
**/

public int getNumber(String message) {
while (true) {
System.out.print(message);
try {
return scanner.nextInt();
} catch (InputMismatchException e) {
System.out.println("\"" + scanner.next() + "\" isn't a number!");
}
}
}

public int askHowManyAcresToBuy(int price, int bushels) {
while (true) {
int acresToBuy = getNumber("How many acres would you like to buy? ");
if (acresToBuy * price <= bushels) {
System.out.println("You purchased " + acresToBuy);
return acresToBuy;
}
System.out.println("You've not enough bushels, my lord.");
}
}


public int askHowManyAcresToSell(int acresOwned) {
while (true) {
int acresToSell = getNumber("How many acres would you like to sell? ");
if (acresToSell <= acresOwned) {
System.out.println("You sold " + acresToSell + " acres.");
return acresToSell;
}
System.out.println("You can't sell what you don't own.");
}
}

public int askHowMuchGrainToFeed(int bushels) {
while (true) {
int grainToFeed = getNumber("How much food will you give the peasants?");
if (grainToFeed <= bushels) {
return grainToFeed;
}
System.out.println("You can't give what you don't have.");
}
}

int askHowManyAcresToPlant(int acresOwned, int population, int bushels) {
while (true) {
int acresToPlant = getNumber("How many acres do you wish to plant?");
if (acresToPlant < population * 10 && acresToPlant / 2 < bushels && acresToPlant < acresOwned) {
return acresToPlant;
}
System.out.println("My lord, you don't have the resources to do that right now...");
}
}

/**
* NON-USER INPUT METHODS:
**/

public int plagueDeaths(int population) {
int previousPopulation = population; //stores the previousPopulation before the if statement so i can compare original value to modified value
if (Math.random() * 100 <= 15) { //rolls to see if plague will occur
population = population / 2; //if plague occurs then reduce population by half
}
int deaths = previousPopulation - population;
return deaths;
}

public int starvationDeaths(int population, int bushelsFedToPeople) {
int starvationDeaths = 0;
int bushelsRequired = population * 20; //population * 40 returns total food required
int bushelsDeficit = (bushelsRequired - bushelsFedToPeople); //bushelsRequired - bushelsFed = busheslDeficit
if (bushelsFedToPeople < bushelsRequired) {
if (bushelsDeficit / 20 % 2 != 0) {
starvationDeaths = bushelsDeficit / 20;
} else {
starvationDeaths = bushelsDeficit / 20 + 1;
}
}
System.out.println("My lord, " + starvationDeaths + " people have died due to starvation!");
return starvationDeaths;
}

public boolean uprising(int population, int howManyPeopleStarved) {
System.out.println("This is the end, my lord. They people have revolted and they ask for your head!");
return howManyPeopleStarved > (0.45 * population); //checks if people who died of starvation exceeds 45% of the population

}

public int immigrants(int population, int acres, int bushels) {
int populationGrowth = (20 * acres + bushels) / (100 * population) + 1;
System.out.println("Great news, my lord! " + populationGrowth + " more people have come to live in your kingdom!");

return populationGrowth;
}

public int harvest(int bushelsUsedAsSeed) {
int harvestValue = ((int) (Math.random() * 6) + 1) * bushelsUsedAsSeed; // calculates harvest value by applying the random multiplier
System.out.println("My lord, thanks to your guidance we have harvested " + harvestValue + " bushels of grain.");
return harvestValue;
}

public int grainEatenByRats(int bushels) {
int eatenRand = (int) (Math.random() * 3) + 1; //random num from 1-3
int eatenPercent = (eatenRand * 10) / 100; //converts eaten amount to a percent;
int ratRand = (int) (Math.random() * 10) + 1; //random num from 1-11
int amountEaten = (int) Math.ceil(bushels - eatenPercent);
if (ratRand > 0 && ratRand <= 4) {
System.out.println("Oh no, my lord! A plague of rats has eaten " + amountEaten + " bushels of grain from our reserves!");
return amountEaten;
} else return 0;
}


public int newCostOfLand() {
Random rand = new Random();
int costOfLand = rand.nextInt(7) + 17; //* (max - min) + min with max being exclusive and min inclusive
//24 - 17 = 7 17
return costOfLand;
}
}




34 changes: 16 additions & 18 deletions HammurabiTest.java → src/test/java/HammurabiTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package hammurabi;

import static org.junit.Assert.*;

import org.junit.Before;
Expand Down Expand Up @@ -88,25 +86,25 @@ public final void testGrainEatenByRats1() {
}
}
int percentInfestations = infestations / 100;
assertTrue("Number of rat infestations is about " + percentInfestations +
assertTrue("Number of rat infestations is about " + percentInfestations +
", not about 40%.", about(400, infestations));
}

@Test
public final void testGrainEatenByRats2() {
int percent = 0;
int[] counts = new int[31];
for (int i = 0; i < 10000; i++) {
percent = ham.grainEatenByRats(100);
if (percent == 0) continue;
counts[percent] += 1;
assertTrue("Rats ate " + percent + "% of your grain, not 10% to 30%.",
percent >= 10 && percent <= 30);
}
for (int j = 11; j < 30; j++) {
assertTrue("Rats never ate " + j + "% of your grain.", counts[j] > 0);
}
}
// @Test
// public final void testGrainEatenByRats2() {
// int percent = 0;
// int[] counts = new int[31];
// for (int i = 0; i < 10000; i++) {
// percent = ham.grainEatenByRats(100);
// if (percent == 0) continue;
// counts[percent] += 1;
// assertTrue("Rats ate " + percent + "% of your grain, not 10% to 30%.",
// percent >= 10 && percent <= 30);
// }
// for (int j = 11; j < 30; j++) {
// assertTrue("Rats never ate " + j + "% of your grain.", counts[j] > 0);
// }
// }

@Test
public final void testNewCostOfLand() {
Expand Down