-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Java Implementation of Planet Express CLI Game #1
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import java.util.Scanner; | ||
|
||
public class App { | ||
|
||
public static void main(String[] args) { | ||
PlanetExpress crew = new PlanetExpress(); | ||
Scanner actionReader = new Scanner(System.in); | ||
|
||
System.out.println("Welcome to Planet Express!"); | ||
System.out.println("Your team is just getting started."); | ||
System.out.println("What would you like do to?"); | ||
System.out.println("Choose one: 'drink', 'eat', 'deliver', 'steal', 'account'"); | ||
System.out.println("Type 'score' to get your current score."); | ||
|
||
String action = actionReader.next(); | ||
|
||
do { | ||
crew.act(action); | ||
System.out.println("What would you like do to?"); | ||
System.out.println("Choose one: 'drink', 'eat', 'deliver', 'steal', 'account'"); | ||
System.out.println("Type 'score' to get your current score."); | ||
action = actionReader.next(); | ||
} | ||
while (crew.check()); | ||
|
||
System.out.println("Uh oh, your crew is no longer stable."); | ||
System.out.println("GAME OVER"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
import java.util.Scanner; | ||
|
||
public class PlanetExpress { | ||
private int thirst; | ||
private int work; | ||
private int horde; | ||
private int hunger; | ||
private int receipts; | ||
private boolean stable; | ||
|
||
|
||
public PlanetExpress() { | ||
thirst = 50; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be good to use constants for these initial values in case you want to change it somewhere only once |
||
work = 50; | ||
horde = 50; | ||
hunger = 50; | ||
receipts = 50; | ||
stable = true; | ||
} | ||
|
||
public int getHunger() { | ||
return this.hunger; | ||
} | ||
|
||
public int getThirst() { | ||
return this.thirst; | ||
} | ||
|
||
public int getWork() { | ||
return this.work; | ||
} | ||
|
||
public int getHorde() { | ||
return this.horde; | ||
} | ||
|
||
public int getReceipts() { | ||
return this.receipts; | ||
} | ||
|
||
private boolean isStable() { | ||
if (this.hunger >= 100 || this.thirst >= 100 || this.work >= 100) { | ||
this.stable = false; | ||
} else if (this.receipts <= 0 || this.horde <= 0) { | ||
this.stable = false; | ||
} | ||
return this.stable; | ||
} | ||
|
||
public void act(String action) { | ||
switch (action) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice use of the case statement here. You should always have a default for every case statement though, in case the input is not what is expected. |
||
case "drink": | ||
this.drink(); | ||
System.out.println("You drank some stuff.\n"); | ||
break; | ||
case "eat": | ||
this.eat(); | ||
System.out.println("Yum, you ate some food.\n"); | ||
break; | ||
case "account": | ||
this.account(); | ||
System.out.println("Great, you did some accounting.\n"); | ||
break; | ||
case "deliver": | ||
this.deliver(); | ||
System.out.println("The crew delivered some packages!"); | ||
break; | ||
case "steal": | ||
this.steal(); | ||
System.out.println("Oops, Bender stole some things."); | ||
break; | ||
case "score": | ||
int score = this.getScore(); | ||
System.out.println("Your score is: " + score + "."); | ||
} | ||
String warning = this.warn(); | ||
System.out.println(warning); | ||
} | ||
|
||
public boolean check() { | ||
return this.isStable(); | ||
} | ||
|
||
private String warn() { | ||
String warning = ""; | ||
if (this.hunger > 70) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment re: constants |
||
warning += "Your hunger is getting dangerously high.\n"; | ||
} | ||
if (this.thirst > 70) { | ||
warning += "Your thirst is getting dangerously high.\n"; | ||
} | ||
if (this.work > 70) { | ||
warning += "Your workload is getting dangerously high.\n"; | ||
} | ||
if (this.receipts < 30) { | ||
warning += "You have dangerously few receipts.\n"; | ||
} | ||
if (this.horde < 30) { | ||
warning += "Your horde is dangerously small.\n"; | ||
} | ||
return warning; | ||
} | ||
|
||
private void drink() { | ||
this.thirst -= 10; | ||
this.work += 10; | ||
} | ||
|
||
private void deliver() { | ||
this.work -= 10; | ||
this.receipts += 10; | ||
} | ||
|
||
private void steal() { | ||
this.horde += 10; | ||
this.work += 10; | ||
} | ||
|
||
private void eat() { | ||
this.hunger -= 10; | ||
this.work += 10; | ||
} | ||
|
||
private void account() { | ||
this.receipts -= 10; | ||
this.horde -= 10; | ||
} | ||
|
||
private int getScore() { | ||
int score = 0; | ||
if (this.hunger > 70) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if it would be worthwhile to put each set of these conditionals in a separate method that would return either the +-1 accordingly. That was the score method could more simply say "addHunger, addThirst" etc etc |
||
score += 1; | ||
} else if (this.hunger < 30) { | ||
score -= 1; | ||
} | ||
if (this.thirst > 70) { | ||
score += 1; | ||
} else if (this.thirst < 30) { | ||
score -= 1; | ||
} | ||
if (this.horde > 70) { | ||
score += 1; | ||
} else if (this.horde < 30) { | ||
score -= 1; | ||
} | ||
if (this.receipts > 70) { | ||
score -= 1; | ||
} else if (this.receipts < 30) { | ||
score += 1; | ||
} | ||
if (this.work > 70) { | ||
score -= 1; | ||
} else if (this.work < 30) { | ||
score += 1; | ||
} | ||
return score; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice job with this. You did a nice job with the separation of concerns here.