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

Filter by extension

Filter by extension


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

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

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

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

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

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

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

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

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

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

4 changes: 3 additions & 1 deletion docs/hamurabi.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/*
import java.io.IOException;
import java.util.Scanner;

Expand Down Expand Up @@ -153,4 +154,5 @@ public static void main(String[] args) throws IOException {
a.newYear();
a.finished();
}
}
}
*/
24 changes: 24 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.hamurabiGLJ </groupId>
<artifactId>hamurabi</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<properties>
<maven.compiler.source>18</maven.compiler.source>
<maven.compiler.target>18</maven.compiler.target>
</properties>

</project>
128 changes: 128 additions & 0 deletions src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package org.hamurabiGLJ.hamurabi;

import java.util.*;

public class Hammurabi {
Random rand = new Random(); // this is an instance variable
Scanner scanner = new Scanner(System.in);
private int land;


public static void main(String[] args) { // required in every Java program
new Hammurabi().playGame();
}

void playGame() {
// declare local variables here: grain, population, etc.
Integer grain = 2800;
Integer population = 100;
Integer land = 1000;
Integer landValue = 19;

// statements go after the declarations
}

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!");
}
}
}
// Jiayong
int askHowManyAcresToBuy(int price, int bushels) {
int maxAcres = bushels / price;
String msg = "How many acres of land would you like to buy?";
int input = getNumber(msg);
while (input > maxAcres) {
String newMsg = String.format("O Great Hammurabi, surely you jest! We'd need %d bushels but we only have %d!",
input * price, bushels);
System.out.println(newMsg);
input = getNumber(msg);
}
return input;
}
// Linda
int askHowManyAcresToSell(int acresOwned) {
int acresToSell = getNumber("How many acres of land would you like to sell?\n");
while (acresToSell > acresOwned) {
System.out.println("O Hammurabi, but you only have " + land + " acres of land!\n");
acresToSell = getNumber("How many acres of land would you like to sell?\n");
}
return acresToSell;
}
// Gychu
int askHowMuchGrainToFeedPeople(int bushels) {
String msg = "How many bushels of grain do you want to feed your people?\n";
int input = getNumber(msg);
while (input > bushels) {
String newMsg = String.format("O Great Hammurabi, surely you jest! We'd need %d bushels but we only have %d!",
input, bushels);
System.out.println(newMsg);
input = getNumber(msg);
}
return input;
}

int askHowManyAcresToPlant(int acresOwned, int population, int bushels) {
String msg = "How many acres do you wish to plant?\n";
int input = getNumber(msg);

while (true) {
if (input > acresOwned) {
String newMsg = String.format("O Great Hammurabi, surely you jest! We'd need %d acres but we only have %!", input, acresOwned);
System.out.println(newMsg);
input = getNumber(msg);
continue;
}
if (input / 10 > population) {
String newMsg = String.format("O Great Hammurabi, surely you jest! We'd need %d people but we only have %d!", input/10, population);
System.out.println(newMsg);
input = getNumber(msg);
continue;
}
if (input > bushels / 2) {
String newMsg = String.format("O Great Hammurabi, surely you jest! We'd need %d bushels but we only have %d!", input*2, bushels);
System.out.println(newMsg);
input = getNumber(msg);
continue;
}
return input;
}
}

int plagueDeaths(int population) {
if (rand.nextInt(101) <= 15) {
return population / 2;
}
return 0;
}

int starvationDeaths(int population, int bushelsFedToPeople) {
return 0;
}

boolean uprising(int population, int howManyPeopleStarved) {
return (((double)howManyPeopleStarved / population) >= 0.45);
}

int immigrants(int population, int acresOwned, int grainInStorage) {
return (20 * acresOwned + grainInStorage) / (100 * population) + 1;
}

int harvest(int acres) {
int yield = rand.nextInt(1,7);
return acres * yield;
}

int grainEatenByRats(int bushels) {
return 0;
}

int newCostOfLand() {
return rand.nextInt(17,24);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package hammurabi;
package org.hamurabiGLJ.hamurabi;

import static org.junit.Assert.*;

Expand Down Expand Up @@ -42,7 +42,7 @@ public final void testPlagueDeaths2() {
assertEquals("In a plague, " + deaths + "% of your people die, not 50%.",
50, deaths);
}

@Test
public final void testStarvationDeaths() {
int deaths = ham.starvationDeaths(100, 1639);
Expand Down Expand Up @@ -88,7 +88,7 @@ public final void testGrainEatenByRats1() {
}
}
int percentInfestations = infestations / 100;
assertTrue("Number of rat infestations is about " + percentInfestations +
assertTrue("Number of rat infestations is about " + percentInfestations +
", not about 40%.", about(400, infestations));
}

Expand Down