-3

I need to add some sort of score at the top, but I'm not sure how.

I've tried playing around with some of functions and moving around score functions but I'm coming up without a proper score counter.

Here's the User players class:

Score myInt = new Score();
myInt.score();
if(Pig != null)
{
    World farm;
    farm = getWorld();
    farm.removeObject(Pig);
}
if(Sheep != null)
{
    World farm;
    farm = getWorld();
    farm.removeObject(Sheep);
}
if(Bonus != null)
{
    World farm;
    farm = getWorld();
    farm.removeObject(Bonus);
}

Here is the score method in the Score class:

public void score()
{
        int score = 0;
}

I don't have the scores up yet, but I'm looking to have the Pig be 1 point, the Sheep to be 3, and the Bonus to be 10, and as you play along and move the player, as you eat the objects, you should get the corresponding points. The points should go into the int score object, but the score won't show up in the first code.

TravJ14
  • 27
  • 8

1 Answers1

0

Heey it looks like you are still struggling with the basic java concepts. But i think you are trying to something like the following. You have three types that will add a point to your score object. In java and other Object Oriented languages you will usually use a baseClass or Interface to handle such behaviour. In this case that would be for instance the interface ScoreAble:

 interface ScoreAble {
    int score();
}

your three classes Pig, Sheep and Bonus will implement this interface:

public class Bonus implements ScoreAble {
  public int score() {
    return 10;
  }
}

public class Sheep implements ScoreAble {
  public int score() {
    return 3;
  }
}

public class Pig implements ScoreAble {
  public int score() {
    return 1;
  }
}

i would recommend changing

public void score()
{
      private int score = 0;
}

to

public class Score {
  int score = 0;

  public void addScore(ScoreAble scoreAble) {
    this.score += scoreAble.score();
  }

  public void addScore(int i) {
    this.score += i;
  }

  public void lowerScore(int i) {
    this.score -= i;
   }

  public int getScore() {
    return this.score;
  }

  public void setScore(int score) {
    this.score = score;
  }
}

I don't know how your World class looks like, but i recommend adding these lines:

class World {
  private List<ScoreAble> scoreAbles = new ArrayList<>();
  public List<ScoreAble> getScoreAbles() {
    return this.scoreAbles;
  }

  public void setScoreAbles(List<ScoreAble> scoreAbles) {
    this.scoreAbles = scoreAbles;
  }

 public void addScoreAble(ScoreAble scoreAble) {
  this.scoreAbles.add(scoreAble);

  }
}

you can now change your score adding code to:


World farm = getWorld();
for (ScoreAble scoreAble : farm.getScoreAbles()) {
      score.addScore(scoreAble);
      farm.removeObject(scoreAble)
    }
farm.getScoreAbles().clear();

I am not familiar with greenfoot, so if something does not work or is unclear don't be afraid to ask

Stephan Hogenboom
  • 1,543
  • 2
  • 17
  • 29