Created
November 8, 2012 19:39
-
-
Save erikdietrich/4041037 to your computer and use it in GitHub Desktop.
Bowling Score Calculator
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 BowlingScoreCalculator | |
{ | |
private readonly Frame[] _frames = new Frame[10]; | |
private int _currentFrame; | |
private Frame LastFrame { get { return _frames[_currentFrame - 1]; } } | |
private Frame TwoFramesAgo { get { return _frames[_currentFrame - 2]; } } | |
public int Score { get; private set; } | |
public void BowlFrame(Frame frame) | |
{ | |
AddMarkBonuses(frame); | |
Score += frame.Total; | |
_frames[_currentFrame++] = frame; | |
} | |
private void AddMarkBonuses(Frame frame) | |
{ | |
if (WereLastTwoFramesStrikes()) | |
Score += frame.Total + frame.FirstThrow; | |
else if (WasLastFrameAStrike()) | |
Score += frame.Total; | |
else if (WasLastFrameASpare()) | |
Score += frame.FirstThrow; | |
} | |
private bool WereLastTwoFramesStrikes() | |
{ | |
return WasLastFrameAStrike() && _currentFrame > 1 && TwoFramesAgo.IsStrike; | |
} | |
private bool WasLastFrameAStrike() | |
{ | |
return _currentFrame > 0 && LastFrame.IsStrike; | |
} | |
private bool WasLastFrameASpare() | |
{ | |
return _currentFrame > 0 && LastFrame.IsSpare; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment