Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.