From e1901176c5cd9d6ea6d16544017f384e97a4c004 Mon Sep 17 00:00:00 2001 From: Jake Lawhorne Date: Sat, 18 Mar 2023 19:16:38 -0400 Subject: [PATCH 1/2] Large amounts of lab completed, no tests performed --- .idea/.gitignore | 3 + .idea/misc.xml | 13 + .idea/modules.xml | 8 + .idea/uiDesigner.xml | 124 ++++++++ .idea/vcs.xml | 6 + HammurabiTest.java | 125 -------- README.md | 5 +- docs/hamurabi.java | 292 ++++++++--------- docs/matuszek/HammurabiTest.java | 125 -------- hamurabi.iml | 21 ++ out/production/hamurabi/hammurabi/.gitignore | 23 ++ .../hamurabi/hammurabi/.idea/.gitignore | 3 + .../hamurabi/hammurabi/.idea/misc.xml | 13 + .../hamurabi/hammurabi/.idea/modules.xml | 8 + .../hamurabi/hammurabi/.idea/uiDesigner.xml | 124 ++++++++ .../hamurabi/hammurabi/.idea/vcs.xml | 6 + out/production/hamurabi/hammurabi/LICENSE | 21 ++ out/production/hamurabi/hammurabi/README.md | 229 ++++++++++++++ .../hammurabi/docs/matuszek/06-hammurabi.html | 299 ++++++++++++++++++ .../hammurabi/docs/matuszek/README..md | 6 + .../hamurabi/hammurabi/hamurabi.iml | 21 ++ out/production/hamurabi/hammurabi/pom.xml | 12 + pom.xml | 12 + src/main/java/Hammurabi.java | 276 ++++++++++++++++ src/test/java/HammurabiTest.java | 126 ++++++++ 25 files changed, 1503 insertions(+), 398 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/uiDesigner.xml create mode 100644 .idea/vcs.xml delete mode 100644 HammurabiTest.java delete mode 100644 docs/matuszek/HammurabiTest.java create mode 100644 hamurabi.iml create mode 100644 out/production/hamurabi/hammurabi/.gitignore create mode 100644 out/production/hamurabi/hammurabi/.idea/.gitignore create mode 100644 out/production/hamurabi/hammurabi/.idea/misc.xml create mode 100644 out/production/hamurabi/hammurabi/.idea/modules.xml create mode 100644 out/production/hamurabi/hammurabi/.idea/uiDesigner.xml create mode 100644 out/production/hamurabi/hammurabi/.idea/vcs.xml create mode 100644 out/production/hamurabi/hammurabi/LICENSE create mode 100644 out/production/hamurabi/hammurabi/README.md create mode 100644 out/production/hamurabi/hammurabi/docs/matuszek/06-hammurabi.html create mode 100644 out/production/hamurabi/hammurabi/docs/matuszek/README..md create mode 100644 out/production/hamurabi/hammurabi/hamurabi.iml create mode 100644 out/production/hamurabi/hammurabi/pom.xml create mode 100644 pom.xml create mode 100644 src/main/java/Hammurabi.java create mode 100644 src/test/java/HammurabiTest.java diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..d460235 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,13 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..57e16c0 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/HammurabiTest.java b/HammurabiTest.java deleted file mode 100644 index 5cf1cc9..0000000 --- a/HammurabiTest.java +++ /dev/null @@ -1,125 +0,0 @@ -package hammurabi; - -import static org.junit.Assert.*; - -import org.junit.Before; -import org.junit.Test; - -public class HammurabiTest { - - Hammurabi ham; - - boolean about(double expected, double actual) { - return actual > 0.90 * expected && actual < 1.10 * expected; - } - - @Before - public void setUp() throws Exception { - ham = new Hammurabi(); - } - - @Test - public final void testPlagueDeaths1() { - int number_of_plagues = 0; - for (int i = 0; i < 10000; i++) { - int deaths = ham.plagueDeaths(100); - if (deaths > 0) { - number_of_plagues += 1; - } - } - int percentPlagues = number_of_plagues / 100; - assertTrue("Number of plagues is about " + percentPlagues + ", not about 15%.", - about(1500, number_of_plagues)); - } - - @Test - public final void testPlagueDeaths2() { - int deaths = 0; - for (int i = 0; i < 10000; i++) { - deaths = ham.plagueDeaths(100); - if (deaths > 0) break; - } - assertEquals("In a plague, " + deaths + "% of your people die, not 50%.", - 50, deaths); - } - - @Test - public final void testStarvationDeaths() { - int deaths = ham.starvationDeaths(100, 1639); - assertEquals("Wrong number of starvations deaths.", 19, deaths); - deaths = ham.starvationDeaths(100, 2500); - if (deaths < 0) { - fail("You starved a negative number of people!"); - } - } - - @Test - public final void testUprising() { - assertTrue("Should have had an uprising!", ham.uprising(1000, 451)); - assertFalse("Should not have had an uprising!", ham.uprising(1000, 449)); - } - - @Test - public final void testImmigrants() { - int imm = ham.immigrants(10, 1200, 500); - assertEquals("Wrong number of immigrants.", 25, imm); - } - - @Test - public final void testHarvest() { - int[] yield = new int[7]; - for (int i = 0; i < 1000; i++) { - int harvest = ham.harvest(1); - assertTrue("Illegal harvest per acre: " + harvest, harvest > 0 && harvest <= 6); - yield[harvest] += 1; - } - for (int j = 1; j <= 6; j++) { - assertTrue("You never have a yield of " + j + " bushels per acre.", yield[j] > 0); - } - } - - @Test - public final void testGrainEatenByRats1() { - int infestations = 0; - for (int i = 0; i < 1000; i++) { - int eaten = ham.grainEatenByRats(100); - if (eaten > 0) { - infestations += 1; - } - } - int percentInfestations = infestations / 100; - 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 testNewCostOfLand() { - int[] cost = new int[24]; - for (int i = 0; i < 1000; i++) { - int price = ham.newCostOfLand(); - assertTrue("Illegal cost of land: " + price, price >= 17 && price <= 23); - cost[price] += 1; - } - for (int j = 17; j <= 23; j++) { - assertTrue("You never have a land cost of " + j + " bushels per acre.", cost[j] > 0); - } - } - -} - diff --git a/README.md b/README.md index e4672c9..2b38cf1 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ Define a single class named `Hammurabi`. Use the following skeleton (but leave o ```java package hammurabi; // package declaration + import java.util.Random; // imports go here import java.util.Scanner; @@ -33,8 +34,8 @@ public class Hammurabi { // must save in a file named Hammurabi.java Random rand = new Random(); // this is an instance variable Scanner scanner = new Scanner(System.in); - public static void main(String\[\] args) { // required in every Java program - new Hammurabi().playGame(); + public static void main(String\[\]args) { // required in every Java program + new hammurabi.src.main.java.Hammurabi().playGame(); } void playGame() { diff --git a/docs/hamurabi.java b/docs/hamurabi.java index 9947af3..c032f5e 100644 --- a/docs/hamurabi.java +++ b/docs/hamurabi.java @@ -8,149 +8,149 @@ // I'm serious. // (how the hell would you ever be able to TEST this piece of code?) // -public class HAMURABI { - static int totalDeaths = 0, percentDied = 0, year = 0, population = 95, stores = 2800, immigrants = 5, deaths, - harvest = 3000, yeild = 3, acres = harvest / yeild, eaten = harvest - stores, landPrice, fullPeople, temp; - static boolean plague = false; - final static String FINK = "DUE TO THIS EXTREME MISMANAGEMENT YOU HAVE NOT ONLY\n" + - "BEEN IMPEACHED AND THROWN OUT OF OFFICE BUT YOU HAVE\n" + - "ALSO BEEN DECLARED PERSONA NON GRATA!!\n"; - Scanner input = new Scanner(System.in); - - private void newYear() { - year += 1; - population += immigrants; - landPrice = (int) (10 * Math.random() + 17); - System.out.println(report()); - do { - System.out.print("HOW MANY ACRES DO YOU WISH TO BUY? "); - temp = input.nextInt(); - if (temp < 0) - epicFail(0); - if (temp * landPrice > stores) - System.out.println("HAMURABI: THINK AGAIN. YOU HAVE ONLY\n" + - stores + " BUSHELS OF GRAIN. NOW THEN,"); - } while (temp * landPrice > stores); - acres += temp; - stores -= temp * landPrice; - do { - System.out.print("HOW MANY ACRES DO YOU WISH TO SELL? "); - temp = input.nextInt(); - if (temp < 0) - epicFail(0); - if (temp > acres) - System.out.println("HAMURABI: THINK AGAIN. YOU OWN ONLY " + acres + " ACRES. NOW THEN,"); - } while (temp > acres); - stores += temp * landPrice; - acres -= temp; - do { - System.out.print("\nHOW MANY BUSHELS DO YOU WISH TO FEED YOUR PEOPLE? "); - temp = input.nextInt(); - if (temp < 0) - epicFail(0); - if (temp > stores) - System.out.println("HAMURABI: THINK AGAIN. YOU HAVE ONLY\n" + - stores + " BUSHELS OF GRAIN. NOW THEN,"); - } while (temp > stores); - fullPeople = temp / 20; - stores -= temp; - do { - System.out.print("\nHOW MANY ACRES DO YOU WISH TO PLANT WITH SEED? "); - temp = input.nextInt(); - if (temp < 0) - epicFail(0); - if (temp > acres) - System.out.println("HAMURABI: THINK AGAIN. YOU OWN ONLY " + acres + " ACRES. NOW THEN,"); - if (temp / 2 > stores) - System.out.println("HAMURABI: THINK AGAIN. YOU HAVE ONLY\n" + - stores + " BUSHELS OF GRAIN. NOW THEN,"); - if (temp > population * 10) - System.out.println("BUT YOU HAVE ONLY" + population + "PEOPLE TO TEND THE FIELDS. NOW THEN,"); - } while (temp > acres || temp / 2 > stores || temp > population * 10); - stores -= temp / 2; - yeild = (int) (Math.random() * 5 + 1); - harvest = temp * yeild; - temp = (int) (Math.random() * 5 + 1); - if (temp % 2 != 1) - eaten = (stores / temp); - else - eaten = 0; - stores += (harvest - eaten); - immigrants = (int) (Math.random() * 5 + 1) * - (20 * acres + stores) / population / 100 + 1; - if (population > fullPeople) { - deaths = population - fullPeople; - if (deaths > .45 * population) - epicFail(1); - percentDied = ((year - 1) * percentDied + deaths * 100 / population) / year; - population = fullPeople; - totalDeaths += deaths; - } - if (20 * Math.random() >= 17) - plague = true; - plague = false; - } - - private static String report() { - String answer = "\nHAMURABI: I BEG TO REPORT TO YOU,\n" + - "IN YEAR " + year + ", " + deaths + " PEOPLE STARVED, " + immigrants + " CAME TO THE CITY.\n"; - if (plague) { - population = population / 2; - answer += "A HORRIBLE PLAGUE STRUCK! HALF THE PEOPLE DIED.\n"; - } - answer += "POPULATION IS NOW " + population + ".\n" + - "THE CITY NOW OWNS " + acres + " ACRES.\n" + - "YOU HARVESTED " + yeild + " BUSHELS PER ACRE.\n" + - "RATS ATE " + eaten + " BUSHELS.\n" + - "YOU NOW HAVE " + stores + " BUSHELS IN STORE\n\n" + - "LAND IS TRADING AT " + landPrice + " BUSHELS PER ACRE."; - return answer; - } - - - private static void epicFail(int x) { - String reason = ""; - switch (x) { - case 0: reason = "HAMURABI: I CANNOT DO WHAT YOU WISH.\n" + - "GET YOURSELF ANOTHER STEWARD!!!!!"; break; - case 1: reason = "YOU STARVED " + deaths + " PEOPLE IN ONE YEAR!!!\n" + - FINK; break; - } - System.out.println(reason); - System.exit(0); - } - - private void finished() { - String answer = "IN YOUR 10-YEAR TERM OF OFFICE, " + percentDied + " PERCENT OF THE\n" + - "POPULATION STARVED PER YEAR ON AVERAGE, I.E., A TOTAL OF\n" + - totalDeaths + " PEOPLE DIED!!\n" + - "YOU STARTED WITH 10 ACRES PER PERSON AND ENDED WITH\n" + - acres / population + " ACRES PER PERSON\n\n"; - if (percentDied > 33 || acres / population < 7) - answer += FINK; - else if (percentDied > 10 || acres / population < 9) - answer += "YOUR HEAVY-HANDED PERFORMANCE SMACKS OF NERO AND IVAN IV.\n" + - "THE PEOPLE (REMAINING) FIND YOU AN UNPLEASANT RULER, AND,\n" + - "FRANKLY, HATE YOUR GUTS!"; - else if (percentDied > 3 || acres / population < 10) - answer += "YOUR PERFORMANCE COULD HAVE BEEN SOMEWHAT BETTER, BUT\n" + - "REALLY WASN'T TOO BAD AT ALL.\n" + - Math.random() * population * .8 + " PEOPLE WOULD" + - "DEARLY LIKE TO SEE YOU ASSASSINATED BUT WE ALL HAVE OUR" + - "TRIVIAL PROBLEMS"; - else - answer += "A FANTASTIC PERFORMANCE!!! CHARLEMANGE, DISRAELI, AND\n" + - "JEFFERSON COMBINED COULD NOT HAVE DONE BETTER!"; - answer += "\n\n\n\n\n\n\n\n\n\nSo long for now."; - System.out.println(answer); - } - - public static void main(String[] args) throws IOException { - HAMURABI a = new HAMURABI(); - System.out.println("\t\t\t\tHAMURABI\n\t CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n" + - "TRY YOUR HAND AT GOVERNING ANCIENT SUMERIA\nSUCCESSFULLY FOR A TEN-YEAR TERM OF OFFICE."); - while (year < 10) - a.newYear(); - a.finished(); - } -} \ No newline at end of file +//public class HAMURABI { +// static int totalDeaths = 0, percentDied = 0, year = 0, population = 95, stores = 2800, immigrants = 5, deaths, +// harvest = 3000, yeild = 3, acres = harvest / yeild, eaten = harvest - stores, landPrice, fullPeople, temp; +// static boolean plague = false; +// final static String FINK = "DUE TO THIS EXTREME MISMANAGEMENT YOU HAVE NOT ONLY\n" + +// "BEEN IMPEACHED AND THROWN OUT OF OFFICE BUT YOU HAVE\n" + +// "ALSO BEEN DECLARED PERSONA NON GRATA!!\n"; +// Scanner input = new Scanner(System.in); +// +// private void newYear() { +// year += 1; +// population += immigrants; +// landPrice = (int) (10 * Math.random() + 17); +// System.out.println(report()); +// do { +// System.out.print("HOW MANY ACRES DO YOU WISH TO BUY? "); +// temp = input.nextInt(); +// if (temp < 0) +// epicFail(0); +// if (temp * landPrice > stores) +// System.out.println("HAMURABI: THINK AGAIN. YOU HAVE ONLY\n" + +// stores + " BUSHELS OF GRAIN. NOW THEN,"); +// } while (temp * landPrice > stores); +// acres += temp; +// stores -= temp * landPrice; +// do { +// System.out.print("HOW MANY ACRES DO YOU WISH TO SELL? "); +// temp = input.nextInt(); +// if (temp < 0) +// epicFail(0); +// if (temp > acres) +// System.out.println("HAMURABI: THINK AGAIN. YOU OWN ONLY " + acres + " ACRES. NOW THEN,"); +// } while (temp > acres); +// stores += temp * landPrice; +// acres -= temp; +// do { +// System.out.print("\nHOW MANY BUSHELS DO YOU WISH TO FEED YOUR PEOPLE? "); +// temp = input.nextInt(); +// if (temp < 0) +// epicFail(0); +// if (temp > stores) +// System.out.println("HAMURABI: THINK AGAIN. YOU HAVE ONLY\n" + +// stores + " BUSHELS OF GRAIN. NOW THEN,"); +// } while (temp > stores); +// fullPeople = temp / 20; +// stores -= temp; +// do { +// System.out.print("\nHOW MANY ACRES DO YOU WISH TO PLANT WITH SEED? "); +// temp = input.nextInt(); +// if (temp < 0) +// epicFail(0); +// if (temp > acres) +// System.out.println("HAMURABI: THINK AGAIN. YOU OWN ONLY " + acres + " ACRES. NOW THEN,"); +// if (temp / 2 > stores) +// System.out.println("HAMURABI: THINK AGAIN. YOU HAVE ONLY\n" + +// stores + " BUSHELS OF GRAIN. NOW THEN,"); +// if (temp > population * 10) +// System.out.println("BUT YOU HAVE ONLY" + population + "PEOPLE TO TEND THE FIELDS. NOW THEN,"); +// } while (temp > acres || temp / 2 > stores || temp > population * 10); +// stores -= temp / 2; +// yeild = (int) (Math.random() * 5 + 1); +// harvest = temp * yeild; +// temp = (int) (Math.random() * 5 + 1); +// if (temp % 2 != 1) +// eaten = (stores / temp); +// else +// eaten = 0; +// stores += (harvest - eaten); +// immigrants = (int) (Math.random() * 5 + 1) * +// (20 * acres + stores) / population / 100 + 1; +// if (population > fullPeople) { +// deaths = population - fullPeople; +// if (deaths > .45 * population) +// epicFail(1); +// percentDied = ((year - 1) * percentDied + deaths * 100 / population) / year; +// population = fullPeople; +// totalDeaths += deaths; +// } +// if (20 * Math.random() >= 17) +// plague = true; +// plague = false; +// } +// +// private static String report() { +// String answer = "\nHAMURABI: I BEG TO REPORT TO YOU,\n" + +// "IN YEAR " + year + ", " + deaths + " PEOPLE STARVED, " + immigrants + " CAME TO THE CITY.\n"; +// if (plague) { +// population = population / 2; +// answer += "A HORRIBLE PLAGUE STRUCK! HALF THE PEOPLE DIED.\n"; +// } +// answer += "POPULATION IS NOW " + population + ".\n" + +// "THE CITY NOW OWNS " + acres + " ACRES.\n" + +// "YOU HARVESTED " + yeild + " BUSHELS PER ACRE.\n" + +// "RATS ATE " + eaten + " BUSHELS.\n" + +// "YOU NOW HAVE " + stores + " BUSHELS IN STORE\n\n" + +// "LAND IS TRADING AT " + landPrice + " BUSHELS PER ACRE."; +// return answer; +// } +// +// +// private static void epicFail(int x) { +// String reason = ""; +// switch (x) { +// case 0: reason = "HAMURABI: I CANNOT DO WHAT YOU WISH.\n" + +// "GET YOURSELF ANOTHER STEWARD!!!!!"; break; +// case 1: reason = "YOU STARVED " + deaths + " PEOPLE IN ONE YEAR!!!\n" + +// FINK; break; +// } +// System.out.println(reason); +// System.exit(0); +// } +// +// private void finished() { +// String answer = "IN YOUR 10-YEAR TERM OF OFFICE, " + percentDied + " PERCENT OF THE\n" + +// "POPULATION STARVED PER YEAR ON AVERAGE, I.E., A TOTAL OF\n" + +// totalDeaths + " PEOPLE DIED!!\n" + +// "YOU STARTED WITH 10 ACRES PER PERSON AND ENDED WITH\n" + +// acres / population + " ACRES PER PERSON\n\n"; +// if (percentDied > 33 || acres / population < 7) +// answer += FINK; +// else if (percentDied > 10 || acres / population < 9) +// answer += "YOUR HEAVY-HANDED PERFORMANCE SMACKS OF NERO AND IVAN IV.\n" + +// "THE PEOPLE (REMAINING) FIND YOU AN UNPLEASANT RULER, AND,\n" + +// "FRANKLY, HATE YOUR GUTS!"; +// else if (percentDied > 3 || acres / population < 10) +// answer += "YOUR PERFORMANCE COULD HAVE BEEN SOMEWHAT BETTER, BUT\n" + +// "REALLY WASN'T TOO BAD AT ALL.\n" + +// Math.random() * population * .8 + " PEOPLE WOULD" + +// "DEARLY LIKE TO SEE YOU ASSASSINATED BUT WE ALL HAVE OUR" + +// "TRIVIAL PROBLEMS"; +// else +// answer += "A FANTASTIC PERFORMANCE!!! CHARLEMANGE, DISRAELI, AND\n" + +// "JEFFERSON COMBINED COULD NOT HAVE DONE BETTER!"; +// answer += "\n\n\n\n\n\n\n\n\n\nSo long for now."; +// System.out.println(answer); +// } +// +// public static void main(String[] args) throws IOException { +// HAMURABI a = new HAMURABI(); +// System.out.println("\t\t\t\tHAMURABI\n\t CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n" + +// "TRY YOUR HAND AT GOVERNING ANCIENT SUMERIA\nSUCCESSFULLY FOR A TEN-YEAR TERM OF OFFICE."); +// while (year < 10) +// a.newYear(); +// a.finished(); +// } +//} \ No newline at end of file diff --git a/docs/matuszek/HammurabiTest.java b/docs/matuszek/HammurabiTest.java deleted file mode 100644 index 5cf1cc9..0000000 --- a/docs/matuszek/HammurabiTest.java +++ /dev/null @@ -1,125 +0,0 @@ -package hammurabi; - -import static org.junit.Assert.*; - -import org.junit.Before; -import org.junit.Test; - -public class HammurabiTest { - - Hammurabi ham; - - boolean about(double expected, double actual) { - return actual > 0.90 * expected && actual < 1.10 * expected; - } - - @Before - public void setUp() throws Exception { - ham = new Hammurabi(); - } - - @Test - public final void testPlagueDeaths1() { - int number_of_plagues = 0; - for (int i = 0; i < 10000; i++) { - int deaths = ham.plagueDeaths(100); - if (deaths > 0) { - number_of_plagues += 1; - } - } - int percentPlagues = number_of_plagues / 100; - assertTrue("Number of plagues is about " + percentPlagues + ", not about 15%.", - about(1500, number_of_plagues)); - } - - @Test - public final void testPlagueDeaths2() { - int deaths = 0; - for (int i = 0; i < 10000; i++) { - deaths = ham.plagueDeaths(100); - if (deaths > 0) break; - } - assertEquals("In a plague, " + deaths + "% of your people die, not 50%.", - 50, deaths); - } - - @Test - public final void testStarvationDeaths() { - int deaths = ham.starvationDeaths(100, 1639); - assertEquals("Wrong number of starvations deaths.", 19, deaths); - deaths = ham.starvationDeaths(100, 2500); - if (deaths < 0) { - fail("You starved a negative number of people!"); - } - } - - @Test - public final void testUprising() { - assertTrue("Should have had an uprising!", ham.uprising(1000, 451)); - assertFalse("Should not have had an uprising!", ham.uprising(1000, 449)); - } - - @Test - public final void testImmigrants() { - int imm = ham.immigrants(10, 1200, 500); - assertEquals("Wrong number of immigrants.", 25, imm); - } - - @Test - public final void testHarvest() { - int[] yield = new int[7]; - for (int i = 0; i < 1000; i++) { - int harvest = ham.harvest(1); - assertTrue("Illegal harvest per acre: " + harvest, harvest > 0 && harvest <= 6); - yield[harvest] += 1; - } - for (int j = 1; j <= 6; j++) { - assertTrue("You never have a yield of " + j + " bushels per acre.", yield[j] > 0); - } - } - - @Test - public final void testGrainEatenByRats1() { - int infestations = 0; - for (int i = 0; i < 1000; i++) { - int eaten = ham.grainEatenByRats(100); - if (eaten > 0) { - infestations += 1; - } - } - int percentInfestations = infestations / 100; - 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 testNewCostOfLand() { - int[] cost = new int[24]; - for (int i = 0; i < 1000; i++) { - int price = ham.newCostOfLand(); - assertTrue("Illegal cost of land: " + price, price >= 17 && price <= 23); - cost[price] += 1; - } - for (int j = 17; j <= 23; j++) { - assertTrue("You never have a land cost of " + j + " bushels per acre.", cost[j] > 0); - } - } - -} - diff --git a/hamurabi.iml b/hamurabi.iml new file mode 100644 index 0000000..7801486 --- /dev/null +++ b/hamurabi.iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/hamurabi/hammurabi/.gitignore b/out/production/hamurabi/hammurabi/.gitignore new file mode 100644 index 0000000..a1c2a23 --- /dev/null +++ b/out/production/hamurabi/hammurabi/.gitignore @@ -0,0 +1,23 @@ +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* diff --git a/out/production/hamurabi/hammurabi/.idea/.gitignore b/out/production/hamurabi/hammurabi/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/out/production/hamurabi/hammurabi/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/out/production/hamurabi/hammurabi/.idea/misc.xml b/out/production/hamurabi/hammurabi/.idea/misc.xml new file mode 100644 index 0000000..d460235 --- /dev/null +++ b/out/production/hamurabi/hammurabi/.idea/misc.xml @@ -0,0 +1,13 @@ + + + + + + + + + \ No newline at end of file diff --git a/out/production/hamurabi/hammurabi/.idea/modules.xml b/out/production/hamurabi/hammurabi/.idea/modules.xml new file mode 100644 index 0000000..57e16c0 --- /dev/null +++ b/out/production/hamurabi/hammurabi/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/out/production/hamurabi/hammurabi/.idea/uiDesigner.xml b/out/production/hamurabi/hammurabi/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/out/production/hamurabi/hammurabi/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/hamurabi/hammurabi/.idea/vcs.xml b/out/production/hamurabi/hammurabi/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/out/production/hamurabi/hammurabi/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/out/production/hamurabi/hammurabi/LICENSE b/out/production/hamurabi/hammurabi/LICENSE new file mode 100644 index 0000000..22474e4 --- /dev/null +++ b/out/production/hamurabi/hammurabi/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 Zip Code Wilmington Core + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/out/production/hamurabi/hammurabi/README.md b/out/production/hamurabi/hammurabi/README.md new file mode 100644 index 0000000..2b38cf1 --- /dev/null +++ b/out/production/hamurabi/hammurabi/README.md @@ -0,0 +1,229 @@ +# hamurabi +an ancient (in more ways than one) computer game (text) + +## General idea of the lab + +*Hammurabi* is a very old computer game (like about as old as Kris). 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 Sumer, 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 survive** +> * **Each person can farm at most 10 acres of land** +> * **It takes 2 bushels of grain to farm an acre of land** +> * **The 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!** + +### Maven + +There is NO `pom.xml` file in this repo. You need to add that. Use `vscode` or `IntelliJ` to make sure your project is a Maven project with a POM file. + +### Details + +Define a single class named `Hammurabi`. Use the following skeleton (but leave out the `//` comments): + +```java +package hammurabi; // package declaration + +import java.util.Random; // imports go here +import java.util.Scanner; + +public class Hammurabi { // must save in a file named Hammurabi.java + Random rand = new Random(); // this is an instance variable + Scanner scanner = new Scanner(System.in); + + public static void main(String\[\]args) { // required in every Java program + new hammurabi.src.main.java.Hammurabi().playGame(); + } + + void playGame() { + // declare local variables here: grain, population, etc. + // statements go after the declations + } + + //other methods go here +} +``` + +### Playing the game + +Here is what you start the game 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: + + 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 your `playGame` method needs to do each year, for up to 10 years: + +> Ask the player the following questions. These questions should be asked **in this order.** Do **not** give the player a chance to "back up" and change a previous answer. Each question should be asked in a separate method, and the answer returned as the value of the method, as specified below: +> +> `int askHowManyAcresToBuy(int price, int bushels)` +> +> Asks the player how many acres of land to buy, and returns that number. You must have enough grain to pay for your purchase. +> +> ` +> int askHowManyAcresToSell(int acresOwned)` +> +> Asks the player how many acres of land to sell, and returns that number. You can't sell more than you have. +> Do not ask this question if the player is buying land; it doesn't make sense to do both in one turn. +> +> ` +> int askHowMuchGrainToFeedPeople(int bushels)` +> +> Ask the player how much grain to feed people, and returns that number. You can't feed them more grain than you have. You **can** feed them more than they need to survive. +> +> ` +> int askHowManyAcresToPlant(int acresOwned, int population, int bushels)` +> +> Ask the player how many acres to plant with grain, and returns that number. You must have enough acres, enough grain, and enough people to do the planting. Any grain left over goes into storage for next year. +> +> For each question, do "sanity checking"; that is, test whether the answer is possible (you have enough grain, you have enough people to do the planting etc.), and keep asking until you get a possible value. (For example, `O Great Hammurabi, surely you jest! We have only 3415 bushels left!`) +> +> Then the method needs to determine: +> +> 1. If there is a plague, and how many people die from it. +> 2. How many people starved. +> 3. How many people came to the city. +> 4. How good the harvest is. +> 5. If you have a problem with rats, and how much grain they eat. +> 6. How much land costs (for deciding what to do next). +> +> These can all be local variables of your `playGame` method. +> +> Use the following methods, **in this order,** to make the necessary calculations: +> +> `int plagueDeaths(int population)` +> +> Each year, there is a 15% chance of a horrible plague. When this happens, half your people die. Return the number of plague deaths (possibly zero). +> +> ` +> int starvationDeaths(int population, int bushelsFedToPeople)` +> +> 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. You don't get any benefit from having happy subjects. Return the number of deaths from starvation (possibly zero). +> +> ` +> boolean uprising(int population, int howManyPeopleStarved)` +> +> Return `true` if more than 45% of the people starve. (This will cause you to be immediately thrown out of office, ending the game.) +> +> ` +> int immigrants(int population, int acresOwned, int grainInStorage)` +> +> Nobody will come to the city if people are starving (so don't call this method). 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`. +> +> ` +> int harvest(int acres, int bushelsUsedAsSeed)` +> +> Choose a random integer between 1 and 6, inclusive. 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). Return the number of bushels harvested. +> +> ` +> int grainEatenByRats(int bushels)` +> +> There is a 40% chance that you will have a rat infestation. When this happens, rats will eat somewhere between 10% and 30% of your grain. Return the amount of grain eaten by rats (possibly zero). +> +> ` +> int newCostOfLand()` +> +> The price of land is random, and ranges from 17 to 23 bushels per acre. Return the new price for the next set of decisions the player has to make. (The player will need this information in order to buy or sell land.) + +Do these computations, _in this order,_ for each of ten years. Each computation should be done in a separate method, and none of these methods should read anything or print anything. Since most methods change the amount of grain available, you will need to keep this information in a variable so it is available to the next method that needs it. + +When the computations are finished, call a method `printSummary` to print the summary for the year. This method will take several parameters. + +When the game ends, use a method `finalSummary` to print out a final summary, and to 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. + +Your `playGame` method will be quite long, but very straightforward; it does very little but call other methods. + +All the required arithmetic in this program should be **integer**. You do not need doubles. + +### Urgent! + +> It is **strongly** 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)`. To get a new random number in the range `min..max`, use the expression `rand.nextInt(max - min + 1) + min`. You can use the `rand` method 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..._ }` + +#### Getting input + +To get a number from the player, add this method to your program: + +```java + /** + * Prints the given message (which should ask the user for some integral + * quantity), and returns the number entered by the user. If the user's + * response isn't an integer, the question is repeated until the user + * does give a integer response. + * + * @param message The request to present to the user. + * @return The user's numeric response. + */ + 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!"); + } + } + } +``` + +Here is an example of how you can use this method: + +> `sell = getNumber("O great Hammurabi, how many acres shall you sell?");` + +### Structure of the solution + +* Project name: `Hammurabi` +* Package name: `hammurabi` +* Class names and method signatures: + * `class Hammurabi` + * `public static void main(String[] args)` + * Numerous other methods, as described above. + +The above are requirements. **The methods that do calculations**, not input/output, **will be tested by the test methods in the test class**, so be sure to get the spelling, capitalization, and number and types of arguments correct (argument _names_ are irrelevant). You may have additional methods if you wish. + +### Yo! Test it! + +You'll see there is a Test File (HammurabiTest.java) you need to be able to run against your code to prove your solution works. Use It!! + +But realize that you'll have to structure your solution so that app classes are in one source direstory and your test files is in another. Study your other labs to figure out what to do. + +### Credits + +Adapted from: CIT 591 Assignment 3: Hammurabi, Fall 2010, David Matuszek (this guy is a Rock Star) + diff --git a/out/production/hamurabi/hammurabi/docs/matuszek/06-hammurabi.html b/out/production/hamurabi/hammurabi/docs/matuszek/06-hammurabi.html new file mode 100644 index 0000000..78bbf24 --- /dev/null +++ b/out/production/hamurabi/hammurabi/docs/matuszek/06-hammurabi.html @@ -0,0 +1,299 @@ + + + + + + + CIT591 Hammurabi + + + + + +

CIT 591 Assignment 3: + Hammurabi
+ Fall 2010, David Matuszek

+ +

Purpose 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!

+
+ +

Details

+ +

Define a single class named Hammurabi. Use the following skeleton (but leave out the // comments):

+
package hammurabi;               // package declaration
+
+import java.util.Random;         // imports go here
+import java.util.Scanner;
+
+/**
+ * The Hammurabi game.
+ * @author Your name goes here
+ * @author Your partner's name goes here
+ */
+public class Hammurabi {         // must save in a file named Hammurabi.java
+    Random rand = new Random();  // this is an instance variable
+    Scanner scanner = new Scanner(System.in);
+
+    public static void main(String[] args) { // required in every Java program
+        new Hammurabi().playGame();
+    }
+
+    void playGame() {
+        // declare local variables here: grain, population, etc.
+        // statements go after the declations
+    }
+
+    //other methods go here
+}
+

Playing the game

+

Here is what you start the game 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 your playGame method needs to do each year, for up to 10 years:

+ +
+

Ask the player the following questions. These questions should be asked in this order. Do not give the player a chance to "back up" and change a previous answer. Each question should be asked in a separate method, and the answer returned as the value of the method, as specified below:

+
+
int askHowManyAcresToBuy(int price, int bushels)
+
Asks the player how many acres of land to buy, and returns that number. You must have enough grain to pay for your purchase.
+

+ int askHowManyAcresToSell(int acresOwned)
+
Asks the player how many acres of land to sell, and returns that number. You can't sell more than you have.
+ Do not ask this question if the player is buying land; it doesn't make sense to do both in one turn.
+

+ int askHowMuchGrainToFeedPeople(int bushels)
+
Ask the player how much grain to feed people, and returns that number. You can't feed them more grain than you have. You can feed them more than they need to survive.
+

+ int askHowManyAcresToPlant(int acresOwned, int population, int bushels)
+
Ask the player how many acres to plant with grain, and returns that number. You must have enough acres, enough grain, and enough people to do the planting. Any grain left over goes into storage for next year.
+
+

For each + question, do "sanity checking"; that is, test whether the answer is + possible (you have enough grain, you have enough people to do the planting + etc.), and keep asking until you get a possible value. (For example, + O Great Hammurabi, surely you jest! We have only 3415 bushels left!)

+ +

Then the method needs to determine:

+ +
    +
  1. If there is a plague, and how many people die from it.
  2. +
  3. How many people starved.
  4. +
  5. How many people came to the city.
  6. +
  7. How good the harvest is.
  8. +
  9. If you have a problem with rats, and how much grain they eat.
  10. +
  11. How much land costs (for deciding what to do next).
  12. +
+

These can all be local variables of your playGame method.

+

Use the following methods, in this order, to make the necessary calculations:

+
+
int plagueDeaths(int population)
+
Each year, there is a 15% chance of a horrible plague. When this + happens, half your people die. Return the number of plague deaths (possibly zero).
+

+ int starvationDeaths(int population, int bushelsFedToPeople)
+
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. You don't get any benefit from having happy subjects. Return the number of deaths from starvation (possibly zero).
+

+ boolean uprising(int population, int howManyPeopleStarved)
+
Return true if more than + 45% of the people starve. (This will cause you to be immediately thrown out of office, ending the game.)
+

+ int immigrants(int population, int acresOwned, int grainInStorage)
+
Nobody will come to the city if people are starving (so don't call this method). 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.
+

+ int harvest(int acres, int bushelsUsedAsSeed)
+
Choose a random integer between 1 and 6, inclusive. 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). Return the number of bushels harvested.
+

+ int grainEatenByRats(int bushels)
+
There is a 40% chance that you will have a rat infestation. When this + happens, rats will eat somewhere between 10% and 30% of your + grain. Return the amount of grain eaten by rats (possibly zero).
+

+ int newCostOfLand()
+
The price of land is random, and ranges from 17 to 23 bushels per + acre. Return the new price for the next set of decisions the player has to make. (The player will need this information in order to buy or sell + land.)
+
+
+

Do these computations, in this order, for each of ten years. Each + computation should be done in a separate method, and none of these methods + should read anything or print anything. Since most methods change the amount of grain available, you will need to keep this information in a variable so it is available to the next method that needs it.

+ +

When the computations are finished, call a method printSummary to print the + summary for the year. This method will take several parameters.

+ + +

When the game ends , use a method finalSummary to print out a final summary, and to 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.

+

Your playGame method will be quite long, but very straightforward; it does very little but call other methods.

+

All the required arithmetic in this program should be integer. You + do not need doubles.

+ +

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). + To get a new random number in the range min..max, use + the expression rand.nextInt(max - min + 1) + min. You + can use the rand method 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... + }

+
+ +

Getting input

+

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

+
+/**
+ * Prints the given message (which should ask the user for some integral
+ * quantity), and returns the number entered by the user. If the user's
+ * response isn't an integer, the question is repeated until the user
+ * does give a integer response.
+ * 
+ * @param message The request to present to the user.
+ * @return The user's numeric response.
+ */
+ 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!");
+        }
+    }
+}
+
+ +

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

