From 4991597f68d6a2e375e54a3dc1da2b3c3602d9a9 Mon Sep 17 00:00:00 2001 From: maggie Date: Sat, 15 Jul 2023 13:17:18 -0400 Subject: [PATCH 1/6] Created empty methods. --- docs/hamurabi.java | 156 ---------- docs/matuszek/HammurabiTest.java | 125 -------- pom.xml | 40 +++ src/main/java/Hammurabi.java | 283 ++++++++++++++++++ .../test/java/HammurabiTest.java | 2 - 5 files changed, 323 insertions(+), 283 deletions(-) delete mode 100644 docs/hamurabi.java delete mode 100644 docs/matuszek/HammurabiTest.java create mode 100644 pom.xml create mode 100644 src/main/java/Hammurabi.java rename HammurabiTest.java => src/test/java/HammurabiTest.java (99%) diff --git a/docs/hamurabi.java b/docs/hamurabi.java deleted file mode 100644 index 9947af3..0000000 --- a/docs/hamurabi.java +++ /dev/null @@ -1,156 +0,0 @@ -import java.io.IOException; -import java.util.Scanner; - -// this seems to be an example of a solution that mimics the original BASIC code the author was writing from. -// -// it's a great example of Very Bad Java. -// Do not write Java like this. If you do, do NOT tell people you went to Zip Code. -// 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 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/pom.xml b/pom.xml new file mode 100644 index 0000000..69144ec --- /dev/null +++ b/pom.xml @@ -0,0 +1,40 @@ + + + 4.0.0 + + groupId + hamurabi + 1.0-SNAPSHOT + + UTF-8 + 11 + 11 + + + + + + junit + junit + 4.12 + test + + + junit + junit + 4.13.1 + test + + + junit + junit + 4.13.1 + compile + + + + + + \ 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..c0a417f --- /dev/null +++ b/src/main/java/Hammurabi.java @@ -0,0 +1,283 @@ +import java.io.IOException; +import java.util.Scanner; + +public class Hammurabi { + + public static void main(String[] args) throws IOException { + Hammurabi a = new Hammurabi(); + 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(); + } + + + + + + + + + + + + + + + + // VARIABLES TO USE + + static int year = 0; // Current Year + static boolean plague = false; // If there is a plague or not + Scanner input = new Scanner(System.in); // Scanner to take in user input + + // People + static int totalDeaths = 0; // Total number of deaths from starvation + static int population = 95; // Current population + static int percentDied = 0; // Percent of people who died????? + static int immigrants = 5; // Number of new people to add to the population + static int deaths; // ????????????????????? + + // Bushels + static int stores = 2800; // total number of Bushels + static int harvest = 3000; // number of bushels harvested + + // Land + static int acres = 1000; // total number of acres + static int yield; // yield = ??????? + static int landPrice; // landPrice = land price in bushels + + // Other fields + int temp; // Temporary value to hold input from user + + int eaten = harvest - stores, fullPeople; + + + 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"; + + public void playGame(){ + + } + + // UPDATE TO NEW YEAR METHODS + private void newYear(){ + // New Year has begun. year is updated + year += 1; + + // Update population by adding immigrants and subtracting totalDeaths + // TODO update immigrants to be immigrants() + // TODO update totalDeaths to be combo of plagueDeaths() and starvationDeaths() + population += immigrants - totalDeaths; + + // Update land price + landPrice = newCostOfLand(); + } + + // Function to calculate number of immigrants + // TODO OLAMIDE + public int immigrants(int population, int acresOwned, int grainInStorage){ + return 0; + } + + // Function to determine new cost of land + // TODO OLAMIDE + public int newCostOfLand() { + return 0; + } + + + // METHODS TO TAKE IN USER INPUT + + public void updateAcres(){ + + } + + public void feedPeople(){ + + } + + + + + + + + + + + // OLD CODE TO REFERENCE BUT *NOT* USE + private void newYear1() { + + year += 1; // + population += immigrants; // + landPrice = (int) (10 * Math.random() + 17); // + System.out.println(report()); + // USER UPDATING ACRES + 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; + + // USER FEEDING PEOPLE + 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; + + // USER PLANTING SEED + 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); + + // CALCULATING IMMIGRANTS + 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; + } + + // DETERMINING PLAGUE + 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 int plagueDeaths(int population){ + return 0; + } + + public int starvationDeaths(int population, int bushelsFedToPeople){ + return 0; + } + + public boolean uprising(int population, int howManyPeopleStarved){ + return true; + } + + + + public int harvest(int acres){ + return 0; + } + + public int grainEatenByRats(int bushels){ + return 0; + } + + + + public void printSummary(){ + } + + public void finalSummary(){ + } + + + + +} diff --git a/HammurabiTest.java b/src/test/java/HammurabiTest.java similarity index 99% rename from HammurabiTest.java rename to src/test/java/HammurabiTest.java index 5cf1cc9..e4b6d84 100644 --- a/HammurabiTest.java +++ b/src/test/java/HammurabiTest.java @@ -1,5 +1,3 @@ -package hammurabi; - import static org.junit.Assert.*; import org.junit.Before; From 019bf3fdaf175726e74627f48777a3cc5ae23603 Mon Sep 17 00:00:00 2001 From: maggie Date: Sat, 15 Jul 2023 15:00:21 -0400 Subject: [PATCH 2/6] Updated user input method names --- src/main/java/Hammurabi.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/main/java/Hammurabi.java b/src/main/java/Hammurabi.java index c0a417f..d31990e 100644 --- a/src/main/java/Hammurabi.java +++ b/src/main/java/Hammurabi.java @@ -58,6 +58,7 @@ public static void main(String[] args) throws IOException { "BEEN IMPEACHED AND THROWN OUT OF OFFICE BUT YOU HAVE\n" + "ALSO BEEN DECLARED PERSONA NON GRATA!!\n"; + public void playGame(){ } @@ -90,16 +91,20 @@ public int newCostOfLand() { // METHODS TO TAKE IN USER INPUT + public int askHowManyAcresToBuy(int price, int bushels){ - public void updateAcres(){ - + return 0; } - public void feedPeople(){ - + public int askHowManyAcresToSell(int acresOwned){ + return 0; + } + public int askHowMuchGrainToFeedPeople(int bushels){ + return 0; + } + public int askHowManyAcresToPlant(int acresOwned, int population, int bushels){ + return 0; } - - @@ -175,6 +180,7 @@ private void newYear1() { eaten = 0; stores += (harvest - eaten); + // CALCULATING IMMIGRANTS immigrants = (int) (Math.random() * 5 + 1) * (20 * acres + stores) / population / 100 + 1; @@ -187,6 +193,7 @@ private void newYear1() { totalDeaths += deaths; } + // DETERMINING PLAGUE if (20 * Math.random() >= 17) plague = true; From d8fc032a1128e5658e399706ca2775f27c4153ed Mon Sep 17 00:00:00 2001 From: Olamide Date: Sat, 15 Jul 2023 15:00:56 -0400 Subject: [PATCH 3/6] update --- src/main/java/Hammurabi.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/Hammurabi.java b/src/main/java/Hammurabi.java index c0a417f..8a811ba 100644 --- a/src/main/java/Hammurabi.java +++ b/src/main/java/Hammurabi.java @@ -79,12 +79,18 @@ private void newYear(){ // Function to calculate number of immigrants // TODO OLAMIDE public int immigrants(int population, int acresOwned, int grainInStorage){ - return 0; + int numOfImmigrants; + numOfImmigrants = (20 * acresOwned + grainInStorage) / (100 * population) +1; + + return numOfImmigrants; } // Function to determine new cost of land // TODO OLAMIDE public int newCostOfLand() { + + int price = (int) (Math.random() *6) + 17; + return 0; } From 94030b1cd712fecde758baba0b84b8290b92543a Mon Sep 17 00:00:00 2001 From: maggie Date: Sat, 15 Jul 2023 15:26:04 -0400 Subject: [PATCH 4/6] Updated user input method names --- src/main/java/Hammurabi.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/Hammurabi.java b/src/main/java/Hammurabi.java index d31990e..687a4ff 100644 --- a/src/main/java/Hammurabi.java +++ b/src/main/java/Hammurabi.java @@ -92,7 +92,12 @@ public int newCostOfLand() { // METHODS TO TAKE IN USER INPUT public int askHowManyAcresToBuy(int price, int bushels){ + System.out.print("HOW MANY ACRES DO YOU WISH TO BUY? "); + temp = input.nextInt(); + if (temp < 0){ + System.out.print("") + } return 0; } From 4e4ac11a81d8c88ab7ea9029114cf3e8a8d8af4e Mon Sep 17 00:00:00 2001 From: maggie Date: Sat, 15 Jul 2023 17:48:32 -0400 Subject: [PATCH 5/6] Play game method --- src/main/java/Hammurabi.java | 130 +++++++++++++++++++++++++---------- 1 file changed, 92 insertions(+), 38 deletions(-) diff --git a/src/main/java/Hammurabi.java b/src/main/java/Hammurabi.java index 7fd40da..dbf72ef 100644 --- a/src/main/java/Hammurabi.java +++ b/src/main/java/Hammurabi.java @@ -4,12 +4,8 @@ public class Hammurabi { public static void main(String[] args) throws IOException { - Hammurabi a = new Hammurabi(); - 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(); + new Hammurabi().playGame(); + } @@ -28,6 +24,7 @@ public static void main(String[] args) throws IOException { // VARIABLES TO USE + static boolean playing = true; static int year = 0; // Current Year static boolean plague = false; // If there is a plague or not Scanner input = new Scanner(System.in); // Scanner to take in user input @@ -51,7 +48,8 @@ public static void main(String[] args) throws IOException { // Other fields int temp; // Temporary value to hold input from user - int eaten = harvest - stores, fullPeople; + static int eaten = harvest - stores; + static int fullPeople; final static String FINK = "DUE TO THIS EXTREME MISMANAGEMENT YOU HAVE NOT ONLY\n" + @@ -60,7 +58,11 @@ public static void main(String[] args) throws IOException { public void playGame(){ - + 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 (playing){ + newYear(); + } } // UPDATE TO NEW YEAR METHODS @@ -71,7 +73,7 @@ private void newYear(){ // Update population by adding immigrants and subtracting totalDeaths // TODO update immigrants to be immigrants() // TODO update totalDeaths to be combo of plagueDeaths() and starvationDeaths() - population += immigrants - totalDeaths; + population += immigrants(population,acres,stores) - totalDeaths; // Update land price landPrice = newCostOfLand(); @@ -98,27 +100,80 @@ public int newCostOfLand() { // METHODS TO TAKE IN USER INPUT public int askHowManyAcresToBuy(int price, int bushels){ - System.out.print("HOW MANY ACRES DO YOU WISH TO BUY? "); - temp = input.nextInt(); - - if (temp < 0){ - System.out.print("") - } - return 0; + do { + System.out.print("HOW MANY ACRES DO YOU WISH TO BUY? "); + temp = input.nextInt(); + if (temp < 0) { + epicFail(0); + } + if (temp * price > bushels){ + System.out.println("HAMMURABI: THINK AGAIN. YOU HAVE ONLY\n" + + bushels + " BUSHELS OF GRAIN. NOW THEN,"); + } + } while (temp * price > bushels); + + return temp; } public int askHowManyAcresToSell(int acresOwned){ - return 0; + do { + System.out.print("HOW MANY ACRES DO YOU WISH TO SELL? "); + temp = input.nextInt(); + if (temp < 0) + epicFail(0); + if (temp > acresOwned) + System.out.println("HAMMURABI: THINK AGAIN. YOU OWN ONLY " + acresOwned + " ACRES. NOW THEN,"); + } while (temp > acresOwned); + + return temp; } public int askHowMuchGrainToFeedPeople(int bushels){ - return 0; + do { + System.out.print("\nHOW MANY BUSHELS DO YOU WISH TO FEED YOUR PEOPLE? "); + temp = input.nextInt(); + if (temp < 0) + epicFail(0); + if (temp > bushels) + System.out.println("HAMMURABI: THINK AGAIN. YOU HAVE ONLY\n" + + bushels + " BUSHELS OF GRAIN. NOW THEN,"); + } while (temp > bushels); + return temp; } public int askHowManyAcresToPlant(int acresOwned, int population, int bushels){ - return 0; - } + do { + System.out.print("\nHOW MANY ACRES DO YOU WISH TO PLANT WITH SEED? "); + temp = input.nextInt(); + if (temp < 0) + epicFail(0); + if (temp > acresOwned) + System.out.println("HAMURABI: THINK AGAIN. YOU OWN ONLY " + acresOwned + " ACRES. NOW THEN,"); + if (temp / 2 > bushels) + System.out.println("HAMURABI: THINK AGAIN. YOU HAVE ONLY\n" + + bushels + " 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 > acresOwned || temp / 2 > bushels || temp > population * 10); + return temp; + } + // Display Methods + 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 " + yield + " 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; + } @@ -132,6 +187,7 @@ private void newYear1() { landPrice = (int) (10 * Math.random() + 17); // System.out.println(report()); // USER UPDATING ACRES + /* do { System.out.print("HOW MANY ACRES DO YOU WISH TO BUY? "); temp = input.nextInt(); @@ -141,8 +197,10 @@ private void newYear1() { 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(); @@ -151,10 +209,13 @@ private void newYear1() { if (temp > acres) System.out.println("HAMURABI: THINK AGAIN. YOU OWN ONLY " + acres + " ACRES. NOW THEN,"); } while (temp > acres); + + */ stores += temp * landPrice; acres -= temp; // USER FEEDING PEOPLE + /* do { System.out.print("\nHOW MANY BUSHELS DO YOU WISH TO FEED YOUR PEOPLE? "); temp = input.nextInt(); @@ -164,10 +225,13 @@ private void newYear1() { System.out.println("HAMURABI: THINK AGAIN. YOU HAVE ONLY\n" + stores + " BUSHELS OF GRAIN. NOW THEN,"); } while (temp > stores); + + */ fullPeople = temp / 20; stores -= temp; // USER PLANTING SEED + /* do { System.out.print("\nHOW MANY ACRES DO YOU WISH TO PLANT WITH SEED? "); temp = input.nextInt(); @@ -181,9 +245,11 @@ private void newYear1() { 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; + yield = (int) (Math.random() * 5 + 1); + harvest = temp * yield; temp = (int) (Math.random() * 5 + 1); if (temp % 2 != 1) eaten = (stores / temp); @@ -191,7 +257,7 @@ private void newYear1() { eaten = 0; stores += (harvest - eaten); - + /* // CALCULATING IMMIGRANTS immigrants = (int) (Math.random() * 5 + 1) * (20 * acres + stores) / population / 100 + 1; @@ -209,25 +275,13 @@ private void newYear1() { 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) { From 037d6de7305f58f7a4b9247c425bb8518276a804 Mon Sep 17 00:00:00 2001 From: olamide Date: Sat, 15 Jul 2023 17:50:02 -0400 Subject: [PATCH 6/6] update --- src/main/java/Hammurabi.java | 56 ++++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 12 deletions(-) diff --git a/src/main/java/Hammurabi.java b/src/main/java/Hammurabi.java index 8a811ba..3966541 100644 --- a/src/main/java/Hammurabi.java +++ b/src/main/java/Hammurabi.java @@ -1,6 +1,6 @@ import java.io.IOException; import java.util.Scanner; - +import java.util.Random; public class Hammurabi { public static void main(String[] args) throws IOException { @@ -32,6 +32,7 @@ public static void main(String[] args) throws IOException { static boolean plague = false; // If there is a plague or not Scanner input = new Scanner(System.in); // Scanner to take in user input + public Random rand = new Random(); // People static int totalDeaths = 0; // Total number of deaths from starvation static int population = 95; // Current population @@ -51,7 +52,7 @@ public static void main(String[] args) throws IOException { // Other fields int temp; // Temporary value to hold input from user - int eaten = harvest - stores, fullPeople; + static int eaten = harvest - stores, fullPeople; final static String FINK = "DUE TO THIS EXTREME MISMANAGEMENT YOU HAVE NOT ONLY\n" + @@ -89,9 +90,9 @@ public int immigrants(int population, int acresOwned, int grainInStorage){ // TODO OLAMIDE public int newCostOfLand() { - int price = (int) (Math.random() *6) + 17; + int price = (int) (rand.nextInt(7) + 17); - return 0; + return price; } @@ -172,8 +173,8 @@ private void newYear1() { 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; + yield = (int) (Math.random() * 5 + 1); + harvest = temp * yield; temp = (int) (Math.random() * 5 + 1); if (temp % 2 != 1) eaten = (stores / temp); @@ -208,7 +209,7 @@ private static String report() { } answer += "POPULATION IS NOW " + population + ".\n" + "THE CITY NOW OWNS " + acres + " ACRES.\n" + - "YOU HARVESTED " + yeild + " BUSHELS PER ACRE.\n" + + "YOU HARVESTED " + yield + " 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."; @@ -254,25 +255,56 @@ else if (percentDied > 3 || acres / population < 10) } public int plagueDeaths(int population){ - return 0; + int chance = rand.nextInt(100); + if (chance < 15 ) { + return population / 2; + } else { + + return 0; + } } public int starvationDeaths(int population, int bushelsFedToPeople){ - return 0; + int starving = population - (bushelsFedToPeople / 20); + + if (bushelsFedToPeople >= (population * 20)) { + return 0; + } else { + + return starving; + } + } public boolean uprising(int population, int howManyPeopleStarved){ - return true; + if (howManyPeopleStarved > (0.45 * population)) { + return true; + } else { + + return false; + } } public int harvest(int acres){ - return 0; + int yield = rand.nextInt(6) + 1; + int harvested = yield * acres; + + return harvested; } public int grainEatenByRats(int bushels){ - return 0; + int chance = rand.nextInt(100); + + if (chance < 40) { + double percentEatenByRats = (rand.nextDouble() /5 + 0.1); + double bushelsEatenByRats = bushels * (percentEatenByRats); + return (int) bushelsEatenByRats; + } else { + return 0; + } + }