Untitled

mail@pastecode.io avatar
unknown
java
21 days ago
1.9 kB
6
Indexable
Never
package videogame;

public class VideoGame {

    public static void main(String[] args) {
        Game theGame = new Game();
        int highScore = theGame.playManyTimes(5);
        System.out.println("highScore = " + highScore);
    }
}

class Game {
    private Level levelOne;
    private Level levelTwo;
    private Level levelThree;
    private boolean bonus;
    public Game() {

    }
    public boolean isBonus() {
        return bonus;
    }
    public void play() {
        bonus = ((int)(Math.random()*2) == 0 ? true : false);
        levelOne = new Level();
        levelTwo = new Level();
        levelThree = new Level();
        System.out.println("==play====");
        System.out.println("bonus = " + bonus);
    }
    public int getScore() {
//part a
        int totalScore = 0;
        
        if (levelOne.goalReached()) {
            totalScore += levelOne.getPoints();
        }
        if (levelTwo.goalReached()) {
            totalScore += levelTwo.getPoints();
        }

        if (levelThree.goalReached()) {
            totalScore += levelThree.getPoints();
        }

        return (totalScore);
    }
    public int playManyTimes(int num) {
//part b
        int highScore = 0;
        for(int i=0; i < num; i++) {
            play();
            int currentScore = getScore();

            if (currentScore > highScore) {
                highScore = currentScore;
            }

        }
        return highScore;
    }

}

class Level {
    boolean goal;
    int points;
    Level() {
        goal = ((int)(Math.random()*2) == 0 ? true : false);
        System.out.println("goal = " + goal);
        points = (int)(Math.random()*101);
        System.out.println("points = " + points);
    }
    public boolean goalReached() {
        return goal;
    }
    public int getPoints() {
        return points;
    }
}