+ +
+

sell = getNumber("O great Hammurabi, how many acres shall you sell?");

+
+ +

Structure of the assignment:

+ + + +

The above are requirements. The methods that do calculations, not input/output, will be tested by my test methods, so be sure to get the spelling, capitalization, and number and types of arguments correct (argument names are irrelevant). You may have additional methods if you wish.

+ +

Due date:

+ +

Thursday, October 21, before midnight. Turn in your zipped assignment via + Blackboard. Email submissions will incur the wrath of Hammurabi (and won't be graded).

+ + + diff --git a/out/production/hamurabi/hammurabi/docs/matuszek/README..md b/out/production/hamurabi/hammurabi/docs/matuszek/README..md new file mode 100644 index 0000000..796d85c --- /dev/null +++ b/out/production/hamurabi/hammurabi/docs/matuszek/README..md @@ -0,0 +1,6 @@ +# Notes + +from the original Dave Matuszek course of 2010. + +https://www.cis.upenn.edu/~matuszek/cit591-2010/Assignments/06-hammurabi.html + diff --git a/out/production/hamurabi/hammurabi/hamurabi.iml b/out/production/hamurabi/hammurabi/hamurabi.iml new file mode 100644 index 0000000..7801486 --- /dev/null +++ b/out/production/hamurabi/hammurabi/hamurabi.iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/hamurabi/hammurabi/pom.xml b/out/production/hamurabi/hammurabi/pom.xml new file mode 100644 index 0000000..5481738 --- /dev/null +++ b/out/production/hamurabi/hammurabi/pom.xml @@ -0,0 +1,12 @@ + + + 4.0.0 + + groupId + hamurabi + 1.0-SNAPSHOT + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..5481738 --- /dev/null +++ b/pom.xml @@ -0,0 +1,12 @@ + + + 4.0.0 + + groupId + hamurabi + 1.0-SNAPSHOT + + + \ No newline at end of file diff --git a/src/main/java/Hammurabi.java b/src/main/java/Hammurabi.java new file mode 100644 index 0000000..bd909ca --- /dev/null +++ b/src/main/java/Hammurabi.java @@ -0,0 +1,276 @@ +package hammurabi.src.main.java; +import java.util.InputMismatchException; +import java.util.Random; +import java.util.Scanner; +public class Hammurabi { + Random rand = new Random(); + Scanner scanner = new Scanner(System.in); + + int year; + int bushels; + int population; + int acresOfLand; + int landValue; + int landToFarm; + int totalDeaths; + int grainToEat; + int plagueDeaths; + int starvationDeaths; + boolean uprising = false; + int immigrants; + + int efficiency; + + + public static void main(String[] args) { + Hammurabi ham = new Hammurabi(); + ham.playGame(); + + } + + void playGame(){ + gameInit(); + + askHowManyAcresToBuy(this.landValue, this.bushels); + askHowManyAcresToSell(this.acresOfLand); + askHowMuchGrainToFeedPeople(this.bushels); + askHowManyAcresToPlant(this.acresOfLand, this.population, this.bushels); + + plagueDeaths(this.population); + starvationDeaths(this.population, this.grainToEat); + uprising(this.population, this.starvationDeaths); + + if(starvationDeaths == 0){ + immigrants(this.population, this.acresOfLand, this.bushels); + } + + harvest(this.la); + + + + + + + + + } + + public void gameInit(){ + year = 1; + bushels = 2800; + population = 100; + acresOfLand = 1000; + landValue = 19; + + System.out.println(" ▄█ █▄ ▄████████ ▄▄▄▄███▄▄▄▄ ▄▄▄▄███▄▄▄▄ ███ █▄ ▄████████ ▄████████ ▀█████████▄ ▄█ \n" + + " ███ ███ ███ ███ ▄██▀▀▀███▀▀▀██▄ ▄██▀▀▀███▀▀▀██▄ ███ ███ ███ ███ ███ ███ ███ ███ ███ \n" + + " ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███▌ \n" + + " ▄███▄▄▄▄███▄▄ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ▄███▄▄▄▄██▀ ███ ███ ▄███▄▄▄██▀ ███▌ \n" + + "▀▀███▀▀▀▀███▀ ▀███████████ ███ ███ ███ ███ ███ ███ ███ ███ ▀▀███▀▀▀▀▀ ▀███████████ ▀▀███▀▀▀██▄ ███▌ \n" + + " ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ▀███████████ ███ ███ ███ ██▄ ███ \n" + + " ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ \n" + + " ███ █▀ ███ █▀ ▀█ ███ █▀ ▀█ ███ █▀ ████████▀ ███ ███ ███ █▀ ▄█████████▀ █▀ \n" + + " ███ ███ "); + System.out.println("Congratulations, you are the newest ruler of ancient Sumer, elected for a ten year term of office." + + "\nYour duties are to dispense food, direct farming, and buy and sell land as needed to support your people." + + "\nWatch out for rat infestiations and the plague! Grain is the general currency, measured in bushels." + + "\nThe following will help you in your decisions:\n" + + "\n" + + "Each person needs at least 20 bushels of grain per year to survive\n" + + "Each person can farm at most 10 acres of land\n" + + "It takes 2 bushels of grain to farm an acre of land\n" + + "The market price for land fluctuates yearly\n" + + "Rule wisely and you will be showered with appreciation at the end of your term.\n" + + "Rule poorly and you will be sacrificed to the blood gods!"); + + + } + + public void summaryOfYear(){ + System.out.println( + "O' great Hammurabi!\n" + + "You are in year " + year + " of your ten year rule.\n" + + "In the previous year 0 people starved to death.\n" + + "In the previous year 5 people entered the kingdom.\n" + + "The population is now 1000.\n" + + "We harvested 3000 bushels at 3 bushels per acre.\n" + + "Rats destroyed 200 bushels, leaving 2800 bushels in storage.\n" + + "The city owns 1000 acres of land.\n" + + "Land is currently worth 19 bushels per acre."); + } + + int getAcresToBuy(String message) { + int acresToBuy = 0; + while (true) { + System.out.print(message); + try { + acresToBuy = scanner.nextInt(); + if(acresToBuy > bushels / landValue){ + System.out.println("Sire surely you jest, we have only " + bushels + " of grain to spend!"); + } else if(acresToBuy < 0){ + System.out.println("Sire we are buying land now, not selling it!"); + } else if(acresToBuy == 0){ + break; + } + else break; + } + catch (InputMismatchException e) { + System.out.println("\"" + acresToBuy + "\" isn't a number!"); + scanner.next(); + } + } + bushels -= acresToBuy * landValue; + acresOfLand += acresToBuy; + return acresToBuy; + } + int getAcresToSell(String message) { + int acresToSell = 0; + while (true) { + System.out.print(message); + try { + acresToSell = scanner.nextInt(); + if(acresToSell > acresOfLand){ + System.out.println("Sire surely you jest, we have only " + acresOfLand + " acres of land to sell!"); + } else if(acresToSell < 0){ + System.out.println("Sire we are selling land now, not buying it!"); + } else if(acresToSell == 0){ + break; + } + else break; + } + catch (InputMismatchException e) { + System.out.println("\"" + acresToSell + "\" isn't a number!"); + scanner.next(); + } + + } + acresOfLand -= acresToSell; + bushels += acresToSell * landValue; + return acresToSell; + } + + int getGrainToEat(String message) { + int grainToEat = 0; + while (true) { + System.out.print(message); + try { + grainToEat = scanner.nextInt(); + if(grainToEat > bushels){ + System.out.println("Sire surely you jest, we have only " + bushels + " of grain to use!"); + } else if(grainToEat < 0){ + System.out.println("Sire if we steal the peasants food, they will surely pave the streets with your blood!"); + } else if(grainToEat == 0){ + System.out.println("Sire if we don't feed our people, we will have no kingdom to rule!"); + } + else break; + } + catch (InputMismatchException e) { + System.out.println("\"" + grainToEat + "\" isn't a number!"); + scanner.next(); + } + + } + bushels -= grainToEat; + return this.grainToEat = grainToEat; + } + + int getLandToFarm(String message) { + int landToFarm = 0; + while (true) { + System.out.print(message); + try { + landToFarm = scanner.nextInt(); + if(landToFarm > acresOfLand / (population * 10)){ + System.out.println("Sire surely you jest, we do not have enough workers to farm that land!"); + } else if(landToFarm < 0){ + System.out.println("Sire we are not in the position to destroy our farmland!"); + } else if(landToFarm == 0){ + System.out.println("Sire if we don't feed our people, we will have no kingdom to rule!"); + } + else break; + } + catch (InputMismatchException e) { + System.out.println("\"" + landToFarm + "\" isn't a number!"); + scanner.next(); + } + + } + bushels -= landToFarm * 2; + return this.landToFarm = landToFarm; + } + + public int askHowManyAcresToBuy(int price, int bushels){ + int buy = getAcresToBuy("O' great Hammurabi, how many acres of land should we purchase?"); + return buy; + } + + + + public int askHowManyAcresToSell(int acresOwned){ + int sell = getAcresToSell("0' great Hammurabi, how many acres of land should we sell?"); + return sell; + + } + + public int askHowMuchGrainToFeedPeople(int bushels){ + int grainToGive = getGrainToEat("O' great Hammurabi, how much grain should we give our people?"); + return grainToGive; + + + } + public int askHowManyAcresToPlant(int acresOwned, int population, int bushels){ + int acresToPlant = getLandToFarm("O' great Hammurabi, how much land should we farm this year?"); + return acresToPlant; + } + + public int plagueDeaths(int population){ + int plagueDeaths = 0; + int spreadPlague = (int) Math.random(); + if(spreadPlague <= 0.15){ + System.out.println("O' great Hammurabi! A plague is ravaging the land! Our population will surely suffer!"); + plagueDeaths = population / 2; + this.population -= plagueDeaths; + return this.plagueDeaths = plagueDeaths; + + } else return this.plagueDeaths = plagueDeaths; + } + + public int starvationDeaths(int population, int bushelsToFeedPeople){ + int starvationDeaths = 0; + if((population * 20) > bushelsToFeedPeople){ + System.out.println("O' great Hammurabi, you did not feed enough people! Some of your followers will starve to death!"); + starvationDeaths = (population * 20) % bushelsToFeedPeople; + return this.starvationDeaths = starvationDeaths; + } else return this.starvationDeaths = starvationDeaths; + } + + public boolean uprising(int population, int starvationDeaths){ + if(starvationDeaths >= (int)(population * 0.45) * 100){ + System.out.println("O' moronic Hammurabi, for starving your people you will be sacrificed to the blood gods!"); + return this.uprising = true; + } else return this.uprising = false; + } + + public int immigrants(int population, int acresOfLand, int bushels){ + int immigrants; + immigrants = (20 * acresOfLand + bushels) / (100 * population) + 1; + return this.immigrants = immigrants; + } + + public int harvest(int landUsedForFarming){ + int bushelsHarvested = 0; + this.efficiency = (rand.nextInt(7) + 1); + this.bushels += bushelsHarvested * efficiency; + return bushelsHarvested; + } + public int grainEatenByRats(int bushels){ + int ratInfestation = 0; + int bushelsEatenByRats = 0; + + + + } + + + +} diff --git a/src/test/java/HammurabiTest.java b/src/test/java/HammurabiTest.java new file mode 100644 index 0000000..d8788cb --- /dev/null +++ b/src/test/java/HammurabiTest.java @@ -0,0 +1,126 @@ +package hammurabi.src.test.java; + +import static org.junit.Assert.*; + +import hammurabi.src.main.java.Hammurabi; +import org.junit.Before; +import org.junit.Test; + +//public class HammurabiTest { +// +// Hammurabi ham; +// +// boolean about(double expected, double actual) { +// return actual > 0.90 * expected && actual < 1.10 * expected; +// } +// +// @Before +// public void setUp() throws Exception { +// ham = new Hammurabi(); +// } +// +// @Test +// public final void testPlagueDeaths1() { +// int number_of_plagues = 0; +// for (int i = 0; i < 10000; i++) { +// int deaths = ham.plagueDeaths(100); +// if (deaths > 0) { +// number_of_plagues += 1; +// } +// } +// int percentPlagues = number_of_plagues / 100; +// assertTrue("Number of plagues is about " + percentPlagues + ", not about 15%.", +// about(1500, number_of_plagues)); +// } +// +// @Test +// public final void testPlagueDeaths2() { +// int deaths = 0; +// for (int i = 0; i < 10000; i++) { +// deaths = ham.plagueDeaths(100); +// if (deaths > 0) break; +// } +// assertEquals("In a plague, " + deaths + "% of your people die, not 50%.", +// 50, deaths); +// } +// +// @Test +// public final void testStarvationDeaths() { +// int deaths = ham.starvationDeaths(100, 1639); +// assertEquals("Wrong number of starvations deaths.", 19, deaths); +// deaths = ham.starvationDeaths(100, 2500); +// if (deaths < 0) { +// fail("You starved a negative number of people!"); +// } +// } +// +// @Test +// public final void testUprising() { +// assertTrue("Should have had an uprising!", ham.uprising(1000, 451)); +// assertFalse("Should not have had an uprising!", ham.uprising(1000, 449)); +// } +// +// @Test +// public final void testImmigrants() { +// int imm = ham.immigrants(10, 1200, 500); +// assertEquals("Wrong number of immigrants.", 25, imm); +// } +// +// @Test +// public final void testHarvest() { +// int[] yield = new int[7]; +// for (int i = 0; i < 1000; i++) { +// int harvest = ham.harvest(1); +// assertTrue("Illegal harvest per acre: " + harvest, harvest > 0 && harvest <= 6); +// yield[harvest] += 1; +// } +// for (int j = 1; j <= 6; j++) { +// assertTrue("You never have a yield of " + j + " bushels per acre.", yield[j] > 0); +// } +// } +// +// @Test +// public final void testGrainEatenByRats1() { +// int infestations = 0; +// for (int i = 0; i < 1000; i++) { +// int eaten = ham.grainEatenByRats(100); +// if (eaten > 0) { +// infestations += 1; +// } +// } +// int percentInfestations = infestations / 100; +// 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 testNewCostOfLand() { +// int[] cost = new int[24]; +// for (int i = 0; i < 1000; i++) { +// int price = ham.newCostOfLand(); +// assertTrue("Illegal cost of land: " + price, price >= 17 && price <= 23); +// cost[price] += 1; +// } +// for (int j = 17; j <= 23; j++) { +// assertTrue("You never have a land cost of " + j + " bushels per acre.", cost[j] > 0); +// } +// } +// +//} +// From 04993720e02d9949466ed0a0af10e02b33ff3c52 Mon Sep 17 00:00:00 2001 From: Jake Lawhorne Date: Sun, 19 Mar 2023 12:05:34 -0400 Subject: [PATCH 2/2] Lab finished tests passed --- src/main/java/Hammurabi.java | 165 +++++++++++++++------ src/test/java/HammurabiTest.java | 236 +++++++++++++++---------------- 2 files changed, 243 insertions(+), 158 deletions(-) diff --git a/src/main/java/Hammurabi.java b/src/main/java/Hammurabi.java index bd909ca..a99a094 100644 --- a/src/main/java/Hammurabi.java +++ b/src/main/java/Hammurabi.java @@ -12,46 +12,50 @@ public class Hammurabi { int acresOfLand; int landValue; int landToFarm; - int totalDeaths; int grainToEat; int plagueDeaths; int starvationDeaths; boolean uprising = false; int immigrants; - int efficiency; + int bushelsEatenByRats; + + int totalDeaths; public static void main(String[] args) { Hammurabi ham = new Hammurabi(); + ham.gameInit(); ham.playGame(); - + ham.gameResults(); } void playGame(){ - gameInit(); - - askHowManyAcresToBuy(this.landValue, this.bushels); - askHowManyAcresToSell(this.acresOfLand); - askHowMuchGrainToFeedPeople(this.bushels); - askHowManyAcresToPlant(this.acresOfLand, this.population, this.bushels); - - plagueDeaths(this.population); - starvationDeaths(this.population, this.grainToEat); - uprising(this.population, this.starvationDeaths); - - if(starvationDeaths == 0){ - immigrants(this.population, this.acresOfLand, this.bushels); - } - - harvest(this.la); - - - + do { + askHowManyAcresToBuy(this.landValue, this.bushels); + askHowManyAcresToSell(this.acresOfLand); + askHowMuchGrainToFeedPeople(this.bushels); + askHowManyAcresToPlant(this.acresOfLand, this.population, this.bushels); + + plagueDeaths(this.population); + starvationDeaths(this.population, this.grainToEat); + uprising(this.population, this.starvationDeaths); + if(uprising == true){ + break; + } + if (starvationDeaths == 0) { + immigrants(this.population, this.acresOfLand, this.bushels); + } + harvest(this.landToFarm); + grainEatenByRats(this.bushels); + newCostOfLand(); + summaryOfYear(); + resetValues(); + } while(uprising == false && year < 11); } @@ -81,7 +85,18 @@ public void gameInit(){ "It takes 2 bushels of grain to farm an acre of land\n" + "The market price for land fluctuates yearly\n" + "Rule wisely and you will be showered with appreciation at the end of your term.\n" + - "Rule poorly and you will be sacrificed to the blood gods!"); + "Rule poorly and you will be sacrificed to the blood gods!\n\n"); + + System.out.println( + "O' great Hammurabi!\n" + + "You are in year 1 of your ten year rule.\n" + + "In the previous year 0 people starved to death.\n" + + "In the previous year 5 people entered the kingdom.\n" + + "The population is now 100.\n" + + "We harvested 3000 bushels at 3 bushels per acre.\n" + + "Rats destroyed 200 bushels, leaving 2800 bushels in storage.\n" + + "The city owns 1000 acres of land.\n" + + "Land is currently worth 19 bushels per acre."); } @@ -89,14 +104,15 @@ public void gameInit(){ public void summaryOfYear(){ System.out.println( "O' great Hammurabi!\n" + - "You are in year " + year + " of your ten year rule.\n" + - "In the previous year 0 people starved to death.\n" + - "In the previous year 5 people entered the kingdom.\n" + - "The population is now 1000.\n" + - "We harvested 3000 bushels at 3 bushels per acre.\n" + - "Rats destroyed 200 bushels, leaving 2800 bushels in storage.\n" + - "The city owns 1000 acres of land.\n" + - "Land is currently worth 19 bushels per acre."); + "You are in year " + year + " of your ten year rule.\n" + + "In the previous year " + starvationDeaths + " people starved to death.\n" + + "In the previous year " + plagueDeaths + " people died from the plague. \n" + + "In the previous year " + immigrants + " people entered the kingdom.\n" + + "The population is now " + population + ".\n" + + "We harvested " + (landToFarm * efficiency) + " bushels at " + efficiency + " bushels per acre.\n" + + "Rats destroyed " + bushelsEatenByRats + " bushels, leaving " + bushels + " bushels in storage.\n" + + "The city owns " + acresOfLand + " acres of land.\n" + + "Land is currently worth " + landValue + " bushels per acre."); } int getAcresToBuy(String message) { @@ -180,7 +196,7 @@ int getLandToFarm(String message) { System.out.print(message); try { landToFarm = scanner.nextInt(); - if(landToFarm > acresOfLand / (population * 10)){ + if(landToFarm > population * 10){ System.out.println("Sire surely you jest, we do not have enough workers to farm that land!"); } else if(landToFarm < 0){ System.out.println("Sire we are not in the position to destroy our farmland!"); @@ -225,11 +241,12 @@ public int askHowManyAcresToPlant(int acresOwned, int population, int bushels){ public int plagueDeaths(int population){ int plagueDeaths = 0; - int spreadPlague = (int) Math.random(); + double spreadPlague = Math.random(); if(spreadPlague <= 0.15){ System.out.println("O' great Hammurabi! A plague is ravaging the land! Our population will surely suffer!"); plagueDeaths = population / 2; this.population -= plagueDeaths; + this.totalDeaths += plagueDeaths; return this.plagueDeaths = plagueDeaths; } else return this.plagueDeaths = plagueDeaths; @@ -239,14 +256,24 @@ public int starvationDeaths(int population, int bushelsToFeedPeople){ int starvationDeaths = 0; if((population * 20) > bushelsToFeedPeople){ System.out.println("O' great Hammurabi, you did not feed enough people! Some of your followers will starve to death!"); - starvationDeaths = (population * 20) % bushelsToFeedPeople; + starvationDeaths = (population - (bushelsToFeedPeople / 20)); + this.totalDeaths += starvationDeaths; return this.starvationDeaths = starvationDeaths; } else return this.starvationDeaths = starvationDeaths; } public boolean uprising(int population, int starvationDeaths){ - if(starvationDeaths >= (int)(population * 0.45) * 100){ - System.out.println("O' moronic Hammurabi, for starving your people you will be sacrificed to the blood gods!"); + if(starvationDeaths >= (int)(population * 0.45)){ + System.out.println("O' moronic Hammurabi, for starving your people you will be sacrificed to the blood gods!\n\n\n"); + System.out.println(" ▄██████▄ ▄████████ ▄▄▄▄███▄▄▄▄ ▄████████ ▄██████▄ ▄█ █▄ ▄████████ ▄████████ \n" + + " ███ ███ ███ ███ ▄██▀▀▀███▀▀▀██▄ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ \n" + + " ███ █▀ ███ ███ ███ ███ ███ ███ █▀ ███ ███ ███ ███ ███ █▀ ███ ███ \n" + + " ▄███ ███ ███ ███ ███ ███ ▄███▄▄▄ ███ ███ ███ ███ ▄███▄▄▄ ▄███▄▄▄▄██▀ \n" + + "▀▀███ ████▄ ▀███████████ ███ ███ ███ ▀▀███▀▀▀ ███ ███ ███ ███ ▀▀███▀▀▀ ▀▀███▀▀▀▀▀ \n" + + " ███ ███ ███ ███ ███ ███ ███ ███ █▄ ███ ███ ███ ███ ███ █▄ ▀███████████ \n" + + " ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ \n" + + " ████████▀ ███ █▀ ▀█ ███ █▀ ██████████ ▀██████▀ ▀██████▀ ██████████ ███ ███ \n" + + " ███ ███ "); return this.uprising = true; } else return this.uprising = false; } @@ -254,20 +281,78 @@ public boolean uprising(int population, int starvationDeaths){ public int immigrants(int population, int acresOfLand, int bushels){ int immigrants; immigrants = (20 * acresOfLand + bushels) / (100 * population) + 1; + this.population += immigrants; return this.immigrants = immigrants; } public int harvest(int landUsedForFarming){ int bushelsHarvested = 0; - this.efficiency = (rand.nextInt(7) + 1); - this.bushels += bushelsHarvested * efficiency; + this.efficiency = (rand.nextInt(6 - 1 + 1) + 1); + bushelsHarvested = landUsedForFarming * efficiency; + this.bushels += bushelsHarvested; return bushelsHarvested; } public int grainEatenByRats(int bushels){ - int ratInfestation = 0; + double cropDestruction = 0; int bushelsEatenByRats = 0; - + cropDestruction = ((Math.random() * (30 - 10 + 1)) + 10) / 100; + if(rand.nextInt(100) <= 39){ + System.out.println("O' great Hammurabi, our crops have been infested by rats! Our crops face destruction!"); + bushelsEatenByRats = (int) (bushels * cropDestruction); + this.bushels -= bushelsEatenByRats; + this.bushelsEatenByRats = bushelsEatenByRats; + return bushelsEatenByRats; + } else { + return 0; + } + } + + public int newCostOfLand(){ + int landValue; + landValue = rand.nextInt(23 - 17 + 1) + 17; + this.landValue = landValue; + this.year++; + return this.landValue; + } + public void resetValues(){ + this.plagueDeaths = 0; + this.bushelsEatenByRats = 0; + } + + public void gameResults(){ + int percentageDied = (totalDeaths / population) * 100; + if(percentageDied > 80 || acresOfLand < 300){ + System.out.println(" ▄█ █▄ ▄██████▄ ▄████████ ███ ███ ▄█ █▄ ▄█ ▄████████ ▄████████ ▄████████ \n" + + "███ ███ ███ ███ ███ ███ ▀█████████▄ ▀█████████▄ ███ ███ ███ ███ ███ ███ ███ ███ ███ \n" + + "███ ███ ███ ███ ███ ███ ▀███▀▀██ ▀███▀▀██ ███ ███ ███ ███ █▀ ███ █▀ ███ █▀ \n" + + "███ ███ ███ ███ ▄███▄▄▄▄██▀ ███ ▀ ███ ▀ ▄███▄▄▄▄███▄▄ ███ ▄███▄▄▄ ███ ███ \n" + + "███ ███ ███ ███ ▀▀███▀▀▀▀▀ ███ ███ ▀▀███▀▀▀▀███▀ ███ ▀▀███▀▀▀ ▀███████████ ▀███████████ \n" + + "███ ███ ███ ███ ▀███████████ ███ ███ ███ ███ ███ ███ █▄ ███ ███ \n" + + "███ ▄█▄ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███▌ ▄ ███ ███ ▄█ ███ ▄█ ███ \n" + + " ▀███▀███▀ ▀██████▀ ███ ███ ▄████▀ ▄████▀ ███ █▀ █████▄▄██ ██████████ ▄████████▀ ▄████████▀ \n" + + " ███ ███ ▀ "); + } else if((percentageDied < 80 && percentageDied > 50) || (acresOfLand > 300 && acresOfLand < 700)){ + System.out.println(" ▄█ █▄ ▄██████▄ ▄████████ ███ ▄█ █▄ ▄██ ▄ \n" + + "███ ███ ███ ███ ███ ███ ▀█████████▄ ███ ███ ███ ██▄ \n" + + "███ ███ ███ ███ ███ ███ ▀███▀▀██ ███ ███ ███▄▄▄███ \n" + + "███ ███ ███ ███ ▄███▄▄▄▄██▀ ███ ▀ ▄███▄▄▄▄███▄▄ ▀▀▀▀▀▀███ \n" + + "███ ███ ███ ███ ▀▀███▀▀▀▀▀ ███ ▀▀███▀▀▀▀███▀ ▄██ ███ \n" + + "███ ███ ███ ███ ▀███████████ ███ ███ ███ ███ ███ \n" + + "███ ▄█▄ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ \n" + + " ▀███▀███▀ ▀██████▀ ███ ███ ▄████▀ ███ █▀ ▀█████▀ \n" + + " ███ ███ "); + } else if(percentageDied < 50 || acresOfLand > 700){ + System.out.println(" ▄██████▄ ▄██████▄ ████████▄ ▄█ ▄█▄ ▄█ ███▄▄▄▄ ▄██████▄ \n" + + " ███ ███ ███ ███ ███ ▀███ ███ ▄███▀ ███ ███▀▀▀██▄ ███ ███ \n" + + " ███ █▀ ███ ███ ███ ███ ███▐██▀ ███▌ ███ ███ ███ █▀ \n" + + " ▄███ ███ ███ ███ ███ ▄█████▀ ███▌ ███ ███ ▄███ \n" + + "▀▀███ ████▄ ███ ███ ███ ███ ▀▀█████▄ ███▌ ███ ███ ▀▀███ ████▄ \n" + + " ███ ███ ███ ███ ███ ███ ███▐██▄ ███ ███ ███ ███ ███ \n" + + " ███ ███ ███ ███ ███ ▄███ ███ ▀███▄ ███ ███ ███ ███ ███ \n" + + " ████████▀ ▀██████▀ ████████▀ ███ ▀█▀ █▀ ▀█ █▀ ████████▀ \n" + + " ▀ "); + } } diff --git a/src/test/java/HammurabiTest.java b/src/test/java/HammurabiTest.java index d8788cb..8227551 100644 --- a/src/test/java/HammurabiTest.java +++ b/src/test/java/HammurabiTest.java @@ -6,121 +6,121 @@ import org.junit.Before; import org.junit.Test; -//public class HammurabiTest { -// -// Hammurabi ham; -// -// boolean about(double expected, double actual) { -// return actual > 0.90 * expected && actual < 1.10 * expected; -// } -// -// @Before -// public void setUp() throws Exception { -// ham = new Hammurabi(); -// } -// -// @Test -// public final void testPlagueDeaths1() { -// int number_of_plagues = 0; -// for (int i = 0; i < 10000; i++) { -// int deaths = ham.plagueDeaths(100); -// if (deaths > 0) { -// number_of_plagues += 1; -// } -// } -// int percentPlagues = number_of_plagues / 100; -// assertTrue("Number of plagues is about " + percentPlagues + ", not about 15%.", -// about(1500, number_of_plagues)); -// } -// -// @Test -// public final void testPlagueDeaths2() { -// int deaths = 0; -// for (int i = 0; i < 10000; i++) { -// deaths = ham.plagueDeaths(100); -// if (deaths > 0) break; -// } -// assertEquals("In a plague, " + deaths + "% of your people die, not 50%.", -// 50, deaths); -// } -// -// @Test -// public final void testStarvationDeaths() { -// int deaths = ham.starvationDeaths(100, 1639); -// assertEquals("Wrong number of starvations deaths.", 19, deaths); -// deaths = ham.starvationDeaths(100, 2500); -// if (deaths < 0) { -// fail("You starved a negative number of people!"); -// } -// } -// -// @Test -// public final void testUprising() { -// assertTrue("Should have had an uprising!", ham.uprising(1000, 451)); -// assertFalse("Should not have had an uprising!", ham.uprising(1000, 449)); -// } -// -// @Test -// public final void testImmigrants() { -// int imm = ham.immigrants(10, 1200, 500); -// assertEquals("Wrong number of immigrants.", 25, imm); -// } -// -// @Test -// public final void testHarvest() { -// int[] yield = new int[7]; -// for (int i = 0; i < 1000; i++) { -// int harvest = ham.harvest(1); -// assertTrue("Illegal harvest per acre: " + harvest, harvest > 0 && harvest <= 6); -// yield[harvest] += 1; -// } -// for (int j = 1; j <= 6; j++) { -// assertTrue("You never have a yield of " + j + " bushels per acre.", yield[j] > 0); -// } -// } -// -// @Test -// public final void testGrainEatenByRats1() { -// int infestations = 0; -// for (int i = 0; i < 1000; i++) { -// int eaten = ham.grainEatenByRats(100); -// if (eaten > 0) { -// infestations += 1; -// } -// } -// int percentInfestations = infestations / 100; -// 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 testNewCostOfLand() { -// int[] cost = new int[24]; -// for (int i = 0; i < 1000; i++) { -// int price = ham.newCostOfLand(); -// assertTrue("Illegal cost of land: " + price, price >= 17 && price <= 23); -// cost[price] += 1; -// } -// for (int j = 17; j <= 23; j++) { -// assertTrue("You never have a land cost of " + j + " bushels per acre.", cost[j] > 0); -// } -// } -// -//} -// +public class HammurabiTest { + + Hammurabi ham; + + boolean about(double expected, double actual) { + return actual > 0.90 * expected && actual < 1.10 * expected; + } + + @Before + public void setUp() throws Exception { + ham = new Hammurabi(); + } + + @Test + public final void testPlagueDeaths1() { + int number_of_plagues = 0; + for (int i = 0; i < 10000; i++) { + int deaths = ham.plagueDeaths(100); + if (deaths > 0) { + number_of_plagues += 1; + } + } + int percentPlagues = number_of_plagues / 100; + assertTrue("Number of plagues is about " + percentPlagues + ", not about 15%.", + about(1500, number_of_plagues)); + } + + @Test + public final void testPlagueDeaths2() { + int deaths = 0; + for (int i = 0; i < 10000; i++) { + deaths = ham.plagueDeaths(100); + if (deaths > 0) break; + } + assertEquals("In a plague, " + deaths + "% of your people die, not 50%.", + 50, deaths); + } + + @Test + public final void testStarvationDeaths() { + int deaths = ham.starvationDeaths(100, 1639); + assertEquals("Wrong number of starvations deaths.", 19, deaths); + deaths = ham.starvationDeaths(100, 2500); + if (deaths < 0) { + fail("You starved a negative number of people!"); + } + } + + @Test + public final void testUprising() { + assertTrue("Should have had an uprising!", ham.uprising(1000, 451)); + assertFalse("Should not have had an uprising!", ham.uprising(1000, 449)); + } + + @Test + public final void testImmigrants() { + int imm = ham.immigrants(10, 1200, 500); + assertEquals("Wrong number of immigrants.", 25, imm); + } + + @Test + public final void testHarvest() { + int[] yield = new int[7]; + for (int i = 0; i < 1000; i++) { + int harvest = ham.harvest(1); + assertTrue("Illegal harvest per acre: " + harvest, harvest > 0 && harvest <= 6); + yield[harvest] += 1; + } + for (int j = 1; j <= 6; j++) { + assertTrue("You never have a yield of " + j + " bushels per acre.", yield[j] > 0); + } + } + + @Test + public final void testGrainEatenByRats1() { + int infestations = 0; + for (int i = 0; i < 1000; i++) { + int eaten = ham.grainEatenByRats(100); + if (eaten > 0) { + infestations += 1; + } + } + int percentInfestations = infestations / 100; + 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 testNewCostOfLand() { + int[] cost = new int[24]; + for (int i = 0; i < 1000; i++) { + int price = ham.newCostOfLand(); + assertTrue("Illegal cost of land: " + price, price >= 17 && price <= 23); + cost[price] += 1; + } + for (int j = 17; j <= 23; j++) { + assertTrue("You never have a land cost of " + j + " bushels per acre.", cost[j] > 0); + } + } + +} +