Developing a Java Console Game
Actor.java
package actors;
import java.util.Random;
import location.Direction;
import location.Location;
import stats.AttackStats;
import stats.DefenseStats;
import stats.GoldStats;
import stats.HealthStats;
public class Actor extends LocationObject{
protected AttackStats attackStats;
protected DefenseStats defenseStats;
protected HealthStats healthStats;
protected GoldStats goldStats;
public Actor(Location location) {
super(location);
this.attackStats = new AttackStats();
this.defenseStats = new DefenseStats();
this.healthStats = new HealthStats();
this.goldStats = new GoldStats();
}
public int getAttack() {
return attackStats.getValue();
}
public int getDefense() {
return defenseStats.getValue();
}
public int getGold() {
return goldStats.getValue();
}
public int getHealth() {
return healthStats.getValue();
}
public void addGold(int value) {
goldStats.modify(value);
}
public boolean reduceHealth(int value) {
if (value >= getHealth()) {
healthStats.modify(-getHealth());
return false;
}
healthStats.modify(-value);
return true;
}
public boolean reduceGold(int value) {
goldStats.modify(-value);
return true;
}
public void move(Direction dir) {
location = location.move(dir);
}
public static LocationObject generateActor(Location location) {
Random random = new Random();
int r = random.nextInt(3);
switch (r) {
case 0:
return new Nothing(location);
case 1:
return new Chest(location);
case 2:
return new Enemy(location);
}
return null;
}
}
Character.java
package actors;
import items.Armor;
import items.Gold;
import items.Health;
import items.Item;
import items.Weapon;
import location.Direction;
import location.Location;
public class Character extends Actor{
private String name;
private Origin origin;
public Character(String name, Origin origin) {
super(new Location());
this.name = name;
this.origin = origin;
}
public void move(Direction dir) {
super.move(dir);
System.out.println(name + " " + origin.toString() + " moves " + dir.toString() + ", now at " + location.toString());
}
public void applyItem(Item item) {
if (item instanceof Armor) {
defenseStats.modify(item.getImproveValue());
}
else if (item instanceof Weapon) {
attackStats.modify(item.getImproveValue());
}
else if (item instanceof Health) {
healthStats.modify(item.getImproveValue());
}
else if (item instanceof Gold) {
goldStats.modify(item.getImproveValue());
}
}
public String getFullName() {
return name + " " + origin.toString();
}
@Override
public String toString() {
String result = getFullName() + "\n";
result += "Your " + attackStats.toString() + "\n";
result += "Your " + defenseStats.toString() + "\n";
result += "Your " + healthStats.toString() + "\n";
result += "Your " + goldStats.toString() + "\n";
return result;
}
}
Chest.java
package actors;
import java.util.Random;
import items.Armor;
import items.Empty;
import items.Gold;
import items.Health;
import items.Item;
import items.Weapon;
import location.Location;
public class Chest extends LocationObject{
private Item item;
public Chest(Location location) {
super(location);
Random random = new Random();
int r = random.nextInt(5);
switch (r) {
case 0:
item = new Armor();
break;
case 1:
item = new Weapon();
break;
case 2:
item = new Health();
break;
case 3:
item = new Gold();
break;
case 4:
item = new Empty();
break;
}
}
public Item takeItem() {
Item taken = item;
item = new Empty();
return taken;
}
@Override
public String toString() {
String result = "At that location is a chest. You open it to find:\n";
result += item.toString() + "\n";
return result;
}
}
Elf.java
package actors;
public class Elf extends Origin {
@Override
public String toString() {
return "the Elf";
}
}
Enemy.java
package actors;
import location.Location;
import stats.AttackStats;
import stats.DefenseStats;
import stats.GoldStats;
import stats.HealthStats;
public class Enemy extends Actor{
public Enemy(Location location) {
super(location);
this.attackStats = new AttackStats();
this.defenseStats = new DefenseStats();
this.healthStats = new HealthStats();
this.goldStats = new GoldStats();
}
@Override
public String toString() {
String result = "At that location is an enemy.\n";
result += "Its " + attackStats.toString() + "\n";
result += "Its " + defenseStats.toString() + "\n";
result += "Its " + healthStats.toString() + "\n";
result += "Its " + goldStats.toString() + "\n";
return result;
}
}
LocationObject.java
package actors;
import location.Location;
public class LocationObject {
protected Location location;
public LocationObject(Location location) {
this.location = location;
}
public Location getLocation() {
return location;
}
}
Nord.java
package actors;
public class Nord extends Origin {
@Override
public String toString() {
return "the Nord";
}
}
Nothing.java
package actors;
import location.Location;
public class Nothing extends LocationObject{
public Nothing(Location location) {
super(location);
}
@Override
public String toString() {
return "At that location is nothing";
}
}
Origin.java
package actors;
public class Origin {
}
Source (behavior)
Behavior.java
package behavior;
public enum Behavior {
run,
bribe,
fight;
}
BribeBehavior.java
package behavior;
import java.util.Map;
import java.util.Random;
import actors.Enemy;
import actors.LocationObject;
import actors.Nothing;
import location.Direction;
import location.Location;
public class BribeBehavior {
private Enemy enemy;
private int enemyAmount;
private Map map;
public BribeBehavior(Enemy enemy, Map map) {
this.enemy = enemy;
this.map = map;
Random random = new Random();
this.enemyAmount = random.nextInt(100) + 1;
}
public boolean bribing(int amount) {
if (amount >= enemyAmount) {
Direction dir = Direction.getRandom();
Location old = enemy.getLocation();
enemy.move(dir);
System.out.println("Bribe successful! Enemy moves " + dir.toString() + " away from you");
Location next = enemy.getLocation();
map.put(old, new Nothing(old));
map.put(next, enemy);
return true;
}
System.out.println("Bribe failed");
return false;
}
}
FightBehavior.java
package behavior;
import java.util.Map;
import java.util.Random;
import actors.Actor;
import actors.Character;
import actors.Enemy;
import actors.LocationObject;
import location.Location;
public class FightBehavior {
private Character character;
private Enemy enemy;
private Map map;
public FightBehavior(Character character, Enemy enemy, Map map) {
this.character = character;
this.enemy = enemy;
this.map = map;
}
public boolean fighting() {
Random random = new Random();
int r = random.nextInt(2);
Actor[] order = new Actor[2];
order[r] = character;
order[1-r] = enemy;
boolean start = true;
while (true) {
for (int i = 0; i<2; i++) {
System.out.println();
if (order[i] instanceof Character) {
Character c = (Character)order[i];
Enemy e = (Enemy)order[1-i];
if (start) {
start = false;
System.out.println(c.getFullName() + " gets to attack first.");
}
else {
System.out.println(c.getFullName() + " gets to attack.");
}
double rAtt = 0.1 * random.nextInt(11);
double rDef = 0.1 * random.nextInt(11);
int att = c.getAttack();
int def = e.getDefense();
int damage = (int)((Math.floor(rAtt * att - rDef*def) >0) ? Math.floor(rAtt * att - rDef*def) : 0);
System.out.println("Attack: " + att + "*" + String.format("%.1f",rAtt) + " Defense: " + def + "*" + String.format("%.1f",rDef) + " -> attack of " + damage);
boolean alive = e.reduceHealth(damage);
System.out.println("Enemy health: " + damage + " attack success -> health " + e.getHealth());
if (!alive) {
System.out.println("Enemy defeated! " + e.getGold() + " gold collected");
c.addGold(e.getGold());
return true;
}
}
else {
Character c = (Character)order[1-i];
Enemy e = (Enemy)order[i];
if (start) {
start = false;
System.out.println("Enemy gets to attack first.");
}
else {
System.out.println("Enemy gets to attack.");
}
double rAtt = 0.1 * random.nextInt(11);
double rDef = 0.1 * random.nextInt(11);
int att = e.getAttack();
int def = c.getDefense();
int damage = (int)((Math.floor(rAtt * att - rDef*def) >0) ? Math.floor(rAtt * att - rDef*def) : 0);
System.out.println("Attack: " + att + "*" + String.format("%.1f",rAtt) + " Defense: " + def + "*" + String.format("%.1f",rDef) + " -> attack of " + damage);
boolean alive = c.reduceHealth(damage);
System.out.println(c.getFullName() + " health: " + damage + " attack success -> health " + c.getHealth());
if (!alive) {
System.out.println("You were defeated! Game Over!!!");
return false;
}
}
}
}
}
}
RunBehavior.java
package behavior;
import java.util.Map;
import actors.Actor;
import actors.Character;
import actors.Enemy;
import actors.LocationObject;
import location.Direction;
import location.Location;
public class RunBehavior {
private Map map;
public RunBehavior(Map map) {
this.map = map;
}
public Location running(Character character) {
Direction dir = Direction.getRandom();
Location next = character.getLocation().move(dir);
if (!map.containsKey(next)) {
map.put(next, Actor.generateActor(next));
}
if (map.get(next) instanceof Enemy) {
System.out.println("Failed to run");
return null;
}
character.move(dir);
return next;
}
}