Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding .NET Core support using C# 7.0
- Loading branch information
Showing
4 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
namespace TDD_Katas_NETCore.Bowling_Game | ||
{ | ||
public class Game | ||
{ | ||
private readonly int[] _rolls = new int[21]; | ||
private int _currentRoll; | ||
|
||
public void Roll(int pins) | ||
{ | ||
_rolls[_currentRoll++] = pins; | ||
} | ||
|
||
public int Score() | ||
{ | ||
var score = 0; | ||
var frameIndex = 0; | ||
for (var frame = 0; frame < 10; frame++) | ||
if (IsStrike(frameIndex)) | ||
{ | ||
score += StrikeBonus(frameIndex); | ||
frameIndex++; | ||
} | ||
else if (IsSpare(frameIndex)) | ||
{ | ||
score += 10 + SpareBonus(frameIndex); | ||
frameIndex += 2; | ||
} | ||
|
||
else | ||
{ | ||
score += SumOfBallsInFrames(frameIndex); | ||
frameIndex += 2; | ||
} | ||
return score; | ||
} | ||
|
||
private bool IsStrike(int frameIndex) => _rolls[frameIndex] == 10; | ||
|
||
|
||
|
||
private int SumOfBallsInFrames(int frameIndex) => _rolls[frameIndex] + _rolls[frameIndex + 1]; | ||
|
||
private int SpareBonus(int frameIndex) => _rolls[frameIndex + 2]; | ||
|
||
private bool IsSpare(int frameIndex) => _rolls[frameIndex] + _rolls[frameIndex + 1] == 10; | ||
|
||
private int StrikeBonus(int frameIndex) => 10 + _rolls[frameIndex + 1] + _rolls[frameIndex + 2]; | ||
|
||
|
||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using System; | ||
using NUnit.Framework; | ||
|
||
namespace TDD_Katas_NETCore.Bowling_Game | ||
{ | ||
[TestFixture] | ||
[Category("TheBowlingGameKataNETCore")] | ||
public class TestGame | ||
{ | ||
[SetUp] | ||
public void Setup() | ||
{ | ||
_game = new Game(); | ||
_frames = 10; | ||
} | ||
|
||
[TearDown] | ||
public void TearDown() | ||
{ | ||
_game = null; | ||
_frames = 0; | ||
} | ||
|
||
private Game _game; | ||
private int _frames; | ||
|
||
private void RollStrike() | ||
{ | ||
_game.Roll(10); | ||
} | ||
|
||
private void FrameHits(int pin, int count) | ||
{ | ||
for (var i = 0; i < count; i++) | ||
_game.Roll(pin); | ||
} | ||
|
||
private void RollSpare() | ||
{ | ||
_game.Roll(5); | ||
_game.Roll(5); | ||
} | ||
|
||
[Test] | ||
public void Can_Get_Calculate_Full_Game_Scores() | ||
{ | ||
FrameHits(10, 12); | ||
Console.WriteLine("Roll Total - {0}, Result - {1}", 300, _game.Score()); | ||
Assert.That(300, Is.EqualTo(_game.Score())); | ||
} | ||
|
||
|
||
[Test] | ||
public void Can_Get_Calculate_Scores() | ||
{ | ||
FrameHits(0, _frames); | ||
Assert.That(0, Is.EqualTo(_game.Score())); | ||
} | ||
|
||
[Test] | ||
public void Can_Get_Calculate_Single_Scores() | ||
{ | ||
_game.Roll(0); | ||
Console.WriteLine("Roll Total - {0}, Result - {1}", 0, _game.Score()); | ||
Assert.That(0, Is.EqualTo(_game.Score())); | ||
} | ||
|
||
[Test] | ||
public void Can_Get_Calculate_Spare_Scores() | ||
{ | ||
RollSpare(); | ||
_game.Roll(3); | ||
FrameHits(0, 17); | ||
Console.WriteLine("Roll Total - {0}, Result - {1}", 16, _game.Score()); | ||
Assert.That(16, Is.EqualTo(_game.Score())); | ||
} | ||
|
||
[Test] | ||
public void Can_Get_Calculate_Strike_Scores() | ||
{ | ||
RollStrike(); | ||
_game.Roll(3); | ||
_game.Roll(4); | ||
FrameHits(0, 16); | ||
Console.WriteLine("Roll Total - {0}, Result - {1}", 24, _game.Score()); | ||
Assert.That(24, Is.EqualTo(_game.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp2.0</TargetFramework> | ||
<RootNamespace>TDD_Katas_NETCore</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> | ||
<LangVersion>7</LangVersion> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="NUnit" Version="3.9.0" /> | ||
<PackageReference Include="NUnit3TestAdapter" Version="3.9.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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