Agile Principles,Patterns,And Practices in C# 之保龄球积分

/* *************************
 * Function:bowling ball for score
 * Reladsed Time:2008-08-11
 * Writer:Robot.H
 * code segment:Agile Principles,Patterns,And Practices in C#
*************************
*/
public   class  Scorer
{
        
// index 
         private   int  ball;
        
// there is 21 throw in bowlingball sports
         private   int [] throws  =   new   int [ 21 ];
        
// store the current throw
         private   int  currentThrow;
        
// chiense meaning is buzhong[补中]
         private   bool  Spare()
        {
            
return  throws[ball]  +  throws[ball  +   1 ==   10 ;
        }
        
// chiense meaning is quanzhong[全中]
         private   bool  Strike()
        {
            
return  throws[ball]  ==   10 ;
        }
        
private   int  TwoBallInFrame
        {
            
get  {  return  throws[ball]  +  throws[ball  +   1 ]; }
        }
        
private   int  NextBallForSpare
        { 
get  {  return  throws[ball  +   2 ]; } }
        
private   int  NextBallForStrike
        { 
get  {  return  throws[ball  +   1 +  throws[ball  +   2 ]; } }

        
public    void  AddThrow( int  pins)
        {
            throws[currentThrow
++ =  pins;
        }
        
public   int  ScoreForFrame( int  theFrame)
        {
            
this .ball  =   0 ;
            
int  score = 0 ;
            
for  ( int  currframe  =   0 ; currframe  <  theFrame; currframe ++ )
            {
                
if  (Strike())
                {
                    score 
+=   10   +  NextBallForStrike;
                    ball
++ ;
                }
                
else   if  (Spare())
                {
                    score 
+=   10   +  NextBallForSpare;
                    ball 
+=   2 ;
                }
                
else
                {
                    score 
+=  TwoBallInFrame;
                    ball 
+=   2 ;
                }
            }
            
return  score;
}
public   class  Game
{
        
private   int  currentFrame  =   0 ;
        
private   bool  isFirstThrow  =   true ;
        
private  Scorer scorer  =   new  Scorer();

        
public   int  Score
        { 
get  {  return  ScoreForFrame(currentFrame); } }

        
public   void  Add( int  pins)
        {
            scorer.AddThrow(pins);
            AdjustCurrentFrame(pins);
        }
        
private   void  AdjustCurrentFrame( int  pins)
        {
            
if  (LastBallInFrame(pins))
                AdvanceFrame();
            
else
                isFirstThrow 
=   false ;
        }
        
private   bool  LastBallInFrame( int  pins)
        {
            
return  Strike(pins) || ( ! isFirstThrow);
        }
        
private   bool  Strike( int  pins)
        { 
            
return  (isFirstThrow && pins == 10 );
        }
        
private   void  AdvanceFrame()
        {
            currentFrame
++ ;
            
if  (currentFrame  >   10 )
                currentFrame 
=   10 ;
        }
        
public   int  ScoreForFrame( int  currentframe)
        {
            
return  scorer.ScoreForFrame(currentframe);
        }
}
    
class  Program
    {
        
static   void  Main( string [] args)
        {
            Game game 
=   new  Game();
            game.Add(
1 );
            game.Add(
4 );
            game.Add(
4 );
            game.Add(
5 );
            game.Add(
6 );
            game.Add(
4 );
            game.Add(
5 );
            game.Add(
5 );
            game.Add(
10 );
            game.Add(
0 );
            game.Add(
1 );
            game.Add(
7 );
            game.Add(
3 );
            game.Add(
6 );
            game.Add(
4 );
            game.Add(
10 );
            game.Add(
2 );
            game.Add(
8 );
            game.Add(
6 );

            Console.WriteLine(
" score: "   +  game.Score);
            
            Console.Read();
        }
    }

你可能感兴趣的:(Pattern)