Jump to content
  • Advertisement
Sign in to follow this  
Javacell

[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.

If you intended to correct an error in the post then please contact us.

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: ");
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?

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
boolean not_done = true;
boolean first_player = true;

while(not_done)
{
if(first_player)
{
//...
}
else
{
//...
}

first_player = !first_player;
}

Share this post


Link to post
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 this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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:

Share this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!