这篇博文分享下保龄球计分算法。
计分规则描述如下:
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:
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框架来实现。
程序的总体结构如下:
其中,第二个工程为对应的单元测试。
MainWindow设计如下:
MainWindow.xaml和MainWindow.xaml.cs如下:
<Window x:Class="BowlingKate.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Bowling Kate" Height="347.898" Width="967.493"> <StackPanel> <StackPanel Orientation="Horizontal" > <GroupBox Header="第1轮" > <GroupItem> <StackPanel> <StackPanel Orientation="Horizontal"> <TextBox x:Name="Frame1_1" Width="40" Height="40" FontSize="30" TextAlignment="Center" Background="LightCyan"/> <TextBox x:Name="Frame1_2" Width="40" Height="40" FontSize="30" TextAlignment="Center" Background="LightCyan"/> </StackPanel> <TextBlock Text="本轮得分" Background="LightPink"/> <TextBlock x:Name="Frame1_Score" Width="80" Height="40" FontSize="30" TextAlignment="Center" Background="LightGreen" /> </StackPanel> </GroupItem> </GroupBox> <GroupBox Header="第2轮" > <GroupItem> <StackPanel> <StackPanel Orientation="Horizontal"> <TextBox x:Name="Frame2_1" Width="40" Height="40" FontSize="30" TextAlignment="Center" Background="LightCyan"/> <TextBox x:Name="Frame2_2" Width="40" Height="40" FontSize="30" TextAlignment="Center" Background="LightCyan"/> </StackPanel> <TextBlock Text="本轮得分" Background="LightPink"/> <TextBlock x:Name="Frame2_Score" Width="80" Height="40" FontSize="30" TextAlignment="Center" Background="LightGreen" /> </StackPanel> </GroupItem> </GroupBox> <GroupBox Header="第3轮" > <GroupItem> <StackPanel> <StackPanel Orientation="Horizontal"> <TextBox x:Name="Frame3_1" Width="40" Height="40" FontSize="30" TextAlignment="Center" Background="LightCyan"/> <TextBox x:Name="Frame3_2" Width="40" Height="40" FontSize="30" TextAlignment="Center" Background="LightCyan"/> </StackPanel> <TextBlock Text="本轮得分" Background="LightPink"/> <TextBlock x:Name="Frame3_Score" Width="80" Height="40" FontSize="30" TextAlignment="Center" Background="LightGreen" /> </StackPanel> </GroupItem> </GroupBox> <GroupBox Header="第4轮" > <GroupItem> <StackPanel> <StackPanel Orientation="Horizontal"> <TextBox x:Name="Frame4_1" Width="40" Height="40" FontSize="30" TextAlignment="Center" Background="LightCyan"/> <TextBox x:Name="Frame4_2" Width="40" Height="40" FontSize="30" TextAlignment="Center" Background="LightCyan"/> </StackPanel> <TextBlock Text="本轮得分" Background="LightPink"/> <TextBlock x:Name="Frame4_Score" Width="80" Height="40" FontSize="30" TextAlignment="Center" Background="LightGreen" /> </StackPanel> </GroupItem> </GroupBox> <GroupBox Header="第5轮" > <GroupItem> <StackPanel> <StackPanel Orientation="Horizontal"> <TextBox x:Name="Frame5_1" Width="40" Height="40" FontSize="30" TextAlignment="Center" Background="LightCyan"/> <TextBox x:Name="Frame5_2" Width="40" Height="40" FontSize="30" TextAlignment="Center" Background="LightCyan"/> </StackPanel> <TextBlock Text="本轮得分" Background="LightPink"/> <TextBlock x:Name="Frame5_Score" Width="80" Height="40" FontSize="30" TextAlignment="Center" Background="LightGreen" /> </StackPanel> </GroupItem> </GroupBox> <GroupBox Header="第6轮" > <GroupItem> <StackPanel> <StackPanel Orientation="Horizontal"> <TextBox x:Name="Frame6_1" Width="40" Height="40" FontSize="30" TextAlignment="Center" Background="LightCyan"/> <TextBox x:Name="Frame6_2" Width="40" Height="40" FontSize="30" TextAlignment="Center" Background="LightCyan"/> </StackPanel> <TextBlock Text="本轮得分" Background="LightPink"/> <TextBlock x:Name="Frame6_Score" Width="80" Height="40" FontSize="30" TextAlignment="Center" Background="LightGreen" /> </StackPanel> </GroupItem> </GroupBox> <GroupBox Header="第7轮" > <GroupItem> <StackPanel> <StackPanel Orientation="Horizontal"> <TextBox x:Name="Frame7_1" Width="40" Height="40" FontSize="30" TextAlignment="Center" Background="LightCyan"/> <TextBox x:Name="Frame7_2" Width="40" Height="40" FontSize="30" TextAlignment="Center" Background="LightCyan"/> </StackPanel> <TextBlock Text="本轮得分" Background="LightPink"/> <TextBlock x:Name="Frame7_Score" Width="80" Height="40" FontSize="30" TextAlignment="Center" Background="LightGreen" /> </StackPanel> </GroupItem> </GroupBox> <GroupBox Header="第8轮" > <GroupItem> <StackPanel> <StackPanel Orientation="Horizontal"> <TextBox x:Name="Frame8_1" Width="40" Height="40" FontSize="30" TextAlignment="Center" Background="LightCyan"/> <TextBox x:Name="Frame8_2" Width="40" Height="40" FontSize="30" TextAlignment="Center" Background="LightCyan"/> </StackPanel> <TextBlock Text="本轮得分" Background="LightPink"/> <TextBlock x:Name="Frame8_Score" Width="80" Height="40" FontSize="30" TextAlignment="Center" Background="LightGreen" /> </StackPanel> </GroupItem> </GroupBox> <GroupBox Header="第9轮" > <GroupItem> <StackPanel> <StackPanel Orientation="Horizontal"> <TextBox x:Name="Frame9_1" Width="40" Height="40" FontSize="30" TextAlignment="Center" Background="LightCyan"/> <TextBox x:Name="Frame9_2" Width="40" Height="40" FontSize="30" TextAlignment="Center" Background="LightCyan"/> </StackPanel> <TextBlock Text="本轮得分" Background="LightPink"/> <TextBlock x:Name="Frame9_Score" Width="80" Height="40" FontSize="30" TextAlignment="Center" Background="LightGreen" /> </StackPanel> </GroupItem> </GroupBox> <GroupBox Header="第10轮" > <GroupItem> <StackPanel> <StackPanel Orientation="Horizontal"> <TextBox x:Name="Frame10_1" Width="40" Height="40" FontSize="30" TextAlignment="Center" Background="LightCyan"/> <TextBox x:Name="Frame10_2" Width="40" Height="40" FontSize="30" TextAlignment="Center" Background="LightCyan"/> <TextBox x:Name="Frame10_3" Width="40" Height="40" FontSize="30" TextAlignment="Center" Background="LightCyan"/> </StackPanel> <TextBlock Text="本轮得分" Background="LightPink"/> <TextBlock x:Name="Frame10_Score" Width="120" Height="40" FontSize="30" TextAlignment="Center" Background="LightGreen" /> </StackPanel> </GroupItem> </GroupBox> </StackPanel> <TextBlock Text="0-9,X-全中,/-补中" FontSize="16" Foreground="Cyan" Background="DarkKhaki"/> <TextBlock Text="总得分"/> <TextBlock x:Name="SumOfScore" FontSize="80" Text="Your Score" Foreground="Red" Background="Azure"/> <Button Content="计算得分" FontSize="16" Height="40" Width="400" Click="OnClick"/> </StackPanel> </Window>