Last active
August 29, 2015 14:00
-
-
Save TheSecretSquad/1acf6e2862f5617b2059 to your computer and use it in GitHub Desktop.
Bowling Score Calculator Kata from Daedtech.com Alternative Design Question - http://www.daedtech.com/tdd-for-breaking-problems-apart-3-finishing-up
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; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
namespace ScratchPadTest | |
{ | |
public class Frame | |
{ | |
public static Frame Strike { get { return new Frame(10, 0); } } | |
public class UnderflowException : Exception { } | |
public class OverflowException : Exception { } | |
protected const int Mark = 10; | |
public int FirstThrow { get; protected set; } | |
public int SecondThrow { get; protected set; } | |
public bool IsStrike { get { return FirstThrow == Frame.Mark; } } | |
public bool IsSpare { get { return !IsStrike && BaseTotal == Frame.Mark; } } | |
public virtual int Total(Frames previousFrames) | |
{ | |
int total = BaseTotal; | |
if (WereLastTwoFramesStrikes(previousFrames)) | |
total += BaseTotal + FirstThrow; | |
else if (WasLastFrameAStrike(previousFrames)) | |
total += BaseTotal; | |
else if (WasLastFrameASpare(previousFrames)) | |
total += FirstThrow; | |
return total; | |
} | |
private int BaseTotal { get { return FirstThrow + SecondThrow; } } | |
private bool WereLastTwoFramesStrikes(Frames frames) | |
{ | |
return WasLastFrameAStrike(frames) && frames.Count > 1 && frames.BackFromLast(2).IsStrike; | |
} | |
private bool WasLastFrameAStrike(Frames frames) | |
{ | |
return frames.Count > 0 && frames.Last.IsStrike; | |
} | |
private bool WasLastFrameASpare(Frames frames) | |
{ | |
return frames.Count > 0 && frames.BackFromLast(1).IsSpare; | |
} | |
protected Frame() { } | |
public Frame(int firstThrow, int secondThrow) | |
{ | |
if (firstThrow < 0 || secondThrow < 0) | |
throw new UnderflowException(); | |
if (firstThrow + secondThrow > Mark) | |
throw new OverflowException(); | |
FirstThrow = firstThrow; | |
SecondThrow = secondThrow; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment