Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Trying to do a working loop

loop java

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1 Error

Error

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 72 posts

Posted 23 June 2014 - 09:21 AM

package STRAT;

import java.util.Scanner;

public class Game {     

	public void startGame(){
		
		Scanner input = new Scanner(System.in);
		
		Hero H = new Hero();
		Enemies E = new Enemies();
		Store S = new Store();
		Death D = new Death();
		Points P = new Points();
		Restart R = new Restart();
		Loop L = new Loop();
		BattlePoints BP = new BattlePoints();
		PlayerBP PBP = new PlayerBP();
		
		String battle1 = input.nextLine(); 
		
		PBP.PlayerBattlePoints = PBP.PlayerBattlePoints + BP.bp1;

		if(battle1.equals("1")){
			System.out.println("");
			E.enemyOneHealth = E.enemyOneHealth - H.attackOne;
			System.out.println("BAM!");
			System.out.println("");
			System.out.println("Enemy Health = " + E.enemyOneHealth);
		} else if(battle1.equals("2")){
			System.out.println("");
			E.enemyOneHealth = E.enemyOneHealth - H.attackTwo;
			System.out.println("BAM!");
			System.out.println("");
			System.out.println("Enemy Health = " + E.enemyOneHealth);
		} else if(battle1.equalsIgnoreCase("3")){
			System.out.println("");
			E.enemyOneHealth = E.enemyOneHealth - H.attackThree;
			System.out.println("BAM!");
			System.out.println("");
			System.out.println("Enemy Health = " + E.enemyOneHealth);
			} else if(battle1.equalsIgnoreCase("Restart")){
					R.isRestarting();
				} else {
				System.out.println("");
				System.out.println("Invalid Input");
				return;
			}
		if(E.enemyOneHealth == D.death){
			H.heroHealth = H.heroHealth + 0;
		}
		if(E.enemyOneHealth < D.death){
			H.heroHealth = H.heroHealth + 0;
		} else {
		System.out.println("");
		System.out.println("ATTACK RETURNED!");
		H.heroHealth = H.heroHealth - E.eOA1;
		}
		if(E.eOA1 == 0){
		System.out.println("");
		System.out.println("Attack Missed!");
		System.out.println("                                                                                                                            BP = " + PBP.PlayerBattlePoints);
		System.out.println("");
		System.out.println("                                                                                                                            HP = " + H.heroHealth);
		} else {
		System.out.println("                                                                                                                            BP = " + PBP.PlayerBattlePoints);
		System.out.println("");
		System.out.println("                                                                                                                            HP = " + H.heroHealth);
		} if(E.enemyOneHealth == D.death){
			System.out.println("");
			System.out.println("YOU WIN!");
			System.out.println("");
			P.playerPoints = true;
			H.myPoints = H.myPoints + P.PP1;
			System.out.println("You Gained " + P.PP1 + " Points!");
			System.out.println("");
			System.out.println("                                                                                                                            PP = " + P.PP1);
		} else if(E.enemyOneHealth < D.death){
			System.out.println("");
			System.out.println("YOU WIN!");
			System.out.println("");
			P.playerPoints = true;
			H.myPoints = H.myPoints + P.PP1;
			System.out.println("You Gained " + P.PP1 + " Points!");
			System.out.println("");
			System.out.println("                                                                                                                            PP = " + P.PP1);

			}
		if(H.heroHealth == D.death){
			System.out.println("");
			System.out.println("You Died!");
			System.out.println("");
			System.out.println("Restart?");
			System.out.println("");
		} else if(H.heroHealth < D.death){
			System.out.println("");
			System.out.println("You Died!");
			System.out.println("");
			System.out.println("Restart?");
			System.out.println("");
			}
				String battle1a = input.nextLine();
				
				if(battle1a.equals("1")){
					System.out.println("");
					E.enemyOneHealth = E.enemyOneHealth - H.attackOne;
					System.out.println("BAM!");
					System.out.println("");
					System.out.println("Enemy Health = " + E.enemyOneHealth);
				} else if(battle1a.equals("2")){
					System.out.println("");
					E.enemyOneHealth = E.enemyOneHealth - H.attackTwo;
					System.out.println("BAM!");
					System.out.println("");
					System.out.println("Enemy Health = " + E.enemyOneHealth);
				} else if(battle1a.equalsIgnoreCase("3")){
					System.out.println("");
					E.enemyOneHealth = E.enemyOneHealth - H.attackThree;
					System.out.println("BAM!");
					System.out.println("");
					System.out.println("Enemy Health = " + E.enemyOneHealth);
					} else if(battle1a.equalsIgnoreCase("Restart")){
							R.isRestarting();
					} else {
						System.out.println("");
						System.out.println("Invalid Input");
						return;
					}
				if(E.enemyOneHealth == D.death){
					H.heroHealth = H.heroHealth + 0;
				}
				if(E.enemyOneHealth < D.death){
					H.heroHealth = H.heroHealth + 0;
				} else {
				System.out.println("");
				System.out.println("ATTACK RETURNED!");
				H.heroHealth = H.heroHealth - E.eOA1;
				}
				if(E.eOA1 == 0){
				System.out.println("");
				System.out.println("Attack Missed!");
				System.out.println("");
				System.out.println("                                                                                                                            HP = " + H.heroHealth);
				} else {
				System.out.println("");
				System.out.println("                                                                                                                            HP = " + H.heroHealth);
			}
				if(E.enemyOneHealth == D.death){
					System.out.println("");
					System.out.println("YOU WIN!");
					System.out.println("");
					P.playerPoints = true;
					H.myPoints = H.myPoints + P.PP1;
					System.out.println("You Gained " + P.PP1 + " Points!");
					System.out.println("");
					System.out.println("                                                                                                                            PP = " + P.PP1);
				} else if(E.enemyOneHealth < D.death){
					System.out.println("");
					System.out.println("YOU WIN!");
					System.out.println("");
					P.playerPoints = true;
					H.myPoints = H.myPoints + P.PP1;
					System.out.println("You Gained " + P.PP1 + " Points!");
					System.out.println("");
					System.out.println("                                                                                                                            PP = " + P.PP1);
		
			}
				if(H.heroHealth == D.death){
					System.out.println("");
					System.out.println("You Died!");
					System.out.println("");
					System.out.println("Restart?");
					System.out.println("");
				} else if(H.heroHealth < D.death){
					System.out.println("");
					System.out.println("You Died!");
					System.out.println("");
					System.out.println("Restart?");
					System.out.println("");
			}
			
				String battle1b = input.nextLine();
				
				if(battle1b.equals("1")){
					System.out.println("");
					E.enemyOneHealth = E.enemyOneHealth - H.attackOne;
					System.out.println("BAM!");
					System.out.println("");
					System.out.println("Enemy Health = " + E.enemyOneHealth);
				} else if(battle1b.equals("2")){
					System.out.println("");
					E.enemyOneHealth = E.enemyOneHealth - H.attackTwo;
					System.out.println("BAM!");
					System.out.println("");
					System.out.println("Enemy Health = " + E.enemyOneHealth);
				} else if(battle1b.equalsIgnoreCase("3")){
					System.out.println("");
					E.enemyOneHealth = E.enemyOneHealth - H.attackThree;
					System.out.println("BAM!");
					System.out.println("");
					System.out.println("Enemy Health = " + E.enemyOneHealth);
					} else if(battle1b.equalsIgnoreCase("Restart")){
						R.isRestarting();
					} else {
						System.out.println("");
						System.out.println("Invalid Input");
						return;
					}
				if(E.enemyOneHealth == D.death){
					H.heroHealth = H.heroHealth + 0;
				}
				if(E.enemyOneHealth < D.death){
					H.heroHealth = H.heroHealth + 0;
				} else {
				System.out.println("");
				System.out.println("ATTACK RETURNED!");
				H.heroHealth = H.heroHealth - E.eOA1;
				}
				if(E.enemyOneHealth == D.death){
					System.out.println("");
					System.out.println("YOU WIN!");
					System.out.println("");
					P.playerPoints = true;
					H.myPoints = H.myPoints + P.PP1;
					System.out.println("You Gained " + P.PP1 + " Points!");
					System.out.println("");
					System.out.println("                                                                                                                            PP = " + P.PP1);
				} else if(E.enemyOneHealth < D.death){
					System.out.println("");
					System.out.println("YOU WIN!");
					System.out.println("");
					P.playerPoints = true;
					H.myPoints = H.myPoints + P.PP1;
					System.out.println("You Gained " + P.PP1 + " Points!");
					System.out.println("");
					System.out.println("                                                                                                                            PP = " + P.PP1);
	
			}
			if(E.eOA1 == 0){
			System.out.println("");
			System.out.println("Attack Missed!");
			System.out.println("");
			System.out.println("                                                                                                                            HP = " + H.heroHealth);
			} else {
			System.out.println("");
			System.out.println("                                                                                                                            HP = " + H.heroHealth);
			}
			if(H.heroHealth == D.death){
				System.out.println("");
				System.out.println("You Died!");
				System.out.println("");
				System.out.println("Restart?");
				System.out.println("");
			} else if(H.heroHealth < D.death){
				System.out.println("");
				System.out.println("You Died!");
				System.out.println("");
				System.out.println("Restart?");
				System.out.println("");
			}
			
				
			String restart = input.nextLine();
			
			if(restart.equalsIgnoreCase("Restart")){
				R.isRestarting();
			} else {
				System.out.println("");
				System.out.println("Invalid Input");
				return;
			}
			if(E.enemyOneHealth == D.death){
				System.out.println("");
				System.out.println("YOU WIN!");
				System.out.println("");
				P.playerPoints = true;
				H.myPoints = H.myPoints + P.PP1;
				System.out.println("You Gained " + P.PP1 + " Points!");
				System.out.println("");
				System.out.println("                                                                                                                            PP = " + P.PP1);
			} else if(E.enemyOneHealth < D.death){
				System.out.println("");
				System.out.println("YOU WIN!");
				System.out.println("");
				P.playerPoints = true;
				H.myPoints = H.myPoints + P.PP1;
				System.out.println("You Gained " + P.PP1 + " Points!");
				System.out.println("");
				System.out.println("                                                                                                                            PP = " + P.PP1);
	

}
	}
}

What im trying to do is loop to the top of my code, without it kinda restarting. I'm making a game (but it's not an actual game, type game, that is why I am putting it here) and what im trying to do is, have it loop, until either, I die or I kill boss 1.

 

Here is part of my code.

 

(I couldn't get my code under here, so it is on top lol)

 

At the end, I made a loop that wasn't really successful, I would call my void, which in the class it called the startGame(), but when I was done, it would reset, which is not what I wanted. EDIT: I removed the loop, so it's not in the code. But, I had it in there before.

 

Too, if someone helps me with this, I'll most likely shorten my battle strings.

 

Thank you so much! :D


Edited by Error, 23 June 2014 - 09:22 AM.


#2 0xDEADBEEF

0xDEADBEEF

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 790 posts

Posted 23 June 2014 - 12:24 PM

So basically you want to have is something like:

boolean canPlay = true;
while(canPlay) {
    canPlay = startGame();
}

 

Now you say that the game resets - this is because you're reseting something in startGame which shouldn't be reset that way. Since you've got a game class, you should move any information which should be alive for the whole of the lifetime of that class as instance variables of that class. Any information that is only applicable to the current run of the game, should be created and initialised in 'startGame'.

 

That will probably solve your issue.


Creating SEGFAULTs since 1995.


#3 Error

Error

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 72 posts

Posted 26 June 2014 - 10:16 AM

I tried your idea DEADBEEF and it still resets it. I have Boolean canPlay = true; at the top and then I have something like...

} else if(E.enemyOneHealth > D.death){

while(canPlay = true){

startGame();

 

But it just resets for me still.



#4 0xDEADBEEF

0xDEADBEEF

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 790 posts

Posted 26 June 2014 - 10:27 AM

Exactly what is being reset?


Creating SEGFAULTs since 1995.


#5 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts

Posted 26 June 2014 - 11:11 AM

Although I am not really a java programmer, it seems to me that your structure should be different.  You have everything inside the startGame method.  Everything is a local variable.

 

Those should be moved outside that method and made into members of that class.  The constructor should initialize those variables.

 

Also, you should have some means of tracking if a player wins or loses in the class, not the method.  There should be winning and losing methods that simply set a member variable and display the results.  Give your program some structure and it will be easier to follow.


My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth

#6 Error

Error

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 72 posts

Posted 26 June 2014 - 03:36 PM

I think I get what you're saying, I think DEADBEEF said the same thing too. So, for instance, would I do this....

 

class dead{

 

int dead = 0;

 

if(H.heroHeatlh = dead){

System.out.println("You lose);

}

if(E.enemyHealth = dead);

System.out.println("You win");

 

Or would you do that in the enemy and hero class itself? Like

 

class hero{

 

int heroHealth = 100;

 

if(heroHealth = D.dead){

System.out.println("You lose");

 

class enemy{

 

int enemyHealth = 100;

 

if(enemyHealth = D.dead){

System.out.println("You win");

 

Which one would I do and why?



#7 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts

Posted 27 June 2014 - 05:25 AM

Yes, the player and the enemies should control their own statuses, like hit points, etc.  They should have properties that show if they are still alive, etc.

 

Again, it's been a while since I have done Java, so it may be mixed with C#, but you should be able to get the idea of the following.

 

What you have is:

public class Game {

    public void startGame(){
        
        Scanner input = new Scanner(System.in);
        
        Hero H = new Hero();
        Enemies E = new Enemies();
        Store S = new Store();
        Death D = new Death();
        Points P = new Points();
        Restart R = new Restart();
        Loop L = new Loop();
        BattlePoints BP = new BattlePoints();
        PlayerBP PBP = new PlayerBP();
...

Where all of the variables are local to startGame.

public class Game {

        public Hero H;
        public List<Enemies> E;
        private Scanner _input;

    public Game( Scanner  input)
   {    
        this._input = input;
        this.H = new Hero();
        this.E = new List<Enemies>();
   }  
    
   public void startGame(){
        
   .....

This way, they are members of the class.  They are initialized in the constructor.

 

Also, I created a dependency injection of the Scanner.  That way, you can run the game from console, or text file, etc., depending on what you want to do.  Might help for testing purposes.  Create a text file with your anticipated moves and run it from the text file to see how/if it works.

 

     


My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download