1.题目
题目地址:http://cyber-dojo.org/setup_default_start_point/show/?from=individual

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 theten 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

写一个程序来得判决十杆保龄球比赛。
* 输入:字符串(描述如下)表示保龄球游戏
* 输出:整数的分数
*
* 评分规则:
* 保龄球的每一场比赛,或称“直线”,包括10圈,或者是投球手的“框架”。 在
* 每一轮中,投球手会尝试两次 把十个球都打掉。 如果一轮中的第一个球击倒了所有10个球, 这就是所谓的“击球”。框架结束了,这个框架的得分是10加上被击倒的总杆数 接下来一轮。
* 如果在一个框架内的第二个球击倒了所有10个引脚, 这就是所谓的“备用件”,这轮结束了,这个框架的得分是10加上下一球击中的得分数。
* 如果在两个球之后,仍然有至少一个球没有被击倒,这轮的分数是简单的 :那两个球中被击倒球的总数。
* 如果你在最后(第10)帧中得到一个备胎,你就会得到一个额外的球,更多的奖金。如果你在最后(10次)罢工 框架你得到两个额外的球。 这些额外的投掷是同一个回合的一部分。
* 如果一个额外的球击倒所有的引脚,整个过程 不重复。奖励球只被使用过 计算最终帧的分数。 游戏得分是所有框架得分的总和。
* 例子: X表示击球 /表示有备件 -表示遗漏 |表示一个框架边界 ||之后的字符表示奖励球
* X|X|X| X|X|X| X|X|X|X| || XX
* 在所有的10局中,第一个球被击中10次。两个额外的球,都是好球。
* 每一帧的分数== 10 +下两球的分数球== 10 + 10 + 10 = 30
* 总分= 10帧x 30 = 300
* 9-|9-|9-|9-|9-|9-|9-|9-|9-|9-||
* 第一个球击中了9个球。 每一帧的第二个球错过最后的剩余球。没有额外的球。
* 每帧得分== 9 总分= 10帧x 9 = 90
* 5/|5/|5/| 5/|5/|5/| 5/|5/|5/|5/||5
* 第一个球击中了5个引脚,第二个球击中了之后的引脚。
* 每一帧得分=10 + 下一球球==10 + 5 = 15
* 总分= 10帧x15 = 150
*
* X| 7/| 9-|x|-8 | 8/ |-6|X|X|X||81
* 总分= 10+10 + 10+9 + 9 + 10+8 + 8 + 10+0 + 6 + 10+10+10 + 10+10+8 +10+8+1
* =90 + 77
* = 167
* 20 39 48 66 74 84 90 120 148 167

2.测试

