Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Ants vs Zombies project help

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Ants vs Zombies project help

    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);
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Ants vs Zombies project help

    why the attack method doesn't work.
    Please explain. What does the method do or not do?

    elementAt(0) and the takeDamage() are not working.
    Please explain what "not working" means.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Ants vs Zombies project help

    My understanding is that the attack method in every zombie type, gets an ant from an array. Then the ant is attack for the appropriate amount of damage that the zombie does. The elementAt(0) is suppose to call the first ant in line. But I believe elementAt is a vector method. I've tried using get(0), which is for ArrayList, but that doesn't work. As for takeDamage() it does the appropriate damage to the ant. For example takeDamage(35, this) means that this type of zombie does 35 damage to the ant in front. Eclipse is telling me that I don't have the right amount of arguments. But I based my Ant class off of the Zombie class (which the professor wrote and I can't change). So it should work?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Ants vs Zombies project help

    Eclipse is telling me that I don't have the right amount of arguments
    Please copy the full text of the error messages and paste it here.

    believe elementAt is a vector method.
    That is what the API doc is for. Its a place you can read about classes and their methods:
    Java Platform SE 7
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Project
    By dustty in forum JDBC & Databases
    Replies: 1
    Last Post: September 9th, 2012, 09:42 AM
  2. Please help me with my project
    By mrprogrammer in forum What's Wrong With My Code?
    Replies: 9
    Last Post: August 21st, 2012, 07:58 AM
  3. need help in my project
    By flana in forum The Cafe
    Replies: 4
    Last Post: October 11th, 2011, 06:13 AM
  4. Can anyone help me on my project?
    By alesana514 in forum Paid Java Projects
    Replies: 1
    Last Post: December 16th, 2009, 09:24 AM
  5. Need Help With Project
    By jstew132 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 11th, 2009, 07:15 PM