csharp 保龄球比赛卡塔(来自鲍勃叔叔)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 保龄球比赛卡塔(来自鲍勃叔叔)相关的知识,希望对你有一定的参考价值。

// --------------------------------------------------------------------------------------------------------------------
// <copyright file="GameTests.cs" company="">
//   
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace BowlingGameTests
{
    using BowlingGame;

    using Microsoft.VisualStudio.TestTools.UnitTesting;

    [TestClass]
    public class GameTests
    {
        #region Fields

        private Game g;

        #endregion

        #region Public Methods and Operators

        [TestCleanup]
        public void Cleanup()
        {
            this.g = null;
        }

        [TestMethod]
        public void ComplexGame()
        {
            this.RollStrike();
            this.RollSpare();
            this.g.Roll(8);
            this.g.Roll(1);
            this.RollStrike();
            this.RollStrike();
            this.g.Roll(6);
            this.g.Roll(2);
            this.g.Roll(8);
            this.g.Roll(1);
            this.g.Roll(0);
            this.g.Roll(8);
            this.RollStrike();
            this.g.Roll(6);
            this.g.Roll(4);
            this.g.Roll(9);

            Assert.AreEqual(155, this.g.Score());
        }

        [TestInitialize]
        public void Initialize()
        {
            this.g = new Game();
        }

        [TestMethod]
        public void TestAllOnes()
        {
            this.RollMany(20, 1);

            Assert.AreEqual(20, this.g.Score());
        }

        [TestMethod]
        public void TestAllZeroes()
        {
            this.RollMany(20, 0);

            Assert.AreEqual(0, this.g.Score());
        }

        [TestMethod]
        public void TestOneSpare()
        {
            this.RollSpare();
            this.g.Roll(3);
            this.RollMany(17, 0);

            Assert.AreEqual(16, this.g.Score());
        }

        [TestMethod]
        public void TestOneStrike()
        {
            this.RollStrike();
            this.g.Roll(2);
            this.g.Roll(4);
            this.RollMany(16, 0);

            Assert.AreEqual(22, this.g.Score());
        }

        [TestMethod]
        public void TestPerfectGame()
        {
            this.RollMany(12, 10);
            Assert.AreEqual(300, this.g.Score());
        }

        #endregion

        #region Methods

        private void RollMany(int rolls, int pins)
        {
            for (int i = 0; i < rolls; i++)
            {
                this.g.Roll(pins);
            }
        }

        private void RollSpare()
        {
            this.g.Roll(5);
            this.g.Roll(5);
        }

        private void RollStrike()
        {
            this.g.Roll(10);
        }

        #endregion
    }
}
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Game.cs" company="">
//   
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace BowlingGame
{
    public class Game
    {
        #region Fields

        private readonly int[] rolls = new int[21];

        private int currentRoll;

        #endregion

        #region Public Methods and Operators

        public void Roll(int pins)
        {
            this.rolls[this.currentRoll++] = pins;
        }

        public int Score()
        {
            int score = 0;
            int roll = 0;

            for (int frame = 0; frame < 10; frame++)
            {
                if (this.IsStrike(roll))
                {
                    score += 10 + this.StrikeBonus(roll);
                    roll++;
                }
                else if (this.IsSpare(roll))
                {
                    score += 10 + this.SpareBonus(roll);
                    roll += 2;
                }
                else
                {
                    score += this.rolls[roll] + this.rolls[roll + 1];
                    roll += 2;
                }
            }

            return score;
        }

        #endregion

        #region Methods

        private bool IsSpare(int roll)
        {
            return this.rolls[roll] + this.rolls[roll + 1] == 10;
        }

        private bool IsStrike(int roll)
        {
            return this.rolls[roll] == 10;
        }

        private int SpareBonus(int roll)
        {
            return this.rolls[roll + 2];
        }

        private int StrikeBonus(int roll)
        {
            return this.rolls[roll + 1] + this.rolls[roll + 2];
        }

        #endregion
    }
}

以上是关于csharp 保龄球比赛卡塔(来自鲍勃叔叔)的主要内容,如果未能解决你的问题,请参考以下文章

实践中的应用程序架构 - Bob 大叔 [关闭]

卡塔尔世界杯亚洲球队表现给我们的一些启示和思考

卡塔尔世界杯亚洲球队表现给我们的一些启示和思考

世界杯黑技术专题「原理探索专题」一文解读一下“卡塔尔世界杯”的先进技术之半自动越位技术SAOT+比赛用球Al Rihla

NFT+体育,卡塔尔世界杯有哪些NFT看点!

卡塔尔世界杯出现了半自动越位识别技术Feelix Palm动作轨迹捕捉等黑科技,一起来看看吧。