1
\$\begingroup\$

I am sharing my Bowling Game Score Kata made in Java, it is available for whoever wants to code review it. I appreciate any comments.

class BowlingGameScore {

    private int[] pinsKnockedDown = new int[21];
    private int currentRoll = 0;

    void roll(int pinsKnockedDown) {
        this.pinsKnockedDown[currentRoll++] = pinsKnockedDown;
    }

    int score() {
        int totalScore = 0;
        int rollNumber = 0;

        for (int frameNumber = 0; frameNumber < 10; frameNumber++) {
            int frameScore;

            if (isSpare(rollNumber)) {
                frameScore = 10 + getSpareBonus(rollNumber);
                rollNumber += 2;

            } else if (isStrike(pinsKnockedDown[rollNumber])) {
                frameScore = 10 + getStrikeBonus(rollNumber);
                rollNumber++;

            } else {
                frameScore = pinsKnockedDown[rollNumber] + pinsKnockedDown[rollNumber + 1];
                rollNumber += 2;
            }

            totalScore += frameScore;
        }

        return totalScore;
    }

    private int getStrikeBonus(int roll) {
        return pinsKnockedDown[roll + 1] + pinsKnockedDown[roll + 2];
    }

    private boolean isStrike(int roll) {
        return roll == 10;
    }

    private int getSpareBonus(int roll) {
        return pinsKnockedDown[roll + 2];
    }

    private boolean isSpare(int roll) {
        return pinsKnockedDown[roll] + pinsKnockedDown[roll + 1] == 10;
    }
}

And its tests:

class BowlingGameScoreTest {
    private BowlingGameScore game;

    @BeforeEach
    void setUp() {
        game = new BowlingGameScore();
    }

    @Test
    void
    game_no_pins_get_hit() {
        rollMany(0, 20);

        Assertions.assertEquals(0, game.score());
    }

    @Test
    void
    game_just_1_pin_get_hit() {
        rollMany(1, 20);

        Assertions.assertEquals(20, game.score());
    }

    @Test
    void
    game_with_1_spare() {
        rollSpare();
        game.roll(3);
        rollMany(0, 17);

        Assertions.assertEquals(16, game.score());
    }

    @Test
    void
    game_with_1_strike() {
        game.roll(10);
        game.roll(3);
        game.roll(4);
        rollMany(0, 16);

        Assertions.assertEquals(24, game.score());
    }

    @Test
    void
    game_of_strikes() {
        rollMany(10, 12);

        Assertions.assertEquals(300, game.score());
    }

    @Test
    void
    knockdown_nine_and_miss_ten_times() {

        int firstRollPinsKnockedDown = 9;
        int secondRollPinsKnockedDown = 0;
        int repeatTimes = 10;
        rollPair(repeatTimes, firstRollPinsKnockedDown, secondRollPinsKnockedDown);

        Assertions.assertEquals(90, game.score());
    }


    @Test
    void
    knockdown_five_and_spare_ten_times_and_fice() {
        rollPair(10, 5, 5);
        game.roll(5);

        Assertions.assertEquals(150, game.score());
    }

    private void rollPair(int repeatTimes, int firstRollPinsKnockedDown, int secondRollPinsKnockedDown) {
        for (int i = 0; i < repeatTimes; i++) {
            game.roll(firstRollPinsKnockedDown);
            game.roll(secondRollPinsKnockedDown);
        }
    }

    private void rollSpare() {
        game.roll(5);
        game.roll(5);
    }


    private void rollMany(int pins, int times) {
        for (int i = 0; i < times; i++) {
            game.roll(pins);
        }
    }
}
\$\endgroup\$
  • 2
    \$\begingroup\$ The problem statement would be very helpful. \$\endgroup\$ – vnp Jul 18 at 0:33
0
\$\begingroup\$

As far as BowlingGameScore goes, LGTM. I have no remarks. (And usually I have lots.)

In the tests, after each void (e.g. before game_no_pins_get_hit), it's a bit odd to have a newline.

Also, a static import to turn Assertions.assertEquals into a concise assertEquals would be convenient.

\$\endgroup\$
  • \$\begingroup\$ thank you for your code-review @J_H. I included the static import in my code. regarding the new lines after void, it is Intellij auto format, do you know how to customize it to keep the following format ? @ Test void (new line) game_no_pins_get_hit() { \$\endgroup\$ – Michel Dambros Figueiredo Jul 20 at 22:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.