SlideShare a Scribd company logo
1 of 53
Bowling Game Kata Object Mentor, Inc. fitnesse.org Copyright    2005  by Object Mentor, Inc All copies must retain this page unchanged. www.junit.org www.objectmentor.com blog.objectmentor.com
Scoring Bowling. 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. 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.) 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. 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.
The Requirements. ,[object Object],[object Object],[object Object]
A quick design session Clearly we need the Game class.
A quick design session A game has 10 frames.
A quick design session A frame has 1 or two rolls.
A quick design session The tenth frame has two or three rolls. It is different from all the other frames.
A quick design session The score function must iterate through all the frames, and calculate all their scores.
A quick design session The score for a spare or a strike depends on the frame’s successor
Begin. ,[object Object],[object Object],import junit.framework.TestCase; public class BowlingGameTest extends TestCase { }
Begin. ,[object Object],[object Object],import junit.framework.TestCase; public class BowlingGameTest extends TestCase { } Execute this program and verify that you get the following error: No tests found in BowlingGameTest
The first test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { public void testGutterGame() throws Exception { Game  g = new  Game (); } }
The first test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { public void testGutterGame() throws Exception { Game g = new Game(); } } public class Game { }
The first test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { public void testGutterGame() throws Exception { Game g = new Game(); } } public class Game { }
The first test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { public void testGutterGame() throws Exception { Game g = new Game(); for (int i=0; i<20; i++) g. roll (0); } } public class Game { }
The first test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { public void testGutterGame() throws Exception { Game g = new Game(); for (int i=0; i<20; i++) g.roll(0); } } public class Game { public void roll(int pins) { } }
The first test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { public void testGutterGame() throws Exception { Game g = new Game(); for (int i=0; i<20; i++) g.roll(0); assertEquals(0, g. score ()); } } public class Game { public void roll(int pins) { } }
The first test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { public void testGutterGame() throws Exception { Game g = new Game(); for (int i=0; i<20; i++) g.roll(0); assertEquals(0, g.score()); } } public class Game { public void roll(int pins) { } public int score() { return -1; } } expected:<0> but was:<-1>
The first test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { public void testGutterGame() throws Exception { Game g = new Game(); for (int i=0; i<20; i++) g.roll(0); assertEquals(0, g.score()); } } public class Game { public void roll(int pins) { } public int score() { return 0; } }
The Second test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { public void testGutterGame() throws Exception { Game g = new Game(); for (int i = 0; i < 20; i++) g.roll(0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { Game g = new Game(); for (int i = 0; i < 20; i++) g.roll(1); assertEquals(20, g.score()); } } public class Game { public void roll(int pins) { } public int score() { return 0; } }
The Second test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { public void testGutterGame() throws Exception { Game g = new Game(); for (int i = 0; i < 20; i++) g.roll(0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { Game g = new Game(); for (int i = 0; i < 20; i++) g.roll(1); assertEquals(20, g.score()); } } public class Game { public void roll(int pins) { } public int score() { return 0; } } - Game creation is duplicated - roll loop is duplicated
The Second test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { public void testGutterGame() throws Exception { Game g = new Game(); for (int i = 0; i < 20; i++) g.roll(0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { Game g = new Game(); for (int i = 0; i < 20; i++) g.roll(1); assertEquals(20, g.score()); } } public class Game { public void roll(int pins) { } public int score() { return 0; } } - Game creation is duplicated - roll loop is duplicated expected:<20> but was:<0>
The Second test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } public void testGutterGame() throws Exception { for (int i = 0; i < 20; i++) g.roll(0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { for (int i = 0; i < 20; i++) g.roll(1); assertEquals(20, g.score()); } } public class Game { private int score = 0; public void roll(int pins) { score += pins; } public int score() { return score; } } - roll loop is duplicated
The Second test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } public void testGutterGame() throws Exception { int n = 20; int pins = 0; for (int i = 0; i <  n ; i++) { g.roll( pins ); } assertEquals(0, g.score()); } public void testAllOnes() throws Exception { for (int i = 0; i < 20; i++) g.roll(1); assertEquals(20, g.score()); } } public class Game { private int score = 0; public void roll(int pins) { score += pins; } public int score() { return score; } } - roll loop is duplicated
The Second test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } public void testGutterGame() throws Exception { int n = 20; int pins = 0; rollMany(n, pins); assertEquals(0, g.score()); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testAllOnes() throws Exception { for (int i = 0; i < 20; i++) g.roll(1); assertEquals(20, g.score()); } } public class Game { private int score = 0; public void roll(int pins) { score += pins; } public int score() { return score; } } - roll loop is duplicated
The Second test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } public void testGutterGame() throws Exception { rollMany( 20, 0 ); assertEquals(0, g.score()); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testAllOnes() throws Exception { for (int i = 0; i < 20; i++) g.roll(1); assertEquals(20, g.score()); } } public class Game { private int score = 0; public void roll(int pins) { score += pins; } public int score() { return score; } } - roll loop is duplicated
The Second test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } } public class Game { private int score = 0; public void roll(int pins) { score += pins; } public int score() { return score; } } - roll loop is duplicated
The Second test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } } public class Game { private int score = 0; public void roll(int pins) { score += pins; } public int score() { return score; } }
The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { g.roll(5); g.roll(5); // spare g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } } public class Game { private int score = 0; public void roll(int pins) { score += pins; } public int score() { return score; } } - ugly comment in test.
The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { g.roll(5); g.roll(5); // spare g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } } public class Game { private int score = 0; public void roll(int pins) { score += pins; } public int score() { return score; } } - ugly comment in test. expected:<16> but was:<13>
The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { g.roll(5); g.roll(5); // spare g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } } public class Game { private int score = 0; public void roll(int pins) { score += pins; } public int score() { return score; } } - ugly comment in test. tempted to use flag to remember previous roll.  So design must be wrong.
The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { g.roll(5); g.roll(5); // spare g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } } public class Game { private int score = 0; public void roll(int pins) { score += pins; } public int score() { return score; } } - ugly comment in test. roll() calculates score, but name does not imply that. score() does not calculate score, but name implies that it does. Design is wrong.  Responsibilities are misplaced.
The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } //  public void testOneSpare() throws Exception { //  g.roll(5); //  g.roll(5); // spare //  g.roll(3); //  rollMany(17,0); //  assertEquals(16,g.score()); //  } } public class Game { private int score = 0; public void roll(int pins) { score += pins; } public int score() { return score; } } - ugly comment in test.
The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } //  public void testOneSpare() throws Exception { //  g.roll(5); //  g.roll(5); // spare //  g.roll(3); //  rollMany(17,0); //  assertEquals(16,g.score()); //  } } public class Game { private int score = 0; private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { score += pins; rolls[currentRoll++] = pins; } public int score() { return score; } } - ugly comment in test.
The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } //  public void testOneSpare() throws Exception { //  g.roll(5); //  g.roll(5); // spare //  g.roll(3); //  rollMany(17,0); //  assertEquals(16,g.score()); //  } } public class Game { private int score = 0; private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { score += pins; rolls[currentRoll++] = pins; } public int score() { int score = 0; for (int i = 0; i < rolls.length; i++) score += rolls[i]; return score; } } - ugly comment in test.
The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } //  public void testOneSpare() throws Exception { //  g.roll(5); //  g.roll(5); // spare //  g.roll(3); //  rollMany(17,0); //  assertEquals(16,g.score()); //  } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; for (int i = 0; i < rolls.length; i++) score += rolls[i]; return score; } } - ugly comment in test.
The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { g.roll(5); g.roll(5); // spare g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; for (int i = 0; i < rolls.length; i++) score += rolls[i]; return score; } } - ugly comment in test. expected:<16> but was:<13>
The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { g.roll(5); g.roll(5); // spare g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; for (int i = 0; i < rolls.length; i++)  { if (rolls[i] + rolls[i+1] == 10) // spare score += ... score += rolls[i]; } return score; } } - ugly comment in test. This isn’t going to work because i might not refer to the first ball of the frame. Design is still wrong. Need to walk through array two balls (one frame) at a time.
The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } //  public void testOneSpare() throws Exception { //  g.roll(5); //  g.roll(5); // spare //  g.roll(3); //  rollMany(17,0); //  assertEquals(16,g.score()); //  } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; for (int i = 0; i < rolls.length; i++) score += rolls[i]; return score; } } - ugly comment in test.
The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } //  public void testOneSpare() throws Exception { //  g.roll(5); //  g.roll(5); // spare //  g.roll(3); //  rollMany(17,0); //  assertEquals(16,g.score()); //  } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; int i = 0; for ( int frame = 0; frame < 10; frame++ )  { score += rolls[i]  + rolls[i+1] ; i += 2; } return score; } } - ugly comment in test.
The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { g.roll(5); g.roll(5); // spare g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; int i = 0; for (int frame = 0; frame < 10; frame++)  { score += rolls[i] + rolls[i+1]; i += 2; } return score; } } - ugly comment in test. expected:<16> but was:<13>
The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { g.roll(5); g.roll(5); // spare g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; int i = 0; for (int frame = 0; frame < 10; frame++) { if (rolls[i] + rolls[i + 1] == 10) // spare { score += 10 + rolls[i + 2]; i += 2; } else { score += rolls[i] + rolls[i + 1]; i += 2; } } return score; } } - ugly comment in test.
The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { g.roll(5); g.roll(5); // spare g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; int i = 0; for (int frame = 0; frame < 10; frame++) { if (rolls[i] + rolls[i + 1] == 10) // spare { score += 10 + rolls[i + 2]; i += 2; } else { score += rolls[i] + rolls[i + 1]; i += 2; } } return score; } } ,[object Object],[object Object],[object Object]
The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { g.roll(5); g.roll(5); // spare g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; int frameIndex = 0; for (int frame = 0; frame < 10; frame++) { if (rolls[frameIndex] +  rolls[frameIndex + 1] == 10) // spare { score += 10 + rolls[frameIndex + 2]; frameIndex += 2; } else { score += rolls[frameIndex] +  rolls[frameIndex + 1]; frameIndex += 2; } } return score; } } ,[object Object],[object Object]
The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { g.roll(5); g.roll(5); // spare g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; int frameIndex = 0; for (int frame = 0; frame < 10; frame++) { if ( isSpare(frameIndex) ) { score += 10 + rolls[frameIndex + 2]; frameIndex += 2; } else { score += rolls[frameIndex] + rolls[frameIndex + 1]; frameIndex += 2; } } return score; } private boolean isSpare(int frameIndex) { return rolls[frameIndex] + rolls[frameIndex + 1] == 10; } } ,[object Object]
The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { rollSpare(); g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } private void rollSpare() { g.roll(5); g.roll(5); } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; int frameIndex = 0; for (int frame = 0; frame < 10; frame++) { if (isSpare(frameIndex)) { score += 10 + rolls[frameIndex + 2]; frameIndex += 2; } else { score += rolls[frameIndex] + rolls[frameIndex + 1]; frameIndex += 2; } } return score; } private boolean isSpare(int frameIndex) { return rolls[frameIndex] + rolls[frameIndex + 1] == 10; } } -
The Fourth test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { ... public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { rollSpare(); g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } public void testOneStrike() throws Exception { g.roll(10); // strike g.roll(3); g.roll(4); rollMany(16, 0); assertEquals(24, g.score()); } private void rollSpare() { g.roll(5); g.roll(5); } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; int frameIndex = 0; for (int frame = 0; frame < 10; frame++) { if (isSpare(frameIndex)) { score += 10 + rolls[frameIndex + 2]; frameIndex += 2; } else { score += rolls[frameIndex] + rolls[frameIndex + 1]; frameIndex += 2; } } return score; } private boolean isSpare(int frameIndex) { return rolls[frameIndex] + rolls[frameIndex + 1] == 10; } } - ugly comment in testOneStrike. expected:<24> but was:<17>
The Fourth test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { ... public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { rollSpare(); g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } public void testOneStrike() throws Exception { g.roll(10); // strike g.roll(3); g.roll(4); rollMany(16, 0); assertEquals(24, g.score()); } private void rollSpare() { g.roll(5); g.roll(5); } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; int frameIndex = 0; for (int frame = 0; frame < 10; frame++) { if (rolls[frameIndex] == 10) // strike { score += 10 + rolls[frameIndex+1] + rolls[frameIndex+2]; frameIndex++; } else  if (isSpare(frameIndex)) { score += 10 + rolls[frameIndex + 2]; frameIndex += 2; } else { score += rolls[frameIndex] + rolls[frameIndex + 1]; frameIndex += 2; } } return score; } private boolean isSpare(int frameIndex) { return rolls[frameIndex] + rolls[frameIndex + 1] == 10; } } ,[object Object],[object Object],[object Object]
The Fourth test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { ... public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { rollSpare(); g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } public void testOneStrike() throws Exception { g.roll(10); // strike g.roll(3); g.roll(4); rollMany(16, 0); assertEquals(24, g.score()); } private void rollSpare() { g.roll(5); g.roll(5); } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; int frameIndex = 0; for (int frame = 0; frame < 10; frame++) { if (rolls[frameIndex] == 10) // strike { score += 10 + strikeBonus(frameIndex); frameIndex++; } else if (isSpare(frameIndex)) { score += 10 + spareBonus(frameIndex); frameIndex += 2; } else { score += sumOfBallsInFrame(frameIndex); frameIndex += 2; } } return score; } private int sumOfBallsInFrame(int frameIndex) { return rolls[frameIndex]+rolls[frameIndex+1]; } private int spareBonus(int frameIndex) { return rolls[frameIndex + 2]; } private int strikeBonus(int frameIndex) { return rolls[frameIndex+1]+rolls[frameIndex+2]; } private boolean isSpare(int frameIndex) { return rolls[frameIndex]+rolls[frameIndex+1] == 10; } } ,[object Object],[object Object]
The Fourth test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { ... public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { rollSpare(); g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } public void testOneStrike() throws Exception { g.roll(10); // strike g.roll(3); g.roll(4); rollMany(16, 0); assertEquals(24, g.score()); } private void rollSpare() { g.roll(5); g.roll(5); } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; int frameIndex = 0; for (int frame = 0; frame < 10; frame++) { if ( isStrike(frameIndex) ) { score += 10 + strikeBonus(frameIndex); frameIndex++; } else if (isSpare(frameIndex)) { score += 10 + spareBonus(frameIndex); frameIndex += 2; } else { score += sumOfBallsInFrame(frameIndex); frameIndex += 2; } } return score; } private boolean isStrike(int frameIndex) { return rolls[frameIndex] == 10; } private int sumOfBallsInFrame(int frameIndex) { return rolls[frameIndex] + rolls[frameIndex+1]; } private int spareBonus(int frameIndex) { return rolls[frameIndex+2]; } private int strikeBonus(int frameIndex) { return rolls[frameIndex+1] + rolls[frameIndex+2]; } private boolean isSpare(int frameIndex) { return rolls[frameIndex]+rolls[frameIndex+1] == 10; } } ,[object Object]
The Fourth test. ... public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { rollSpare(); g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } public void testOneStrike() throws Exception { rollStrike(); g.roll(3); g.roll(4); rollMany(16, 0); assertEquals(24, g.score()); } private void rollStrike() { g.roll(10);  } private void rollSpare() { g.roll(5); g.roll(5); } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; int frameIndex = 0; for (int frame = 0; frame < 10; frame++) { if (isStrike(frameIndex)) { score += 10 + strikeBonus(frameIndex); frameIndex++; } else if (isSpare(frameIndex)) { score += 10 + spareBonus(frameIndex); frameIndex += 2; } else { score += sumOfBallsInFrame(frameIndex); frameIndex += 2; } } return score; } private boolean isStrike(int frameIndex) { return rolls[frameIndex] == 10; } private int sumOfBallsInFrame(int frameIndex) { return rolls[frameIndex] + rolls[frameIndex+1]; } private int spareBonus(int frameIndex) { return rolls[frameIndex+2]; } private int strikeBonus(int frameIndex) { return rolls[frameIndex+1] + rolls[frameIndex+2]; } private boolean isSpare(int frameIndex) { return rolls[frameIndex]+rolls[frameIndex+1] == 10; } }
The Fifth test. ... public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { rollSpare(); g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } public void testOneStrike() throws Exception { rollStrike(); g.roll(3); g.roll(4); rollMany(16, 0); assertEquals(24, g.score()); } public void testPerfectGame() throws Exception { rollMany(12,10); assertEquals(300, g.score()); } private void rollStrike() { g.roll(10); } private void rollSpare() { g.roll(5); g.roll(5); } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; int frameIndex = 0; for (int frame = 0; frame < 10; frame++) { if (isStrike(frameIndex)) { score += 10 + strikeBonus(frameIndex); frameIndex++; } else if (isSpare(frameIndex)) { score += 10 + spareBonus(frameIndex); frameIndex += 2; } else { score += sumOfBallsInFrame(frameIndex); frameIndex += 2; } } return score; } private boolean isStrike(int frameIndex) { return rolls[frameIndex] == 10; } private int sumOfBallsInFrame(int frameIndex) { return rolls[frameIndex] + rolls[frameIndex+1]; } private int spareBonus(int frameIndex) { return rolls[frameIndex+2]; } private int strikeBonus(int frameIndex) { return rolls[frameIndex+1] + rolls[frameIndex+2]; } private boolean isSpare(int frameIndex) { return rolls[frameIndex]+rolls[frameIndex+1] == 10; } }
End

More Related Content

What's hot

QA Fest 2019. Андрей Солнцев. Selenide для профи
QA Fest 2019. Андрей Солнцев. Selenide для профиQA Fest 2019. Андрей Солнцев. Selenide для профи
QA Fest 2019. Андрей Солнцев. Selenide для профиQAFest
 
The lazy programmer's guide to writing thousands of tests
The lazy programmer's guide to writing thousands of testsThe lazy programmer's guide to writing thousands of tests
The lazy programmer's guide to writing thousands of testsScott Wlaschin
 
Kiss PageObjects [01-2017]
Kiss PageObjects [01-2017]Kiss PageObjects [01-2017]
Kiss PageObjects [01-2017]Iakiv Kramarenko
 
Enterprise Tic-Tac-Toe
Enterprise Tic-Tac-ToeEnterprise Tic-Tac-Toe
Enterprise Tic-Tac-ToeScott Wlaschin
 
Page object with selenide
Page object with selenidePage object with selenide
Page object with selenideCOMAQA.BY
 
liftIO 2022 quasiquote
liftIO 2022 quasiquoteliftIO 2022 quasiquote
liftIO 2022 quasiquoteHyunseok Cho
 
Collection v3
Collection v3Collection v3
Collection v3Sunil OS
 
Writing and using Hamcrest Matchers
Writing and using Hamcrest MatchersWriting and using Hamcrest Matchers
Writing and using Hamcrest MatchersShai Yallin
 
Design for Testability: A Tutorial for Devs and Testers
Design for Testability: A Tutorial for Devs and TestersDesign for Testability: A Tutorial for Devs and Testers
Design for Testability: A Tutorial for Devs and TestersTechWell
 
Java Serialization
Java SerializationJava Serialization
Java Serializationjeslie
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first stepsRenato Primavera
 
Java agents for fun and (not so much) profit
Java agents for fun and (not so much) profitJava agents for fun and (not so much) profit
Java agents for fun and (not so much) profitRobert Munteanu
 
Java Basics
Java BasicsJava Basics
Java BasicsSunil OS
 
20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction20.4 Java interfaces and abstraction
20.4 Java interfaces and abstractionIntro C# Book
 

What's hot (15)

QA Fest 2019. Андрей Солнцев. Selenide для профи
QA Fest 2019. Андрей Солнцев. Selenide для профиQA Fest 2019. Андрей Солнцев. Selenide для профи
QA Fest 2019. Андрей Солнцев. Selenide для профи
 
The lazy programmer's guide to writing thousands of tests
The lazy programmer's guide to writing thousands of testsThe lazy programmer's guide to writing thousands of tests
The lazy programmer's guide to writing thousands of tests
 
Kiss PageObjects [01-2017]
Kiss PageObjects [01-2017]Kiss PageObjects [01-2017]
Kiss PageObjects [01-2017]
 
Enterprise Tic-Tac-Toe
Enterprise Tic-Tac-ToeEnterprise Tic-Tac-Toe
Enterprise Tic-Tac-Toe
 
Page object with selenide
Page object with selenidePage object with selenide
Page object with selenide
 
liftIO 2022 quasiquote
liftIO 2022 quasiquoteliftIO 2022 quasiquote
liftIO 2022 quasiquote
 
Collection v3
Collection v3Collection v3
Collection v3
 
KRS
KRSKRS
KRS
 
Writing and using Hamcrest Matchers
Writing and using Hamcrest MatchersWriting and using Hamcrest Matchers
Writing and using Hamcrest Matchers
 
Design for Testability: A Tutorial for Devs and Testers
Design for Testability: A Tutorial for Devs and TestersDesign for Testability: A Tutorial for Devs and Testers
Design for Testability: A Tutorial for Devs and Testers
 
Java Serialization
Java SerializationJava Serialization
Java Serialization
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first steps
 
Java agents for fun and (not so much) profit
Java agents for fun and (not so much) profitJava agents for fun and (not so much) profit
Java agents for fun and (not so much) profit
 
Java Basics
Java BasicsJava Basics
Java Basics
 
20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction
 

Viewers also liked

Discovering and Monitoring Product Features and the Opinions on them with OPI...
Discovering and Monitoring Product Features and the Opinions on them with OPI...Discovering and Monitoring Product Features and the Opinions on them with OPI...
Discovering and Monitoring Product Features and the Opinions on them with OPI...Eirini Ntoutsi
 
Light steel villa catalogue log
Light steel villa catalogue logLight steel villa catalogue log
Light steel villa catalogue logeishimachinery
 
EISHI CO. main eps machine catalogue
EISHI CO. main eps machine catalogueEISHI CO. main eps machine catalogue
EISHI CO. main eps machine catalogueeishimachinery
 
eishi eps machinery co. introduction
eishi eps machinery co. introductioneishi eps machinery co. introduction
eishi eps machinery co. introductioneishimachinery
 
Eishi Company Profile 修改好的
Eishi Company Profile 修改好的Eishi Company Profile 修改好的
Eishi Company Profile 修改好的eishimachinery
 
Senior Leadership Development Case Study
Senior Leadership Development Case Study Senior Leadership Development Case Study
Senior Leadership Development Case Study Abhinandan Chatterjee
 
#HR General Vs. #HRBP and how to make the latter work for work!
#HR General Vs. #HRBP and how to make the latter work for work!#HR General Vs. #HRBP and how to make the latter work for work!
#HR General Vs. #HRBP and how to make the latter work for work!Abhinandan Chatterjee
 

Viewers also liked (13)

Discovering and Monitoring Product Features and the Opinions on them with OPI...
Discovering and Monitoring Product Features and the Opinions on them with OPI...Discovering and Monitoring Product Features and the Opinions on them with OPI...
Discovering and Monitoring Product Features and the Opinions on them with OPI...
 
Light steel villa catalogue log
Light steel villa catalogue logLight steel villa catalogue log
Light steel villa catalogue log
 
EISHI CO. main eps machine catalogue
EISHI CO. main eps machine catalogueEISHI CO. main eps machine catalogue
EISHI CO. main eps machine catalogue
 
eishi eps machinery co. introduction
eishi eps machinery co. introductioneishi eps machinery co. introduction
eishi eps machinery co. introduction
 
Eishi Company Profile 修改好的
Eishi Company Profile 修改好的Eishi Company Profile 修改好的
Eishi Company Profile 修改好的
 
10 Frame Flashed
10 Frame Flashed10 Frame Flashed
10 Frame Flashed
 
NeeMo@OpenCoffeeXX
NeeMo@OpenCoffeeXXNeeMo@OpenCoffeeXX
NeeMo@OpenCoffeeXX
 
Senior Leadership Development Case Study
Senior Leadership Development Case Study Senior Leadership Development Case Study
Senior Leadership Development Case Study
 
Primavera
PrimaveraPrimavera
Primavera
 
IERF - I Train Corporate Onboarding
IERF - I Train Corporate OnboardingIERF - I Train Corporate Onboarding
IERF - I Train Corporate Onboarding
 
China eCommerce PoV
China eCommerce PoVChina eCommerce PoV
China eCommerce PoV
 
#HR General Vs. #HRBP and how to make the latter work for work!
#HR General Vs. #HRBP and how to make the latter work for work!#HR General Vs. #HRBP and how to make the latter work for work!
#HR General Vs. #HRBP and how to make the latter work for work!
 
Corporate Accelerator
Corporate AcceleratorCorporate Accelerator
Corporate Accelerator
 

Similar to Bowling Game Kata

Bowling game kata
Bowling game kataBowling game kata
Bowling game kataCarol Bruno
 
Bowling game kata
Bowling game kataBowling game kata
Bowling game kataJoe McCall
 
Bowling game kata
Bowling game kataBowling game kata
Bowling game kataRahman Ash
 
Bowling Game Kata in C# Adapted
Bowling Game Kata in C# AdaptedBowling Game Kata in C# Adapted
Bowling Game Kata in C# AdaptedMike Clement
 
Test-Driven Development Fundamentals on Force.com
Test-Driven Development Fundamentals on Force.comTest-Driven Development Fundamentals on Force.com
Test-Driven Development Fundamentals on Force.comSalesforce Developers
 
I dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdfI dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdfarchanaemporium
 
The main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdfThe main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdfasif1401
 
This is Java,I am currently stumped on how to add a scoreboard for.pdf
This is Java,I am currently stumped on how to add a scoreboard for.pdfThis is Java,I am currently stumped on how to add a scoreboard for.pdf
This is Java,I am currently stumped on how to add a scoreboard for.pdfanjandavid
 
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdfObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdfrajkumarm401
 
package com.tictactoe; public class Main {public void play() {.pdf
package com.tictactoe; public class Main {public void play() {.pdfpackage com.tictactoe; public class Main {public void play() {.pdf
package com.tictactoe; public class Main {public void play() {.pdfinfo430661
 
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdfIn Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdfanjandavid
 
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdfWorking with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdfudit652068
 
You will write a multi-interface version of the well-known concentra.pdf
You will write a multi-interface version of the well-known concentra.pdfYou will write a multi-interface version of the well-known concentra.pdf
You will write a multi-interface version of the well-known concentra.pdfFashionColZone
 
public interface Game Note interface in place of class { .pdf
public interface Game  Note interface in place of class { .pdfpublic interface Game  Note interface in place of class { .pdf
public interface Game Note interface in place of class { .pdfkavithaarp
 
java slip for bachelors of business administration.pdf
java slip for bachelors of business administration.pdfjava slip for bachelors of business administration.pdf
java slip for bachelors of business administration.pdfkokah57440
 
The Ring programming language version 1.5 book - Part 9 of 31
The Ring programming language version 1.5 book - Part 9 of 31The Ring programming language version 1.5 book - Part 9 of 31
The Ring programming language version 1.5 book - Part 9 of 31Mahmoud Samir Fayed
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...MaruMengesha
 
Testing a 2D Platformer with Spock
Testing a 2D Platformer with SpockTesting a 2D Platformer with Spock
Testing a 2D Platformer with SpockAlexander Tarlinder
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_22-Feb-2021_L9-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_22-Feb-2021_L9-...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_22-Feb-2021_L9-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_22-Feb-2021_L9-...MaruMengesha
 

Similar to Bowling Game Kata (20)

Bowling game kata
Bowling game kataBowling game kata
Bowling game kata
 
Bowling game kata
Bowling game kataBowling game kata
Bowling game kata
 
Bowling game kata
Bowling game kataBowling game kata
Bowling game kata
 
Bowling Game Kata in C# Adapted
Bowling Game Kata in C# AdaptedBowling Game Kata in C# Adapted
Bowling Game Kata in C# Adapted
 
Test-Driven Development Fundamentals on Force.com
Test-Driven Development Fundamentals on Force.comTest-Driven Development Fundamentals on Force.com
Test-Driven Development Fundamentals on Force.com
 
I dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdfI dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdf
 
The main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdfThe main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdf
 
This is Java,I am currently stumped on how to add a scoreboard for.pdf
This is Java,I am currently stumped on how to add a scoreboard for.pdfThis is Java,I am currently stumped on how to add a scoreboard for.pdf
This is Java,I am currently stumped on how to add a scoreboard for.pdf
 
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdfObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
 
package com.tictactoe; public class Main {public void play() {.pdf
package com.tictactoe; public class Main {public void play() {.pdfpackage com.tictactoe; public class Main {public void play() {.pdf
package com.tictactoe; public class Main {public void play() {.pdf
 
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdfIn Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
 
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdfWorking with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
 
You will write a multi-interface version of the well-known concentra.pdf
You will write a multi-interface version of the well-known concentra.pdfYou will write a multi-interface version of the well-known concentra.pdf
You will write a multi-interface version of the well-known concentra.pdf
 
public interface Game Note interface in place of class { .pdf
public interface Game  Note interface in place of class { .pdfpublic interface Game  Note interface in place of class { .pdf
public interface Game Note interface in place of class { .pdf
 
java slip for bachelors of business administration.pdf
java slip for bachelors of business administration.pdfjava slip for bachelors of business administration.pdf
java slip for bachelors of business administration.pdf
 
The Ring programming language version 1.5 book - Part 9 of 31
The Ring programming language version 1.5 book - Part 9 of 31The Ring programming language version 1.5 book - Part 9 of 31
The Ring programming language version 1.5 book - Part 9 of 31
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...
 
Testing a 2D Platformer with Spock
Testing a 2D Platformer with SpockTesting a 2D Platformer with Spock
Testing a 2D Platformer with Spock
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_22-Feb-2021_L9-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_22-Feb-2021_L9-...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_22-Feb-2021_L9-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_22-Feb-2021_L9-...
 
662305 LAB13
662305 LAB13662305 LAB13
662305 LAB13
 

Recently uploaded

Taken Pilot Episode Story pitch Document
Taken Pilot Episode Story pitch DocumentTaken Pilot Episode Story pitch Document
Taken Pilot Episode Story pitch Documentf4ssvxpz62
 
Hi Class Call Girls In Goa 7028418221 Call Girls In Anjuna Beach Escort Services
Hi Class Call Girls In Goa 7028418221 Call Girls In Anjuna Beach Escort ServicesHi Class Call Girls In Goa 7028418221 Call Girls In Anjuna Beach Escort Services
Hi Class Call Girls In Goa 7028418221 Call Girls In Anjuna Beach Escort ServicesApsara Of India
 
Vip Delhi Ncr Call Girls Best Services Available
Vip Delhi Ncr Call Girls Best Services AvailableVip Delhi Ncr Call Girls Best Services Available
Vip Delhi Ncr Call Girls Best Services AvailableKomal Khan
 
(伦敦大学毕业证学位证成绩单-PDF版)
(伦敦大学毕业证学位证成绩单-PDF版)(伦敦大学毕业证学位证成绩单-PDF版)
(伦敦大学毕业证学位证成绩单-PDF版)twfkn8xj
 
Authentic No 1 Amil Baba In Pakistan Authentic No 1 Amil Baba In Karachi No 1...
Authentic No 1 Amil Baba In Pakistan Authentic No 1 Amil Baba In Karachi No 1...Authentic No 1 Amil Baba In Pakistan Authentic No 1 Amil Baba In Karachi No 1...
Authentic No 1 Amil Baba In Pakistan Authentic No 1 Amil Baba In Karachi No 1...First NO1 World Amil baba in Faisalabad
 
Call Girls CG Road 7397865700 Independent Call Girls
Call Girls CG Road 7397865700  Independent Call GirlsCall Girls CG Road 7397865700  Independent Call Girls
Call Girls CG Road 7397865700 Independent Call Girlsssuser7cb4ff
 
Gripping Adult Web Series You Can't Afford to Miss
Gripping Adult Web Series You Can't Afford to MissGripping Adult Web Series You Can't Afford to Miss
Gripping Adult Web Series You Can't Afford to Missget joys
 
Call Girls SG Highway 7397865700 Ridhima Hire Me Full Night
Call Girls SG Highway 7397865700 Ridhima Hire Me Full NightCall Girls SG Highway 7397865700 Ridhima Hire Me Full Night
Call Girls SG Highway 7397865700 Ridhima Hire Me Full Nightssuser7cb4ff
 
8377087607 Full Enjoy @24/7 Call Girls in Patel Nagar Delhi NCR
8377087607 Full Enjoy @24/7 Call Girls in Patel Nagar Delhi NCR8377087607 Full Enjoy @24/7 Call Girls in Patel Nagar Delhi NCR
8377087607 Full Enjoy @24/7 Call Girls in Patel Nagar Delhi NCRdollysharma2066
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377087607
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377087607FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377087607
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377087607dollysharma2066
 
Statement Of Intent - - Copy.documentfile
Statement Of Intent - - Copy.documentfileStatement Of Intent - - Copy.documentfile
Statement Of Intent - - Copy.documentfilef4ssvxpz62
 
定制(UofT毕业证书)加拿大多伦多大学毕业证成绩单原版一比一
定制(UofT毕业证书)加拿大多伦多大学毕业证成绩单原版一比一定制(UofT毕业证书)加拿大多伦多大学毕业证成绩单原版一比一
定制(UofT毕业证书)加拿大多伦多大学毕业证成绩单原版一比一lvtagr7
 
LE IMPOSSIBRU QUIZ (Based on Splapp-me-do)
LE IMPOSSIBRU QUIZ (Based on Splapp-me-do)LE IMPOSSIBRU QUIZ (Based on Splapp-me-do)
LE IMPOSSIBRU QUIZ (Based on Splapp-me-do)bertfelixtorre
 
fmovies-Movies hold a special place in the hearts
fmovies-Movies hold a special place in the heartsfmovies-Movies hold a special place in the hearts
fmovies-Movies hold a special place in the heartsa18205752
 
NO1 WorldWide Amil baba in pakistan Amil Baba in Karachi Black Magic Islamaba...
NO1 WorldWide Amil baba in pakistan Amil Baba in Karachi Black Magic Islamaba...NO1 WorldWide Amil baba in pakistan Amil Baba in Karachi Black Magic Islamaba...
NO1 WorldWide Amil baba in pakistan Amil Baba in Karachi Black Magic Islamaba...Amil baba
 
Call Girls Chorasi 7397865700 Ridhima Hire Me Full Night
Call Girls Chorasi 7397865700 Ridhima Hire Me Full NightCall Girls Chorasi 7397865700 Ridhima Hire Me Full Night
Call Girls Chorasi 7397865700 Ridhima Hire Me Full Nightssuser7cb4ff
 
Call Girls Sanand 7397865700 Ridhima Hire Me Full Night
Call Girls Sanand 7397865700 Ridhima Hire Me Full NightCall Girls Sanand 7397865700 Ridhima Hire Me Full Night
Call Girls Sanand 7397865700 Ridhima Hire Me Full Nightssuser7cb4ff
 
Call Girls Near Delhi Pride Hotel New Delhi 9873777170
Call Girls Near Delhi Pride Hotel New Delhi 9873777170Call Girls Near Delhi Pride Hotel New Delhi 9873777170
Call Girls Near Delhi Pride Hotel New Delhi 9873777170Sonam Pathan
 
Hifi Laxmi Nagar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ D...
Hifi Laxmi Nagar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ D...Hifi Laxmi Nagar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ D...
Hifi Laxmi Nagar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ D...srsj9000
 

Recently uploaded (20)

Taken Pilot Episode Story pitch Document
Taken Pilot Episode Story pitch DocumentTaken Pilot Episode Story pitch Document
Taken Pilot Episode Story pitch Document
 
Hi Class Call Girls In Goa 7028418221 Call Girls In Anjuna Beach Escort Services
Hi Class Call Girls In Goa 7028418221 Call Girls In Anjuna Beach Escort ServicesHi Class Call Girls In Goa 7028418221 Call Girls In Anjuna Beach Escort Services
Hi Class Call Girls In Goa 7028418221 Call Girls In Anjuna Beach Escort Services
 
Vip Delhi Ncr Call Girls Best Services Available
Vip Delhi Ncr Call Girls Best Services AvailableVip Delhi Ncr Call Girls Best Services Available
Vip Delhi Ncr Call Girls Best Services Available
 
(伦敦大学毕业证学位证成绩单-PDF版)
(伦敦大学毕业证学位证成绩单-PDF版)(伦敦大学毕业证学位证成绩单-PDF版)
(伦敦大学毕业证学位证成绩单-PDF版)
 
Authentic No 1 Amil Baba In Pakistan Authentic No 1 Amil Baba In Karachi No 1...
Authentic No 1 Amil Baba In Pakistan Authentic No 1 Amil Baba In Karachi No 1...Authentic No 1 Amil Baba In Pakistan Authentic No 1 Amil Baba In Karachi No 1...
Authentic No 1 Amil Baba In Pakistan Authentic No 1 Amil Baba In Karachi No 1...
 
Call Girls CG Road 7397865700 Independent Call Girls
Call Girls CG Road 7397865700  Independent Call GirlsCall Girls CG Road 7397865700  Independent Call Girls
Call Girls CG Road 7397865700 Independent Call Girls
 
Gripping Adult Web Series You Can't Afford to Miss
Gripping Adult Web Series You Can't Afford to MissGripping Adult Web Series You Can't Afford to Miss
Gripping Adult Web Series You Can't Afford to Miss
 
Call Girls SG Highway 7397865700 Ridhima Hire Me Full Night
Call Girls SG Highway 7397865700 Ridhima Hire Me Full NightCall Girls SG Highway 7397865700 Ridhima Hire Me Full Night
Call Girls SG Highway 7397865700 Ridhima Hire Me Full Night
 
8377087607 Full Enjoy @24/7 Call Girls in Patel Nagar Delhi NCR
8377087607 Full Enjoy @24/7 Call Girls in Patel Nagar Delhi NCR8377087607 Full Enjoy @24/7 Call Girls in Patel Nagar Delhi NCR
8377087607 Full Enjoy @24/7 Call Girls in Patel Nagar Delhi NCR
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377087607
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377087607FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377087607
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377087607
 
Statement Of Intent - - Copy.documentfile
Statement Of Intent - - Copy.documentfileStatement Of Intent - - Copy.documentfile
Statement Of Intent - - Copy.documentfile
 
定制(UofT毕业证书)加拿大多伦多大学毕业证成绩单原版一比一
定制(UofT毕业证书)加拿大多伦多大学毕业证成绩单原版一比一定制(UofT毕业证书)加拿大多伦多大学毕业证成绩单原版一比一
定制(UofT毕业证书)加拿大多伦多大学毕业证成绩单原版一比一
 
LE IMPOSSIBRU QUIZ (Based on Splapp-me-do)
LE IMPOSSIBRU QUIZ (Based on Splapp-me-do)LE IMPOSSIBRU QUIZ (Based on Splapp-me-do)
LE IMPOSSIBRU QUIZ (Based on Splapp-me-do)
 
fmovies-Movies hold a special place in the hearts
fmovies-Movies hold a special place in the heartsfmovies-Movies hold a special place in the hearts
fmovies-Movies hold a special place in the hearts
 
Call Girls Koti 7001305949 all area service COD available Any Time
Call Girls Koti 7001305949 all area service COD available Any TimeCall Girls Koti 7001305949 all area service COD available Any Time
Call Girls Koti 7001305949 all area service COD available Any Time
 
NO1 WorldWide Amil baba in pakistan Amil Baba in Karachi Black Magic Islamaba...
NO1 WorldWide Amil baba in pakistan Amil Baba in Karachi Black Magic Islamaba...NO1 WorldWide Amil baba in pakistan Amil Baba in Karachi Black Magic Islamaba...
NO1 WorldWide Amil baba in pakistan Amil Baba in Karachi Black Magic Islamaba...
 
Call Girls Chorasi 7397865700 Ridhima Hire Me Full Night
Call Girls Chorasi 7397865700 Ridhima Hire Me Full NightCall Girls Chorasi 7397865700 Ridhima Hire Me Full Night
Call Girls Chorasi 7397865700 Ridhima Hire Me Full Night
 
Call Girls Sanand 7397865700 Ridhima Hire Me Full Night
Call Girls Sanand 7397865700 Ridhima Hire Me Full NightCall Girls Sanand 7397865700 Ridhima Hire Me Full Night
Call Girls Sanand 7397865700 Ridhima Hire Me Full Night
 
Call Girls Near Delhi Pride Hotel New Delhi 9873777170
Call Girls Near Delhi Pride Hotel New Delhi 9873777170Call Girls Near Delhi Pride Hotel New Delhi 9873777170
Call Girls Near Delhi Pride Hotel New Delhi 9873777170
 
Hifi Laxmi Nagar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ D...
Hifi Laxmi Nagar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ D...Hifi Laxmi Nagar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ D...
Hifi Laxmi Nagar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ D...
 

Bowling Game Kata

  • 1. Bowling Game Kata Object Mentor, Inc. fitnesse.org Copyright  2005 by Object Mentor, Inc All copies must retain this page unchanged. www.junit.org www.objectmentor.com blog.objectmentor.com
  • 2. Scoring Bowling. 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. 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.) 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. 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.
  • 3.
  • 4. A quick design session Clearly we need the Game class.
  • 5. A quick design session A game has 10 frames.
  • 6. A quick design session A frame has 1 or two rolls.
  • 7. A quick design session The tenth frame has two or three rolls. It is different from all the other frames.
  • 8. A quick design session The score function must iterate through all the frames, and calculate all their scores.
  • 9. A quick design session The score for a spare or a strike depends on the frame’s successor
  • 10.
  • 11.
  • 12. The first test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { public void testGutterGame() throws Exception { Game g = new Game (); } }
  • 13. The first test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { public void testGutterGame() throws Exception { Game g = new Game(); } } public class Game { }
  • 14. The first test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { public void testGutterGame() throws Exception { Game g = new Game(); } } public class Game { }
  • 15. The first test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { public void testGutterGame() throws Exception { Game g = new Game(); for (int i=0; i<20; i++) g. roll (0); } } public class Game { }
  • 16. The first test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { public void testGutterGame() throws Exception { Game g = new Game(); for (int i=0; i<20; i++) g.roll(0); } } public class Game { public void roll(int pins) { } }
  • 17. The first test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { public void testGutterGame() throws Exception { Game g = new Game(); for (int i=0; i<20; i++) g.roll(0); assertEquals(0, g. score ()); } } public class Game { public void roll(int pins) { } }
  • 18. The first test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { public void testGutterGame() throws Exception { Game g = new Game(); for (int i=0; i<20; i++) g.roll(0); assertEquals(0, g.score()); } } public class Game { public void roll(int pins) { } public int score() { return -1; } } expected:<0> but was:<-1>
  • 19. The first test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { public void testGutterGame() throws Exception { Game g = new Game(); for (int i=0; i<20; i++) g.roll(0); assertEquals(0, g.score()); } } public class Game { public void roll(int pins) { } public int score() { return 0; } }
  • 20. The Second test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { public void testGutterGame() throws Exception { Game g = new Game(); for (int i = 0; i < 20; i++) g.roll(0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { Game g = new Game(); for (int i = 0; i < 20; i++) g.roll(1); assertEquals(20, g.score()); } } public class Game { public void roll(int pins) { } public int score() { return 0; } }
  • 21. The Second test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { public void testGutterGame() throws Exception { Game g = new Game(); for (int i = 0; i < 20; i++) g.roll(0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { Game g = new Game(); for (int i = 0; i < 20; i++) g.roll(1); assertEquals(20, g.score()); } } public class Game { public void roll(int pins) { } public int score() { return 0; } } - Game creation is duplicated - roll loop is duplicated
  • 22. The Second test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { public void testGutterGame() throws Exception { Game g = new Game(); for (int i = 0; i < 20; i++) g.roll(0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { Game g = new Game(); for (int i = 0; i < 20; i++) g.roll(1); assertEquals(20, g.score()); } } public class Game { public void roll(int pins) { } public int score() { return 0; } } - Game creation is duplicated - roll loop is duplicated expected:<20> but was:<0>
  • 23. The Second test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } public void testGutterGame() throws Exception { for (int i = 0; i < 20; i++) g.roll(0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { for (int i = 0; i < 20; i++) g.roll(1); assertEquals(20, g.score()); } } public class Game { private int score = 0; public void roll(int pins) { score += pins; } public int score() { return score; } } - roll loop is duplicated
  • 24. The Second test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } public void testGutterGame() throws Exception { int n = 20; int pins = 0; for (int i = 0; i < n ; i++) { g.roll( pins ); } assertEquals(0, g.score()); } public void testAllOnes() throws Exception { for (int i = 0; i < 20; i++) g.roll(1); assertEquals(20, g.score()); } } public class Game { private int score = 0; public void roll(int pins) { score += pins; } public int score() { return score; } } - roll loop is duplicated
  • 25. The Second test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } public void testGutterGame() throws Exception { int n = 20; int pins = 0; rollMany(n, pins); assertEquals(0, g.score()); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testAllOnes() throws Exception { for (int i = 0; i < 20; i++) g.roll(1); assertEquals(20, g.score()); } } public class Game { private int score = 0; public void roll(int pins) { score += pins; } public int score() { return score; } } - roll loop is duplicated
  • 26. The Second test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } public void testGutterGame() throws Exception { rollMany( 20, 0 ); assertEquals(0, g.score()); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testAllOnes() throws Exception { for (int i = 0; i < 20; i++) g.roll(1); assertEquals(20, g.score()); } } public class Game { private int score = 0; public void roll(int pins) { score += pins; } public int score() { return score; } } - roll loop is duplicated
  • 27. The Second test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } } public class Game { private int score = 0; public void roll(int pins) { score += pins; } public int score() { return score; } } - roll loop is duplicated
  • 28. The Second test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } } public class Game { private int score = 0; public void roll(int pins) { score += pins; } public int score() { return score; } }
  • 29. The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { g.roll(5); g.roll(5); // spare g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } } public class Game { private int score = 0; public void roll(int pins) { score += pins; } public int score() { return score; } } - ugly comment in test.
  • 30. The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { g.roll(5); g.roll(5); // spare g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } } public class Game { private int score = 0; public void roll(int pins) { score += pins; } public int score() { return score; } } - ugly comment in test. expected:<16> but was:<13>
  • 31. The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { g.roll(5); g.roll(5); // spare g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } } public class Game { private int score = 0; public void roll(int pins) { score += pins; } public int score() { return score; } } - ugly comment in test. tempted to use flag to remember previous roll. So design must be wrong.
  • 32. The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { g.roll(5); g.roll(5); // spare g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } } public class Game { private int score = 0; public void roll(int pins) { score += pins; } public int score() { return score; } } - ugly comment in test. roll() calculates score, but name does not imply that. score() does not calculate score, but name implies that it does. Design is wrong. Responsibilities are misplaced.
  • 33. The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } // public void testOneSpare() throws Exception { // g.roll(5); // g.roll(5); // spare // g.roll(3); // rollMany(17,0); // assertEquals(16,g.score()); // } } public class Game { private int score = 0; public void roll(int pins) { score += pins; } public int score() { return score; } } - ugly comment in test.
  • 34. The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } // public void testOneSpare() throws Exception { // g.roll(5); // g.roll(5); // spare // g.roll(3); // rollMany(17,0); // assertEquals(16,g.score()); // } } public class Game { private int score = 0; private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { score += pins; rolls[currentRoll++] = pins; } public int score() { return score; } } - ugly comment in test.
  • 35. The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } // public void testOneSpare() throws Exception { // g.roll(5); // g.roll(5); // spare // g.roll(3); // rollMany(17,0); // assertEquals(16,g.score()); // } } public class Game { private int score = 0; private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { score += pins; rolls[currentRoll++] = pins; } public int score() { int score = 0; for (int i = 0; i < rolls.length; i++) score += rolls[i]; return score; } } - ugly comment in test.
  • 36. The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } // public void testOneSpare() throws Exception { // g.roll(5); // g.roll(5); // spare // g.roll(3); // rollMany(17,0); // assertEquals(16,g.score()); // } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; for (int i = 0; i < rolls.length; i++) score += rolls[i]; return score; } } - ugly comment in test.
  • 37. The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { g.roll(5); g.roll(5); // spare g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; for (int i = 0; i < rolls.length; i++) score += rolls[i]; return score; } } - ugly comment in test. expected:<16> but was:<13>
  • 38. The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { g.roll(5); g.roll(5); // spare g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; for (int i = 0; i < rolls.length; i++) { if (rolls[i] + rolls[i+1] == 10) // spare score += ... score += rolls[i]; } return score; } } - ugly comment in test. This isn’t going to work because i might not refer to the first ball of the frame. Design is still wrong. Need to walk through array two balls (one frame) at a time.
  • 39. The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } // public void testOneSpare() throws Exception { // g.roll(5); // g.roll(5); // spare // g.roll(3); // rollMany(17,0); // assertEquals(16,g.score()); // } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; for (int i = 0; i < rolls.length; i++) score += rolls[i]; return score; } } - ugly comment in test.
  • 40. The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } // public void testOneSpare() throws Exception { // g.roll(5); // g.roll(5); // spare // g.roll(3); // rollMany(17,0); // assertEquals(16,g.score()); // } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; int i = 0; for ( int frame = 0; frame < 10; frame++ ) { score += rolls[i] + rolls[i+1] ; i += 2; } return score; } } - ugly comment in test.
  • 41. The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { g.roll(5); g.roll(5); // spare g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; int i = 0; for (int frame = 0; frame < 10; frame++) { score += rolls[i] + rolls[i+1]; i += 2; } return score; } } - ugly comment in test. expected:<16> but was:<13>
  • 42. The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { g.roll(5); g.roll(5); // spare g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; int i = 0; for (int frame = 0; frame < 10; frame++) { if (rolls[i] + rolls[i + 1] == 10) // spare { score += 10 + rolls[i + 2]; i += 2; } else { score += rolls[i] + rolls[i + 1]; i += 2; } } return score; } } - ugly comment in test.
  • 43.
  • 44.
  • 45.
  • 46. The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { rollSpare(); g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } private void rollSpare() { g.roll(5); g.roll(5); } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; int frameIndex = 0; for (int frame = 0; frame < 10; frame++) { if (isSpare(frameIndex)) { score += 10 + rolls[frameIndex + 2]; frameIndex += 2; } else { score += rolls[frameIndex] + rolls[frameIndex + 1]; frameIndex += 2; } } return score; } private boolean isSpare(int frameIndex) { return rolls[frameIndex] + rolls[frameIndex + 1] == 10; } } -
  • 47. The Fourth test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase { ... public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { rollSpare(); g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } public void testOneStrike() throws Exception { g.roll(10); // strike g.roll(3); g.roll(4); rollMany(16, 0); assertEquals(24, g.score()); } private void rollSpare() { g.roll(5); g.roll(5); } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; int frameIndex = 0; for (int frame = 0; frame < 10; frame++) { if (isSpare(frameIndex)) { score += 10 + rolls[frameIndex + 2]; frameIndex += 2; } else { score += rolls[frameIndex] + rolls[frameIndex + 1]; frameIndex += 2; } } return score; } private boolean isSpare(int frameIndex) { return rolls[frameIndex] + rolls[frameIndex + 1] == 10; } } - ugly comment in testOneStrike. expected:<24> but was:<17>
  • 48.
  • 49.
  • 50.
  • 51. The Fourth test. ... public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { rollSpare(); g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } public void testOneStrike() throws Exception { rollStrike(); g.roll(3); g.roll(4); rollMany(16, 0); assertEquals(24, g.score()); } private void rollStrike() { g.roll(10); } private void rollSpare() { g.roll(5); g.roll(5); } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; int frameIndex = 0; for (int frame = 0; frame < 10; frame++) { if (isStrike(frameIndex)) { score += 10 + strikeBonus(frameIndex); frameIndex++; } else if (isSpare(frameIndex)) { score += 10 + spareBonus(frameIndex); frameIndex += 2; } else { score += sumOfBallsInFrame(frameIndex); frameIndex += 2; } } return score; } private boolean isStrike(int frameIndex) { return rolls[frameIndex] == 10; } private int sumOfBallsInFrame(int frameIndex) { return rolls[frameIndex] + rolls[frameIndex+1]; } private int spareBonus(int frameIndex) { return rolls[frameIndex+2]; } private int strikeBonus(int frameIndex) { return rolls[frameIndex+1] + rolls[frameIndex+2]; } private boolean isSpare(int frameIndex) { return rolls[frameIndex]+rolls[frameIndex+1] == 10; } }
  • 52. The Fifth test. ... public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { rollSpare(); g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } public void testOneStrike() throws Exception { rollStrike(); g.roll(3); g.roll(4); rollMany(16, 0); assertEquals(24, g.score()); } public void testPerfectGame() throws Exception { rollMany(12,10); assertEquals(300, g.score()); } private void rollStrike() { g.roll(10); } private void rollSpare() { g.roll(5); g.roll(5); } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; int frameIndex = 0; for (int frame = 0; frame < 10; frame++) { if (isStrike(frameIndex)) { score += 10 + strikeBonus(frameIndex); frameIndex++; } else if (isSpare(frameIndex)) { score += 10 + spareBonus(frameIndex); frameIndex += 2; } else { score += sumOfBallsInFrame(frameIndex); frameIndex += 2; } } return score; } private boolean isStrike(int frameIndex) { return rolls[frameIndex] == 10; } private int sumOfBallsInFrame(int frameIndex) { return rolls[frameIndex] + rolls[frameIndex+1]; } private int spareBonus(int frameIndex) { return rolls[frameIndex+2]; } private int strikeBonus(int frameIndex) { return rolls[frameIndex+1] + rolls[frameIndex+2]; } private boolean isSpare(int frameIndex) { return rolls[frameIndex]+rolls[frameIndex+1] == 10; } }
  • 53. End