diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..70c1730 Binary files /dev/null and b/.DS_Store differ diff --git a/.idea/.DS_Store b/.idea/.DS_Store new file mode 100644 index 0000000..5a17801 Binary files /dev/null and b/.idea/.DS_Store differ 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/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..cc644d4 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__junit_junit_4_13_2.xml b/.idea/libraries/Maven__junit_junit_4_13_2.xml new file mode 100644 index 0000000..606c352 --- /dev/null +++ b/.idea/libraries/Maven__junit_junit_4_13_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_apiguardian_apiguardian_api_1_1_2.xml b/.idea/libraries/Maven__org_apiguardian_apiguardian_api_1_1_2.xml new file mode 100644 index 0000000..6ac1c42 --- /dev/null +++ b/.idea/libraries/Maven__org_apiguardian_apiguardian_api_1_1_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml new file mode 100644 index 0000000..f58bbc1 --- /dev/null +++ b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_5_9_1.xml b/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_5_9_1.xml new file mode 100644 index 0000000..182f93a --- /dev/null +++ b/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_5_9_1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_api_5_9_1.xml b/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_api_5_9_1.xml new file mode 100644 index 0000000..f88d0a9 --- /dev/null +++ b/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_api_5_9_1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_engine_5_9_1.xml b/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_engine_5_9_1.xml new file mode 100644 index 0000000..7d13c06 --- /dev/null +++ b/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_engine_5_9_1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_params_5_9_1.xml b/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_params_5_9_1.xml new file mode 100644 index 0000000..1cbbb38 --- /dev/null +++ b/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_params_5_9_1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_junit_platform_junit_platform_commons_1_9_1.xml b/.idea/libraries/Maven__org_junit_platform_junit_platform_commons_1_9_1.xml new file mode 100644 index 0000000..79aa3d3 --- /dev/null +++ b/.idea/libraries/Maven__org_junit_platform_junit_platform_commons_1_9_1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_junit_platform_junit_platform_engine_1_9_1.xml b/.idea/libraries/Maven__org_junit_platform_junit_platform_engine_1_9_1.xml new file mode 100644 index 0000000..31a3ca5 --- /dev/null +++ b/.idea/libraries/Maven__org_junit_platform_junit_platform_engine_1_9_1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_opentest4j_opentest4j_1_2_0.xml b/.idea/libraries/Maven__org_opentest4j_opentest4j_1_2_0.xml new file mode 100644 index 0000000..fbc1b16 --- /dev/null +++ b/.idea/libraries/Maven__org_opentest4j_opentest4j_1_2_0.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..0f8e7ab --- /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..16ae36c --- /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..8306744 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/HammurabiTest.java b/HammurabiTest.java index 5cf1cc9..de9fa7d 100644 --- a/HammurabiTest.java +++ b/HammurabiTest.java @@ -1,5 +1,3 @@ -package hammurabi; - import static org.junit.Assert.*; import org.junit.Before; @@ -8,7 +6,7 @@ public class HammurabiTest { Hammurabi ham; - + boolean about(double expected, double actual) { return actual > 0.90 * expected && actual < 1.10 * expected; } diff --git a/Hamurabi.iml b/Hamurabi.iml new file mode 100644 index 0000000..679a871 --- /dev/null +++ b/Hamurabi.iml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index e4672c9..e465791 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ 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 +public class GameEngine { // must save in a file named GameEngine.java Random rand = new Random(); // this is an instance variable Scanner scanner = new Scanner(System.in); diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..3c17dce --- /dev/null +++ b/pom.xml @@ -0,0 +1,30 @@ + + + 4.0.0 + + groupId + Hamurabi + 1.0-SNAPSHOT + + + org.junit.jupiter + junit-jupiter + RELEASE + test + + + junit + junit + 4.13.2 + test + + + + 19 + 19 + + + + \ No newline at end of file diff --git a/src/.DS_Store b/src/.DS_Store new file mode 100644 index 0000000..44ae82a Binary files /dev/null and b/src/.DS_Store differ diff --git a/src/main/java/App.java b/src/main/java/App.java new file mode 100644 index 0000000..561a086 --- /dev/null +++ b/src/main/java/App.java @@ -0,0 +1,9 @@ +public class App { + public static void main(String[] args) { + + UserInterface ui = new UserInterface(); + + ui.main(); + + } +} diff --git a/src/main/java/Console.java b/src/main/java/Console.java new file mode 100644 index 0000000..4413e44 --- /dev/null +++ b/src/main/java/Console.java @@ -0,0 +1,241 @@ +import java.util.InputMismatchException; +import java.util.Scanner; + +public class Console { + Scanner scanner = new Scanner(System.in); + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// INPUT +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + public int acresToBuySOUT(int price){ + int acresToBuy = getNumber("How many acres would you like to buy? The price is currently " + price + " bushels per acre."); +// int acresToBuy = getNumber("How many galaxies would you like to buy? The price is currently " + price + " stars per galaxy."); + return acresToBuy; + } + + public int acresToSellSOUT(int price){ + int acresToSell = getNumber("How many acres would you like to sell? The Price is currently " + price + " bushels per acre."); +// int acresToSell = getNumber("How many galaxies would you like to sell? The price is currently " + price + " stars per galaxy."); + return acresToSell; + } + + public int bushelsToFeed(){ + int bushelsFed = getNumber("How many bushels would you like to feed your nation?"); +// int bushelsFed = getNumber("How many stars would you like to feed your aliens with?"); + return bushelsFed; + } + + public int acresToPlant(){ +// int acresPlanted = getNumber("How many galaxies to duplicate stars?"); + int acresPlanted = getNumber("How many acres to plant?"); + return acresPlanted; + } + + public boolean exitGame() { + System.out.println("\n"); + int options = getNumber("Would you like to play again? 1 = Yes, 2 = No"); + boolean answer = true; + switch (options) { + case 1: + System.out.println("That's the spirit! Let's play again!"); + answer = true; + break; + case 2: + System.out.println("Goodbye :-("); + answer = false; + break; + } + return answer; + } + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// OUTPUT +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + public void announceGame(int population, int bushels, int landsOwned, int price) { + System.out.println("\n"); + System.out.println("Congratulations, you are the newest ruler of ancient Sumer, elected for a ten year term of office."); + System.out.println("Your duties are to dispense food, direct farming, and buy and sell land as needed to support your people."); + System.out.println("Grain is the general currency, measured in bushels."); + System.out.println("\n"); + System.out.println("The following will help you in your decisions:"); + System.out.println("\n"); + + System.out.println("**Each person needs at least 20 bushels of grain per year to survive**"); + System.out.println("**Each person can farm at most 10 acres of land**"); + System.out.println("**It takes 1 bushel of grain to farm an acre of land**"); + System.out.println("**The market price for land fluctuates yearly**"); + System.out.println("**It's recommended to calculate before answering, there is no turning back**"); + System.out.println("\n"); + System.out.println("**WARNING!!!**"); + System.out.println("**THE GAME IS OVER WHEN DEATH BY STARVATION IS MORE THAN 45% OF CURRENT POPULATION!!!**"); + System.out.println("**THE GAME IS OVER WHEN ACRES ARE LESS THAN 7 PER POPULATION!!!**"); + System.out.println("\n"); + System.out.println("Now, let's begin!"); + System.out.println("\n"); + System.out.println("O great One! Your kingdom await your orders!"); + System.out.println("There are " + population + " people that you must feed!"); + System.out.println("Our coffers have " + bushels + " bushels in storage."); + System.out.println("The city owns " + landsOwned + " acres of land."); + System.out.println("Land is available to purchase for " + price + " bushels per acre"); + System.out.println("Rats destroyed 200 bushels"); + System.out.println("\n"); +// System.out.println("\n"); +// System.out.println("Greetings, commander. You have been selected by the Universe of ZCW8.2 to lead for ten million years."); +// System.out.println("For the sake of the aliens, you need to feed them with stars, and using galaxies to duplicate the stars"); +// System.out.println("You may also buy and sell galaxies by the transaction of stars"); +// System.out.println("\n"); +// System.out.println("The following will help you in your decisions:"); +// System.out.println("\n"); +// System.out.println("**Each alien needs at least 20 stars per 1 million years to survive**"); +// System.out.println("**Each alien can utilize at most 10 galaxies to grow stars**"); +// System.out.println("**To duplicate stars, each galaxy needs 1 star**"); +// System.out.println("**The stars cost for galaxy fluctuates every 1 million years**"); +// System.out.println("**It's recommended to calculate before answering, there is no turning back**"); +// System.out.println("\n"); +// System.out.println("**WARNING!!!**"); +// System.out.println("**THE GAME IS OVER WHEN DEATH BY STARVATION IS MORE THAN 45% OF CURRENT ALIENS!!!**"); +// System.out.println("**THE GAME IS OVER WHEN GALAXIES ARE LESS THAN 7 PER ALIEN!!!**"); +// System.out.println("\n"); +// System.out.println("Now, let's begin!"); +// System.out.println("\n"); +// System.out.println("Commander, ZCW8.2 is currently waiting for your order."); +// System.out.println("There are " + population + " aliens that you must feed"); +// System.out.println("Our wormhole currently has " + bushels + " stars in dimension"); +// System.out.println("ZCW8.2 has " + landsOwned + " galaxies in our possession"); +// System.out.println("Galaxies are available for trading for " + price + " stars"); +// System.out.println("Space pirates destroyed 200 stars"); +// System.out.println("\n"); + } + + public void printSummary(int year, int immigrants, int sDeaths, int pDeaths, int population, int rats, int bushels, int landsOwned, int price) { +// System.out.println("\n"); +// System.out.println("\n"); +// System.out.println("Greetings, Commander"); +// System.out.println("You are now in year " + year + " million of your ten million year rule"); +// System.out.println("\n"); +// System.out.println("In the previous 1 million years, " + immigrants + " aliens came to ZCW8.2"); +// System.out.println("In the previous 1 million years, " + sDeaths + " aliens starved to death"); +// System.out.println("In the previous 1 million years, " + pDeaths + " aliens died from EMPs"); +// System.out.println("The population is now " + population); +//// System.out.println("We harvested " + state.getHarvest() + " bushels"); <-Bugged due to RNG, DO WE WANT TO FIX THIS? --EMORY +// System.out.println("Space pirates destroyed " + rats + " stars"); +// System.out.println("There are " + bushels + " stars left in the wormhole"); +// System.out.println("ZCW8.2 has " + landsOwned + " galaxies in our possession"); +// System.out.println("Galaxies are available for trading for " + price + " stars"); +// System.out.println("\n"); +// } + System.out.println("\n"); + System.out.println("\n"); + System.out.println("O great Hammurabi"); + System.out.println("You are now in year " + year + " of your ten year rule."); + System.out.println("\n"); + System.out.println("In the previous year " + immigrants + " people came to the kingdom."); + System.out.println("In the previous year " + sDeaths + " people starved to death."); + System.out.println("In the previous year " + pDeaths + " people died from plague"); + System.out.println("The population is now " + population); +// System.out.println("We harvested " + state.getHarvest() + " bushels"); <-Bugged due to RNG, DO WE WANT TO FIX THIS? --EMORY + System.out.println("Rats destroyed " + rats + " bushels"); + System.out.println("There are only " + bushels + " bushels left in storage."); + System.out.println("The city owns " + landsOwned + " acres of land."); + System.out.println("Land is currently worth " + price + " bushels per acre"); + System.out.println("\n"); +} + + public void prompt(int bushels, int population, int landsOwned) { //<-Newly added + System.out.println("\n"); + System.out.println("Bushels left: " + bushels + + "\npopulation number: " + population + + "\nAcres: " + landsOwned); + System.out.println("\n"); + } +// System.out.println("\n"); +// System.out.println("Stars remaining: " + bushels +// + "\nAliens number: " + population +// + "\nGalaxies: " + landsOwned); +// System.out.println("\n"); +// } + + public void gameEndingPrompt(int deathsPool, int immigrantsPool, int population, int landsOwned) { //<-Newly added +// System.out.printf("You completed the game with %.4f%% starvation deaths out of %s\n", +// (double) deathsPool / (1000 + immigrantsPool) * 100, population); +// System.out.printf("You completed the game with %.4f acres per population\n", +// (double) landsOwned / population); + + System.out.printf("You completed the game with %.4f%% starvation deaths, %s deaths by starvation vs %s of the total population\n", + (double) deathsPool / (100 + immigrantsPool) * 100, deathsPool, 100 + immigrantsPool); + System.out.printf("You completed the game with %.4f acres per population\n", + (double) landsOwned / population); + } +// System.out.printf("You completed the game with %.4f%% starvation deaths, %s deaths by starvation vs %s total population\n", +// (double) deathsPool / (100 + immigrantsPool) * 100, deathsPool, 100 + immigrantsPool); +// System.out.printf("You completed the game with %.4f galaxies per alien\n", +// (double) landsOwned / population); +// } + + public void monster() { + System.out.println("\n"); + System.out.println("You MONSTER!! You killed so many people!! WE WILL AVENGE THEM!!!!!!"); + System.out.println("\n"); + } +// System.out.println("\n"); +// System.out.println("Due to your terrible decisions making, you are hereby dethroned from your Commander position and are being hunted by the aliens."); +// System.out.println("\n"); +// } + + public void boring() { + System.out.println("You brought great... okayness... to our kingdom... thanks for not killing all of us?"); + System.out.println("\n"); + System.out.println("You have brought normalization to your kingdom. You are one boring ruler!"); + System.out.println("\n"); + } +// System.out.println("\n"); +// System.out.println("You have made it to the end of the 10 million years without much damage or extra miles. The aliens thanked you."); +// System.out.println("\n"); +// } + + public void thriving() { + System.out.println("\n"); + System.out.println("Your kingdom is thriving! You are one superb ruler!"); + System.out.println("\n"); + } +// System.out.println("\n"); +// System.out.println("On behalf of ZCW8.2, we thank you for your effort to drive the Universe forward. GLaDOS have a cake for you."); +// System.out.println("\n"); +// } + + public void guts(){ + System.out.println("\n"); + System.out.println("While you brought your kingdom to make it through the years, you are " + + "still a terrible ruler. Your people hated your guts!"); + System.out.println("\n"); + } +// System.out.println("\n"); +// System.out.println("You have made it to the end of 10 million years. However, the aliens are currently building a time machine to undo your mistakes."); +// System.out.println("\n"); +// } +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// SCANNERS +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + int getNumber(String message) { + while (true) { + System.out.println(message); + try { + return scanner.nextInt(); + } catch (InputMismatchException e) { + System.out.println("\"" + scanner.next() + "\" isn't a number!"); + } + } + } + + String getString(String message) { + while (true) { + System.out.println(message); + try { + return scanner.nextLine(); + } catch (InputMismatchException e) { + System.out.println("\"" + scanner.next() + "\" isn't a word!"); + } + } + } +} \ No newline at end of file diff --git a/src/main/java/GameEngine.java b/src/main/java/GameEngine.java new file mode 100644 index 0000000..c3be573 --- /dev/null +++ b/src/main/java/GameEngine.java @@ -0,0 +1,123 @@ +import java.util.*; + +import static java.lang.Math.floor; + +public class GameEngine { + + Random random = new Random(); + + // ***************************************************************************************************************** + // ***************** GAME INITIALIZATION/ROUND INITILIZATION/EXIT + + public GameEngine() { + } + + public int calculateAcresToBuy(int acresToBuy, int price, int bushels){ + if ( (acresToBuy * price) > bushels){ + System.out.println("You FOOL! You're trying to buy more than what you can afford, you get nothing and like it!"); + return 0; + } else { + return acresToBuy; + } + } +// if ( (acresToBuy * price) > bushels){ +// System.out.println("Commander, since you are being greedy, ZCW8.2 malfunctions and hereby unable to purchase any galaxies for 1 million years"); +// return 0; +// } else { +// return acresToBuy; +// } +// } + + public int calculateAcresToSell(int acresToSell, int landOwned) { + if (acresToSell > landOwned) { + System.out.println("Selling too much than what you own!"); + return 0; + } else { + return acresToSell; + } + } +// if (acresToSell > landOwned) { +// System.out.println("Commander, since you are being greedy, ZCW8.2 malfunctions and hereby unable to sell any galaxies for 1 million years"); +// return 0; +// } else { +// return acresToSell; +// } +// } + + public int calculateGrainToFeedPeople(int bushels, int bushelsFed) { + if (bushelsFed > bushels){ + return 0; + } else { + return bushelsFed; + } + } + + public int calculateAcresToPlant(int acresToPlant, int acresOwned, int population, int bushels) { + + //Tested my own method, 7 tests all passed + while(true) { + if (acresToPlant > population*10) { + acresToPlant = population*10; + } else if (acresToPlant > acresOwned) { + acresToPlant = acresOwned; + } else if (acresToPlant > bushels) { + acresToPlant = bushels; + } + if (acresToPlant <= population*10 && + acresToPlant <= acresOwned && + acresToPlant <= bushels) { + break; + } + } +// System.out.println("Update: " + acresToPlant + " " + acresOwned + " " + population + " " + bushels); + return acresToPlant; + } + //Original method + +// if (acresToPlant > acresOwned){ +// System.out.println("You dum dum, you only have " + acresOwned + " acres!"); +// return 0; +// } else if ( acresToPlant/10 > population){ +// System.out.println("You don't have enough people to plant that many acres!!"); +// return 0; +// } else if (acresToPlant/2 > bushels){ +// System.out.println("You don't have enough grain to plant that many acres!"); +// return 0; +// } else { +// return acresToPlant; // do state.changeBushels((acresToPlant*2)*-1) in the UserInterface class... +// } +// } + + // ***************************************************************************************************************** + // ***************** SUPPLEMENTARY FEATURES + + public int plagueDeaths(int population) { + if(random.nextInt(101) < 15) return population/2; + return 0; + } + + public int starvationDeaths(int population, int bushelsFedToPeople) { + if(population - (int) floor(bushelsFedToPeople/20) < 0) return 0; + return population - (int) floor(bushelsFedToPeople/20); + } + + public boolean uprising(int population, int howManyPeopleStarved) { + if ((double) howManyPeopleStarved / population > 0.45) return true; + return false; + } + + public int immigrants (int population, int acresOwned, int grainInStorage, int bushelsFedToPeople) { + if(starvationDeaths(population, bushelsFedToPeople) > 0) return 0; + return (20 * acresOwned + grainInStorage) / (100 * population) + 1; + } + + public int harvest (int bushelsUsedAsSeed) { + //Returning for the number of bushels harvested per acre + return (random.nextInt(6)+1)*bushelsUsedAsSeed; + } + + public int grainEatenByRats (int bushels) { + if(random.nextInt(100) < 40) return (random.nextInt(21)+10)*bushels/100; + return 0; + } +} diff --git a/src/main/java/State.java b/src/main/java/State.java new file mode 100644 index 0000000..bb111ee --- /dev/null +++ b/src/main/java/State.java @@ -0,0 +1,172 @@ +import java.util.Random; + +public class State { + int population, landsOwned, bushels, price, year; + int pDeaths, sDeaths, sDeathsPool, immigrants, harvest, rats, immigrantsPool; + Random random = new Random(); + + public State(){ + } + +// public State(int population, int landsOwned, int bushels, int year, int price, int pDeaths, int sDeaths, int immigrants, int harvest, int rats) { +// setPopulation(population); //<-using setters instead of instance variable +// setLandsOwned(landsOwned); +// setBushels(bushels); +// setYear(year); +// setPrice(price); +// setpDeaths(pDeaths); +// setsDeaths(sDeaths); +// setImmigrants(immigrants); +// setHarvest(harvest); +// setRats(rats); + + // this.population = population; +// this.landsOwned = landsOwned; +// this.bushels = bushels; +// this.year = year; +// this.price = price; +// this.pDeaths = pDeaths; +// this.sDeaths = sDeaths; +// this.immigrants = immigrants; +// this.harvest = harvest; +// this.rats = rats; +// } + + public State(int population, int landsOwned, int bushels, int year, int price){ + setPopulation(population); //<-Using setters instead of instance variable + setLandsOwned(landsOwned); + setBushels(bushels); + setYear(year); + setPrice(price); + setpDeaths(0); + setsDeaths(0); + setImmigrants(immigrants); + setHarvest(harvest); + setRats(rats); +// +// this.population = population; +// this.landsOwned = landsOwned; +// this.bushels = bushels; +// this.year = year; +// this.price = price; +// this.pDeaths = 0; +// this.sDeaths = 0; +// this.immigrants = 0; +// this.harvest = 0; +// this.rats = 0; + } + + public int getPopulation() { + return population; + } + + public void setPopulation(int population) { + this.population = population; + } + + public void changePopulation(int populationChange) { + this.population += populationChange; + } + + public int getLandsOwned() { + return landsOwned; + } + + public void setLandsOwned(int landsOwned) { + this.landsOwned = landsOwned; + } + + public void changeLandsOwned(int acresToChange) { + this.landsOwned += acresToChange; + } + + public int newCostOfLand() { + return random.nextInt(7)+17; + } + + public int getBushels() { + return bushels; + } + + public void setBushels(int bushels) { + this.bushels = bushels; + } + public void changeBushels(int bushelsChanged) { + this.bushels += bushelsChanged; + } + + public int getPrice() { + return price; + } + + public void setPrice(int price) { + this.price = price; + } + + public int getYear() { + return year; + } + + public void setYear(int year) { + this.year = year; + } + + public void incrementYear() { + this.year += 1; + } + public int getpDeaths() { + return pDeaths; + } + + public void setpDeaths(int pDeaths) { + this.pDeaths = pDeaths; + } + + public int getsDeaths() { + return sDeaths; + } + + public void setsDeaths(int sDeaths) { + this.sDeaths = sDeaths; + } + + public int getImmigrants() { + return immigrants; + } + + public void setImmigrants(int immigrants) { + this.immigrants = immigrants; + } + + public int getHarvest() { + return harvest; + } + + public void setHarvest(int harvest) { + this.harvest = harvest; + } + + public int getRats() { + return rats; + } + + public void setRats(int rats) { + this.rats = rats; + } + + public int getsDeathsPool() { + return sDeathsPool; + } + + public void setsDeathsPool(int sDeathsPool) { + this.sDeathsPool = sDeathsPool; + } + + public int getImmigrantsPool() { + return immigrantsPool; + } + + public void setImmigrantsPool(int immigrantsPool) { + this.immigrantsPool = immigrantsPool; + } +} diff --git a/src/main/java/UserInterface.java b/src/main/java/UserInterface.java new file mode 100644 index 0000000..da616d3 --- /dev/null +++ b/src/main/java/UserInterface.java @@ -0,0 +1,184 @@ +public class UserInterface { + + GameEngine game = new GameEngine(); + Console console = new Console(); + State state = new State(); + + + public void main() { + + while (true) { + setUpNewGame(); + console.announceGame(state.getPopulation(), state.getBushels(), state.getLandsOwned(), state.getPrice()); + + while (state.getYear() < 11) { + + // Asks how many acres to buy + askHowManyAcresToBuy(console.acresToBuySOUT(state.getPrice())); + + // Asks how many acres to sell + askHowManyAcresToSell(console.acresToSellSOUT(state.getPrice())); + + if (state.getLandsOwned() < state.getPopulation() * 7) { //<-updated gameover + console.monster(); + break; + } + + // Asks how many bushels to feed to people + int bushelsFed = console.bushelsToFeed(); + askHowManyBushelsToFeedPeople(bushelsFed); + if (game.starvationDeaths(state.getPopulation(), bushelsFed) > state.getPopulation() * 45 / 100) { + + console.monster(); //<-updated gameover + break; + } + + // Asks how many acres to plant with grain + int acresPlanted = console.acresToPlant(); + askHowManyAcresToPlant(acresPlanted); + + //Process the round, print data, and initialize the next round + + processRound(state.getPopulation(), bushelsFed, state.getLandsOwned(), acresPlanted * 2, state.getBushels()); + + console.printSummary(state.getYear(), state.getImmigrants(), state.sDeaths, state.pDeaths, state.getPopulation(), state.getRats(), state.getBushels(), state.getLandsOwned(), state.getPrice()); + + initializeRound(); + + } + + if (state.getYear() == 11) { //<-newly added, it works + console.gameEndingPrompt(state.getsDeathsPool(), state.getImmigrantsPool(), state.getPopulation(), state.getLandsOwned()); + badEnding(); + normalEnding(); + goodEnding(); + break; + } +// if (console.exitGame() == false) break; + } + } + + public void setUpNewGame() { + state.setPopulation(100); + state.setLandsOwned(1000); + state.setBushels(2800); + state.setYear(0); + state.setPrice(19); + state.setpDeaths(0); + state.setsDeaths(0); + state.setImmigrants(0); + state.setHarvest(0); + state.setRats(0); + } + + public void initializeRound() { + state.setpDeaths(0); + state.setsDeaths(0); + state.setImmigrants(0); + state.setHarvest(0); + state.setRats(0); + } + + public void processRound(int population, int bushelsFedToPeople, int acresOwned, int bushelsUsedAsSeed, int bushels) { + + state.setImmigrants(game.immigrants(population, acresOwned, bushels, bushelsFedToPeople)); + state.changePopulation(state.getImmigrants()); + + state.setpDeaths(game.plagueDeaths(state.getPopulation())); + state.changePopulation(-state.getpDeaths()); + + state.setsDeaths(game.starvationDeaths(population, bushelsFedToPeople)); + state.changePopulation(-state.getsDeaths()); + +// state.setHarvest(game.harvest(bushelsUsedAsSeed)); +// state.changeBushels(state.getHarvest()); <-Bugged due to RNG, recommend not use DO WE WANT TO FIX THIS??? -- EMORY + + state.setRats(game.grainEatenByRats(bushels)); + state.changeBushels(-state.getRats()); + + state.setPrice(state.newCostOfLand()); + + state.setsDeathsPool(state.getsDeathsPool() + state.getsDeaths()); + + state.setImmigrantsPool(state.getImmigrantsPool() + state.getImmigrants()); + + state.incrementYear(); + } + + public void askHowManyAcresToBuy(int acresToBuy) { + if (game.calculateAcresToBuy(acresToBuy, state.getPrice(), state.getBushels()) != 0) { + + state.changeLandsOwned(acresToBuy); + state.changeBushels(-(acresToBuy * state.getPrice())); + + console.prompt(state.getBushels(), state.getPopulation(), state.getLandsOwned()); + } + } + + public void askHowManyAcresToSell(int acresToSell) { + if (game.calculateAcresToSell(acresToSell, state.getLandsOwned()) != 0) { + + state.changeLandsOwned(-acresToSell); + state.changeBushels(acresToSell * state.getPrice()); + + console.prompt(state.getBushels(), state.getPopulation(), state.getLandsOwned()); + } + } + + public void askHowManyBushelsToFeedPeople(int bushelsFed) { + + if (game.calculateGrainToFeedPeople(state.getBushels(), bushelsFed) != 0) { + + state.changeBushels(-bushelsFed); + + console.prompt(state.getBushels(), state.getPopulation(), state.getLandsOwned()); + + } else if (game.calculateGrainToFeedPeople(state.getBushels(), bushelsFed) == 0) { + if (game.starvationDeaths(state.getPopulation(), game.calculateGrainToFeedPeople(state.getBushels(), bushelsFed)) > state.getPopulation() * 45 / 100) { + + console.monster(); //<-updated gameover +// console.exitGame(); + + } + } + } + + public void askHowManyAcresToPlant(int acresPlanted) { + acresPlanted = game.calculateAcresToPlant(acresPlanted, state.getLandsOwned(), state.getPopulation(), state.getBushels()); + + state.changeBushels(-acresPlanted); + state.changeBushels(game.harvest(acresPlanted)); + + console.prompt(state.getBushels(), state.getPopulation(), state.getLandsOwned()); + } + + public void badEnding() { //<- Newly added + if ((double)state.getsDeathsPool() / (100 + state.getImmigrantsPool()) * 100 >= 10 && (double)state.getLandsOwned() / state.getPopulation() <= 9 || + (double)state.getsDeathsPool() / (100 + state.getImmigrantsPool()) * 100 >= 10 && (double)state.getLandsOwned() / state.getPopulation() > 9 || + (double)state.getsDeathsPool() / (100 + state.getImmigrantsPool()) * 100 < 10 && (double)state.getLandsOwned() / state.getPopulation() <= 9) { + + console.guts(); + } + } + + public void normalEnding() { //<-Newly added + if ((double)state.getsDeathsPool() / (100 + state.getImmigrantsPool()) * 100 < 10 && (double)state.getsDeathsPool() / (100 + state.getImmigrantsPool()) * 100 >= 3 && + (double)state.getLandsOwned() / state.getPopulation() > 9 && (double)state.getLandsOwned() / state.getPopulation() <= 10 || + (double)state.getsDeathsPool() / (100 + state.getImmigrantsPool()) * 100 < 10 && (double)state.getsDeathsPool() / (100 + state.getImmigrantsPool()) * 100 >= 3 && + (double)state.getLandsOwned() / state.getPopulation() > 9 || + (double)state.getsDeathsPool() / (100 + state.getImmigrantsPool()) * 100 < 3 && + (double)state.getLandsOwned() / state.getPopulation() > 9 && (double)state.getLandsOwned() / state.getPopulation() <= 10) { + + console.boring(); + } + } + + public void goodEnding() { //<-Newly added + if ((double)state.getsDeathsPool() / (100 + state.getImmigrantsPool()) * 100 < 3 && (double)state.getLandsOwned() / state.getPopulation() > 10) { + + console.thriving(); + + } + } + +} \ No newline at end of file diff --git a/src/test/java/GameEngineTest.java b/src/test/java/GameEngineTest.java new file mode 100644 index 0000000..587b5cc --- /dev/null +++ b/src/test/java/GameEngineTest.java @@ -0,0 +1,214 @@ +import org.junit.Assert; +import org.junit.jupiter.api.Test; + +import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + + +class GameEngineTest { + + GameEngine game = new GameEngine(); //<-Added this for test + State state = new State();//<-Added this for test + boolean about(double expected, double actual) { + return actual > 0.90 * expected && actual < 1.10 * expected; + } + + @Test + public void HowManyAcresToBuyTest1() { + //If price of acres are at maximum of 24 + state.setBushels(1000); + state.setLandsOwned(100); + int acresBought = game.calculateAcresToBuy(40, state.newCostOfLand(), state.getBushels()); + Assert.assertEquals(40, acresBought); + } + @Test + public void HowManyAcresToBuyTest2() { + //If price of acres are at minimum of 17 + state.setBushels(679); + state.setLandsOwned(100); + int acresBought = game.calculateAcresToBuy(40, state.newCostOfLand(), state.getBushels()); + Assert.assertEquals(0, acresBought); + } + @Test + public void HowManyAcresToBuyTest3() { + //If price of acres are random + state.setBushels(800); + state.setLandsOwned(100); + int acresBought = game.calculateAcresToBuy(40, state.newCostOfLand(), state.getBushels()); + Assert.assertTrue("Acres bought is " + acresBought, + acresBought == 40 || acresBought == 0); + } + @Test + public void HowManyAcresToBuyTest4() { + //Updated acres Owned + state.setBushels(800); + state.setLandsOwned(100); + state.setLandsOwned(100); + Assert.assertTrue("Acres bought is " + state.getLandsOwned(), + state.getLandsOwned() == 140 || state.getLandsOwned() == 100); + } + + + @Test + public void howManyAcresToPlantTest1() { //Added + //If acres to be planted is more than acres owned + state.setLandsOwned(1000); + state.setPopulation(100); + state.setBushels(1001); + Assert.assertEquals(1000, game.calculateAcresToPlant(1001, state.getLandsOwned(),state.getPopulation(), state.getBushels())); + } + @Test + public void howManyAcresToPlantTest2() { //Added + //If acres to be planted is more than population * 10 + state.setLandsOwned(1000); + state.setPopulation(90); + state.setBushels(1001); + Assert.assertEquals(900, game.calculateAcresToPlant(1000, state.getLandsOwned(),state.getPopulation(), state.getBushels())); + } + @Test + public void howManyAcresToPlantTest3() { //Added + //If acres to be planted is more than bushels + state.setLandsOwned(1100); + state.setPopulation(100); + state.setBushels(900); + Assert.assertEquals(900, game.calculateAcresToPlant(1000, state.getLandsOwned(),state.getPopulation(), state.getBushels())); + } + @Test + public void howManyAcresToPlantTest4() { //Added + //If acres to be planted is more than bushels and bushels is more than population + state.setLandsOwned(1100); + state.setPopulation(65); + state.setBushels(900); + Assert.assertEquals(650, game.calculateAcresToPlant(1000, state.getLandsOwned(), state.getPopulation(), state.getBushels())); + } + + @Test + public void howManyAcresToPlantTest5() { //Added + //If acres to be planted is more than population and population is more than acres owned + state.setLandsOwned(500); + state.setPopulation(65); + state.setBushels(1000); + Assert.assertEquals(500, game.calculateAcresToPlant(1000, state.getLandsOwned(),state.getPopulation(), state.getBushels())); + } + + @Test + public void howManyAcresToPlantTest6() { //Added + //If acres to be planted is more than acres owned and acres owned is more than bushels + state.setLandsOwned(800); + state.setPopulation(65); + state.setBushels(500); + Assert.assertEquals(500, game.calculateAcresToPlant(1000, state.getLandsOwned(),state.getPopulation(), state.getBushels())); + } + @Test + public void howManyAcresToPlantTest7() { //Added + //If everything is lower than acres to be planted + state.setLandsOwned(900); + state.setPopulation(90); + state.setBushels(800); + Assert.assertEquals(800, game.calculateAcresToPlant(1000, state.getLandsOwned(),state.getPopulation(), state.getBushels())); + } + + @Test + public final void testPlagueDeaths1() { + GameEngine game = new GameEngine(); + int number_of_plagues = 0; + for (int i = 0; i < 10000; i++) { + int deaths = game.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() { + GameEngine game = new GameEngine(); + int deaths = 0; + for (int i = 0; i < 10000; i++) { + deaths = game.plagueDeaths(100); + if (deaths > 0) break; + } + assertEquals("In a plague, " + deaths + "% of your people die, not 50%.", + 50, deaths); + } + + @Test + public final void testStarvationDeaths() { + GameEngine game = new GameEngine(); + int deaths = game.starvationDeaths(100, 1639); + assertEquals("Wrong number of starvations deaths.", 19, deaths); + deaths = game.starvationDeaths(100, 2500); + if (deaths < 0) { + fail("You starved a negative number of people!"); + } + } + + @Test + public final void testUprising() { + GameEngine game = new GameEngine(); + assertTrue("Should have had an uprising!", game.uprising(1000, 451)); + assertFalse("Should not have had an uprising!", game.uprising(1000, 449)); + } + + @Test + public final void testImmigrants() { + GameEngine game = new GameEngine(); + int imm = game.immigrants(10, 1200, 500, 200); + assertEquals("Wrong number of immigrants.", 25, imm); + } + + @Test + public final void testHarvest() { + GameEngine game = new GameEngine(); + int[] yield = new int[7]; + for (int i = 0; i < 1000; i++) { + int harvest = game.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() { + GameEngine game = new GameEngine(); + int infestations = 0; + for (int i = 0; i < 1000; i++) { + int eaten = game.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() { + GameEngine game = new GameEngine(); + int percent = 0; + int[] counts = new int[31]; + for (int i = 0; i < 10000; i++) { + percent = game.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); + } + } + + + + + +} \ No newline at end of file diff --git a/src/test/java/StateTest.java b/src/test/java/StateTest.java new file mode 100644 index 0000000..d19d71c --- /dev/null +++ b/src/test/java/StateTest.java @@ -0,0 +1,234 @@ +import org.junit.Assert; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; + +class StateTest { + GameEngine game = new GameEngine(); + + @BeforeEach + public void setUp() { + State state = new State(100, 1000, 3000, 0, 19); + } + + @Test + void getPopulationTest() { + State state = new State(100, 1000, 3000, 0, 19); + int expected = 100; + int actual = state.getPopulation(); + assertEquals(expected, actual); + } + + @Test + void setPopulationTest() { + State state = new State(100, 1000, 3000, 0, 19); + int expected = 200; + state.setPopulation(200); + int actual = state.getPopulation(); + assertEquals(expected, actual); + } + + @Test + void changePopulationTest() { + State state = new State(100, 1000, 3000, 0, 19); + int expected = 50; + state.changePopulation(-50); + int actual = state.getPopulation(); + assertEquals(expected, actual); + } + + @Test + void getLandsOwnedTest() { + State state = new State(100, 1000, 3000, 0, 19); + int expected = 1000; + int actual = state.getLandsOwned(); + assertEquals(expected, actual); + } + + @Test + void setLandsOwnedTest() { + State state = new State(100, 1000, 3000, 0, 19); + int expected = 2000; + state.setLandsOwned(2000); + int actual = state.getLandsOwned(); + assertEquals(expected, actual); + } + + @Test + void changeLandsOwnedTest() { + State state = new State(100, 1000, 3000, 0, 19); + int expected = 900; + state.changeLandsOwned(-100); + int actual = state.getLandsOwned(); + assertEquals(expected, actual); + } + @Test + public final void testNewCostOfLand() { + State state = new State(100, 1000, 3000, 0, 19); + int[] cost = new int[24]; + for (int i = 0; i < 1000; i++) { + int price = state.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); + } + } + @Test + void getBushelsTest() { + State state = new State(100, 1000, 3000, 0, 19); + int expected = 3000; + int actual = state.getBushels(); + assertEquals(expected, actual); + } + + @Test + void setBushelsTest() { + State state = new State(100, 1000, 3000, 0, 19); + int expected = 5000; + state.setBushels(5000); + int actual = state.getBushels(); + assertEquals(expected, actual); + } + + @Test + void changeBushelsTest() { + State state = new State(100, 1000, 3000, 0, 19); + int expected = 2900; + state.changeBushels(-100); + int actual = state.getBushels(); + assertEquals(expected, actual); + } + + @Test + void getPriceTest() { + State state = new State(100, 1000, 3000, 0, 19); + int expected = 19; + int actual = state.getPrice(); + assertEquals(expected, actual); + } + + @Test + void setPriceTest() { + State state = new State(100, 1000, 3000, 0, 19); + int expected = 23; + state.setPrice(23); + int actual = state.getPrice(); + assertEquals(expected, actual); + } + @Test + void getYear() { + State state = new State(100, 1000, 3000, 0, 19); + int expected = 0; + int actual = state.getYear(); + assertEquals(expected, actual); + } + + @Test + void setYear() { + State state = new State(100, 1000, 3000, 0, 19); + int expected = 5; + state.setYear(5); + int actual = state.getYear(); + assertEquals(expected, actual); + } + + @Test + void incrementYear() { + State state = new State(100, 1000, 3000, 0, 19); + int expected = 1; + state.incrementYear(); + int actual = state.getYear(); + assertEquals(expected, actual); + } + @Test + void getpDeathsTest() { + State state = new State(100, 1000, 3000, 0, 19); + state.setpDeaths(game.plagueDeaths(state.getPopulation())); + assertTrue("Plague deaths are: " + state.getpDeaths(), + state.getpDeaths() == 50 || state.getpDeaths() == 0); + } + @Test + void setpDeathsTest() { + State state = new State(100, 1000, 3000, 0, 19); + state.setpDeaths(50); + assertEquals(50, state.getpDeaths()); + } + @Test + void getsDeathsTest() { + State state = new State(100, 1000, 3000, 0, 19); + state.setsDeaths(game.starvationDeaths(state.getPopulation(), 1200)); + int expected = state.getsDeaths(); + assertEquals(40, expected); + } + @Test + void setsDeathsTest() { + State state = new State(100, 1000, 3000, 0, 19); + state.setsDeaths(40); + assertEquals(40, state.getsDeaths()); + } + + @Test + void getImmigrantsTest() { + State state = new State(100, 1000, 3000, 0, 19); + state.setImmigrants(game.immigrants(state.getPopulation(), state.getLandsOwned(), state.getBushels(), 2000)); + assertEquals(3, state.getImmigrants()); + } + @Test + void setImmigrantsTest() { + State state = new State(100, 1000, 3000, 0, 19); + state.setImmigrants(3); + assertEquals(3, state.getImmigrants()); + } + @Test + void getHarvestTest() { + State state = new State(100, 1000, 3000, 0, 19); + state.setHarvest(800); + assertEquals(800, state.getHarvest()); + } + @Test + void setHarvestTest() { + State state = new State(100, 1000, 3000, 0, 19); + } + @Test + void getRats() { + State state = new State(100, 1000, 3000, 0, 19); + state.setRats(game.grainEatenByRats(state.getBushels())); + assertTrue("Infestation: " + state.getRats(), + state.getRats() == 0 || state.getRats() <= 900 && state.getRats() >= 300); + } + @Test + void setRats() { + State state = new State(100, 1000, 3000, 0, 19); + state.setRats(300); + assertEquals(300, state.getRats()); + } + @Test + void getsDeathsPool() { + State state = new State(100, 1000, 3000, 0, 19); + state.setsDeathsPool(state.getsDeathsPool() + state.getsDeaths()); + assertEquals(0, state.getsDeathsPool()); + } + @Test + void setsDeathsPool() { + State state = new State(100, 1000, 3000, 0, 19); + state.setsDeathsPool(10); + assertEquals(10, state.getsDeathsPool()); + } + @Test + void getImmigrantsPoolTest() { + State state = new State(100, 1000, 3000, 0, 19); + state.setImmigrantsPool(state.getImmigrantsPool() + state.getImmigrants()); + assertEquals(0, state.getImmigrantsPool()); + } + @Test + void setImmigrantsPoolTest() { + State state = new State(100, 1000, 3000, 0, 19); + state.setImmigrantsPool(5); + assertEquals(5, state.getImmigrantsPool()); + } + +} \ No newline at end of file diff --git a/target/.DS_Store b/target/.DS_Store new file mode 100644 index 0000000..587b1b8 Binary files /dev/null and b/target/.DS_Store differ