TDD Bowling Game 练习和重构

题目

Write a program to score a game of Ten-Pin Bowling.

Input: string (described below) representing a bowling game
Ouput: integer score

The scoring rules:

Each game, or "line" of bowling, includes ten turns,
or "frames" for the bowler.

In each frame, the bowler gets up to two tries to
knock down all ten pins.

If the first ball in a frame knocks down all ten pins,
this is called a "strike". The frame is over. The score
for the frame is ten plus the total of the pins knocked
down in the next two balls.

If the second ball in a frame knocks down all ten pins,
this is called a "spare". The frame is over. The score
for the frame is ten plus the number of pins knocked
down in the next ball.

If, after both balls, there is still at least one of the
ten pins standing the score for that frame is simply
the total number of pins knocked down in those two balls.

If you get a spare in the last (10th) frame you get one
more bonus ball. If you get a strike in the last (10th)
frame you get two more bonus balls.
These bonus throws are taken as part of the same turn.
If a bonus ball knocks down all the pins, the process
does not repeat. The bonus balls are only used to
calculate the score of the final frame.

The game score is the total of all frame scores.

Examples:

X indicates a strike
/ indicates a spare

  • indicates a miss
    | indicates a frame boundary
    The characters after the || indicate bonus balls

X|X|X|X|X|X|X|X|X|X||XX
Ten strikes on the first ball of all ten frames.
Two bonus balls, both strikes.
Score for each frame == 10 + score for next two
balls == 10 + 10 + 10 == 30
Total score == 10 frames x 30 == 300

9-|9-|9-|9-|9-|9-|9-|9-|9-|9-||
Nine pins hit on the first ball of all ten frames.
Second ball of each frame misses last remaining pin.
No bonus balls.
Score for each frame == 9
Total score == 10 frames x 9 == 90

5/|5/|5/|5/|5/|5/|5/|5/|5/|5/||5
Five pins on the first ball of all ten frames.
Second ball of each frame hits all five remaining
pins, a spare.
One bonus ball, hits five pins.
Score for each frame == 10 + score for next one
ball == 10 + 5 == 15
Total score == 10 frames x 15 == 150

X|7/|9-|X|-8|8/|-6|X|X|X||81
Total score == 167

题目说明

保龄球的计分不难,每一局总共有十格,每一格里面有两球。共有十支球瓶,要尽量在两球之内把球瓶全部击倒,如果第一球就把全部的球瓶都击倒了,也就是“STRIKE”,画面出现“X”,就算完成一格了,所得分数就是10分再加下两球的倒瓶数,但是如果第一球没有全倒时,就要再打一球,如果剩下的球瓶全都击倒,也就是“SPARE”,画面出现“/”,也算完成一格,所得分数为10分再加下一格第一球的倒瓶数,但是如果第二球也没有把球瓶全部击倒的话,那分数就是第一球加第二球倒的瓶数,再接着打下一格。依此类推直到第十格。但是第十格有三球,第十格时如果第一球或第二球将球瓶全部击倒时,可再加打第三球。

关键信息: 1、 打出X,可以奖励下两个球的得分。 2、打出SPARE 可以奖励下一球的得分。奖励是一个球或两个球的得分,一回合最多投两个球。 百度百科-保龄球

测试代码

public class BowlingGameTest {
    @Test
    public void should_10_oneGameScore_strike(){
        String input="X";
        int expect=10;

        int result = BowlingGame.oneGameScore(input);
        assertEquals(expect,result);
    }

    @Test
    public void should_10_oneGameScore_spare(){
        String input="7/";
        int expect=10;
        int result = BowlingGame.oneGameScore(input);
        assertEquals(expect,result);
    }
    @Test
    public void should_7_oneGameScore_miss(){
        String input="7-";
        int expect=7;
        int result = BowlingGame.oneGameScore(input);
        assertEquals(expect,result);
    }
    @Test
    public void should_9_oneGameScore_miss(){
        String input="72";
        int expect=9;
        int result = BowlingGame.oneGameScore(input);
        assertEquals(expect,result);
    }
    @Test
    public void should_0_oneGameScore_miss(){
        String input="--";
        int expect=0;
        int result = BowlingGame.oneGameScore(input);
        assertEquals(expect,result);
    }
    @Test
    public void should_90_oneGameScore_allMiss(){
        String input="9-|9-|9-|9-|9-|9-|9-|9-|9-|9-||";
        int expect=90;
        BowlingGame bowlingGame = new BowlingGame(input);
        int result = bowlingGame.score();
        assertEquals(expect,result);
    }

