Skip to content

Instantly share code, notes, and snippets.

@erikdietrich
Created November 8, 2012 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikdietrich/4041037 to your computer and use it in GitHub Desktop.
Save erikdietrich/4041037 to your computer and use it in GitHub Desktop.
Bowling Score Calculator
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