How to calculate Bowling Score(保龄球计分算法)

  A game of ten-pin bowling is divided into ten rounds (called "frames"). In a frame, each player is given two opportunities to knock down the skittle targets (called "pins"). The player rolls the first ball at the pins. If the first ball knocks down all ten pins, it is called a strike and the frame is completed. When pins are left standing after the first ball, the player rolls a second ball and if all the remaining pins are knocked down, it is called a spare.
  You can get more info about ten-pin bowling, its rules of play and scoring from here

Next I will give u two methods to calculate score for a entire game:

  1. A strike wins you ten points plus the points for the next two balls thrown (for example if you got a strike then followed with a 7 then 2 your value for the strike would be 10+7+2, or 19). A spare wins you ten points plus the points for the next ball thrown (again, if you get a spare then follow it with 7 pins down your value for the spare would be 10+7, or 17). Open frames are added normally (example: you knock down 5 on your first ball and 3 on your second your open frame would be worth 5+3, or 8 points). The maximum score in tenpin bowling is 300. Below is the function to get score:

ContractedBlock.gif ExpandedBlockStart.gif Get Score
ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
/// Take the number of pins for each roll as input
      
/// Return the score of the entire game
      
/// </summary>
/// <param name="pinNum">Number of pins for each roll</param> 
/// <returns>Score</returns>

int GetScore(int[] pinNum)
ExpandedBlockStart.gifContractedBlock.gif
{
    
//Score of the entire game.
    int sum = 0;
    
//score:The score of each round.
    int score;

    
//round: 09: The index for 10 rounds.
    
//pinNum[roll]: The number of pins for each ball thrown.
          for (int round = 1, roll = 0; round <= 10++ round)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
//Strike
        if (pinNum[roll] == 10)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            score 
= 10 + pinNum[roll + 1+ pinNum[roll + 2];
            sum 
+= score;
            roll
++;
              }

        
//Spare
        else if (pinNum[roll] + pinNum[roll + 1== 10)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            score 
= 10 + pinNum[roll + 2];
            sum 
+= score; 
            roll 
+= 2;
        }

        
//Open
        else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            score 
= pinNum[roll] + pinNum[roll + 1];
            sum 
+= score; 
            roll 
+= 2;
        }

    }

    
return sum;
}

  2. There does exist an alternative method for keeping score. It is used by scoreboards at some tournaments, and by some bowling software programs. It is exactly the same as the conventional scoring described above, except that the score is always current.

  The basic rules are as follows:

  • Spare = Points scored for the next ball are doubled (2 points per pin knocked down);
  • Strike = Points scored for the next two balls are doubled (2 points per pin);
  • Two or more strikes in a row = Points scored for the next ball are tripled (3 points per pin). If this roll is not a strike then the next ball is counted with doubled points.
  • If all ten pins are not knocked down, no bonuses are scored on subsequent throws - only actual pinfall.

  The scoring for the tenth frame goes as follows:

  • If a spare was earned in the ninth frame, score double on the first ball in the tenth frame and face value (single) for the second and third balls, if there is a third ball.
  • If a strike was earned in the ninth frame, score double on the first two balls in the tenth frame and single on the third ball.
  • If strikes were earned in the 8th and 9th frames (as in our sample), score triple on the first ball in the tenth, double on the second ball, and single on the third.
  • If the ninth frame was open (all 10 pins not down on 2 balls), then all three balls in the tenth count single.

  Below is the function to get score:

ContractedBlock.gif ExpandedBlockStart.gif Get Score
public static int GetScore(int[] pinNum)
ExpandedBlockStart.gifContractedBlock.gif
{
    
int roll, round;
    
int totalScore = 0;
    
//score:The score of each.
    int score;
    
//index: 09:  The index for 10 rounds.
    
//pinNum[roll]: The number of pins for each ball thrown.
    
    
int[] multiple = new int[pinNum.Count()];
    
for (int i = 0; i < pinNum.Count(); ++i)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        multiple[i] 
= 1;
    }

    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
for (round = 1, roll = 0; round <= 9++round)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//Strike
            if (pinNum[roll] == 10)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (roll - 1 >= 0 && pinNum[roll - 1== 10)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    multiple[roll 
+ 1= 3;
                    multiple[roll 
+ 2= 2;
                }

                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    multiple[roll 
+ 1= 2;
                    multiple[roll 
+ 2= 2;
                }

                score 
= pinNum[roll] * multiple[roll];
                totalScore 
+= score;
                roll
++;
            }

            
//Spare
            else if (pinNum[roll] + pinNum[roll + 1== 10)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                multiple[roll 
+ 2= 2;
                score 
= pinNum[roll] * multiple[roll] + pinNum[roll + 1* multiple[roll + 1];
                totalScore 
+= score;
                roll 
+= 2;
            }

            
//Open
            else if (pinNum[roll] + pinNum[roll + 1< 10)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                score 
= pinNum[roll] * multiple[roll] + pinNum[roll + 1* multiple[roll + 1];
                totalScore 
+= score;
                roll 
+= 2;
            }

            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
throw new Exception("Invalid numbers for round " + round);
            }

        }

        
//For the ninth frame
        
//Strikes are earned if the 8th and 9th frames
        if (multiple[roll] >= 2 && pinNum[roll-1== 10 &&
            multiple[roll 
- 1>= 2 && pinNum[roll-2== 10)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            score 
= pinNum[roll] * 3;
            
if (roll + 1 < pinNum.Count())
                score 
+= pinNum[roll + 1* 2;
            
if (roll + 2 < pinNum.Count())
                score 
+= pinNum[roll + 2];
            totalScore 
+= score;
        }

        
//A strike in the ninth frame
        else if (multiple[roll] == 2 &&
                 pinNum[roll
-1== 10 && 
                 pinNum[roll
-2]!=0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            score 
= pinNum[roll] * 2;
            
if(roll+1<pinNum.Count())
                score 
+= pinNum[roll+1* 2;
            
if (roll + 2 < pinNum.Count())
                score 
+= pinNum[roll + 2];
            totalScore 
+= score;
        }

        
//A spare in the ninth frame
        else if (multiple[roll] == 2)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            score 
= pinNum[roll] * 2;
            
if (roll + 1 < pinNum.Count())
                score 
+= pinNum[roll + 1];
            
if (roll + 2 < pinNum.Count())
                score 
+= pinNum[roll + 2];
            totalScore 
+= score;
        }

        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            score 
= pinNum[roll];
            
if (roll + 1 < pinNum.Count())
                score 
+= pinNum[roll + 1];
            
if (roll + 2 < pinNum.Count())
                score 
+= pinNum[roll + 2];
            totalScore 
+= score;
        }

    }

    
catch (Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        Console.WriteLine(e.Message);
    }


    
return totalScore;
}

 

Go to my home page for more posts

转载于:https://www.cnblogs.com/lantionzy/archive/2009/10/29/1592476.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值