Last active
September 15, 2016 17:50
-
-
Save ngbrown/7856f09d9b257e64390b8141ceb6a4b4 to your computer and use it in GitHub Desktop.
Code to read
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace BowlingKata02 | |
{ | |
public class Scorer | |
{ | |
private readonly IList<int> rolls = new List<int>(); | |
private int currentRoll; | |
private int score; | |
public int CalculateScore() | |
{ | |
score = 0; | |
currentRoll = 0; | |
ScoreAllRolls(); | |
return score; | |
} | |
private void ScoreAllRolls() | |
{ | |
for (var frame = 0; frame < 10; frame++) | |
{ | |
if (rolls[currentRoll] == 10) | |
ScoreStrike(); | |
else if (SumUpRollsFromCurrent(2) == 10) | |
ScoreSpare(); | |
else | |
ScoreNormal(); | |
} | |
} | |
private void ScoreNormal() | |
{ | |
score += SumUpRollsFromCurrent(2); | |
currentRoll += 2; | |
} | |
private void ScoreSpare() | |
{ | |
score += SumUpRollsFromCurrent(3); | |
currentRoll += 2; | |
} | |
private void ScoreStrike() | |
{ | |
score += SumUpRollsFromCurrent(3); | |
currentRoll++; | |
} | |
private int SumUpRollsFromCurrent(int numberOfRolls) | |
{ | |
var sum = 0; | |
for (var i = 0; i < numberOfRolls; i++) | |
sum += rolls[currentRoll + i]; | |
return sum; | |
} | |
public void Roll(int pins) | |
{ | |
rolls.Add(pins); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using NUnit.Framework; | |
namespace BowlingKata02 | |
{ | |
[TestFixture] | |
public class ScorerTest | |
{ | |
private Scorer scorer; | |
[SetUp] | |
public void SetUp() | |
{ | |
scorer = new Scorer(); | |
} | |
private void RollManyBallsWithSameNumberOfPins(int numberOfBalls, int pins) | |
{ | |
for (var i = 0; i < numberOfBalls; i++) | |
scorer.Roll(pins); | |
} | |
[Test] | |
public void CalculateScore_AllBallsAreGutterBalls_0() | |
{ | |
RollManyBallsWithSameNumberOfPins(20, 0); | |
var score = scorer.CalculateScore(); | |
Assert.AreEqual(0, score); | |
} | |
[Test] | |
public void CalculateScore_AllBallsKnockDownOnePin_20() | |
{ | |
RollManyBallsWithSameNumberOfPins(20, 1); | |
var score = scorer.CalculateScore(); | |
Assert.AreEqual(20, score); | |
} | |
[Test] | |
public void CalculateScore_FirstFrameIsStrikeRestOnes_30() | |
{ | |
scorer.Roll(10); | |
RollManyBallsWithSameNumberOfPins(18, 1); | |
var score = scorer.CalculateScore(); | |
Assert.AreEqual(30, score); | |
} | |
[Test] | |
public void CalculateScore_AllFramesAreStrikes_300() | |
{ | |
RollManyBallsWithSameNumberOfPins(12, 10); | |
var score = scorer.CalculateScore(); | |
Assert.AreEqual(300, score); | |
} | |
[Test] | |
public void CalculateScore_FirstFrameIsSpareRestOnes_29() | |
{ | |
RollManyBallsWithSameNumberOfPins(2, 5); | |
RollManyBallsWithSameNumberOfPins(18, 1); | |
var score = scorer.CalculateScore(); | |
Assert.AreEqual(29, score); | |
} | |
[Test] | |
public void CalculateScore_AllFramesAreSpares_150() | |
{ | |
RollManyBallsWithSameNumberOfPins(21, 5); | |
var score = scorer.CalculateScore(); | |
Assert.AreEqual(150, score); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void DoFizzBuzz() | |
{ | |
var combinations = new Tuple<int, string>[] | |
{ | |
new Tuple<int, string> (3, "Fizz"), | |
new Tuple<int, string> (5, "Buzz"), | |
}; | |
for (int i = 1; i <= 100; i++) | |
{ | |
bool found = false; | |
foreach (var comb in combinations) | |
{ | |
if (i % comb.Item1 == 0) | |
{ | |
found = true; | |
Console.Write(comb.Item2); | |
} | |
} | |
if (!found) | |
{ | |
Console.Write(i); | |
} | |
Console.Write(Environment.NewLine); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment