mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-23 07:29:02 -08:00
Merge pull request #200 from journich/main
Bugfixes and cleanup for Hamurabi
This commit is contained in:
@@ -12,6 +12,14 @@ import java.util.Scanner;
|
|||||||
*/
|
*/
|
||||||
public class Hamurabi {
|
public class Hamurabi {
|
||||||
|
|
||||||
|
public static final int INITIAL_POPULATION = 95;
|
||||||
|
public static final int INITIAL_BUSHELS = 2800;
|
||||||
|
public static final int INITIAL_HARVEST = 3000;
|
||||||
|
public static final int INITIAL_LAND_TRADING_AT = 3;
|
||||||
|
public static final int INITIAL_CAME_TO_CITY = 5;
|
||||||
|
public static final int MAX_GAME_YEARS = 10;
|
||||||
|
public static final double MAX_STARVATION_IN_A_YEAR = .45d;
|
||||||
|
|
||||||
private int year;
|
private int year;
|
||||||
private int population;
|
private int population;
|
||||||
private int acres;
|
private int acres;
|
||||||
@@ -19,13 +27,12 @@ public class Hamurabi {
|
|||||||
private int harvest;
|
private int harvest;
|
||||||
private int landTradingAt;
|
private int landTradingAt;
|
||||||
private int cameToCity;
|
private int cameToCity;
|
||||||
private int died;
|
private int starvedInAYear;
|
||||||
private int q;
|
private int starvedOverall;
|
||||||
|
private boolean chanceOfPlague;
|
||||||
private int ratsAte;
|
private int ratsAte;
|
||||||
private double peopleFed;
|
private double peopleFed;
|
||||||
private double totalPlanted;
|
|
||||||
private double percentageStarved;
|
private double percentageStarved;
|
||||||
private int acresToPlant;
|
|
||||||
private int bushelsToFeedPeople;
|
private int bushelsToFeedPeople;
|
||||||
|
|
||||||
// Used for keyboard input
|
// Used for keyboard input
|
||||||
@@ -42,6 +49,7 @@ public class Hamurabi {
|
|||||||
CALCULATE_HARVEST,
|
CALCULATE_HARVEST,
|
||||||
CALCULATE_BABIES,
|
CALCULATE_BABIES,
|
||||||
RESULTS,
|
RESULTS,
|
||||||
|
FINISH_GAME,
|
||||||
GAME_OVER
|
GAME_OVER
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,19 +78,18 @@ public class Hamurabi {
|
|||||||
|
|
||||||
// These are hard coded startup figures from the basic program
|
// These are hard coded startup figures from the basic program
|
||||||
year = 0;
|
year = 0;
|
||||||
population = 95;
|
population = INITIAL_POPULATION;
|
||||||
bushels = 2800;
|
bushels = INITIAL_BUSHELS;
|
||||||
harvest = 3000;
|
harvest = INITIAL_HARVEST;
|
||||||
landTradingAt = 3;
|
landTradingAt = INITIAL_LAND_TRADING_AT;
|
||||||
acres = harvest / landTradingAt;
|
acres = INITIAL_HARVEST / INITIAL_LAND_TRADING_AT;
|
||||||
cameToCity = 5;
|
cameToCity = INITIAL_CAME_TO_CITY;
|
||||||
died = 0;
|
starvedInAYear = 0;
|
||||||
q = 1;
|
starvedOverall = 0;
|
||||||
ratsAte = harvest - bushels;
|
chanceOfPlague = false;
|
||||||
|
ratsAte = INITIAL_HARVEST - INITIAL_BUSHELS;
|
||||||
peopleFed = 0;
|
peopleFed = 0;
|
||||||
totalPlanted = 0;
|
|
||||||
percentageStarved = 0;
|
percentageStarved = 0;
|
||||||
acresToPlant = 0;
|
|
||||||
bushelsToFeedPeople = 0;
|
bushelsToFeedPeople = 0;
|
||||||
|
|
||||||
gameState = GAME_STATE.YEAR_CYCLE;
|
gameState = GAME_STATE.YEAR_CYCLE;
|
||||||
@@ -91,10 +98,16 @@ public class Hamurabi {
|
|||||||
case YEAR_CYCLE:
|
case YEAR_CYCLE:
|
||||||
System.out.println();
|
System.out.println();
|
||||||
year += 1;
|
year += 1;
|
||||||
|
// End of game?
|
||||||
|
if (year > MAX_GAME_YEARS) {
|
||||||
|
gameState = GAME_STATE.RESULTS;
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
System.out.println("HAMURABI: I BEG TO REPORT TO YOU,");
|
System.out.println("HAMURABI: I BEG TO REPORT TO YOU,");
|
||||||
System.out.println("IN YEAR " + year + "," + died + " PEOPLE STARVED," + cameToCity + " CAME TO THE CITY,");
|
System.out.println("IN YEAR " + year + "," + starvedInAYear + " PEOPLE STARVED," + cameToCity + " CAME TO THE CITY,");
|
||||||
population += cameToCity;
|
population += cameToCity;
|
||||||
if (q == 0) {
|
if (chanceOfPlague) {
|
||||||
population /= 2;
|
population /= 2;
|
||||||
System.out.println("A HORRIBLE PLAGUE STRUCK! HALF THE PEOPLE DIED.");
|
System.out.println("A HORRIBLE PLAGUE STRUCK! HALF THE PEOPLE DIED.");
|
||||||
}
|
}
|
||||||
@@ -105,103 +118,95 @@ public class Hamurabi {
|
|||||||
System.out.println("YOU NOW HAVE " + bushels + " BUSHELS IN STORE.");
|
System.out.println("YOU NOW HAVE " + bushels + " BUSHELS IN STORE.");
|
||||||
System.out.println();
|
System.out.println();
|
||||||
|
|
||||||
peopleFed = (int) (Math.random() * 10);
|
landTradingAt = (int) (Math.random() * 10) + 17; // Original formula unchanged
|
||||||
landTradingAt = (int) peopleFed + 17;
|
|
||||||
System.out.println("LAND IS TRADING AT " + landTradingAt + " BUSHELS PER ACRE.");
|
System.out.println("LAND IS TRADING AT " + landTradingAt + " BUSHELS PER ACRE.");
|
||||||
|
|
||||||
gameState = GAME_STATE.BUY_ACRES;
|
gameState = GAME_STATE.BUY_ACRES;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case BUY_ACRES:
|
case BUY_ACRES:
|
||||||
int acresToBuy = displayTextAndGetNumber("HOW MANY ACRES DO YOU WISH TO BUY? ");
|
int acresToBuy = displayTextAndGetNumber("HOW MANY ACRES DO YOU WISH TO BUY? ");
|
||||||
if (acresToBuy < 0) {
|
if (acresToBuy < 0) {
|
||||||
gameState = GAME_STATE.GAME_OVER;
|
gameState = GAME_STATE.FINISH_GAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(acresToBuy >0) {
|
if (acresToBuy > 0) {
|
||||||
if ((landTradingAt * acresToBuy) > bushels) {
|
if ((landTradingAt * acresToBuy) > bushels) {
|
||||||
System.out.println("HAMURABI: THINK AGAIN. YOU HAVE ONLY");
|
notEnoughBushelsMessage();
|
||||||
System.out.println(bushels + " BUSHELS OF GRAIN. NOW THEN,");
|
|
||||||
} else {
|
|
||||||
if (q == 0) {
|
|
||||||
gameState = GAME_STATE.SELL_ACRES;
|
|
||||||
} else {
|
|
||||||
acres += acresToBuy;
|
|
||||||
bushels -= (landTradingAt * acresToBuy);
|
|
||||||
peopleFed = 0;
|
|
||||||
gameState = GAME_STATE.FEED_PEOPLE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// 0 entered as buy so try to sell
|
acres += acresToBuy;
|
||||||
gameState = GAME_STATE.SELL_ACRES;
|
bushels -= (landTradingAt * acresToBuy);
|
||||||
|
peopleFed = 0;
|
||||||
|
gameState = GAME_STATE.FEED_PEOPLE;
|
||||||
}
|
}
|
||||||
break;
|
} else {
|
||||||
|
// 0 entered as buy so try to sell
|
||||||
|
gameState = GAME_STATE.SELL_ACRES;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
case SELL_ACRES:
|
case SELL_ACRES:
|
||||||
int acresToSell = displayTextAndGetNumber("HOW MANY ACRES DO YOU WISH TO SELL? ");
|
int acresToSell = displayTextAndGetNumber("HOW MANY ACRES DO YOU WISH TO SELL? ");
|
||||||
if (acresToSell < 0) {
|
if (acresToSell < 0) {
|
||||||
gameState = GAME_STATE.GAME_OVER;
|
gameState = GAME_STATE.FINISH_GAME;
|
||||||
}
|
}
|
||||||
if (acresToSell < acres) {
|
if (acresToSell < acres) {
|
||||||
acres -= acresToSell;
|
acres -= acresToSell;
|
||||||
bushels += (landTradingAt * acresToSell);
|
bushels += (landTradingAt * acresToSell);
|
||||||
gameState = GAME_STATE.FEED_PEOPLE;
|
gameState = GAME_STATE.FEED_PEOPLE;
|
||||||
} else {
|
} else {
|
||||||
System.out.println("HAMURABI: THINK AGAIN. YOU OWN ONLY " + acres + " ACRES. NOW THEN,");
|
notEnoughLandMessage();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case FEED_PEOPLE:
|
case FEED_PEOPLE:
|
||||||
|
|
||||||
bushelsToFeedPeople = displayTextAndGetNumber("HOW MANY BUSHELS DO YOU WISH TO FEED YOUR PEOPLE ? ");
|
bushelsToFeedPeople = displayTextAndGetNumber("HOW MANY BUSHELS DO YOU WISH TO FEED YOUR PEOPLE ? ");
|
||||||
if (bushelsToFeedPeople < 0) {
|
if (bushelsToFeedPeople < 0) {
|
||||||
gameState = GAME_STATE.GAME_OVER;
|
gameState = GAME_STATE.FINISH_GAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bushelsToFeedPeople <= bushels) {
|
if (bushelsToFeedPeople <= bushels) {
|
||||||
bushels -= bushelsToFeedPeople;
|
bushels -= bushelsToFeedPeople;
|
||||||
peopleFed = 1;
|
peopleFed = 1;
|
||||||
gameState = GAME_STATE.PLANT_SEED;
|
gameState = GAME_STATE.PLANT_SEED;
|
||||||
} else {
|
} else {
|
||||||
System.out.println("HAMURABI: THINK AGAIN. YOU HAVE ONLY");
|
notEnoughBushelsMessage();
|
||||||
System.out.println(bushels + " BUSHELS OF GRAIN. NOW THEN,");
|
}
|
||||||
}
|
break;
|
||||||
break;
|
|
||||||
|
|
||||||
case PLANT_SEED:
|
case PLANT_SEED:
|
||||||
|
|
||||||
acresToPlant = displayTextAndGetNumber("HOW MANY ACRES DO YOU WISH TO PLANT WITH SEED ? ");
|
int acresToPlant = displayTextAndGetNumber("HOW MANY ACRES DO YOU WISH TO PLANT WITH SEED ? ");
|
||||||
if (acresToPlant < 0) {
|
if (acresToPlant < 0) {
|
||||||
gameState = GAME_STATE.GAME_OVER;
|
gameState = GAME_STATE.FINISH_GAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (acresToPlant <= acres) {
|
if (acresToPlant <= acres) {
|
||||||
if(acresToPlant/2 <= bushels) {
|
if (acresToPlant / 2 <= bushels) {
|
||||||
if(acresToPlant < 10*population) {
|
if (acresToPlant < 10 * population) {
|
||||||
bushels -= acresToPlant/2;
|
bushels -= acresToPlant / 2;
|
||||||
peopleFed = (int) (Math.random()*5)+1;
|
peopleFed = (int) (Math.random() * 5) + 1;
|
||||||
landTradingAt = (int) peopleFed;
|
landTradingAt = (int) peopleFed;
|
||||||
harvest = acresToPlant * landTradingAt;
|
harvest = acresToPlant * landTradingAt;
|
||||||
ratsAte = 0;
|
ratsAte = 0;
|
||||||
gameState = GAME_STATE.CALCULATE_HARVEST;
|
gameState = GAME_STATE.CALCULATE_HARVEST;
|
||||||
} else {
|
} else {
|
||||||
System.out.println("BUT YOU HAVE ONLY " + population + " PEOPLE TO TEND THE FIELDS! NOW THEN,");
|
notEnoughPeopleMessage();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
System.out.println("HAMURABI: THINK AGAIN. YOU HAVE ONLY");
|
notEnoughBushelsMessage();
|
||||||
System.out.println("BUSHELS OF GRAIN. NOW THEN,");
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
System.out.println("HAMURABI: THINK AGAIN. YOU OWN ONLY " + acres + " ACRES. NOW THEN,");
|
notEnoughLandMessage();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case CALCULATE_HARVEST:
|
case CALCULATE_HARVEST:
|
||||||
|
|
||||||
if( (int) (peopleFed/2) == peopleFed/2) {
|
if ((int) (peopleFed / 2) == peopleFed / 2) {
|
||||||
// Rats are running wild
|
// Rats are running wild
|
||||||
ratsAte = (int) (bushels / peopleFed);
|
ratsAte = (int) (bushels / peopleFed);
|
||||||
}
|
}
|
||||||
bushels = bushels - ratsAte;
|
bushels = bushels - ratsAte;
|
||||||
bushels += harvest;
|
bushels += harvest;
|
||||||
@@ -210,41 +215,110 @@ public class Hamurabi {
|
|||||||
|
|
||||||
case CALCULATE_BABIES:
|
case CALCULATE_BABIES:
|
||||||
|
|
||||||
cameToCity = (int) (peopleFed*(20*acres+bushels)/population/100+1);
|
cameToCity = (int) (peopleFed * (20 * acres + bushels) / population / 100 + 1);
|
||||||
peopleFed = (int) (bushelsToFeedPeople/20);
|
peopleFed = (bushelsToFeedPeople / 20.0d);
|
||||||
q = (int) ((10*(Math.random()*2)-.3));
|
// Simplify chance of plague to a true/false
|
||||||
if(population <peopleFed) {
|
chanceOfPlague = (int) ((10 * (Math.random() * 2) - .3)) == 0;
|
||||||
|
if (population < peopleFed) {
|
||||||
gameState = GAME_STATE.YEAR_CYCLE;
|
gameState = GAME_STATE.YEAR_CYCLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
double starved = population - peopleFed;
|
double starved = population - peopleFed;
|
||||||
if(starved>.45*population) {
|
if (starved < 0.0d) {
|
||||||
System.out.println();
|
starvedInAYear = 0;
|
||||||
System.out.println("YOU STARVED " + (int) starved + " PEOPLE IN ONE YEAR!!!");
|
|
||||||
System.out.println("DUE TO THIS EXTREME MISMANAGEMENT YOU HAVE NOT ONLY");
|
|
||||||
System.out.println("BEEN IMPEACHED AND THROWN OUT OF OFFICE BUT YOU HAVE");
|
|
||||||
System.out.println("ALSO BEEN DECLARED NATIONAL FINK!!!!");
|
|
||||||
gameState = GAME_STATE.GAME_OVER;
|
|
||||||
} else {
|
|
||||||
percentageStarved = ((year-1)*percentageStarved+(double) (acresToPlant*100/population))/year;
|
|
||||||
population = (int) peopleFed;
|
|
||||||
totalPlanted += acresToPlant;
|
|
||||||
gameState = GAME_STATE.YEAR_CYCLE;
|
gameState = GAME_STATE.YEAR_CYCLE;
|
||||||
|
} else {
|
||||||
|
starvedInAYear = (int) starved;
|
||||||
|
starvedOverall += starvedInAYear;
|
||||||
|
if (starved > MAX_STARVATION_IN_A_YEAR * population) {
|
||||||
|
starvedTooManyPeopleMessage((int) starved);
|
||||||
|
gameState = GAME_STATE.FINISH_GAME;
|
||||||
|
} else {
|
||||||
|
percentageStarved = ((year - 1) * percentageStarved + starved * 100 / population) / year;
|
||||||
|
population = (int) peopleFed;
|
||||||
|
gameState = GAME_STATE.YEAR_CYCLE;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
|
case RESULTS:
|
||||||
|
|
||||||
|
int acresPerPerson = acres / population;
|
||||||
|
|
||||||
|
System.out.println("IN YOUR 10-YEAR TERM OF OFFICE," + String.format("%.2f", percentageStarved) + "% PERCENT OF THE");
|
||||||
|
System.out.println("POPULATION STARVED PER YEAR ON THE AVERAGE, I.E. A TOTAL OF");
|
||||||
|
System.out.println(starvedOverall + " PEOPLE DIED!!");
|
||||||
|
System.out.println("YOU STARTED WITH 10 ACRES PER PERSON AND ENDED WITH");
|
||||||
|
System.out.println(acresPerPerson + " ACRES PER PERSON.");
|
||||||
|
System.out.println();
|
||||||
|
|
||||||
|
if (percentageStarved > 33.0d || acresPerPerson < 7) {
|
||||||
|
starvedTooManyPeopleMessage(starvedOverall);
|
||||||
|
} else if (percentageStarved > 10.0d || acresPerPerson < 9) {
|
||||||
|
heavyHandedMessage();
|
||||||
|
} else if (percentageStarved > 3.0d || acresPerPerson < 10) {
|
||||||
|
couldHaveBeenBetterMessage();
|
||||||
|
} else {
|
||||||
|
fantasticPerformanceMessage();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
gameState = GAME_STATE.FINISH_GAME;
|
||||||
|
|
||||||
|
case FINISH_GAME:
|
||||||
|
System.out.println("SO LONG FOR NOW.");
|
||||||
|
gameState = GAME_STATE.GAME_OVER;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} while (gameState != GAME_STATE.GAME_OVER);
|
} while (gameState != GAME_STATE.GAME_OVER);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void starvedTooManyPeopleMessage(int starved) {
|
||||||
|
System.out.println();
|
||||||
|
System.out.println("YOU STARVED " + starved + " PEOPLE IN ONE YEAR!!!");
|
||||||
|
System.out.println("DUE TO THIS EXTREME MISMANAGEMENT YOU HAVE NOT ONLY");
|
||||||
|
System.out.println("BEEN IMPEACHED AND THROWN OUT OF OFFICE BUT YOU HAVE");
|
||||||
|
System.out.println("ALSO BEEN DECLARED NATIONAL FINK!!!!");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void heavyHandedMessage() {
|
||||||
|
System.out.println("DUE TO THIS EXTREME MISMANAGEMENT YOU HAVE NOT ONLY");
|
||||||
|
System.out.println("BEEN IMPEACHED AND THROWN OUT OF OFFICE BUT YOU HAVE");
|
||||||
|
System.out.println("ALSO BEEN DECLARED NATIONAL FINK!!!!");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void couldHaveBeenBetterMessage() {
|
||||||
|
System.out.println("YOUR PERFORMANCE COULD HAVE BEEN SOMEWHAT BETTER, BUT");
|
||||||
|
System.out.println("REALLY WASN'T TOO BAD AT ALL. " + (int) (Math.random() * (population * .8)) + " PEOPLE");
|
||||||
|
System.out.println("WOULD DEARLY LIKE TO SEE YOU ASSASSINATED BUT WE ALL HAVE OUR");
|
||||||
|
System.out.println("TRIVIAL PROBLEMS.");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void fantasticPerformanceMessage() {
|
||||||
|
System.out.println("A FANTASTIC PERFORMANCE!!! CHARLEMANGE, DISRAELI, AND");
|
||||||
|
System.out.println("JEFFERSON COMBINED COULD NOT HAVE DONE BETTER!");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void notEnoughPeopleMessage() {
|
||||||
|
System.out.println("BUT YOU HAVE ONLY " + population + " PEOPLE TO TEND THE FIELDS! NOW THEN,");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void notEnoughBushelsMessage() {
|
||||||
|
System.out.println("HAMURABI: THINK AGAIN. YOU HAVE ONLY");
|
||||||
|
System.out.println(bushels + " BUSHELS OF GRAIN. NOW THEN,");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void notEnoughLandMessage() {
|
||||||
|
System.out.println("HAMURABI: THINK AGAIN. YOU OWN ONLY " + acres + " ACRES. NOW THEN,");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private void intro() {
|
private void intro() {
|
||||||
System.out.println(simulateTabs(32) + "HAMURABI");
|
System.out.println(simulateTabs(32) + "HAMURABI");
|
||||||
System.out.println(simulateTabs(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY");
|
System.out.println(simulateTabs(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY");
|
||||||
@@ -254,21 +328,6 @@ public class Hamurabi {
|
|||||||
System.out.println();
|
System.out.println();
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean calculate(double playerAnswer, double correctAnswer) {
|
|
||||||
|
|
||||||
boolean gotItRight = false;
|
|
||||||
|
|
||||||
if (Math.abs((playerAnswer - correctAnswer) / correctAnswer) < 0.15) {
|
|
||||||
System.out.println("CLOSE ENOUGH");
|
|
||||||
gotItRight = true;
|
|
||||||
} else {
|
|
||||||
System.out.println("NOT EVEN CLOSE");
|
|
||||||
}
|
|
||||||
System.out.println("CORRECT ANSWER IS " + correctAnswer);
|
|
||||||
System.out.println();
|
|
||||||
|
|
||||||
return gotItRight;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Print a message on the screen, then accept input from Keyboard.
|
* Print a message on the screen, then accept input from Keyboard.
|
||||||
|
|||||||
Reference in New Issue
Block a user