[java] while loop help

Started by
8 comments, last by Javacell 19 years, 6 months ago
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: ");
attack = in.readLine();

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: ");
attack = in.readLine();

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?
Advertisement
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...
garazdawi - 'I put the laughter back in slaughter'
boolean not_done = true;boolean first_player = true;while(not_done){  if(first_player)  {    //...  }  else  {    //...  }  first_player = !first_player;}
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.
Quote:Original post by Oluseyi
boolean 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]
Quote:Original post by Javacell
Quote:Original post by Oluseyi
boolean 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.
garazdawi - 'I put the laughter back in slaughter'
Exactly - don't choose a *name*; choose a *player* (by identifying number).
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:
attack1

You attack with attack1 causing 5 damage to Eggman.

Eggman does 10 damage.
Choose attack1 , attack2, or attack3:
attack2

You attack with attack2 causing 10 damage to Eggman.

Eggman does 10 damage.
Choose attack1 , attack2, or attack3:
attack3

You attack with attack3 causing 15 damage to Eggman.

Eggman does 10 damage.
Choose attack1 , attack2, or attack3:

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;

nvm I figured it out.

This topic is closed to new replies.

Advertisement