保龄球计分java代码_保龄球计分

这篇博文分享下保龄球计分算法。

计分规则描述如下:

A game of tenpins bowling lasts ten frames, in each of which the bowler makes one or two attempts to knock down ten pins arranged in a triangle. If the bowler knocks down all ten pins on the first attempt (that’s called a “strike”), he scores ten pins plus the number of pins knocked down on his next two rolls. If the bowler knocks down all ten pins after two attempts (that’s called a “spare”), he scores ten pins plus the number of pins knocked down on his next roll. If the bowler fails to knock down all ten pins (that’s called an “open frame”), he scores the number of pins he knocked down. The scores accumulate through all ten frames. At the last frame, if necessary, the pins are reset for one or two additional rolls to count the final bonus. The sample scoresheet below shows how the calculations work:

46a0d13d707c3862e5bae74dc979f5a9.png

For instance, the score in the second frame is 9, the sum of the two balls in the open frame. The score in the third frame is 15, which is 10 for the spare plus 5 on the next ball. The score in the ninth frame is 20, which is 10 for the strike plus 10 for the spare on the first two balls of the tenth frame. The score in the tenth frame is 16, which is 10 for the spare plus 6 for the extra ball, which is a bonus ball not really part of any frame (the two balls of the tenth frame have already been rolled).

下面实现这个需求,用WPF框架来实现。

程序的总体结构如下:

1c44f160106520fedb2c83146357a479.png

其中,第二个工程为对应的单元测试。

MainWindow设计如下:

deaab3d3912ec0dec490daf581b82e62.png

MainWindow.xaml和MainWindow.xaml.cs如下:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

View Code

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documents;usingSystem.Windows.Input;usingSystem.Windows.Media;usingSystem.Windows.Media.Imaging;usingSystem.Windows.Navigation;usingSystem.Windows.Shapes;namespaceBowlingKate

{///

///Interaction logic for MainWindow.xaml///

public partial classMainWindow : Window

{publicMainWindow()

{

InitializeComponent();

}publicFrame frame1, frame2, frame3, frame4, frame5, frame6, frame7, frame8, frame9, frame10;public voidInitFrame()

{

frame1= new Frame() { Throw1 = Frame1_1.Text, Throw2 =Frame1_2.Text };

frame2= new Frame() { Throw1 = Frame2_1.Text, Throw2 =Frame2_2.Text };

frame3= new Frame() { Throw1 = Frame3_1.Text, Throw2 =Frame3_2.Text };

frame4= new Frame() { Throw1 = Frame4_1.Text, Throw2 =Frame4_2.Text };

frame5= new Frame() { Throw1 = Frame5_1.Text, Throw2 =Frame5_2.Text };

frame6= new Frame() { Throw1 = Frame6_1.Text, Throw2 =Frame6_2.Text };

frame7= new Frame() { Throw1 = Frame7_1.Text, Throw2 =Frame7_2.Text };

frame8= new Frame() { Throw1 = Frame8_1.Text, Throw2 =Frame8_2.Text };

frame9= new Frame() { Throw1 = Frame9_1.Text, Throw2 =Frame9_2.Text };

frame10= new Frame() { Throw1 = Frame10_1.Text, Throw2 = Frame10_2.Text, Throw3 =Frame10_3.Text };

}public voidShowEachFrameScore(BowlingCalculator bowlingCalculator)

{

Frame1_Score.Text=bowlingCalculator.CalculatorFrame1(frame1, frame2, frame3).ToString();

Frame2_Score.Text=bowlingCalculator.CalculatorFrame2(frame2, frame3, frame4).ToString();

Frame3_Score.Text=bowlingCalculator.CalculatorFrame3(frame3, frame4, frame5).ToString();

Frame4_Score.Text=bowlingCalculator.CalculatorFrame4(frame4, frame5, frame6).ToString();

Frame5_Score.Text=bowlingCalculator.CalculatorFrame5(frame5, frame6, frame7).ToString();

Frame6_Score.Text=bowlingCalculator.CalculatorFrame6(frame6, frame7, frame8).ToString();

Frame7_Score.Text=bowlingCalculator.CalculatorFrame7(frame7, frame8, frame9).ToString();

Frame8_Score.Text=bowlingCalculator.CalculatorFrame8(frame8, frame9, frame10).ToString();

Frame9_Score.Text=bowlingCalculator.CalculatorFrame9(frame9, frame10).ToString();

Frame10_Score.Text=bowlingCalculator.CalculatorFrame10(frame10).ToString();

}public intCalculatorSumOfScore(BowlingCalculator bowlingCalculator)

{int sumOfScore = bowlingCalculator.CalculatorFrame1(frame1, frame2, frame3) + bowlingCalculator.CalculatorFrame2(frame2, frame3, frame4) + bowlingCalculator.CalculatorFrame3(frame3, frame4, frame5) + bowlingCalculator.CalculatorFrame4(frame4, frame5, frame6) +bowlingCalculator.CalculatorFrame5(frame5, frame6, frame7)+ bowlingCalculator.CalculatorFrame6(frame6, frame7, frame8) + bowlingCalculator.CalculatorFrame7(frame7, frame8, frame9) + bowlingCalculator.CalculatorFrame8(frame8, frame9, frame10) + bowlingCalculator.CalculatorFrame9(frame9, frame10) +bowlingCalculator.CalculatorFrame10(frame10);returnsumOfScore;

}private void OnClick(objectsender, RoutedEventArgs e)

{

InitFrame();

ShowEachFrameScore(newBowlingCalculator());

SumOfScore.Text= CalculatorSumOfScore(newBowlingCalculator()).ToString();

}

}

}