    @Test
    public void should_150_oneGameScore_allSpare(){
        String input="5/|5/|5/|5/|5/|5/|5/|5/|5/|5/||5";
        int expect=150;
        BowlingGame bowlingGame = new BowlingGame(input);
        int result = bowlingGame.score();
        assertEquals(expect,result);
    }
    @Test
    public void should_300_oneGameScore_allStrike(){
        String input="X|X|X|X|X|X|X|X|X|X||XX";
        int expect=300;
        BowlingGame bowlingGame = new BowlingGame(input);
        int result = bowlingGame.score();
        assertEquals(expect,result);
    }
    @Test
    public void should_167_oneGameScore_MixScore(){
        String input="X|7/|9-|X|-8|8/|-6|X|X|X||81";
        int expect=167;
        BowlingGame bowlingGame = new BowlingGame(input);
        int result = bowlingGame.score();
        assertEquals(expect,result);
    }
    @Test
    public void should_18_bonus_MixScore(){
        String input="X|7/|9-|X|-8|8/|-6|X|X|X||81";
        int expect=18;
        BowlingGame bowlingGame = new BowlingGame(input);
        int result = bowlingGame.bonus(8);
        assertEquals(expect,result);
    }
}

实现代码

public class BowlingGame {
    String input;
    String[] oneGameScores;
    int score = 0;

    public BowlingGame(String input) {
        this.oneGameScores = input.split("\\|");
        if (this.oneGameScores.length == 12) {
            this.oneGameScores[10] = this.oneGameScores[11];
            this.oneGameScores[11] = "";
        }

        System.out.println(this.oneGameScores.length + "  " + String.join(",", this.oneGameScores));
    }

    public int score() {

        for (int i = 0; i < 10; i++) {
            score += oneGameScore(oneGameScores[i]);
            score += bonus(i);
            System.out.println(" i:"+i+" score:"+score);
        }
        return score;
    }

    public int bonus(int i) {
        String score = oneGameScores[i];
        int bonus = 0;
        if (score.equals("X")) {
            score = oneGameScores[i + 1];
            bonus = oneGameScore(score);
            if (score.equals("X")) {
//                bonus = 10;
                if (i <= 9) {
                    score = oneGameScores[i + 2];
                    score = score.charAt(0)+"-";
                }
                bonus += oneGameScore(score);

            }
            return bonus;
        } else if (score.indexOf("/") != -1) {
            score = oneGameScores[i + 1];
            score = score.charAt(0) + "-";
            bonus = oneGameScore(score);
            return bonus;
        } else {
            return 0;
        }
    }

    public static int oneGameScore(String score) {
        if (score.equals("XX")) {
            return 20;
        } else if (score.indexOf("X") != -1 || score.indexOf("/") != -1) {
            return 10;
        } else if (score.indexOf("-") != -1) {
            score = score.replace("-", "");
            try {
                return Integer.parseInt(score);
            } catch (Exception e) {
                return 0;
            }
        } else {
            try {
                return Integer.parseInt(score.charAt(0) + "") + Integer.parseInt(score.charAt(1) + "");
            } catch (Exception e) {
                return 0;
            }
        }
    }
}

为什么需要重构

  • 这已经不是我第一次写这个Bowling Game。第一是和Bob Deng 结队编程的时候写的,那么时候对bowling 规则并不熟悉,Bob说规则我来写。 当时第一次玩TDD,还是在网页上写代码(cyber-dojo),当时的窘迫比较难忘。关键问题是第二次写写的还是有点乱。作为TDD练习,同一个题目是需要写三遍以上才能慢慢找到一点步调。
  • 写的时候遇到一个BUG,花了些时间查找,查找和定位都不方便。
  • 代码结构不清晰 score() 里面获取每次得分写的比较别扭
  • bonus 需要重构,代码里面的坏味道需要去除
  • 奖励里面有个比较奇怪的处理把一次投球的当一回合处理

重构测试代码

public class BowlingGameTest1 {

    @Test
    public void should_7_oneFrameScore_miss(){
        String input="7-";
        int expect=7;
        BowlingGame1 bowlingGame1 = new BowlingGame1(input);
        int result = bowlingGame1.oneFrameScore(input);
        assertEquals(expect,result);
    }

    @Test
    public void should_10_oneFrameScore_spare(){
        String input="7/";
        int expect=10;
        BowlingGame1 bowlingGame1 = new BowlingGame1(input);
        int result = bowlingGame1.oneFrameScore(input);
        assertEquals(expect,result);
    }
    @Test
    public void should_X_oneFrameScore_strike(){
        String input="X";
        int expect=10;
        BowlingGame1 bowlingGame1 = new BowlingGame1(input);
        int result = bowlingGame1.oneFrameScore(input);
        assertEquals(expect,result);
    }

    @Test
    public void should_20_oneFrameScore_XX(){
        String input="XX";
        int expect=20;
        BowlingGame1 bowlingGame1 = new BowlingGame1(input);
        int result = bowlingGame1.oneFrameScore(input);
        assertEquals(expect,result);
    }
    @Test
    public void should_6_oneFrameScore_6(){
        String input="6";
        int expect=6;
        BowlingGame1 bowlingGame1 = new BowlingGame1(input);
        int result = bowlingGame1.oneFrameScore(input);
        assertEquals(expect,result);
    }
    @Test
    public void should_16_oneFrameScore_X6(){
        String input="X6";
        int expect=16;
        BowlingGame1 bowlingGame1 = new BowlingGame1(input);
        int result = bowlingGame1.oneFrameScore(input);
        assertEquals(expect,result);
    }
    @Test
    public void should_7_oneTurnScore_miss(){
        String input="7-";
        int expect=7;
        BowlingGame1 bowlingGame1 = new BowlingGame1(input);
        int result = bowlingGame1.oneTurnScore(0);
        assertEquals(expect,result);
    }
    @Test
    public void should_13_oneTurnScore_spare(){
        String input="7/|3-";
        int expect=13;
        BowlingGame1 bowlingGame1 = new BowlingGame1(input);
        int result = bowlingGame1.oneTurnScore(0);
        assertEquals(expect,result);
    }
    @Test
    public void should_10_oneTurnScore_spare(){
        String input="7/|-3";
        int expect=10;
        BowlingGame1 bowlingGame1 = new BowlingGame1(input);
        int result = bowlingGame1.oneTurnScore(0);
        assertEquals(expect,result);
    }

