我的循环有什么问题.需要计算汇总

本文关键字:计算 问题 循环 什么 我的 | 更新日期: 2023-09-27 17:56:24

我必须建立一些逻辑来编写某种记分牌。这个想法是这样的:

有很多阶段:

  • 第一阶段你有 2 个数字。 7 和 3=10
  • 第二阶段你还有另外 2 个数字。 5 和 1 =6

循环完成后,所需的结果应为:

  • 第一阶段总分=15(第一阶段总分+第二阶段第一次尝试)
  • 第二阶段总分=21(第一阶段总分+(第一阶段第一次尝试+第二阶段的第二次尝试)

我的循环有什么问题?我似乎没有得到想要的灵魂。

private void Calculate(Player player)
{
    for (int i = 0; i < player.Game.Stages.Length; i++)
    {
        int firstThrow = player.Game.Stages[i].FirstTry;
  int secondThrow = player.Game.Stages[i].SecondTry;
  int sumFirstAndSecond = firstThrow + secondThrow;
  //If firstTry + SecondTry==10 is spare
  if ((sumFirstAndSecond == 10) && (firstThrow != 10) && i != player.Game.Stages.Length- 1)
  {
      int stageScore= player.Game.Stages[i].TotalScore + 
                         player.Game.Stages[i + 1].FirstTry;
             player.Game.Stages[i].TotalScore = stageScore;
         }
    }
}
public class Stage
{
    public int FirstTry { get; set; }
    public int SecondTry { get; set; }
    public int TotalScore { get; set; }
}
public class Player
{
    public Player(string name)
    {
        Name = name;
        Game = new Game(name);
    }
    public Game Game { get; set; }
    public string Name { get; set; }
}
public class Game
{
    public Game(string playerName)
    {
        PlayerName = playerName;
        Stages = new Stage[10];
        for (int i = 0; i < Stages.Length; i++)
        {
       Stages[i] = new Stage();
        }
    }
    public Stage[] Stages { get; internal set; }
    public string PlayerName { get; set; }
}

我的循环有什么问题.需要计算汇总

更改以下内容:

private void Calculate(Player player)
{
    for (int i = 0; i < player.Game.Stages.Length; i++)
    {
        int firstThrow = player.Game.Stages[i].FirstTry;
        int secondThrow = player.Game.Stages[i].SecondTry;
        int sumFirstAndSecond = firstThrow + secondThrow;
        if ((sumFirstAndSecond == 10) && (firstThrow != 10) && i != player.Game.Stages.Length- 1)
        {
            int stageScore= player.Game.Stages[i].TotalScore + player.Game.Stages[i + 1].FirstTry;
            player.Game.Stages[i].TotalScore = sumFirstAndSecond + stageScore;
         }
         else if (i > 0) player.Game.Stages[i].TotalScore = player.Game.Stages[i - 1].TotalScore + sumFirstAndSecond;
    }
}

龄球?试试这个,不要忘记添加未命中(作为 0)。
应该适用于跑步和最终得分。

// All balls, including misses (0)!
public readonly IList<int> _balls = new List<int>();
private int _currentBall;
public int CalculateTotalScore()
{
   int score = 0;
   _currentBall = 0;
   for (var frame = 0; frame < 10; frame++)
   {
       if (_currentBall >= _balls.Count)
           break;
       if (_balls[_currentBall] == 10)
       {
           // Strrrike!
           score += AggregateScoreFromCurrentBall(3);
           _currentBall++;
       }
       else if (AggregateScoreFromCurrentBall(2) == 10)
       {
           // Spare
           score += AggregateScoreFromCurrentBall(3);
           _currentBall += 2;
       }
       else
       {
           score += AggregateScoreFromCurrentBall(2);
           _currentBall += 2;
       }
   }
   return score;
}
private int AggregateScoreFromCurrentBall(int numberOfBallsToSum)
{
    var sum = 0;
    for (var i = 0; i < numberOfBallsToSum; i++)
        if (_currentBall + i < _balls.Count)
           sum += _balls[_currentBall + i];
    return sum;
}