Last active
December 18, 2015 09:19
-
-
Save laribee/5760490 to your computer and use it in GitHub Desktop.
bowlin'
This file contains hidden or 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.Collections; | |
| using System.Collections.Generic; | |
| using MbUnit.Framework; | |
| namespace specs_for_bowling | |
| { | |
| [TestFixture] | |
| public class when_everything_is_wired_up | |
| { | |
| [Test] | |
| public void BowlingGame_NewGame_ScoreIs0() | |
| { | |
| var bowl = new BowlingGame(); | |
| Assert.AreEqual(0,bowl.Score); | |
| } | |
| [Test] | |
| public void BowlingGame_1stRoll5_ScoreIs0() | |
| { | |
| var bowl = new BowlingGame(); | |
| bowl.Roll(5); | |
| Assert.AreEqual(0, bowl.Score); | |
| } | |
| [Test] | |
| public void BowlingGame_Roll5THen4_ScoreIs9() | |
| { | |
| var bowl = new BowlingGame(); | |
| bowl.Roll(5); | |
| bowl.Roll(4); | |
| Assert.AreEqual(9, bowl.Score); | |
| } | |
| [Test] | |
| public void BowlingGame_1stRollSTRIKE_ScoreIs0() | |
| { | |
| var bowl = new BowlingGame(); | |
| bowl.Roll(10); | |
| Assert.AreEqual(0, bowl.Score); | |
| } | |
| [Test] | |
| public void BowlingGame_Roll5THen4Then3_ScoreIs9() | |
| { | |
| var bowl = new BowlingGame(); | |
| bowl.Roll(5); | |
| bowl.Roll(4); | |
| bowl.Roll(3); | |
| Assert.AreEqual(9, bowl.Score); | |
| } | |
| [Test] | |
| public void BowlingGame_Roll5THen4Then3Then2_ScoreIs14() | |
| { | |
| var bowl = new BowlingGame(); | |
| bowl.Roll(5); | |
| bowl.Roll(4); | |
| bowl.Roll(3); | |
| bowl.Roll(2); | |
| Assert.AreEqual(14, bowl.Score); | |
| } | |
| [Test] | |
| public void BowlingGame_Roll5THen5_ScoreIs0() | |
| { | |
| var bowl = new BowlingGame(); | |
| bowl.Roll(5); | |
| bowl.Roll(5); | |
| Assert.AreEqual(0, bowl.Score); | |
| } | |
| [Test] | |
| public void BowlingGame_Roll5THen5Then2_ScoreIs12() | |
| { | |
| var bowl = new BowlingGame(); | |
| bowl.Roll(5); | |
| bowl.Roll(5); | |
| bowl.Roll(2); | |
| Assert.AreEqual(12, bowl.Score); | |
| } | |
| [Test] | |
| public void BowlingGame_Roll5Then5The2Then3_ScoreIs17() | |
| { | |
| var bowl = new BowlingGame(); | |
| bowl.Roll(5); | |
| bowl.Roll(5); | |
| bowl.Roll(2); | |
| bowl.Roll(3); | |
| Assert.AreEqual(17, bowl.Score); | |
| } | |
| [Test] | |
| public void BowlingGame_Roll7Then3The6Then4_ScoreIs16() | |
| { | |
| var bowl = new BowlingGame(); | |
| bowl.Roll(7); | |
| bowl.Roll(3); | |
| bowl.Roll(6); | |
| bowl.Roll(4); | |
| Assert.AreEqual(16, bowl.Score); | |
| } | |
| [Test] | |
| public void BowlingGame_Roll7Then3The6Then4Then1_ScoreIs27() | |
| { | |
| var bowl = new BowlingGame(); | |
| bowl.Roll(7); | |
| bowl.Roll(3); | |
| bowl.Roll(6); | |
| bowl.Roll(4); | |
| bowl.Roll(1); | |
| Assert.AreEqual(27, bowl.Score); | |
| } | |
| [Test] | |
| public void BowlingGame_BowlSTRIKEOnFirstFrame_ScoreIs0() | |
| { | |
| var bowl = new BowlingGame(); | |
| bowl.Roll(10); | |
| Assert.AreEqual(0, bowl.Score); | |
| } | |
| [Test] | |
| public void BowlingGame_BowlSTRIKEOnFirstAndSecondFrame_ScoreIs0() | |
| { | |
| var bowl = new BowlingGame(); | |
| bowl.Roll(10); | |
| bowl.Roll(10); | |
| Assert.AreEqual(0, bowl.Score); | |
| } | |
| [Test] | |
| public void BowlingGame_BowlSTRIKEThen2THen3_ScoreIs20() | |
| { | |
| var bowl = new BowlingGame(); | |
| bowl.Roll(10); | |
| bowl.Roll(2); | |
| bowl.Roll(3); | |
| Assert.AreEqual(20, bowl.Score); | |
| } | |
| } | |
| public class BowlingGame | |
| { | |
| public int Score | |
| { | |
| get | |
| { | |
| var score = 0; | |
| foreach (Frame frame in Frames) | |
| { | |
| score += frame.Score; | |
| } | |
| return score; | |
| } | |
| } | |
| private Frame CurrentFrame = new Frame(); | |
| private IList<Frame> Frames = new List<Frame>(); | |
| private Frame PreviousFrame { get; set; } | |
| public void Roll(int pins) | |
| { | |
| CurrentFrame.Roll(pins); | |
| if (CurrentFrame.DoneRolling) | |
| { | |
| Frames.Add(CurrentFrame); | |
| PreviousFrame = CurrentFrame; | |
| this.CurrentFrame = new Frame(); | |
| } | |
| else | |
| { | |
| // was the last frame a spare? | |
| // if so... hang on to it so we can give it the next roll... | |
| if (PreviousFrame != null && PreviousFrame.IsSpare) | |
| { | |
| PreviousFrame.SpareBonus = pins; | |
| } | |
| if (PreviousFrame != null && PreviousFrame.IsStrike) | |
| { | |
| PreviousFrame.SpareBonus = pins; | |
| } | |
| } | |
| } | |
| } | |
| public class Frame | |
| { | |
| private int? Roll1 { get; set; } | |
| private int Roll2 { get; set; } | |
| public int? SpareBonus { get; set; } | |
| public bool IsSpare { get; set; } | |
| public bool IsStrike { get; set; } | |
| public bool DoneRolling { get; set; } | |
| public int Score | |
| { | |
| get | |
| { | |
| if (Roll1 == null) return 0; | |
| if (IsSpare && (SpareBonus == null)) return 0; | |
| if (IsStrike && (SpareBonus == null)) return 0; | |
| return Roll1.Value + Roll2 + SpareBonus.GetValueOrDefault(0); | |
| } | |
| } | |
| //public bool IsClosed { get { return Score == 10; } } | |
| public void Roll(int roll) | |
| { | |
| if (Roll1 == null) | |
| { | |
| if (roll == 10) | |
| { | |
| DoneRolling = true; | |
| IsStrike = true; | |
| } | |
| Roll1 = roll; | |
| } | |
| else | |
| { | |
| if (Roll1 + roll == 10) IsSpare = true; | |
| DoneRolling = true; | |
| Roll2 = roll; | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment