Sha256: c4db4ceab0cb15dd3ab12ea771893b5808d1898fefd047f6a01ce061c5f4f583
Contents?: true
Size: 1.31 KB
Versions: 12
Compression:
Stored size: 1.31 KB
Contents
using System.Collections.Generic; public class BowlingGame { private const int NumberOfFrames = 10; private const int MaximumFrameScore = 10; private readonly List<int> rolls = new List<int>(); public void Roll(int pins) => rolls.Add(pins); public int Score() { var score = 0; var frameIndex = 0; for (int i = 0; i < NumberOfFrames; i++) { if (IsStrike(frameIndex)) { score += 10 + StrikeBonus(frameIndex); frameIndex += 1; } else if (IsSpare(frameIndex)) { score += 10 + SpareBonus(frameIndex); frameIndex += 2; } else { score += SumOfPinsInFrame(frameIndex); frameIndex += 2; } } return score; } private bool IsStrike(int frameIndex) => rolls[frameIndex] == MaximumFrameScore; private bool IsSpare(int frameIndex) => rolls[frameIndex] + rolls[frameIndex + 1] == MaximumFrameScore; private int StrikeBonus(int frameIndex) => rolls[frameIndex + 1] + rolls[frameIndex + 2]; private int SpareBonus(int frameIndex) => rolls[frameIndex + 2]; private int SumOfPinsInFrame(int frameIndex) => rolls[frameIndex] + rolls[frameIndex + 1]; }
Version data entries
12 entries across 12 versions & 1 rubygems