Below is my Zuckerville implementation:
import java.util.Scanner;
/**
* Zuckerville: a game where the only winner is the one who didn't play.
*
* @author Jose M Vidal
*
*/
public class Zuckerville {
public final static int TIME_TO_HARVEST = 10; //in ticks
public final static int COST_OF_FERTILIZER = 4; //in zuckerdollars
public static void main(String[] args) {
int money = 5; //the amount of "money" the player starts with
int seeds = 0;
int plot = 0; //the number of seeds in the plot
int timeToHarvest = 0; //the time left before the plants get harvested
int time = 0;
boolean isFertilized = false; //whether or not the plot has been fertilized
Scanner keyboard = new Scanner(System.in);
String command;
System.out.println("Welcome to Zuckerville, where the only way to win is not to play!");
System.out.println("We gave you $" + money + " zuckerdollars to get you started! Now go plant!"); //first one's always free
System.out.println("");
do {
System.out.print("[" + time + " $" + money + "] Command: ");
command = keyboard.nextLine();
int spaceIndex = command.indexOf(' ');
if (command.startsWith("deposit")) {
int dollarIndex = command.indexOf('$');
int deposit = Integer.parseInt(command.substring(dollarIndex + 1));
money += deposit;
System.out.println("You now have $" + money);
}
else if (command.startsWith("buy")) {
int amountToBuy = Integer.parseInt(command.substring(spaceIndex + 1));
if (amountToBuy <= money ) {
System.out.println("You bought " + amountToBuy + " seeds.");
seeds += amountToBuy;
money -= amountToBuy;
}
else {
System.out.println("Sorry, you don't have enough zuckerdollars.");
}
}
else if (command.startsWith("plant")) {
if (plot != 0) {
System.out.println("Sorry, there are already seeds planted there. You must wait till after harvest.");
}
else {
for (int i = 0; i < seeds; i++) {
System.out.println("Planted a seed.");
}
plot = seeds;
seeds = 0;
timeToHarvest = TIME_TO_HARVEST;
}
}
else if (command.startsWith("fertilize")) {
if (money < COST_OF_FERTILIZER) {
System.out.println("Sorry, it costs $" + COST_OF_FERTILIZER + " to buy fertilizer.");}
else {
System.out.println("You fertilized your plot!");
isFertilized = true;
money -= COST_OF_FERTILIZER;
}
}
else if (!command.startsWith("quit")) {
System.out.println("What did you say?");
}
time++;
timeToHarvest -= (isFertilized) ? 2 : 1;
if (timeToHarvest <= 0 && plot != 0) { //Harvest the flowers if the time has come.
for (int i =0; i < plot; i++) {
System.out.println("You harvested a beautiful flower.");
}
isFertilized = false; //easy to forget this one
plot = 0;
}
} while (!command.equals("quit"));
System.out.println("Goodbye!");
}
}
No comments:
Post a Comment