Bowling Game KataAdapted for C# and nUnit<br />By Dan Stewart (with modifications by Mike Clement)<br />dan@stewshack.com<...
Scoring Bowling.<br />The game consists of 10 frames as shown above.  In each frame the player has two opportunities to kn...
A quick design session<br />Clearly we need the Game class.<br />
A quick design session<br />A game has 10 frames.<br />
A quick design session<br />A frame has 1 or two rolls.<br />
A quick design session<br />The tenth frame has two or three rolls.<br />It is different from all the other frames.<br />
A quick design session<br />The score function must<br />include all the frames, <br />and calculate all their scores.<br />
A quick design session<br />The score for a spare or a strike depends on the frame’s successor<br />
Begin.<br />Create a class library named BowlingGameKata<br />
Begin.<br />Add a test fixture named BowlingGameTests to the project<br />usingNUnit.Framework;<br />namespaceBowlingGameK...
The first test.<br />[Test]<br />publicvoidGutterGameTest()<br /> {<br />     Game g = new Game();<br />}<br />
The first test.<br />namespaceBowlingGameKata<br />{<br />publicclassGame<br />    {<br />    }<br />}<br />[Test]<br />pu...
The first test.<br />[Test]<br />publicvoidGutterGameTest()<br />{<br />     Game g = new Game();<br />}<br />publicclassG...
The first test.<br />[Test]<br />publicvoidGutterGameTest()<br />{<br />    Gameg = newGame();<br />for(int i = 0; i < 20;...
The first test.<br />public class Game<br />{<br />   publicvoid Roll(int p)<br />{<br />   thrownewSystem.NotImplementedE...
The first test.<br />[Test]<br />publicvoidGutterGameTest()<br />{<br />   Gameg = newGame();<br />for(int i = 0; i < 20; ...
The first test.<br />[Test]<br />publicvoidGutterGameTest()<br />{<br />   Gameg = newGame();<br />for(int i = 0; i < 20; ...
The first test.<br />[Test]<br />publicvoidGutterGameTest()<br />{<br />    Gameg = newGame();<br />for(int i = 0; i < 20;...
The Second test.<br />[Test]<br />publicvoidAllOnesTest()<br />{<br />    Gameg = newGame();<br />    for(int i = 0; i < 2...
The Second test.<br />- Game creation is duplicated<br />- roll loop is duplicated<br />[Test]<br />publicvoidAllOnesTest(...
The Second test.<br />- Game creation is duplicated<br />- roll loop is duplicated<br />[Test]<br />publicvoidAllOnesTest(...
The Second test.<br />- roll loop is duplicated<br />privateGame g;<br />[SetUp]<br />publicvoidSetup() <br />{<br />    g...
The Second test.<br />- roll loop is duplicated<br />[Test]<br />publicvoidGutterGameTest()<br />{<br />int rolls = 20;<br...
The Second test.<br />- roll loop is duplicated<br />[Test]<br />publicvoidGutterGameTest()<br />{<br />int rolls = 20;<br...
The Second test.<br />publicclassGame<br />{<br />privateint score;<br />publicvoid Roll(int pins)<br />{<br />score += pi...
The Second test.<br />[Test]<br />publicvoidGutterGameTest()<br />{<br />RollMany(20, 0);<br />Assert.That(g.Score(), Is.E...
The Third test.<br />- ugly comment in test.<br />[Test]<br />publicvoidOneSpareTest()<br />{<br />g.Roll(5);<br />g.Roll(...
The Third test.<br />- ugly comment in test.<br />publicclassGame<br />{<br />privateint score;<br />publicvoid Roll(int p...
The Third test.<br />- ugly comment in test.<br />Roll() calculates score, but name does not imply that.<br />publicclassG...
The Third test.<br />- ugly comment in test.<br />[Test]<br />publicvoidOneSpareTest()<br />{<br />g.Roll(5);<br />g.Roll(...
The Third test.<br />- ugly comment in test.<br />publicclassGame<br />{<br />privateint score;<br />privateint[] rolls = ...
The Third test.<br />- ugly comment in test.<br />publicclassGame<br />{        <br />privateint[] rolls = newint[21];<br ...
The Third test.<br />- ugly comment in test.<br />[Test]<br />publicvoidOneSpareTest()<br />{<br />g.Roll(5);<br />g.Roll(...
The Third test.<br />- ugly comment in test.<br />publicint Score()<br />{<br />int score = 0;<br />for (int i = 0; i < ro...
The Third test.<br />- ugly comment in test.<br />publicclassGame<br />{        <br />privateint[] rolls = newint[21];<br ...
The Third test.<br />- ugly comment in test.<br />publicint Score()<br />{<br />int score = 0;<br />inti = 0;<br />for (in...
The Third test.<br />- ugly comment in test.<br />[Test]<br />publicvoidOneSpareTest()<br />{<br />g.Roll(5);<br />g.Roll(...
The Third test.<br />- ugly comment in test.<br />publicint Score()<br />{<br />int score = 0;<br />inti = 0;<br />for (in...
The Third test.<br /><ul><li>ugly comment in test.
ugly comment in conditional.</li></ul>publicint Score()<br />{<br />int score = 0;<br />int roll = 0;<br />for (int frame ...
The Third test.<br />- ugly comment in test.<br />publicint Score()<br />{<br />int score = 0;<br />int roll = 0;<br />for...
The Third test.<br />[Test]<br />publicvoidOneSpareTest()<br />{<br />RollSpare();<br />g.Roll(3);<br />RollMany(17, 0);<b...
The Fourth test.<br />- ugly comment in OneStrikeTest.<br />[Test]<br />publicvoidOneStrikeTest()<br />{<br />g.Roll(10); ...
The Fourth test.<br /><ul><li>ugly comment in OneStrikeTest.
ugly comment in conditional.
ugly expressions.</li></ul>publicint Score()<br />{<br />    intscore = 0;<br />introll = 0;<br />for(int frame = 0; frame...
The Fourth test.<br /><ul><li>ugly comment in OneStrikeTest.
ugly comment in conditional.</li></ul>publicint Score()<br />{<br />    intscore = 0;<br />introll = 0;<br />for(int frame...
The Fourth test.<br />- ugly comment in OneStrikeTest.<br />publicint Score()<br />{<br />int score = 0;<br />int roll = 0...
Upcoming SlideShare
Loading in …5
×

Bowling Game Kata in C# Adapted

3,896
-1

Published on

1 Comment
2 Likes
Statistics
Notes
No Downloads
Views
Total Views
3,896
On Slideshare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
62
Comments
1
Likes
2
Embeds 0
No embeds

No notes for slide
  • I would like to thank Uncle Bob Martin and Object Mentor for the bowling game kata.
  • Bowling Game Kata in C# Adapted

    1. 1. Bowling Game KataAdapted for C# and nUnit<br />By Dan Stewart (with modifications by Mike Clement)<br />dan@stewshack.com<br />mike@softwareontheside.com<br />
    2. 2. Scoring Bowling.<br />The game consists of 10 frames as shown above. In each frame the player has two opportunities to knock down 10 pins. The score for the frame is the total number of pins knocked down, plus bonuses for strikes and spares.<br />A spare is when the player knocks down all 10 pins in two tries. The bonus for that frame is the number of pins knocked down by the next roll. So in frame 3 above, the score is 10 (the total number knocked down) plus a bonus of 5 (the number of pins knocked down on the next roll.)<br />A strike is when the player knocks down all 10 pins on his first try. The bonus for that frame is the value of the next two balls rolled.<br />In the tenth frame a player who rolls a spare or strike is allowed to roll the extra balls to complete the frame. However no more than three balls can be rolled in tenth frame.<br />
    3. 3. A quick design session<br />Clearly we need the Game class.<br />
    4. 4. A quick design session<br />A game has 10 frames.<br />
    5. 5. A quick design session<br />A frame has 1 or two rolls.<br />
    6. 6. A quick design session<br />The tenth frame has two or three rolls.<br />It is different from all the other frames.<br />
    7. 7. A quick design session<br />The score function must<br />include all the frames, <br />and calculate all their scores.<br />
    8. 8. A quick design session<br />The score for a spare or a strike depends on the frame’s successor<br />
    9. 9. Begin.<br />Create a class library named BowlingGameKata<br />
    10. 10. Begin.<br />Add a test fixture named BowlingGameTests to the project<br />usingNUnit.Framework;<br />namespaceBowlingGameKata<br />{<br />[TestFixture]<br />publicclassBowlingGameTests<br />{<br /> [Test]<br />publicvoidGutterGameTest()<br /> {<br /> }<br /> }<br />}<br />
    11. 11. The first test.<br />[Test]<br />publicvoidGutterGameTest()<br /> {<br /> Game g = new Game();<br />}<br />
    12. 12. The first test.<br />namespaceBowlingGameKata<br />{<br />publicclassGame<br /> {<br /> }<br />}<br />[Test]<br />publicvoidGutterGameTest()<br />{<br /> Game g = new Game();<br />}<br />
    13. 13. The first test.<br />[Test]<br />publicvoidGutterGameTest()<br />{<br /> Game g = new Game();<br />}<br />publicclassGame<br />{<br />}<br />
    14. 14. The first test.<br />[Test]<br />publicvoidGutterGameTest()<br />{<br /> Gameg = newGame();<br />for(int i = 0; i < 20; i++)<br /> {<br />g.Roll(0);<br /> }<br />}<br />publicclassGame<br />{<br />}<br />
    15. 15. The first test.<br />public class Game<br />{<br /> publicvoid Roll(int p)<br />{<br /> thrownewSystem.NotImplementedException();<br />}<br />}<br />[Test]<br />publicvoidGutterGameTest()<br />{<br /> Gameg = newGame();<br />for(int i = 0; i < 20; i++)<br /> {<br />g.Roll(0);<br /> }<br />}<br />
    16. 16. The first test.<br />[Test]<br />publicvoidGutterGameTest()<br />{<br /> Gameg = newGame();<br />for(int i = 0; i < 20; i++)<br />{<br />g.Roll(0);<br />}<br />Assert.That(g.Score(), Is.EqualTo(0));<br />}<br />public class Game<br />{<br /> publicvoid Roll(int p)<br />{<br /> thrownewSystem.NotImplementedException();<br /> }<br />}<br />
    17. 17. The first test.<br />[Test]<br />publicvoidGutterGameTest()<br />{<br /> Gameg = newGame();<br />for(int i = 0; i < 20; i++)<br />{<br />g.Roll(0);<br />}<br />Assert.That(g.Score(), Is.EqualTo(0));<br />}<br />publicclassGame<br />{<br /> publicvoid Roll(int p)<br /> {<br />thrownew System.NotImplementedException();<br /> }<br />publicobject Score()<br />{<br />thrownew System.NotImplementedException();<br /> }<br />}<br />Failed GutterGameTestthrew exception<br />
    18. 18. The first test.<br />[Test]<br />publicvoidGutterGameTest()<br />{<br /> Gameg = newGame();<br />for(int i = 0; i < 20; i++)<br /> {<br />g.Roll(0);<br /> }<br />Assert.That(g.Score(), Is.EqualTo(0));<br />}<br />publicclassGame<br />{<br />privateint score;<br />publicvoid Roll(int pins)<br /> {<br /> }<br />publicintScore()<br /> {<br />returnscore;<br /> }<br />}<br />
    19. 19. The Second test.<br />[Test]<br />publicvoidAllOnesTest()<br />{<br /> Gameg = newGame();<br /> for(int i = 0; i < 20; i++)<br /> {<br />g.Roll(1);<br /> }<br />Assert.That(g.Score(), Is.EqualTo(20));<br />}<br />publicclassGame<br />{<br />privateint score;<br />publicvoid Roll(int pins)<br /> {<br /> }<br />publicintScore()<br /> {<br />returnscore;<br /> }<br />}<br />
    20. 20. The Second test.<br />- Game creation is duplicated<br />- roll loop is duplicated<br />[Test]<br />publicvoidAllOnesTest()<br />{<br /> Gameg = newGame();<br /> for(int i = 0; i < 20; i++)<br /> {<br />g.Roll(1);<br /> }<br />Assert.That(g.Score(), Is.EqualTo(20));<br />}<br />publicclassGame<br />{<br />privateint score;<br />publicvoid Roll(int pins)<br /> {<br /> }<br />publicintScore()<br /> {<br />returnscore;<br /> }<br />}<br />
    21. 21. The Second test.<br />- Game creation is duplicated<br />- roll loop is duplicated<br />[Test]<br />publicvoidAllOnesTest()<br />{<br /> Gameg = newGame();<br /> for(int i = 0; i < 20; i++)<br /> {<br />g.Roll(1);<br /> }<br />Assert.That(g.Score(), Is.EqualTo(20));<br />}<br />publicclassGame<br />{<br />privateint score;<br />publicvoid Roll(int pins)<br />{<br />}<br />publicintScore()<br />{<br />return score;<br /> }<br />}<br />Assert.AreEqual failed. Expected:<20>. Actual:<0>.<br />
    22. 22. The Second test.<br />- roll loop is duplicated<br />privateGame g;<br />[SetUp]<br />publicvoidSetup() <br />{<br /> g = newGame();<br />}<br />[Test]<br />publicvoidGutterGameTest()<br />{<br />for(int i = 0; i < 20; i++)<br /> {<br />g.Roll(0);<br /> }<br />Assert.That(g.Score(), Is.EqualTo(0));<br />}<br />[Test]<br />publicvoidAllOnesTest()<br />{<br />for(int i = 0; i < 20; i++)<br /> {<br />g.Roll(1);<br /> }<br />Assert.That(g.Score(), Is.EqualTo(20));<br />}<br />publicclassGame<br />{<br />privateint score;<br />publicvoid Roll(int pins)<br />{<br />score += pins;<br />}<br />publicint Score()<br />{<br />return score;<br /> }<br />}<br />
    23. 23. The Second test.<br />- roll loop is duplicated<br />[Test]<br />publicvoidGutterGameTest()<br />{<br />int rolls = 20;<br />intpins = 0;<br />for(int i = 0; i < rolls; i++)<br />{<br />g.Roll(pins);<br />}<br />Assert.That(g.Score(), Is.EqualTo(0));<br />}<br />publicclassGame<br />{<br />privateint score;<br />publicvoid Roll(int pins)<br />{<br />score += pins;<br />}<br />publicint Score()<br />{<br />return score;<br /> }<br />}<br />
    24. 24. The Second test.<br />- roll loop is duplicated<br />[Test]<br />publicvoidGutterGameTest()<br />{<br />int rolls = 20;<br />intpins = 0;<br />RollMany(rolls, pins);<br />for(int i = 0; i < rolls; i++)<br /> {<br />g.Roll(pins);<br /> }<br />Assert.That(g.Score(), Is.EqualTo(0));<br />}<br />publicclassGame<br />{<br />privateint score;<br />publicvoid Roll(int pins)<br />{<br />score += pins;<br />}<br />publicint Score()<br />{<br />return score;<br /> }<br />}<br />
    25. 25. The Second test.<br />publicclassGame<br />{<br />privateint score;<br />publicvoid Roll(int pins)<br />{<br />score += pins;<br />}<br />publicint Score()<br />{<br />return score;<br /> }<br />}<br />[Test]<br />publicvoidGutterGameTest()<br />{<br />RollMany(20, 0);<br />Assert.That(g.Score(), Is.EqualTo(0));<br />}<br />privatevoidRollMany(introlls, int pins)<br />{<br /> for(int i = 0; i < rolls; i++)<br /> {<br />g.Roll(pins);<br /> }<br />}<br />
    26. 26. The Second test.<br />[Test]<br />publicvoidGutterGameTest()<br />{<br />RollMany(20, 0);<br />Assert.That(g.Score(), Is.EqualTo(0));<br />}<br />[Test]<br />publicvoidAllOnesTest()<br />{<br />RollMany(20, 1);<br />Assert.That(g.Score(), Is.EqualTo(20));<br />}<br />privatevoidRollMany(int rolls, int pins)<br />{<br /> for(int i = 0; i < rolls; i++)<br /> {<br />g.Roll(pins);<br /> }<br />}<br />publicclassGame<br />{<br />privateint score;<br />publicvoid Roll(int pins)<br />{<br />score += pins;<br />}<br />publicint Score()<br />{<br />return score;<br /> }<br />}<br />
    27. 27. The Third test.<br />- ugly comment in test.<br />[Test]<br />publicvoidOneSpareTest()<br />{<br />g.Roll(5);<br />g.Roll(5); // spare<br />g.Roll(3);<br />RollMany(17, 0);<br />Assert.That(g.Score(), Is.EqualTo(16));<br />}<br />publicclassGame<br />{<br />privateint score;<br />publicvoid Roll(int pins)<br />{<br />score += pins;<br />}<br />publicint Score()<br />{<br />return score;<br /> }<br />}<br />Failed Assert.AreEqual Expected:<16>. Actual:<13><br />
    28. 28. The Third test.<br />- ugly comment in test.<br />publicclassGame<br />{<br />privateint score;<br />publicvoid Roll(int pins)<br />{<br />score += pins;<br />}<br />publicint Score()<br />{<br />return score;<br /> }<br />}<br />[Test]<br />publicvoidOneSpareTest()<br />{<br />g.Roll(5);<br />g.Roll(5); // spare<br />g.Roll(3);<br />RollMany(17, 0);<br />Assert.That(g.Score(), Is.EqualTo(16));<br />}<br />tempted to use flag to remember previous roll. So design must be wrong.<br />Failed Assert.AreEqual Expected:<16>. Actual:<13><br />
    29. 29. The Third test.<br />- ugly comment in test.<br />Roll() calculates score, but name does not imply that.<br />publicclassGame<br />{<br />privateint score;<br />publicvoid Roll(int pins)<br />{<br />score += pins;<br />}<br />publicint Score()<br />{<br />return score;<br /> }<br />}<br />[Test]<br />publicvoidOneSpareTest()<br />{<br />g.Roll(5);<br />g.Roll(5); // spare<br />g.Roll(3);<br />RollMany(17, 0);<br />Assert.AreEqual(16, g.Score);<br />}<br />Score() does not calculate score, but name implies that it does.<br />Design is wrong. Responsibilities are misplaced.<br />Failed Assert.AreEqual Expected:<16>. Actual:<13><br />
    30. 30. The Third test.<br />- ugly comment in test.<br />[Test]<br />publicvoidOneSpareTest()<br />{<br />g.Roll(5);<br />g.Roll(5); // spare<br />g.Roll(3);<br />RollMany(17, 0);<br />//Assert.That(g.Score(), Is.EqualTo(16));<br />}<br />publicclassGame<br />{<br />privateint score;<br />publicvoid Roll(int pins)<br />{<br />score += pins;<br />}<br />publicint Score()<br />{<br />return score;<br /> }<br />}<br />
    31. 31. The Third test.<br />- ugly comment in test.<br />publicclassGame<br />{<br />privateint score;<br />privateint[] rolls = newint[21];<br />privateint currentRoll;<br />publicvoid Roll(int pins)<br />{<br />rolls[currentRoll++] = pins;<br />score += pins;<br />}<br />publicint Score()<br />{<br />return score;<br /> }<br />}<br />[Test]<br />publicvoidOneSpareTest()<br />{<br />g.Roll(5);<br />g.Roll(5); // spare<br />g.Roll(3);<br />RollMany(17, 0);<br />//Assert.That(g.Score(), Is.EqualTo(16));<br />}<br />
    32. 32. The Third test.<br />- ugly comment in test.<br />publicclassGame<br />{ <br />privateint[] rolls = newint[21];<br />privateint currentRoll;<br />publicvoid Roll(int pins)<br />{<br />rolls[currentRoll++] = pins;<br />}<br />publicint Score()<br />{<br />int score = 0;<br />for (int i = 0; i < rolls.Length; i++)<br />{<br />score += rolls[i];<br />}<br />return score;<br /> }<br />}<br />[Test]<br />publicvoidOneSpareTest()<br />{<br />g.Roll(5);<br />g.Roll(5); // spare<br />g.Roll(3);<br />RollMany(17, 0);<br />//Assert.That(g.Score(), Is.EqualTo(16));<br />}<br />
    33. 33. The Third test.<br />- ugly comment in test.<br />[Test]<br />publicvoidOneSpareTest()<br />{<br />g.Roll(5);<br />g.Roll(5); // spare<br />g.Roll(3);<br />RollMany(17, 0);<br />Assert.That(g.Score(), Is.EqualTo(16));<br />}<br />publicclassGame<br />{ <br />privateint[] rolls = newint[21];<br />privateint currentRoll;<br />publicvoid Roll(int pins)<br />{<br />rolls[currentRoll++] = pins;<br />}<br />publicint Score()<br />{<br />int score = 0;<br />for (int i = 0; i < rolls.Length; i++)<br />{<br />score += rolls[i];<br />}<br />return score;<br /> }<br />}<br />Failed Assert.AreEqual Expected:<16>. Actual:<13><br />
    34. 34. The Third test.<br />- ugly comment in test.<br />publicint Score()<br />{<br />int score = 0;<br />for (int i = 0; i < rolls.Length; i++)<br /> {<br /> // spare<br /> if(rolls[i] + rolls[i+1] == 10)<br />{<br />score += ...<br />}<br />score += rolls[i];<br />}<br />return score;<br />}<br />[Test]<br />publicvoidOneSpareTest()<br />{<br />g.Roll(5);<br />g.Roll(5); // spare<br />g.Roll(3);<br />RollMany(17, 0);<br />Assert.That(g.Score(), Is.EqualTo(16));<br />}<br />This isn’t going to work because i might not refer to the first ball of the frame.<br />Design is still wrong.<br />Need to walk through array two balls (one frame) at a time.<br />Failed Assert.AreEqual Expected:<16>. Actual:<13><br />
    35. 35. The Third test.<br />- ugly comment in test.<br />publicclassGame<br />{ <br />privateint[] rolls = newint[21];<br />privateint currentRoll;<br />publicvoid Roll(int pins)<br />{<br />rolls[currentRoll++] = pins;<br />}<br />publicint Score()<br />{<br />int score = 0;<br />for (int i = 0; i < rolls.Length; i++)<br />{<br />score += rolls[i];<br />}<br />return score;<br /> }<br />}<br />[Test]<br />publicvoidOneSpareTest()<br />{<br />g.Roll(5);<br />g.Roll(5); // spare<br />g.Roll(3);<br />RollMany(17, 0);<br />//Assert.That(g.Score(), Is.EqualTo(16));<br />Assert.Inconclusive();<br />}<br />
    36. 36. The Third test.<br />- ugly comment in test.<br />publicint Score()<br />{<br />int score = 0;<br />inti = 0;<br />for (int frame = 0; frame < 10; frame++)<br />{<br /> score += rolls[i] + rolls[i + 1];<br /> i+= 2;<br />}<br />returnscore;<br />}<br />[Test]<br />publicvoidOneSpareTest()<br />{<br />g.Roll(5);<br />g.Roll(5); // spare<br />g.Roll(3);<br />RollMany(17, 0);<br />//Assert.That(g.Score(), Is.EqualTo(16));<br />Assert.Inconclusive();<br />}<br />
    37. 37. The Third test.<br />- ugly comment in test.<br />[Test]<br />publicvoidOneSpareTest()<br />{<br />g.Roll(5);<br />g.Roll(5); // spare<br />g.Roll(3);<br />RollMany(17, 0);<br />Assert.That(g.Score(), Is.EqualTo(16));<br />}<br />publicint Score()<br />{<br />int score = 0;<br />int i = 0;<br />for (int frame = 0; frame < 10; frame++)<br /> {<br /> score += rolls[i] + rolls[i + 1];<br /> i += 2;<br /> }<br />return score;<br />}<br />Failed Assert.AreEqual Expected:<16>. Actual:<13><br />
    38. 38. The Third test.<br />- ugly comment in test.<br />publicint Score()<br />{<br />int score = 0;<br />inti = 0;<br />for (int frame = 0; frame < 10; frame++)<br />{<br />// spare<br />if (rolls[i] + rolls[i + 1] == 10)<br />{<br />score += 10 + rolls[i + 2];<br />i += 2;<br />}<br />else<br />{<br /> score += rolls[i] + rolls[i + 1]; <br />i += 2;<br /> }<br /> }<br />return score;<br />}<br />[Test]<br />publicvoidOneSpareTest()<br />{<br />g.Roll(5);<br />g.Roll(5); // spare<br />g.Roll(3);<br />RollMany(17, 0);<br />Assert.That(g.Score(), Is.EqualTo(16));<br />}<br />
    39. 39. The Third test.<br /><ul><li>ugly comment in test.
    40. 40. ugly comment in conditional.</li></ul>publicint Score()<br />{<br />int score = 0;<br />int roll = 0;<br />for (int frame = 0; frame < 10; frame++)<br />{<br />// spare<br />if (rolls[roll] + rolls[roll + 1] == 10)<br />{<br />score += 10 + rolls[roll + 2];<br />roll += 2;<br />}<br />else<br />{<br /> score += rolls[roll] + rolls[roll + 1]; <br /> roll += 2;<br /> }<br /> }<br />return score;<br />}<br />[Test]<br />publicvoidOneSpareTest()<br />{<br />g.Roll(5);<br />g.Roll(5); // spare<br />g.Roll(3);<br />RollMany(17, 0);<br />Assert.That(g.Score(), Is.EqualTo(16));<br />}<br />
    41. 41. The Third test.<br />- ugly comment in test.<br />publicint Score()<br />{<br />int score = 0;<br />int roll = 0;<br />for (int frame = 0; frame < 10; frame++)<br />{<br /> if(IsSpare(roll))<br /> {<br /> score += 10 + rolls[roll + 2];<br /> roll += 2;<br /> }<br />else<br /> {<br /> score += rolls[roll] + rolls[roll + 1]; <br /> roll += 2;<br />}<br /> }<br />returnscore;<br />}<br />privateboolIsSpare(int roll)<br />{<br /> returnrolls[roll] +<br /> rolls[roll + 1] == 10;<br />}<br />[Test]<br />publicvoidOneSpareTest()<br />{<br />g.Roll(5);<br />g.Roll(5); // spare<br />g.Roll(3);<br />RollMany(17, 0);<br />Assert.That(g.Score(), Is.EqualTo(16));<br />}<br />
    42. 42. The Third test.<br />[Test]<br />publicvoidOneSpareTest()<br />{<br />RollSpare();<br />g.Roll(3);<br />RollMany(17, 0);<br />Assert.That(g.Score(), Is.EqualTo(16));<br />}<br />privatevoidRollSpare()<br />{<br />g.Roll(5);<br />g.Roll(5);<br />}<br />publicint Score()<br />{<br />int score = 0;<br />int roll = 0;<br />for (int frame = 0; frame < 10; frame++)<br />{<br /> if(IsSpare(roll))<br /> {<br /> score += 10 + rolls[roll + 2];<br /> roll += 2;<br /> }<br />else<br /> {<br /> score += rolls[roll] + rolls[roll + 1]; <br /> roll += 2;<br /> }<br />}<br />returnscore;<br />}<br />privateboolIsSpare(int roll)<br />{<br /> returnrolls[roll] +<br /> rolls[roll + 1] == 10;<br />}<br />
    43. 43. The Fourth test.<br />- ugly comment in OneStrikeTest.<br />[Test]<br />publicvoidOneStrikeTest()<br />{<br />g.Roll(10); // strike<br />g.Roll(3);<br />g.Roll(4);<br />RollMany(16, 0);<br />Assert.That(g.Score(), Is.EqualTo(24));<br />}<br />publicint Score()<br />{<br />int score = 0;<br />int roll = 0;<br />for (int frame = 0; frame < 10; frame++)<br />{<br /> if(IsSpare(roll))<br />{<br /> score += 10 + rolls[roll + 2];<br />}<br />else<br />{<br /> score += rolls[roll] + rolls[roll + 1];<br />}<br />roll += 2;<br />}<br />returnscore;<br />}<br />privatebool IsSpare(int roll)<br />{<br /> returnrolls[roll] +<br /> rolls[roll + 1] == 10;<br />}<br />Failed Assert.AreEqual Expected:<24>. Actual:<17><br />
    44. 44. The Fourth test.<br /><ul><li>ugly comment in OneStrikeTest.
    45. 45. ugly comment in conditional.
    46. 46. ugly expressions.</li></ul>publicint Score()<br />{<br /> intscore = 0;<br />introll = 0;<br />for(int frame = 0; frame < 10; frame++)<br />{<br />if(rolls[roll] == 10) // strike<br />{<br /> score += 10 + rolls[roll + 1]<br /> + rolls[roll + 2];<br />roll++;<br />}<br />elseif (IsSpare(roll))<br />{<br /> score += 10 + rolls[roll + 2];<br />roll += 2;<br />}<br />else<br />{<br /> score += rolls[roll] + rolls[roll + 1];<br />roll += 2;<br />}<br /> }<br /> returnscore;<br />}<br />[Test]<br />publicvoidOneStrikeTest()<br />{<br />g.Roll(10); // strike<br />g.Roll(3);<br />g.Roll(4);<br />RollMany(16, 0);<br />Assert.That(g.Score(), Is.EqualTo(24));<br />}<br />
    47. 47. The Fourth test.<br /><ul><li>ugly comment in OneStrikeTest.
    48. 48. ugly comment in conditional.</li></ul>publicint Score()<br />{<br /> intscore = 0;<br />introll = 0;<br />for(int frame = 0; frame < 10; frame++)<br />{<br />if(rolls[roll] == 10) // strike<br />{<br /> score += 10 + StrikeBonus(roll);<br />roll++;<br />}<br />elseif (IsSpare(roll))<br />{<br /> score += 10 + SpareBonus(roll);<br />roll += 2;<br />}<br />else<br />{<br /> score += SumOfBallsInFrame(roll);<br />roll += 2;<br />}<br />}<br />returnscore;<br />}<br />privateint SumOfBallsInFrame(int roll)<br />{<br /> returnrolls[roll] + rolls[roll + 1];<br />}<br />privateint SpareBonus(int roll)<br />{<br /> returnrolls[roll + 2];<br />}<br />privateint StrikeBonus(int roll)<br />{<br /> returnrolls[roll + 1] + rolls[roll + 2];<br />}<br />[Test]<br />publicvoidOneStrikeTest()<br />{<br />g.Roll(10); // strike<br />g.Roll(3);<br />g.Roll(4);<br />RollMany(16, 0);<br />Assert.That(g.Score(), Is.EqualTo(24));<br />}<br />
    49. 49. The Fourth test.<br />- ugly comment in OneStrikeTest.<br />publicint Score()<br />{<br />int score = 0;<br />int roll = 0;<br />for (int frame = 0; frame < 10; frame++)<br />{<br />if(IsStrike(roll))<br />{<br /> score += 10<br /> + StrikeBonus(roll);<br />roll++;<br />}<br />elseif (IsSpare(roll))<br />{<br />score += 10<br />+ SpareBonus(roll);<br />roll += 2;<br />}<br />else<br />{<br /> score += SumOfBallsInFrame(roll);<br />roll += 2;<br />}<br />}<br />returnscore;<br />}<br />privatebool IsStrike(int roll)<br />{<br /> returnrolls[roll] == 10;<br />}<br />[Test]<br />publicvoidOneStrikeTest()<br />{<br />g.Roll(10); // strike<br />g.Roll(3);<br />g.Roll(4);<br />RollMany(16, 0);<br />Assert.That(g.Score(), Is.EqualTo(24));<br />}<br />
    50. 50. The Fourth test.<br />[Test]<br />publicvoidOneStrikeTest()<br />{<br />RollStrike();<br />g.Roll(3);<br />g.Roll(4);<br />RollMany(16, 0);<br />Assert.That(g.Score(), Is.EqualTo(24));<br />}<br />privatevoidRollStrike()<br />{<br />g.Roll(10);<br />}<br />publicint Score()<br />{<br />int score = 0;<br />int roll = 0;<br />for (int frame = 0; frame < 10; frame++)<br />{<br />if(IsStrike(roll))<br />{<br /> score += 10<br /> + StrikeBonus(roll);<br />roll++;<br />}<br />elseif (IsSpare(roll))<br />{<br />score += 10<br />+ SpareBonus(roll);<br />roll += 2;<br />}<br />else<br />{<br /> score += SumOfBallsInFrame(roll);<br />roll += 2;<br />}<br />}<br />returnscore;<br />}<br />
    51. 51. The Fifth test.<br />[Test]<br />publicvoidPerfectGameTest()<br />{<br />RollMany(12, 10);<br />Assert.That(g.Score(), Is.EqualTo(300));<br />}<br />publicint Score()<br />{<br />int score = 0;<br />int roll = 0;<br />for (int frame = 0; frame < 10; frame++)<br />{<br />if(IsStrike(roll))<br />{<br /> score += 10<br /> + StrikeBonus(roll);<br />roll++;<br />}<br />elseif (IsSpare(roll))<br />{<br />score += 10<br />+ SpareBonus(roll);<br />roll += 2;<br />}<br />else<br />{<br /> score += SumOfBallsInFrame(roll);<br />roll += 2;<br />}<br />}<br />returnscore;<br />}<br />
    52. 52. Discussion/Review<br />
    53. 53. Adapted from:<br />
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×