/
Game.cs
67 lines (59 loc) · 1.68 KB
/
Game.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
namespace Bowling
{
public class Game
{
private readonly int[] _rolls = new int[21];
private int _currentRoll;
public void Roll(int pins)
{
_rolls[_currentRoll++] = pins;
}
public int Score
{
get
{
int score = 0;
int frameIndex = 0;
for (int frame = 0; frame < 10; frame++)
{
if (IsStrike(frameIndex))
{
score += 10 + StrikeBonus(frameIndex);
frameIndex++;
}
else if (IsSpare(frameIndex))
{
score += 10 + SpareBonus(frameIndex);
frameIndex += 2;
}
else
{
score += SumOfBallsInFrame(frameIndex);
frameIndex += 2;
}
}
return score;
}
}
private bool IsStrike(int frameIndex)
{
return _rolls[frameIndex] == 10;
}
private int SumOfBallsInFrame(int frameIndex)
{
return _rolls[frameIndex] + _rolls[frameIndex + 1];
}
private int SpareBonus(int frameIndex)
{
return _rolls[frameIndex + 2];
}
private int StrikeBonus(int frameIndex)
{
return _rolls[frameIndex + 1] + _rolls[frameIndex + 2];
}
private bool IsSpare(int frameIndex)
{
return _rolls[frameIndex] + _rolls[frameIndex + 1] == 10;
}
}
}