I have to make an Ants vs Zombies game for my class. Here is the link for further reference. http://www.csee.umbc.edu/courses/und...ects/project4/
The problem I am having is that we are given most of the code. We just have to make the Ant class, the ants derived from that class, and the Game class.
My problem is that is with the Zombies class. I think the Ant class should be closely modeled off of the Zombie class. However, I'm having a hard time understanding why the attack method doesn't work. I believe he is using a vector, but I'm not allowed to change anything in the classes that he has given us. Here is the zombie class:
package proj4; public abstract class Zombie { private int life; private int reward; private String desc; protected Zombie(int life, int reward, String desc) { this.life = life; this.reward = reward; this.desc = desc; } public static Zombie makeZombie(char c) { switch (c) { case 'A': return new ArmoredZombie(); case 'G': return new GiantZombie(); case 'I': return new InfectedZombie(); case 'R': return new RadioactiveZombie(); case 'Z': return new StandardZombie(); case 'V': return new VoodooZombie(); case 'N': return new ZombieNinja(); case 'S': return new ZombieScientist(); } return null; } public int getLife() { return life; } public int getReward() { return reward; } public String getDesc() { return desc; } public void takeDamage(int amount) { life -= amount; } public abstract void attack(Game g); }
Here is an example of a type of zombie:
package proj4; public class StandardZombie extends Zombie { public StandardZombie() { super(10, 10, "Standard Zombie"); } public void attack(Game g) { Ant a = g.getColony().elementAt(0); a.takeDamage(10, this); } }
Here is my game class:
/** * This is the Game.java file posted in the Project4 project * description. It contains only the nextFight method. Students * must complete the remainder of the Game methods and add the * Game instance variables. */ package proj4; import java.util.*; import java.io.*; // SMM, 11/13/12: I renamed Game_MethodOnly to Game before posting public class Game implements GameInterface { /******************** instance variables ********************/ private boolean gameOver = false; private int food = 100; private int currentRound = 1; private String ants = ""; private String[] antArray; private ArrayList colony; private ArrayList horde; private boolean roundOver = false; private int score = 0; //Vector colony = new Vector(); //Vector horde = new Vector(); /*************** methods declared in GameInterface ****************/ /** * Return the current round for the game. * @return 1 through 5, inclusive */ public int getRoundNumber(int roundNum) { currentRound = roundNum; return currentRound; } /** * Return the amount of food the player's colony currently has. * @return food remaining */ public int getFood() { return food; } /** * Return a string that lists all of the ants in the player's colony. * The list is in order, and has newlines separating ants. * @return Multiline description of colony. */ public String getColonyDesc() { } /** * Callback invoked when the player attempts to recruit an ant. * @param antType Type of ant to recruit * @return true if the player may recruit the ant, false if not. */ public boolean recruitAnt(String antType) { } /** * Read and parse the Zombie String within a zombie file. * @param filename File containing Zombie String */ public void readHordeFile(String filename) { java.io.FileReader file = new java.io.FileReader(filename); java.io.BufferedReader buf = new java.io.BufferedReader(file); try { String zombieString = buf.readLine(); } catch(IOException err) { System.out.println("Error reading file"); } } /** * Return a string that lists all of the zombies in the current * invasion The list is in order, and has newlines separating * zombies. * @return Multiline description of horde. */ public String getHordeDesc() { } /** * Determine if the invasion is over. If the invasion is over, all * remaining ants' health reset to full life. * @return true if there are no ants or no zombies remaining. */ public boolean isInvasionOver() { } /** * Determine if the game is over or not. * @return true if game is over or not. */ public boolean isGameOver() { } /** * Return a string that describe how the game ended. If the * player lost, simply return "Game Over", otherwise return the * player's score. * @return Description of ending condition. */ public String getEndingMessage() { } /** * Return an array of all types of ants that may be recruited. * This array will be used to construct the recruitment buttons * during Recruit phase. */ public String[] getAntTypes() { } /** * Return the cost to recruit a particular ant. * @param antType Type of ant to recruit. * @return Food cost to recruit. */ public int getAntCost(String antType) { } /** * Determine if the invasion is over. If the invasion is over, all * remaining ants' health reset to full life. * @return true if there are no ants or no zombies remaining. */ public void nextFight() {//can't change this method. Ant a = colony.elementAt(0); a.attack(this); Zombie z = horde.elementAt(0); if ((a instanceof LeafcutterAnt) && (z.getLife() <= 0)) { // leafcutters have first strike, so opposing zombie gets no attack } else { z.attack(this); } // reap all things dead boolean keepReaping = true; while (keepReaping) { keepReaping = false; for (int i = 0; i < colony.size(); ) { a = colony.elementAt(i); if (a.getLife() > 0) { i++; } else { colony.remove(i); if (a instanceof CitronellaAnt) { for (Ant a2 : colony) { a2.takeDamage(2); } for (Zombie z2: horde) { z2.takeDamage(2); } } keepReaping = true; } } for (int i = 0; i < horde.size(); ) { z = horde.elementAt(i); if (z.getLife() > 0) { i++; } else { horde.remove(i); food += z.getReward(); } } } if (colony.size() == 0 && horde.size() > 0) { gameOver = true; } } /******************** other methods ********************/ public ArrayList getHorde() { return horde; } public ArrayList getColony() { return colony; } @Override public int getRoundNumber() { // TODO Auto-generated method stub return 0; } /*public void newGame(int roundNum) { currentRound = roundNum; ArrayList colony = new ArrayList(); }*/ }
I'm having problems understanding why the elementAt(0) and the takeDamage() are not working.
Here's the Ant class just in case:
package proj4; public abstract class Ant { private int lifePoints; //private int currentLife; private int foodCost; private int attackPoints; private String antName; protected Ant(int lifePoints, int foodCost, String antName) { this.lifePoints = lifePoints; this.foodCost = foodCost; this.antName = antName; } /** * Precondition - What ant the player chooses * Postcondition - Gives that ant the correct * life points */ public int getLife() { return lifePoints; } public void setLife(int life) { lifePoints = life; } /** * Precondition - none * Postcondition - Returns the cost * of the ant * @return */ public int getCost() { return foodCost; } /** * Precondition - The ant takes damage * from the zombie * Postcondition - The ants life point are reduced * @param damage - how much damage is done * @param giantZombie * @param z - What zombie hit the ant */ public void takeDamage(int damage) { lifePoints -= damage; } /*public void heal() { currentLife = originalLife; } */ //returns a string that shows //the colony public String getColony() { return antName; } public abstract void attack(Game g); }