    @Test
    public void should_19_oneTurnScore_strike(){
        String input="X|81";
        int expect=19;
        BowlingGame1 bowlingGame1 = new BowlingGame1(input);
        int result = bowlingGame1.oneTurnScore(0);
        assertEquals(expect,result);
    }
    @Test
    public void should_23_oneTurnScore_strike(){
        String input="X|X|35";
        int expect=23;
        BowlingGame1 bowlingGame1 = new BowlingGame1(input);
        int result = bowlingGame1.oneTurnScore(0);
        assertEquals(expect,result);
    }

    @Test
    public void should_90_oneGameScore_allMiss(){
        String input="9-|9-|9-|9-|9-|9-|9-|9-|9-|9-||";
        int expect=90;
        BowlingGame1 bowlingGame1 = new BowlingGame1(input);
        int result = bowlingGame1.score();
        assertEquals(expect,result);
    }
    @Test
    public void should_150_oneGameScore_allSpare(){
        String input="5/|5/|5/|5/|5/|5/|5/|5/|5/|5/||5";
        int expect=150;
        BowlingGame1 bowlingGame1 = new BowlingGame1(input);
        int result = bowlingGame1.score();
        assertEquals(expect,result);
    }
    @Test
    public void should_300_oneGameScore_allStrike(){
        String input="X|X|X|X|X|X|X|X|X|X||XX";
        int expect=300;
        BowlingGame1 bowlingGame1 = new BowlingGame1(input);
        int result = bowlingGame1.score();
        assertEquals(expect,result);
    }

    @Test
    public void should_167_oneGameScore_MixScore(){
        String input="X|7/|9-|X|-8|8/|-6|X|X|X||81";
        int expect=167;
        BowlingGame1 bowlingGame1 = new BowlingGame1(input);
        int result = bowlingGame1.score();
        assertEquals(expect,result);
    }
}

实现代码

public class BowlingGame1 {
    String[] oneGameScores;
    int score;

    public BowlingGame1(String input) {
        oneGameScores = input.split("\\|");
        if (this.oneGameScores.length == 12) {
            this.oneGameScores[10] = this.oneGameScores[11];
            this.oneGameScores[11] = "";
        }
    }

    public int score() {
        for (int i = 0; i < 10; i++) {
            score += oneTurnScore(i);
        }
        return score;
    }

    public int oneTurnScore(int i) {
        int score = 0;
        String scoreExpress = oneGameScores[i];
        score = oneFrameScore(scoreExpress);
        if (scoreExpress.equals("X")) {
            score += strikeBonus(i);
        } else if (scoreExpress.indexOf("/") != -1) {
            score += spareBonus(i);
        }
        return score;
    }

    private int strikeBonus(int i) {
        String scoreExpress = oneGameScores[i + 1];
        int bonus = 0;
        if (scoreExpress.equals("X")) {
            bonus = 10;
            scoreExpress = oneGameScores[i + 2];

            scoreExpress = scoreExpress.charAt(0) + "";
            bonus += oneFrameScore(scoreExpress);
            return bonus;
        } else {
            return oneFrameScore(scoreExpress);
        }
    }


    private int spareBonus(int i) {
        String scoreExpress = oneGameScores[i + 1];
        scoreExpress = scoreExpress.charAt(0) +"";
        return oneFrameScore(scoreExpress);
    }

    public int oneFrameScore(String score) {
        if (score.equals("XX")) {
            return 20;
        } else if (score.indexOf("X") != -1) {
            if (score.length() == 2) {
                score = score.replace("X", "");
                return 10 + Integer.parseInt(score);
            }
            return 10;
        } else if (score.indexOf("/") != -1) {
            return 10;
        } else if (score.indexOf("-") != -1) {
            score = score.replace("-", "");
            try {
                return Integer.parseInt(score);
            } catch (Exception e) {
                return 0;
            }
        } else {
            if (score.length() == 1) {

                return Integer.parseInt(score.charAt(0) + "");
            } else {
                return Integer.parseInt(score.charAt(0) + "") + Integer.parseInt(score.charAt(1) + "");

            }

        }
    }


}

重构的时候发现了一个BUG,不知道你是否看出来了!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,635评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,628评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,971评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,986评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,006评论 6 394
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,784评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,475评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,364评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,860评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,008评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,152评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,829评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,490评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,035评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,156评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,428评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,127评论 2 356

推荐阅读更多精彩内容