View Code

Frame封装成Frame.cs类,代码如下:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceBowlingKate

{public classFrame

{public string Throw1 { get; set; }public string Throw2 { get; set; }public string Throw3 { get; set; }

}

}

View Code

计分类BowlingCalculator.cs,如下:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceBowlingKate

{public classBowlingCalculator

{#region CalculatorFrame 1-8

public intCalculatorFrame1(Frame frame1, Frame frame2, Frame frame3)

{if (frame1.Throw1 == "X")

{if (frame2.Throw1 == "X")

{if (frame3.Throw1 == "X")

{return 30;

}return 20 +Convert.ToInt32(frame3.Throw1);

}if (frame2.Throw2 == "/")

{return 20;

}return 10 + Convert.ToInt32(frame2.Throw1) +Convert.ToInt32(frame2.Throw2);

}else if (frame1.Throw2 == "/")

{if (frame2.Throw1 == "X")

{return 20;

}else{return 10 +Convert.ToInt32(frame2.Throw1);

}

}else{return Convert.ToInt32(frame1.Throw1) +Convert.ToInt32(frame1.Throw2);

}

}public intCalculatorFrame2(Frame frame2, Frame frame3, Frame frame4)

{if (frame2.Throw1 == "X")

{if (frame3.Throw1 == "X")

{if (frame4.Throw1 == "X")

{return 30;

}return 20 +Convert.ToInt32(frame4.Throw1);

}if (frame3.Throw2 == "/")

{return 20;

}return 10 + Convert.ToInt32(frame3.Throw1) +Convert.ToInt32(frame3.Throw2);

}else if (frame2.Throw2 == "/")

{if (frame3.Throw1 == "X")

{return 20;

}else{return 10 +Convert.ToInt32(frame3.Throw1);

}

}else{return Convert.ToInt32(frame2.Throw1) +Convert.ToInt32(frame2.Throw2);

}

}public intCalculatorFrame3(Frame frame3, Frame frame4, Frame frame5)

{if (frame3.Throw1 == "X")

{if (frame4.Throw1 == "X")

{if (frame5.Throw1 == "X")

{return 30;

}return 20 +Convert.ToInt32(frame5.Throw1);

}if (frame4.Throw2 == "/")

{return 20;

}return 10 + Convert.ToInt32(frame4.Throw1) +Convert.ToInt32(frame4.Throw2);

}else if (frame3.Throw2 == "/")

{if (frame4.Throw1 == "X")

{return 20;

}else{return 10 +Convert.ToInt32(frame4.Throw1);

}

}else{return Convert.ToInt32(frame3.Throw1) +Convert.ToInt32(frame3.Throw2);

}

}public intCalculatorFrame4(Frame frame4, Frame frame5, Frame frame6)

{if (frame4.Throw1 == "X")

{if (frame5.Throw1 == "X")

{if (frame6.Throw1 == "X")

{return 30;

}return 20 +Convert.ToInt32(frame6.Throw1);

}if (frame5.Throw2 == "/")

{return 20;

}return 10 + Convert.ToInt32(frame5.Throw1) +Convert.ToInt32(frame5.Throw2);

}else if (frame4.Throw2 == "/")

{if (frame5.Throw1 == "X")

{return 20;

}else{return 10 +Convert.ToInt32(frame5.Throw1);

}

}else{return Convert.ToInt32(frame4.Throw1) +Convert.ToInt32(frame4.Throw2);

}

}public intCalculatorFrame5(Frame frame5, Frame frame6, Frame frame7)

{if (frame5.Throw1 == "X")

{if (frame6.Throw1 == "X")

{if (frame7.Throw1 == "X")

{return 30;

}return 20 +Convert.ToInt32(frame7.Throw1);

}if (frame6.Throw2 == "/")

{return 20;

}return 10 + Convert.ToInt32(frame6.Throw1) +Convert.ToInt32(frame6.Throw2);

}else if (frame5.Throw2 == "/")

{if (frame6.Throw1 == "X")

{return 20;

}else{return 10 +Convert.ToInt32(frame6.Throw1);

}

}else{return Convert.ToInt32(frame5.Throw1) +Convert.ToInt32(frame5.Throw2);

}

}public intCalculatorFrame6(Frame frame6, Frame frame7, Frame frame8)

{if (frame6.Throw1 == "X")

{if (frame7.Throw1 == "X")

{if (frame8.Throw1 == "X")

{return 30;

}return 20 +Convert.ToInt32(frame8.Throw1);

}if (frame7.Throw2 == "/")

{return 20;

}return 10 + Convert.ToInt32(frame7.Throw1) +Convert.ToInt32(frame7.Throw2);

}else if (frame6.Throw2 == "/")

{if (frame7.Throw1 == "X")

{return 20;

}else{return 10 +Convert.ToInt32(frame7.Throw1);

}

}else{return Convert.ToInt32(frame6.Throw1) +Convert.ToInt32(frame6.Throw2);

}

}public intCalculatorFrame7(Frame frame7, Frame frame8, Frame frame9)

{if (frame7.Throw1 == "X")

{if (frame8.Throw1 == "X")

{if (frame9.Throw1 == "X")

{return 30;

}return 20 +Convert.ToInt32(frame9.Throw1);

}if (frame8.Throw2 == "/")

{return 20;

}return 10 + Convert.ToInt32(frame8.Throw1) +Convert.ToInt32(frame8.Throw2);

}else if (frame7.Throw2 == "/")

{if (frame8.Throw1 == "X")

{return 20;

}else{return 10 +Convert.ToInt32(frame8.Throw1);

}

}else{return Convert.ToInt32(frame7.Throw1) +Convert.ToInt32(frame7.Throw2);

}

}public intCalculatorFrame8(Frame frame8, Frame frame9, Frame frame10)

{if (frame8.Throw1 == "X")

{if (frame9.Throw1 == "X")

{if (frame10.Throw1 == "X")

{return 30;

}return 20 +Convert.ToInt32(frame10.Throw1);

}if (frame9.Throw2 == "/")

{return 20;

}return 10 + Convert.ToInt32(frame9.Throw1) +Convert.ToInt32(frame9.Throw2);

}else if (frame8.Throw2 == "/")

{if (frame9.Throw1 == "X")

{return 20;

}else{return 10 +Convert.ToInt32(frame9.Throw1);

}

}else{return Convert.ToInt32(frame8.Throw1) +Convert.ToInt32(frame8.Throw2);

}

}#endregion

public intCalculatorFrame9(Frame frame9, Frame frame10)

{if (frame9.Throw1 == "X")

{if (frame10.Throw1 == "X")

{if (frame10.Throw2 == "X")

{return 30;

}else{return 20 +Convert.ToInt32(frame10.Throw2);

}

}else if (frame10.Throw2 == "/")

{return 20;

}else{return 10 + Convert.ToInt32(frame10.Throw1) +Convert.ToInt32(frame10.Throw2);

}

}else if (frame9.Throw2 == "/")

{if (frame10.Throw1 == "X")

{return 20;

}else{return 10 +Convert.ToInt32(frame10.Throw1);

}

}else{return Convert.ToInt32(frame9.Throw1) +Convert.ToInt32(frame9.Throw2);

}

}public intCalculatorFrame10(Frame frame10)

{if (frame10.Throw1 == "X")

{if (frame10.Throw2 == "X")

{if (frame10.Throw3 == "X")

{return 30;

}else{return 20 +Convert.ToInt32(frame10.Throw3);

}

}else if (frame10.Throw3 == "/")

{return 20;

}else{return 10 + Convert.ToInt32(frame10.Throw2) +Convert.ToInt32(frame10.Throw3);

}

}else if (frame10.Throw2 == "/")

{if (frame10.Throw3 == "X")

{return 20;

}else{return 10 +Convert.ToInt32(frame10.Throw3);

}

}else{return Convert.ToInt32(frame10.Throw1) +Convert.ToInt32(frame10.Throw2);

}

}

}

}

