Created
October 13, 2012 15:27
-
-
Save meganehouser/3884994 to your computer and use it in GitHub Desktop.
C# CodeKata (Bowlling Game Kata)
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 System.Collections.Generic; | |
using System.Linq; | |
namespace BowllingGamaKata | |
{ | |
public class Game | |
{ | |
private List<int> rolls; | |
public Game() | |
{ | |
this.rolls = new List<int>(); | |
} | |
public void Roll(int pins) | |
{ | |
this.rolls.Add(pins); | |
} | |
public int Score() | |
{ | |
var currentIndex = 0; | |
var totalScore = Enumerable.Range(1, 10). | |
Select(_ => | |
{ | |
if (IsStrike(currentIndex)) | |
{ | |
var strikeScore = 10 + StrikeBonus(currentIndex); | |
currentIndex++; | |
return strikeScore; | |
} | |
if (IsSpare(currentIndex)) | |
{ | |
var spareScore = 10 + SpareBonus(currentIndex); | |
currentIndex += 2; | |
return spareScore; | |
} | |
var noBonusScore = SumOfPinsInFrame(currentIndex); | |
currentIndex += 2; | |
return noBonusScore; | |
}). | |
Sum(); | |
return totalScore; | |
} | |
private int SumOfPinsInFrame(int currentIndex) | |
{ | |
return this.rolls.ElementAt(currentIndex) | |
+ this.rolls.ElementAt(currentIndex + 1); | |
} | |
private int SpareBonus(int currentIndex) | |
{ | |
return this.rolls.ElementAt(currentIndex + 2); | |
} | |
private int StrikeBonus(int currentIndex) | |
{ | |
return this.rolls.ElementAt(currentIndex + 1) | |
+ this.rolls.ElementAt(currentIndex + 2); | |
} | |
private bool IsStrike(int currentIndex) | |
{ | |
return this.rolls.ElementAt(currentIndex) == 10; | |
} | |
private bool IsSpare(int currentIndex) | |
{ | |
return this.rolls.ElementAt(currentIndex) | |
+ this.rolls.ElementAt(currentIndex + 1) == 10; | |
} | |
} | |
} |
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 System.Linq; | |
using NUnit.Framework; | |
using BowllingGamaKata; | |
namespace BowllingGamaKataTest | |
{ | |
[TestFixture] | |
public class GameTest | |
{ | |
private Game g; | |
[SetUp] | |
public void SetUp() | |
{ | |
this.g = new Game(); | |
} | |
[Test] | |
public void パーフェクトゲーム() | |
{ | |
foreach (var i in Enumerable.Range(0, 12)) | |
{ | |
RollStrike(); | |
} | |
Assert.AreEqual(300, this.g.Score()); | |
} | |
[Test] | |
public void ストライクを1回とる() | |
{ | |
RollStrike(); | |
g.Roll(4); | |
g.Roll(5); | |
RollMany(0, 16); | |
Assert.AreEqual(28, g.Score()); | |
} | |
private void RollStrike() | |
{ | |
g.Roll(10); | |
} | |
[Test] | |
public void スペアを1回とる() | |
{ | |
RollSpare(); | |
g.Roll(3); | |
RollMany(0, 17); | |
Assert.AreEqual(16, g.Score()); | |
} | |
private void RollSpare() | |
{ | |
g.Roll(5); | |
g.Roll(5); | |
} | |
[Test] | |
public void 全フレームガター() | |
{ | |
RollMany(0, 20); | |
Assert.AreEqual(0, g.Score()); | |
} | |
[Test] | |
public void 全投球で1本ずつ倒す() | |
{ | |
RollMany(1, 20); | |
Assert.AreEqual(20, g.Score()); | |
} | |
private void RollMany(int pins, int count) | |
{ | |
foreach (var i in Enumerable.Range(0, count)) | |
{ | |
g.Roll(pins); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment