Execute code until goal is reached.

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 952 Views - Last Post: 24 August 2018 - 05:33 AM Rate Topic: -----

#1 [email protected]   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 125
  • Joined: 24-February 15

Execute code until goal is reached.

Posted 23 August 2018 - 06:05 AM

I have tried to put my code in a while loop, to wait until turrent or enemy hit 0 hp. But it seems like I can't get it to work the way I want. It just repeats itself instead of going further and hit to the condition I have put in.

public class Main
{
    public static void main(String[] args)
    {

        // New objects made
        Tower tower1 = new Tower(100);
        Enemy enemy1 = new Enemy(100);


        while(tower1.health() >= 0 || enemy1.health() >=0) {
            var towHealth = tower1.health();
            var enemHealth = enemy1.health();

            System.out.println("Tower health " + towHealth);
            System.out.println("Enemy health " + enemHealth);

            var towShot = tower1.shoot();
            var enemAtt = enemy1.attack();

            System.out.println("Tower did: " + towShot + " Damage!");
            System.out.println("Enemy did: " + enemAtt + " Damage!");

            var tow1 = towHealth - enemAtt;
            var enem1 = enemHealth - towShot;

            System.out.println("Tower health: " + tow1);
            System.out.println("Enemy heealth: " + enem1);
        }
    }




Is This A Good Question/Topic? 0
  • +

Replies To: Execute code until goal is reached.

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14490
  • View blog
  • Posts: 58,094
  • Joined: 12-June 08

Re: Execute code until goal is reached.

Posted 23 August 2018 - 06:26 AM

Is it an issue where the WHILE loop should be an 'AND' and not an 'OR'?
Was This Post Helpful? 0
  • +
  • -

#3 [email protected]   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 125
  • Joined: 24-February 15

Re: Execute code until goal is reached.

Posted 23 August 2018 - 08:36 AM

No, when I run this program the health of enemy and tower never reaches 0. But when I put it out of the while I can see that the attack of enemy and tower has an effect on each other. Weird why it doesn't work in the while, it just repeats itself, it goes back to 100 health every time and executes again.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14490
  • View blog
  • Posts: 58,094
  • Joined: 12-June 08

Re: Execute code until goal is reached.

Posted 23 August 2018 - 09:12 AM

oh.. derp.. yeah you are declaring variables anew each time the loop runs.

12            var towHealth = tower1.health();
13            var enemHealth = enemy1.health();



Those, and anything else you don't want to be recreated each loop iteration, should be declared outside the scope of the loop.
Was This Post Helpful? 1
  • +
  • -

#5 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2682
  • View blog
  • Posts: 10,723
  • Joined: 03-December 12

Re: Execute code until goal is reached.

Posted 23 August 2018 - 09:19 AM

var towHealth = tower1.health();
var enemHealth = enemy1.health();



Should those be local variables, or should you be calling a method on the instance that lowers the health?
Was This Post Helpful? 0
  • +
  • -

#6 [email protected]   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 125
  • Joined: 24-February 15

Re: Execute code until goal is reached.

Posted 23 August 2018 - 09:34 AM

this are my other two classes. I just store the function inside this variable so I can reuse it. Maybe this is the wrong way.

import java.util.Random;

public class Tower {

    private int towerHealth;
    Random rand;

    public Tower(int towerHealth){
        this.towerHealth = towerHealth;
        this.rand = new Random();
    }

    public int shoot(){
        int towerShot = this.rand.nextInt(25);
        return towerShot;
    }

    public int health(){
        this.towerHealth = towerHealth;
        return towerHealth;
    }
}



import java.util.Random;

public class Enemy {

    private int enemyhealth;
    Random rand;

    public Enemy(int enemyhealth){
        this.enemyhealth = enemyhealth;
        this.rand = new Random();
    }

    public int attack (){
        int enemyAttack = this.rand.nextInt(25);
        return enemyAttack;
    }

    public int health (){
        this.enemyhealth = enemyhealth;
        return enemyhealth;
    }
}


Was This Post Helpful? 0
  • +
  • -

#7 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2682
  • View blog
  • Posts: 10,723
  • Joined: 03-December 12

Re: Execute code until goal is reached.

Posted 23 August 2018 - 09:45 AM

I would think you would want a way to increase and decrease health as well.
Was This Post Helpful? 0
  • +
  • -

#8 [email protected]   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 125
  • Joined: 24-February 15

Re: Execute code until goal is reached.

Posted 23 August 2018 - 09:53 AM

For now, it's just a small program where the enemy attacks the turrent and the turrent the enemy, until someone hits 0 health and I will print out who the winner is. That's why I put it in a while loop, but both never reaches 0 health.
Maybe you can help?
Was This Post Helpful? 0
  • +
  • -

#9 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2682
  • View blog
  • Posts: 10,723
  • Joined: 03-December 12

Re: Execute code until goal is reached.

Posted 23 August 2018 - 10:01 AM

Games tend to run in loops to keep them going anyway. That isn't the issue.

I would suggest redesigning the classes. A base class for starters that have what all players should be able to do (an enemy is a player). Then you derive child classes from the base class and override the methods based on how that player should do things.

That would include a way for the specific class to keep track of its own health, and increase or decrease it based on the need of the game play.
Was This Post Helpful? 1
  • +
  • -

#10 DarenR   User is offline

  • D.I.C Lover

Reputation: 595
  • View blog
  • Posts: 3,839
  • Joined: 12-January 10

Re: Execute code until goal is reached.

Posted 23 August 2018 - 10:28 AM

in your original post you set the health to 100 and it will always hit that because you are calling them in your while loop at ever iteration

  Tower tower1 = new Tower(100);  <--setting it to 100
 var towHealth = tower1.health();  <---constantly calling the 100

Was This Post Helpful? 0
  • +
  • -

#11 [email protected]   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 125
  • Joined: 24-February 15

Re: Execute code until goal is reached.

Posted 23 August 2018 - 10:29 AM

Does something like this look better? Do I need to modify the functions inside tower and enemy?

Player
import java.util.Random;

public class Player {
    private int health;
    private int attack;
    Random rand;

    public Player(int health, int attack){
        this.health = health;
        this.attack = attack;
        this.rand = new Random();
    }

    public int attack(){
        attack = rand.nextInt(25);
        return attack;
    }

    public int health (){
        return health;
    }

}




Enemy
public class Enemy extends Player {

    public Enemy(int health, int attack){
        super(health, attack);
    }
}




Tower
public class Tower extends Player {

    public Tower(int health, int attack){
        super(health, attack);
    }

}



This post has been edited by [email protected]: 23 August 2018 - 10:44 AM

Was This Post Helpful? 0
  • +
  • -

#12 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 895
  • View blog
  • Posts: 3,602
  • Joined: 13-June 14

Re: Execute code until goal is reached.

Posted 23 August 2018 - 10:35 AM

Give the base class a proper name: what is it modelling?
Was This Post Helpful? 0
  • +
  • -

#13 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 120
  • View blog
  • Posts: 843
  • Joined: 05-December 13

Re: Execute code until goal is reached.

Posted 23 August 2018 - 10:45 AM

Declare your variables outside the main void and change them inside the void. Create separate classes to work with attack, health etc
Was This Post Helpful? 0
  • +
  • -

#14 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2682
  • View blog
  • Posts: 10,723
  • Joined: 03-December 12

Re: Execute code until goal is reached.

Posted 23 August 2018 - 11:47 AM

https://www.ideone.com/5JB3Pf

I threw this together in the last half hour. There is room for improvement, but it shows what I was trying to convey.


ndc85430, ironically, that is what I called my base class. What do you think it should be named, considering it does give the abstract of a player in the game.
Was This Post Helpful? 1
  • +
  • -

#15 [email protected]   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 125
  • Joined: 24-February 15

Re: Execute code until goal is reached.

Posted 23 August 2018 - 12:03 PM

Ah I see what you did there with the amount parameter, based on that you can tell enemy attack - amount or says hp potion + amount. This way in the function health will decrease or increase. Smart!!!

I understand parts of the code some things are still a little bit advanced for me. Thanks anyway I will use this as a blueprint, to look back at it. Cool, you put this together in just a half hour, I took me like 2 days to put together the other noob code haha.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2