1 Replies - 530 Views - Last Post: 28 July 2011 - 06:24 AM Rate Topic: -----

#1 warhawk23000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 30-June 11

Two Problems, Array List Shuffling & Setting Health to 0 when it&#

Posted 28 July 2011 - 02:18 AM

I'm currently working on an adventure game, and I have come up with a battle system using an array list. I can't seem to get the health to set to 0 when it gets to a negative number. I am also having trouble figuring out how to make the shuffled array list only add one zombie at a time.

Here is my current code.

import java.util.*;
public class Battle
{
	ArrayList<String> zombies = new ArrayList<String>();
	Random rand = new Random();
	
	
	public void battle()
	{
		Scanner input = new Scanner(System.in);
		int zombieHealth = 20 + rand.nextInt(40);
		int playerHealth = 50;
		boolean win = true;
		
		zombies.add("Zombie Math Teacher");
		// zombies.add("Zombie English Teacher");
// 		zombies.add("Zombie Science Teacher");
		
		
		
		System.out.print("You have encountered a...");
		//zombies.add("Zombie Math Teacher");
		//Collections.shuffle(zombies);
		for (int i = 0; i < zombies.size(); i++)
			System.out.printf(" %s", zombies.get(i));
		do
		{
			System.out.print("\nEnter A to attack:");
			char attack = input.nextLine().charAt(0);
			
			int playerDmg = rand.nextInt(6);
			
			if (playerDmg == 0)
				System.out.printf("You Missed\nZombie Health: %d\n",
					zombieHealth -= playerDmg );
			else	

			for (int i = 0; i < zombies.size(); i++)
			System.out.printf("You hit the %s for %d damage\n Zombie Health: %d\n",
				zombies.get(i), playerDmg, zombieHealth -= playerDmg);
				
			int zombieDmg = rand.nextInt(3);
			
			if (zombieDmg == 0)
				for (int i = 0; i < zombies.size(); i++)	
				System.out.printf("The %s Missed\nPlayer Health: %d\n", 
					zombies.get(i), playerHealth -= zombieDmg);
			else
			
			for (int i = 0; i < zombies.size(); i++)	
				System.out.printf("The %s hits you for %d damage\n Your Health: %d\n",
					zombies.get(i), zombieDmg, playerHealth -= zombieDmg);
				
			if (zombieHealth <= 0)
			{
				zombieHealth = 0;
				win = true;
			}
			else
				win = false;	
			
			
		
		
		}while (win != true);
		
			if (zombieHealth <= 0)
			{
				zombieHealth = 0;
				System.out.print("You WIN!\n\n");
				zombies.remove("Zombie Math Teacher");
			}
			else
			{
				playerHealth = 0;
				System.out.print("Game Over\n\n");	
			}
	}
}


Any help would be appreciated.

P.S. I would also like to figure out how to remove the zombie permanently so he doesn't show up again.

Thanks again.

Is This A Good Question/Topic? 0
  • +

Replies To: Two Problems, Array List Shuffling & Setting Health to 0 when it&#

#2 ipushmycar  Icon User is offline

  • D.I.C Regular

Reputation: 86
  • View blog
  • Posts: 390
  • Joined: 29-August 10

Re: Two Problems, Array List Shuffling & Setting Health to 0 when it&#

Posted 28 July 2011 - 06:24 AM

if (playerDmg == 0)
				System.out.printf("You Missed\nZombie Health: %d\n",
					zombieHealth -= playerDmg );



Why are your subtracting if there was no damage?


Use a for loop to add each zombie one at a time.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1