I am working on a OOP final project which is a text based survival game. I am currently trying to finish it up by having the player enter how many enemies they would like to fight then it loops through it until the amount is met. What would be the best route? I want it so that enemies can be selected twice, but my partner wants to do a shuffle of the array because less work.
Mostly I am stuck on where and what kind of loop I should make and where should I place it.
This is some code in the main class.
Entity entity = new Entity();
ArrayList<Entity> enemies = new ArrayList<>();
EnemyList enemyList = new EnemyList();
Entity troll = new Troll();
troll.setName("Troll");
enemyList.characters.add(troll);
Entity imp = new Imp();
imp.setName("Imp");
enemyList.characters.add(imp);
Entity knight = new Knight();
knight.setName("knight");
enemyList.characters.add(knight);
Entity skeleton = new Skeleton();
skeleton.setName("Skeleton");
enemyList.characters.add(skeleton);
Integer[] array = new Integer[enemyList.characters.size()];
for (int i = 0; i < array.length; i++) {
array[i] = i;
}
Collections.shuffle(Arrays.asList(array));
for (int i = 0; i < enemyCount; i++) {
for (Entity someEnemy : enemyList.characters) {
System.out.println(enemyList.characters.get(array[i]));
System.out.println(someEnemy.getName(entity) + "\n");
System.out.println("A " + someEnemy.getName(entity) + " Appears! It has " + someEnemy.getHealth() + "HP");
while (someEnemy.getHealth() > 0) {
int attack = character.getAttack();
System.out.println("You hit the " + someEnemy.getName(entity) + " for " + character.getAttack());
int monsterTotalHealth = someEnemy.setHealth(someEnemy.getHealth() - attack);
System.out.println(someEnemy.getName(entity) + " has " + monsterTotalHealth + "HP Left");
System.out.println("");
if (someEnemy.getHealth() > 0) {
System.out.println("The monster attacks back for " + someEnemy.getStrength());
int remainingHP = character.damageDelt(someEnemy.getStrength());
System.out.println("Your remaining health is " + remainingHP);
System.out.println("");
character.setHealth(character.getHealth());
}
if (character.isDead()) {
System.out.println("You have been defeated!");
System.exit(0);
} else if (someEnemy.getHealth() < 0) {
System.out.println("Fighting Next monster");
}
}
}
}
Entity class
public class Entity {
private String name;
private int health;
private int level;
private int vitality;
private int strength;
private int resistance;
private int dexterity;
private int endurance;
private int intelligence;
private int attack;
public String getName(Entity someenemy) {
return name;
}
public int getHealth() {
return health;
}
public int getLevel() {
return level;
}
public int getVitality() {
return vitality;
}
public int getStrength() {
return strength;
}
public int getResistance() {
return resistance;
}
public int getDexterity() {
return dexterity;
}
public int getEndurance() {
return endurance;
}
public int getIntelligence() {
return intelligence;
}
public int getAttack() { return attack; }
public void setName(String name) {
this.name = name;
}
public int setHealth(int health) {
this.health = health;
return health;
}
public void setLevel(int level) {
this.level = level;
}
public int setVitality(int vitality) {
this.vitality = vitality;
return vitality;
}
public void setStrength(int strength) {
this.strength = strength;
}
public void setResistance(int resistance) {
this.resistance = resistance;
}
public void setDexterity(int dexterity) {
this.dexterity = dexterity;
}
public void setEndurance(int endurance) {
this.endurance = endurance;
}
public void setIntelligence(int intelligence) {
this.intelligence = intelligence;
}
public void setAttack(int attack) {
this.attack = attack;
}
@Override
public String toString() {
return "This enemy is appearing: " + name +
", health is " + health +
", strength is " + strength;
}
}
EnemyList class
import java.util.ArrayList;
import java.util.List;
public class EnemyList {
List<Entity> characters = new ArrayList<>();
public void print()
{
for (Entity entity : characters)
{
System.out.println(entity.toString());
}
}
}
Mechanics Interface
interface Mechanics {
boolean isDead();
int damageDelt (int damage);
//int levelup();
}
Ok, will try to help here.
First of all, I think this is unnecessary:
Integer[] array = new Integer[enemyList.characters.size()];
for (int i = 0; i < array.length; i++) {
array[i] = i;
}
Collections.shuffle(Arrays.asList(array));
You already have an array
, contained in the enemyList
instance, called characters.
Now let's say your user wants to fight with 5 enemies. To randomize the event, you shuffle the array first.
Collections.shuffle(enemyList.characters);
Assuming that maxEnemies = 5
, let's go to the loop:
boolean win = true;
for (int i=0;i<maxEnemies;i++)
{
Entity enemy = enemyList.characters.get(i);
String enemyName = enemy.getName();
System.out.println("Fighting "+enemyName);
//... your logic here
if (character.isDead())
{
win=false;
break;
}
}
if (!win)
System.out.println("You have been defeated! You are a disgrace to your family");
else
System.out.println("You've defeated "+maxEnemies+" bastards. You WON!
Tiger blood runs through your veins");
Note I moved the "fighting next enemy" text to the beginning of the loop clause, since the shown text would be incorrect when the last enemy has been defeated (there will be no next enemy).
As the index is based on your count (0-4), no monster will appear twice.
Note that you should validate the user input, deniying the input if the number of enemies he wills to fight is bigger than the number of Entities stored in the array. So the input regarding number of entities to fight <= enemyList.characters.size
I made some changes to the exit method as well, but feel free to change them so it fits your purposes. Instead of killing the thread, you could just call break()
in order to finish the loop, and continue with the process the way you want.