package cs.game.bowling;import org.junit.Test;import junit.framework.Assert;public class BowlingGameTest {@Testpublic void 只有一个球() {//givenString givenStr = "X|";int expectedScore = 10;BowlingGame bowlingGame = new BowlingGame();//whenint actualScore = bowlingGame.caculateScore(givenStr);//thenAssert.assertEquals(expectedScore, actualScore);}@Testpublic void 有两个球() {//givenString givenStr = "X|9-|";int expectedScore = 28;BowlingGame bowlingGame = new BowlingGame();//whenint actualScore = bowlingGame.caculateScore(givenStr);//thenAssert.assertEquals(expectedScore, actualScore);}@Testpublic void 有三个球_1() {//givenString givenStr = "X|9-|8-|";int expectedScore = 36;BowlingGame bowlingGame = new BowlingGame();//whenint actualScore = bowlingGame.caculateScore(givenStr);//thenAssert.assertEquals(expectedScore, actualScore);}@Testpublic void 有三个球_2() {//givenString givenStr = "X|X|8-|";int expectedScore = 54;BowlingGame bowlingGame = new BowlingGame();//whenint actualScore = bowlingGame.caculateScore(givenStr);//thenAssert.assertEquals(expectedScore, actualScore);}@Testpublic void 有三个球_3() {//givenString givenStr = "X|-9|8-|";int expectedScore = 36;BowlingGame bowlingGame = new BowlingGame();//whenint actualScore = bowlingGame.caculateScore(givenStr);//thenAssert.assertEquals(expectedScore, actualScore);}@Testpublic void 有三个球_4() {//givenString givenStr = "8/|9/|8-|";int expectedScore = 45;BowlingGame bowlingGame = new BowlingGame();//whenint actualScore = bowlingGame.caculateScore(givenStr);//thenAssert.assertEquals(expectedScore, actualScore);}@Testpublic void 有十个球_1() {//givenString givenStr = "5/|5/|5/|5/|5/|5/|5/|5/|5/|5/||5";int expectedScore = 150;BowlingGame bowlingGame = new BowlingGame();//whenint actualScore = bowlingGame.caculateScore(givenStr);//thenAssert.assertEquals(expectedScore, actualScore);}@Testpublic void 有十个球_2() {//givenString givenStr = "X|7/|9-|X|-8|8/|-6|X|X|X|81";int expectedScore = 167;BowlingGame bowlingGame = new BowlingGame();//whenint actualScore = bowlingGame.caculateScore(givenStr);//thenAssert.assertEquals(expectedScore, actualScore);}@Testpublic void 有十个球_3() {//givenString givenStr = "X|X|X|X|X|X|X|X|X|X||XX";int expectedScore = 300;BowlingGame bowlingGame = new BowlingGame();//whenint actualScore = bowlingGame.caculateScore(givenStr);//thenAssert.assertEquals(expectedScore, actualScore);}
}

3.实现代码

package cs.game.bowling;/*** * @author shuai.chen* @date 2018年9月12日* @Description* *              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* *            写一个程序来得判决十杆保龄球比赛。*            输入:字符串(描述如下)表示保龄球游戏*            输出:整数的分数 *              *            评分规则: *              保龄球的每一场比赛,或称“直线”,包括10圈,或者是投球手的“框架”。 在*              每一轮中,投球手会尝试两次 把十个球都打掉。 如果一轮中的第一个球击倒了所有10个球, 这就是所谓的“击球”。框架结束了,这个框架的得分是10加上被击倒的总杆数 接下来一轮。 *              如果在一个框架内的第二个球击倒了所有10个引脚, 这就是所谓的“备用件”,这轮结束了,这个框架的得分是10加上下一球击中的得分数。 *              如果在两个球之后,仍然有至少一个球没有被击倒,这轮的分数是简单的 :那两个球中被击倒球的总数。*              如果你在最后(第10)帧中得到一个备胎,你就会得到一个额外的球,更多的奖金。如果你在最后(10次)罢工 框架你得到两个额外的球。 这些额外的投掷是同一个回合的一部分。*              如果一个额外的球击倒所有的引脚,整个过程 不重复。奖励球只被使用过 计算最终帧的分数。 游戏得分是所有框架得分的总和。 *              例子: X表示击球 /表示有备件 -表示遗漏   |表示一个框架边界  ||之后的字符表示奖励球*              *               X|X|X| X|X|X| X|X|X|X| || XX*               在所有的10局中,第一个球被击中10次。两个额外的球,都是好球。*               每一帧的分数== 10 +下两球的分数球== 10 + 10 + 10 = 30 *               总分= 10帧x 30 = 300 *                *                9-|9-|9-|9-|9-|9-|9-|9-|9-|9-||*                第一个球击中了9个球。 每一帧的第二个球错过最后的剩余球。没有额外的球。 *                每帧得分== 9 总分= 10帧x 9 = 90 *                *                5/|5/|5/| 5/|5/|5/| 5/|5/|5/|5/||5*              第一个球击中了5个引脚,第二个球击中了之后的引脚。*              每一帧得分=10 + 下一球球==10 + 5 = 15 *              总分= 10帧x15 = 150 *              *              X| 7/| 9-|x|-8 | 8/ |-6|X|X|X||81*              总分= 10+10  + 10+9 + 9     + 10+8 + 8 + 10+0 + 6 +     10+10+10 + 10+10+8 +10+8+1 *                =90 + 77*                = 167*              20   39   48   66   74   84   90    120   148   167 * * */
public class BowlingGame {public int caculateScore(String str) {/**总分数*/int totalScore = 0;/**总回合数,只要使用一种策略,就自增*/int totalShot = 0;for(int i = 0; i < str.length(); i++) {int score = 0;if(totalShot < 10) {/**策略一:X 则加后面两球*/if("X".equals(str.charAt(i) + "")) {totalShot++;score += 10;int index = 0;for (int j = i + 1; j < str.length(); j++) {if(2 == index) {break;}if(str.charAt(j) >= 49 && str.charAt(j) <= 57) {score += str.charAt(j) - 48;index++;}if(str.charAt(j) == '-') {index++;}if(str.charAt(j) == 'X') {score += 10;index++;}if(str.charAt(j) == '/') {score = score - (str.charAt(--j) - 48) + 10;j++;index++;}}}/** 策略二、三*/if (str.charAt(i) >= 49 && str.charAt(i) <= 57) {score += str.charAt(i) - 48;int index = 0;char ch = str.charAt(i);char cha = str.charAt(i + 1);/** 策略二:两球10分加后面一球*/if(str.charAt(i+1) == '/') {totalShot++;score = score - (str.charAt(i) - 48) + 10;for (int j = i+2; j < str.length(); j++) {if(index == 1) {break;}if(str.charAt(j) == '-') {index++;}if(str.charAt(j) >= 49 && str.charAt(j) <= 57) {score += str.charAt(j) - 48;index++;}if(str.charAt(j) == 'X') {score += 10;index++;}}}/** 策略三:2球未全中则不加后面*/if(str.charAt(i+1) == '-') {totalShot++;score += 0;}}/** 策略四、五、六*/if (str.charAt(i) == '-') {int index = 0;/** 策略四:第一球未中,第二球未中*/if(str.charAt(i+1) == '-') {totalShot++;}/** 策略五:第一球未中,第二球未全中*/if(str.charAt(i+1) >= 49 && str.charAt(i+1) <= 57) {totalShot++;score += str.charAt(i+1) - 48;}/** 策略六:第一球未中,第二球全中*/if(str.charAt(i+1) == 'X') {totalShot++;score += 10;for (int j = i+2; j < str.length(); j++) {if(index == 1) {break;}if(str.charAt(j) == '-') {index++;}if(str.charAt(j) >= 49 && str.charAt(j) <= 57) {score += str.charAt(j) - 48;index++;}if(str.charAt(j) == 'X') {score += 10;index++;}}}//自增是因为-是第一个球,下一个可能是1-9的数字或X,不自增就会多加一次i++;}}totalScore += score;}return totalScore;}public static void main(String[] args) {System.out.println('1' - 48);}
}

5.TDD实现10回合保龄球比赛相关推荐

  1. java写一个程序给保龄球比赛计分的程序

    thoughwork思沃学院的题,还想看看能不能提前去心仪的公司体会下结对编程顺便感受下公司气氛,可是提交的时候发现代码写错了,要求用的是函数我却没用,提交总是出问题,好难过,还不想改,也改不对,毕竟 ...

  2. 《动手深度学习》4.10. 实战Kaggle比赛:预测房价

    4.10. 实战Kaggle比赛:预测房价 本节内容预览 数据 下载和缓存数据集 访问和读取数据集 使用pandas读入并处理数据 数据预处理 处理缺失值&对数值类数据标准化 处理离散值-on ...

  3. 04.10. 实战Kaggle比赛:预测房价

    4.10. 实战Kaggle比赛:预测房价 详细介绍数据预处理.模型设计和超参数选择. 通过亲身实践,你将获得一手经验,这些经验将有益数据科学家的职业成长. import hashlib import ...

  4. TDD系列3-TDD过程实例-保龄球单局积分算法

    认识了TDD,我们以实际案例过程来更好的学习TDD. 案例需求 保龄球单局积分规则为: 1.保龄球按顺序每轮允许投2个球,投完10轮为1局. 2.每击倒1个瓶得1分.投完一轮将两个球的"所得 ...

  5. python模拟足球比赛_python初体验 —— 模拟体育竞技

    python初体验 -->>> 模拟体育竞技 一.排球训练营 1. 简介: 模拟不同的两个队伍进行排球的模拟比赛. 2. 模拟原理: 通过输入各自的能力值(Ⅰ),模拟比赛的进行( P ...

  6. 【算法题】2660. 保龄球游戏的获胜者

    题目: 给你两个下标从 0 开始的整数数组 player1 和 player2 ,分别表示玩家 1 和玩家 2 击中的瓶数. 保龄球比赛由 n 轮组成,每轮的瓶数恰好为 10 . 假设玩家在第 i 轮 ...

  7. 保龄球游戏的获胜者、找出叠涂元素----2023/4/30

    保龄球游戏的获胜者----2023/4/30 给你两个下标从 0 开始的整数数组 player1 和 player2 ,分别表示玩家 1 和玩家 2 击中的瓶数. 保龄球比赛由 n 轮组成,每轮的瓶数 ...

  8. java-保龄球比赛

    要求: 1.保龄球比赛分为10轮,每轮10个球,且每轮分别有2把机会. 2.如果在第一把就击倒所有球(10),那么称为全中. 3.如果在第一把没完全击倒,那么第二次如果全部倒了,则称为补中. 4.在第 ...

  9. bzoj 3877: [Ahoi2014]保龄球

    Description [故事背景] JYY很喜欢打保龄球,虽然技术不高,但是还是总想着的高分.这里JYY 将向你介绍他所参加的特殊保龄球比赛的规则,然后请你帮他得到尽量多的分数. [问题描述] 一场 ...

最新文章

  1. 丢弃掉那些BeanUtils工具类吧,MapStruct真香!!!
  2. 解决Missing artifact com.microsoft.sqlserver:sqljdbc4:jar:4.0问题
  3. MFC 学习的基本概念
  4. 【FI学习笔记】客户发票收款清账
  5. 赠人玫瑰,手有余香-期待协作更新机器学习的公益项目
  6. 荷兰国旗问题(分三块)
  7. JPA 多条件、多表查询
  8. arduino点阵声音频谱_Arduino基础入门篇19—点阵屏
  9. .5-浅析webpack源码之入口函数
  10. CentOS 7 最小化安装后的注意事项(一)
  11. 一不小心画了 24 张图剖析计网应用层协议!
  12. Android中获取视频的第一帧图片的三种方法
  13. 邓俊辉数据结构学习心得系列——如何正确衡量一个算法的好坏
  14. win7或者win10碰到需要administrator权限才能删除的解决办法
  15. 制作一个简单HTML西安旅游网页(HTML+CSS)
  16. 王者荣耀android换ios,2021王者荣耀安卓账号可以转苹果吗 2021年安卓账号转移到ios方法...
  17. office文档在线预览工具平台选型
  18. QML <2> Canvas 自定义绘制 网易云歌单封面实现
  19. 找论文的几个实用网站
  20. mesh repair

热门文章

  1. WKWebView访问Https不受信任的站点的设置
  2. 某次 ctf Mobile 0x01 解题过程
  3. 云账户合法吗_阿里云帮助中心-阿里云,领先的云计算服务提供商
  4. fortran print
  5. 2021年广东省电力市场运行现状分析:累计交易电量2951.7亿千瓦时[图]
  6. 基于51单片机的室内湿度加湿温度声光报警智能自动控制装置设计
  7. 内蒙古中级职称计算机考试时间,2019年内蒙古中级会计职称考试时间安排
  8. e5系列 4路服务器,“芯”力量:四款至强E5 v4双路服务器横评
  9. OkHttp解析(三)关于Okio
  10. OkHttp 官方文档