View Code

程序运行如下:

1fc65c70b38647e8b792c74ac2b4e84e.png

单元测试UnitTest1.cs如下:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

usingSystem;usingMicrosoft.VisualStudio.TestTools.UnitTesting;namespaceUnitTestBowlingCalculator

{

[TestClass]public classUnitTest1

{public staticBowlingKate.Frame frame1, frame2, frame3, frame4, frame5, frame6, frame7, frame8, frame9, frame10;

[ClassInitialize]public static voidInitializeFrames(TestContext context)

{

frame1= new BowlingKate.Frame() { Throw1 = "X"};

frame2= new BowlingKate.Frame() { Throw1 = "3", Throw2 = "/"};

frame3= new BowlingKate.Frame() { Throw1 = "X"};

frame4= new BowlingKate.Frame() { Throw1 = "X"};

frame5= new BowlingKate.Frame() { Throw1 = "X"};

frame6= new BowlingKate.Frame() { Throw1 = "X"};

frame7= new BowlingKate.Frame() { Throw1 = "X"};

frame8= new BowlingKate.Frame() { Throw1 = "X"};

frame9= new BowlingKate.Frame() { Throw1 = "X"};

frame10= new BowlingKate.Frame() { Throw1 = "X", Throw2 = "2", Throw3 = "/"};

}

[TestMethod]public voidTestCalculatorFrame1()

{

BowlingKate.BowlingCalculator bowlingCalculator= newBowlingKate.BowlingCalculator();int score1=bowlingCalculator.CalculatorFrame1(frame1, frame2, frame3);

Assert.AreEqual(score1,20);

}

[TestMethod]public voidTestCalculatorFrame2()

{

BowlingKate.BowlingCalculator bowlingCalculator= newBowlingKate.BowlingCalculator();int score1 =bowlingCalculator.CalculatorFrame2(frame2, frame3, frame4);

Assert.AreEqual(score1,20);

}

[TestMethod]public voidTestCalculatorFrame3()

{

BowlingKate.BowlingCalculator bowlingCalculator= newBowlingKate.BowlingCalculator();int score1 =bowlingCalculator.CalculatorFrame3(frame3, frame4, frame5);

Assert.AreEqual(score1,30);

}

[TestMethod]public voidTestCalculatorFrame9()

{

BowlingKate.BowlingCalculator bowlingCalculator= newBowlingKate.BowlingCalculator();int score1 =bowlingCalculator.CalculatorFrame9(frame9, frame10);

Assert.AreEqual(score1,22);

}

[TestMethod]public voidTestCalculatorFrame10()

{

BowlingKate.BowlingCalculator bowlingCalculator= newBowlingKate.BowlingCalculator();int score1 =bowlingCalculator.CalculatorFrame10(frame10);

Assert.AreEqual(score1,20);

}

}

}

View Code

测试运行如下:

81c5339b005cc15169c352349acc3de5.png

说明:计分算法1-8相同,应重构。

希望对正好遇到Bowling计分的博友有点帮助~

参考后续博文:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值