开发者

What wrong with my loop .Need to calculate a running total

I have to build some logic to write some sort of scoreboard. The idea is this:

There are many stages:

  • 1st stage you have 2 numbers. 7 and 3=10
  • 2nd stage you have another 2 numbers. 5 and 1 =6

After the loop has finished, the wanted result should be:

  • Stage 1 total score=15 (total 1st Stage + firstTry of secondStage)
  • Stage 2 total score=21 (total 1st Stage + (firstTry + SecondTry of SecondStage)

What's wrong with my loop? I dont seem to get the wanted resoult.

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; }

}


Change this:

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;
    }
}


Bowling? Try this, and do not forget to add misses (as 0).
Should work for both running and final scoring.

// 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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