Agile Principles,Patterns,And Practices in C# 之保齡球積分

來源:互聯網
上載者:User
/**************************
 * 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();
        }
    }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.