Skip to content

Instantly share code, notes, and snippets.

@erikdietrich
Created November 8, 2012 20:11
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save erikdietrich/4041234 to your computer and use it in GitHub Desktop.
The Tenth Frame Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ScratchPadTest
{
public class TenthFrame : Frame
{
public class IllegalFrameException : Exception { }
public int ThirdThrow { get; private set; }
public override int Total { get { return base.Total + ThirdThrow; } }
public TenthFrame(int firstThrow, int secondThrow, int thirdThrow)
{
ValidateIndividualThrows(firstThrow, secondThrow, thirdThrow);
CheckConsecutiveThrows(firstThrow, secondThrow);
CheckConsecutiveThrows(secondThrow, thirdThrow);
CheckForIllegalThirdThrow(firstThrow, secondThrow, thirdThrow);
FirstThrow = firstThrow;
SecondThrow = secondThrow;
ThirdThrow = thirdThrow;
}
private static void CheckForIllegalThirdThrow(int first, int second, int third)
{
if (first + second < Mark && third > 0)
throw new IllegalFrameException();
}
private static void CheckConsecutiveThrows(int first, int second)
{
if (first != Mark && first + second > Mark)
throw new IllegalFrameException();
}
private static void ValidateIndividualThrows(params int[] throws)
{
if (throws.Any(th => th < 0))
throw new UnderflowException();
if (throws.Any(th => th > Mark))
throw new OverflowException();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment