# [java] while loop help

This topic is 5469 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

For my code, I want to have it so when it choses a player(Eggman or You) and after the attack that it choses the next player then back to the previous player. For example it chooses Eggman, Eggman attacks, now it should go to You, You attack, then it goes back to Eggman, and so forth. If it chooses You, You attack, then it goes to Eggman, and Eggman attacks and so forth. The names[r] is the random choosing part.
if(names[r].equals("Eggman"))
{
System.out.println("Does "+ attacks[r] + " damage.");

}
else

if(names[r].equals("You"))
{
System.out.println("Choose attack1 , attack2, or attack3: ");

if(attack.equals("attack1"))
{
System.out.println( "\n" + "You attack with attack1 causing " + attack1 + " damage to Eggman."+"\n");
}
else
if (attack.equals("attack2"))
{
System.out.println( "\n" + "You attack with attack2 causing " + attack2 + " damage to Eggman."+"\n");
}
else
if(attack.equals("attack3"))
{
System.out.println( "\n" + "You attack with attack3 causing " + attack3 + " damage to Eggman."+"\n");

}
}

{
if(names[r].equals("Eggman"))
{
System.out.println("\n" + "Choose attack1 , attack2, or attack3: ");

if(attack.equals("attack1"))
System.out.println( "\n" + "You attack with attack1 causing " + attack1 + " damage to Eggman."+"\n");
else
if (attack.equals("attack2"))
System.out.println( "\n" + "You attack with attack2 causing " + attack2 + " damage to Eggman."+"\n");
else
if(attack.equals("attack3"))
System.out.println( "\n" + "You attack with attack3 causing " + attack3 + " damage to Eggman."+"\n");

}
else

if(names[r].equals("You"))
{
System.out.println("Eggman does "+ attacks[r] + " damage.");
}

}


This does exactly what I want except it doesn't loop the alternating. How would I loop it with while?

##### Share on other sites
You should keep somekind of index to keep track of who's turn it is right now. Like if index==1 then eggman gets to fire and if index==2 then th eplayer fire...

##### Share on other sites
boolean not_done = true;boolean first_player = true;while(not_done){  if(first_player)  {    //...  }  else  {    //...  }  first_player = !first_player;}

##### Share on other sites
I would probably create an abstract class Player and extend those for a "computer" player & a "real" player. Then you just make an attacker that references to the real player and a defender that references to the computer player.
Now in the while loop you just call the appropriate functions on attacker/defender and switch them. I'm just not fond of those if/else structures just checking all possibilities.

##### Share on other sites
Quote:
 Original post by Oluseyiboolean not_done = true;boolean first_player = true;while(not_done){ if(first_player) { //... } else { //... } first_player = !first_player;}

I don't undestand how to do that after randomly choosing a name(that are strings), which are Eggman and You. My first part of my code asks for a player name, a user inputs a name, then it randomly chooses between your name and Eggman to decide on who will go first. And Lotuspec I shall read up on abstact classes.

[Edited by - Javacell on September 28, 2004 2:00:30 PM]

##### Share on other sites
Quote:
Original post by Javacell
Quote:
 Original post by Oluseyiboolean not_done = true;boolean first_player = true;while(not_done){ if(first_player) { //... } else { //... } first_player = !first_player;}

I don't undestand how to do that after randomly choosing a name(that are strings), which are Eggman and You. My first part of my code asks for a player name, a user inputs a name, then it randomly chooses between your name and Eggman to decide on who will go first.
I would do it like this

boolean first_player=((new java.util.Random().nextInt()%2)==0);

this way a random player starts. You might have to throw in some extra peranteses in that to make it compile..not sure.

##### Share on other sites
Exactly - don't choose a *name*; choose a *player* (by identifying number).

##### Share on other sites
Ok guys it does the random players now! thanks! Only problem is with Eggman, Eggman does the same attack each loop.
For the random part of Eggman's attacks I have:
int[] attacks = { attack1, attack2, attack3 };    	int A = attacks.length;		int p = (int) (Math.random() * A);

For his attack part in the loop I have:
players[r] = 0;         System.out.println("Eggman does "+ attacks + " damage.");

When it randomly choses an attack for Eggman the first time it sticks with that same attack each time like so:
Quote:
 Eggman does 10 damage.Choose attack1 , attack2, or attack3:attack1You attack with attack1 causing 5 damage to Eggman.Eggman does 10 damage.Choose attack1 , attack2, or attack3:attack2You attack with attack2 causing 10 damage to Eggman.Eggman does 10 damage.Choose attack1 , attack2, or attack3:attack3You attack with attack3 causing 15 damage to Eggman.Eggman does 10 damage.Choose attack1 , attack2, or attack3:

##### Share on other sites
Instead of random, I meant the turn based worked now.
Also my 3 attacks are like this:
int LP = 50;		int attack1 = LP-45;		int attack2 = LP-40;		int attack3 = LP-35;

##### Share on other sites
nvm I figured it out.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 22
• 17
• 46