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.or
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.
+ r o ll( p in s : in t )
+ s c o r e ( ) : in t
G a m e
• Write a class named “Game” that has two
methods
– roll(pins : int) is called each time the player
rolls a ball. The argument is the number of
pins knocked down.
– score() : int is called only at the very end of
the game. It returns the total score for that
game.
A quick design session
+ r o ll( p in s : in t )
+ s c o r e ( ) : in t
G a m e
Clearly we need the Game class.
A quick design session
+ r o ll( p in s : in t )
+ s c o r e ( ) : in t
G a m e F r a m e
1 0
A game has 10 frames.
A quick design session
+ r o ll( p in s : in t )
+ s c o r e ( ) : in t
G a m e F r a m e
- p in s : in t
R o ll1 0 1 . . 2
A frame has 1 or two rolls.
A quick design session
+ r o ll( p in s : in t )
+ s c o r e ( ) : in t
G a m e F r a m e
T e n t h F r a m e
- p in s : in t
R o ll1 0 1 . . 2
1
The tenth frame has two or three rolls.
It is different from all the other frames.
A quick design session
+ r o ll( p in s : in t )
+ s c o r e ( ) : in t
G a m e
+ s c o r e ( ) : in t
F r a m e
T e n t h F r a m e
- p in s : in t
R o ll1 0 1 . . 2
1
The score function must
iterate through all the
frames, and calculate
all their scores.
A quick design session
+ r o ll( p in s : in t )
+ s c o r e ( ) : in t
G a m e
+ s c o r e ( ) : in t
F r a m e
T e n t h F r a m e
- p in s : in t
R o ll1 0 1 . . 2
1
n e x t f r a m e The score for a spare or a strike
depends on the frame’s successor
Begin.
• Create a project named BowlingGame
• Create a unit test named
BowlingGameTest
import junit.framework.TestCase;
public class BowlingGameTest extends TestCase {
}
Begin.
• Create a project named BowlingGame
• Create a unit test named
BowlingGameTest
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;
}
}
-ugly comment in test.
-ugly comment in conditional.
-i is a bad name for this variable
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;
}
}
-ugly comment in test.
-ugly comment in conditional.
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;
}
}
-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 {
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;
}
}
-ugly comment in testOneStrike.
-ugly comment in conditional.
-ugly expressions.
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;
}
}
-ugly comment in testOneStrike.
-ugly comment in conditional.
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;
}
}
-ugly comment in testOneStrike.
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

Testing a 2D Platformer with Spock
Testing a 2D Platformer with SpockTesting a 2D Platformer with Spock
Testing a 2D Platformer with SpockAlexander Tarlinder
 
The Ring programming language version 1.5.1 book - Part 60 of 180
The Ring programming language version 1.5.1 book - Part 60 of 180The Ring programming language version 1.5.1 book - Part 60 of 180
The Ring programming language version 1.5.1 book - Part 60 of 180Mahmoud Samir Fayed
 
PyconKR 2018 Deep dive into Coroutine
PyconKR 2018 Deep dive into CoroutinePyconKR 2018 Deep dive into Coroutine
PyconKR 2018 Deep dive into CoroutineDaehee Kim
 
The Ring programming language version 1.8 book - Part 66 of 202
The Ring programming language version 1.8 book - Part 66 of 202The Ring programming language version 1.8 book - Part 66 of 202
The Ring programming language version 1.8 book - Part 66 of 202Mahmoud Samir Fayed
 
Node.js flow control
Node.js flow controlNode.js flow control
Node.js flow controlSimon Su
 
Design Patterns in Game Programming
Design Patterns in Game ProgrammingDesign Patterns in Game Programming
Design Patterns in Game ProgrammingBruno Cicanci
 
The Ring programming language version 1.6 book - Part 79 of 189
The Ring programming language version 1.6 book - Part 79 of 189The Ring programming language version 1.6 book - Part 79 of 189
The Ring programming language version 1.6 book - Part 79 of 189Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 66 of 189
The Ring programming language version 1.6 book - Part 66 of 189The Ring programming language version 1.6 book - Part 66 of 189
The Ring programming language version 1.6 book - Part 66 of 189Mahmoud Samir Fayed
 
AST: threats and opportunities
AST: threats and opportunitiesAST: threats and opportunities
AST: threats and opportunitiesAlexander Lifanov
 
The Ring programming language version 1.4.1 book - Part 15 of 31
The Ring programming language version 1.4.1 book - Part 15 of 31The Ring programming language version 1.4.1 book - Part 15 of 31
The Ring programming language version 1.4.1 book - Part 15 of 31Mahmoud Samir Fayed
 
Programação de Jogos - Design Patterns
Programação de Jogos - Design PatternsProgramação de Jogos - Design Patterns
Programação de Jogos - Design PatternsBruno Cicanci
 
The Ring programming language version 1.2 book - Part 44 of 84
The Ring programming language version 1.2 book - Part 44 of 84The Ring programming language version 1.2 book - Part 44 of 84
The Ring programming language version 1.2 book - Part 44 of 84Mahmoud Samir Fayed
 
Playing with camera preview buffers on BlackBerry 10
Playing with camera preview buffers on BlackBerry 10Playing with camera preview buffers on BlackBerry 10
Playing with camera preview buffers on BlackBerry 10Raimon Ràfols
 
The Ring programming language version 1.6 book - Part 184 of 189
The Ring programming language version 1.6 book - Part 184 of 189The Ring programming language version 1.6 book - Part 184 of 189
The Ring programming language version 1.6 book - Part 184 of 189Mahmoud Samir Fayed
 

What's hot (18)

Aa
AaAa
Aa
 
Gilat_ch02.pdf
Gilat_ch02.pdfGilat_ch02.pdf
Gilat_ch02.pdf
 
Gilat_ch03.pdf
Gilat_ch03.pdfGilat_ch03.pdf
Gilat_ch03.pdf
 
project3
project3project3
project3
 
Testing a 2D Platformer with Spock
Testing a 2D Platformer with SpockTesting a 2D Platformer with Spock
Testing a 2D Platformer with Spock
 
The Ring programming language version 1.5.1 book - Part 60 of 180
The Ring programming language version 1.5.1 book - Part 60 of 180The Ring programming language version 1.5.1 book - Part 60 of 180
The Ring programming language version 1.5.1 book - Part 60 of 180
 
PyconKR 2018 Deep dive into Coroutine
PyconKR 2018 Deep dive into CoroutinePyconKR 2018 Deep dive into Coroutine
PyconKR 2018 Deep dive into Coroutine
 
The Ring programming language version 1.8 book - Part 66 of 202
The Ring programming language version 1.8 book - Part 66 of 202The Ring programming language version 1.8 book - Part 66 of 202
The Ring programming language version 1.8 book - Part 66 of 202
 
Node.js flow control
Node.js flow controlNode.js flow control
Node.js flow control
 
Design Patterns in Game Programming
Design Patterns in Game ProgrammingDesign Patterns in Game Programming
Design Patterns in Game Programming
 
The Ring programming language version 1.6 book - Part 79 of 189
The Ring programming language version 1.6 book - Part 79 of 189The Ring programming language version 1.6 book - Part 79 of 189
The Ring programming language version 1.6 book - Part 79 of 189
 
The Ring programming language version 1.6 book - Part 66 of 189
The Ring programming language version 1.6 book - Part 66 of 189The Ring programming language version 1.6 book - Part 66 of 189
The Ring programming language version 1.6 book - Part 66 of 189
 
AST: threats and opportunities
AST: threats and opportunitiesAST: threats and opportunities
AST: threats and opportunities
 
The Ring programming language version 1.4.1 book - Part 15 of 31
The Ring programming language version 1.4.1 book - Part 15 of 31The Ring programming language version 1.4.1 book - Part 15 of 31
The Ring programming language version 1.4.1 book - Part 15 of 31
 
Programação de Jogos - Design Patterns
Programação de Jogos - Design PatternsProgramação de Jogos - Design Patterns
Programação de Jogos - Design Patterns
 
The Ring programming language version 1.2 book - Part 44 of 84
The Ring programming language version 1.2 book - Part 44 of 84The Ring programming language version 1.2 book - Part 44 of 84
The Ring programming language version 1.2 book - Part 44 of 84
 
Playing with camera preview buffers on BlackBerry 10
Playing with camera preview buffers on BlackBerry 10Playing with camera preview buffers on BlackBerry 10
Playing with camera preview buffers on BlackBerry 10
 
The Ring programming language version 1.6 book - Part 184 of 189
The Ring programming language version 1.6 book - Part 184 of 189The Ring programming language version 1.6 book - Part 184 of 189
The Ring programming language version 1.6 book - Part 184 of 189
 

Viewers also liked

Viewers also liked (14)

ib_foresight
ib_foresightib_foresight
ib_foresight
 
Kristobal balentziaga
Kristobal balentziagaKristobal balentziaga
Kristobal balentziaga
 
ORIGAMI FOR ADHD!
ORIGAMI FOR ADHD!ORIGAMI FOR ADHD!
ORIGAMI FOR ADHD!
 
HONESTY FOR KIDS!
HONESTY FOR KIDS!HONESTY FOR KIDS!
HONESTY FOR KIDS!
 
Wam v usv-eyes_and_ears_9-4-11
Wam v usv-eyes_and_ears_9-4-11Wam v usv-eyes_and_ears_9-4-11
Wam v usv-eyes_and_ears_9-4-11
 
137 sustainable coastal_development_final
137 sustainable coastal_development_final137 sustainable coastal_development_final
137 sustainable coastal_development_final
 
Tara Expeditions
Tara ExpeditionsTara Expeditions
Tara Expeditions
 
Luon energy
Luon energyLuon energy
Luon energy
 
DIRTT
DIRTTDIRTT
DIRTT
 
Srinivas kilambi
Srinivas kilambiSrinivas kilambi
Srinivas kilambi
 
Witt Energy
Witt EnergyWitt Energy
Witt Energy
 
Ecostation
EcostationEcostation
Ecostation
 
Opoc Engine
Opoc EngineOpoc Engine
Opoc Engine
 
Bandalong Litter Trap
Bandalong Litter TrapBandalong Litter Trap
Bandalong Litter Trap
 

Similar to Bowling game kata

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
 
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
 
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
 
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
 
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
 
The Ring programming language version 1.5.3 book - Part 60 of 184
The Ring programming language version 1.5.3 book - Part 60 of 184The Ring programming language version 1.5.3 book - Part 60 of 184
The Ring programming language version 1.5.3 book - Part 60 of 184Mahmoud Samir Fayed
 
The Ring programming language version 1.5.3 book - Part 50 of 184
The Ring programming language version 1.5.3 book - Part 50 of 184The Ring programming language version 1.5.3 book - Part 50 of 184
The Ring programming language version 1.5.3 book - Part 50 of 184Mahmoud Samir Fayed
 
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
 
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.9 book - Part 60 of 210
The Ring programming language version 1.9 book - Part 60 of 210The Ring programming language version 1.9 book - Part 60 of 210
The Ring programming language version 1.9 book - Part 60 of 210Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 55 of 189
The Ring programming language version 1.6 book - Part 55 of 189The Ring programming language version 1.6 book - Part 55 of 189
The Ring programming language version 1.6 book - Part 55 of 189Mahmoud Samir Fayed
 
The Ring programming language version 1.5.3 book - Part 79 of 184
The Ring programming language version 1.5.3 book - Part 79 of 184The Ring programming language version 1.5.3 book - Part 79 of 184
The Ring programming language version 1.5.3 book - Part 79 of 184Mahmoud Samir Fayed
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_01-Mar-2021_L12...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_01-Mar-2021_L12...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_01-Mar-2021_L12...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_01-Mar-2021_L12...MaruMengesha
 
The Ring programming language version 1.7 book - Part 55 of 196
The Ring programming language version 1.7 book - Part 55 of 196The Ring programming language version 1.7 book - Part 55 of 196
The Ring programming language version 1.7 book - Part 55 of 196Mahmoud Samir Fayed
 
Tic tac toe on c++ project
Tic tac toe on c++ projectTic tac toe on c++ project
Tic tac toe on c++ projectUtkarsh Aggarwal
 
New Tools for a More Functional C++
New Tools for a More Functional C++New Tools for a More Functional C++
New Tools for a More Functional C++Sumant Tambe
 
The Ring programming language version 1.9 book - Part 80 of 210
The Ring programming language version 1.9 book - Part 80 of 210The Ring programming language version 1.9 book - Part 80 of 210
The Ring programming language version 1.9 book - Part 80 of 210Mahmoud Samir Fayed
 
The Ring programming language version 1.3 book - Part 52 of 88
The Ring programming language version 1.3 book - Part 52 of 88The Ring programming language version 1.3 book - Part 52 of 88
The Ring programming language version 1.3 book - Part 52 of 88Mahmoud Samir Fayed
 

Similar to Bowling game kata (20)

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
 
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
 
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
 
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
 
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-...
 
The Ring programming language version 1.5.3 book - Part 60 of 184
The Ring programming language version 1.5.3 book - Part 60 of 184The Ring programming language version 1.5.3 book - Part 60 of 184
The Ring programming language version 1.5.3 book - Part 60 of 184
 
The Ring programming language version 1.5.3 book - Part 50 of 184
The Ring programming language version 1.5.3 book - Part 50 of 184The Ring programming language version 1.5.3 book - Part 50 of 184
The Ring programming language version 1.5.3 book - Part 50 of 184
 
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
 
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.9 book - Part 60 of 210
The Ring programming language version 1.9 book - Part 60 of 210The Ring programming language version 1.9 book - Part 60 of 210
The Ring programming language version 1.9 book - Part 60 of 210
 
The Ring programming language version 1.6 book - Part 55 of 189
The Ring programming language version 1.6 book - Part 55 of 189The Ring programming language version 1.6 book - Part 55 of 189
The Ring programming language version 1.6 book - Part 55 of 189
 
The Ring programming language version 1.5.3 book - Part 79 of 184
The Ring programming language version 1.5.3 book - Part 79 of 184The Ring programming language version 1.5.3 book - Part 79 of 184
The Ring programming language version 1.5.3 book - Part 79 of 184
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_01-Mar-2021_L12...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_01-Mar-2021_L12...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_01-Mar-2021_L12...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_01-Mar-2021_L12...
 
The Ring programming language version 1.7 book - Part 55 of 196
The Ring programming language version 1.7 book - Part 55 of 196The Ring programming language version 1.7 book - Part 55 of 196
The Ring programming language version 1.7 book - Part 55 of 196
 
Tic tac toe on c++ project
Tic tac toe on c++ projectTic tac toe on c++ project
Tic tac toe on c++ project
 
662305 LAB13
662305 LAB13662305 LAB13
662305 LAB13
 
New Tools for a More Functional C++
New Tools for a More Functional C++New Tools for a More Functional C++
New Tools for a More Functional C++
 
The Ring programming language version 1.9 book - Part 80 of 210
The Ring programming language version 1.9 book - Part 80 of 210The Ring programming language version 1.9 book - Part 80 of 210
The Ring programming language version 1.9 book - Part 80 of 210
 
The Ring programming language version 1.3 book - Part 52 of 88
The Ring programming language version 1.3 book - Part 52 of 88The Ring programming language version 1.3 book - Part 52 of 88
The Ring programming language version 1.3 book - Part 52 of 88
 

Recently uploaded

Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsAndrey Dotsenko
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 

Recently uploaded (20)

Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 

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.or 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. The Requirements. + r o ll( p in s : in t ) + s c o r e ( ) : in t G a m e • Write a class named “Game” that has two methods – roll(pins : int) is called each time the player rolls a ball. The argument is the number of pins knocked down. – score() : int is called only at the very end of the game. It returns the total score for that game.
  • 4. A quick design session + r o ll( p in s : in t ) + s c o r e ( ) : in t G a m e Clearly we need the Game class.
  • 5. A quick design session + r o ll( p in s : in t ) + s c o r e ( ) : in t G a m e F r a m e 1 0 A game has 10 frames.
  • 6. A quick design session + r o ll( p in s : in t ) + s c o r e ( ) : in t G a m e F r a m e - p in s : in t R o ll1 0 1 . . 2 A frame has 1 or two rolls.
  • 7. A quick design session + r o ll( p in s : in t ) + s c o r e ( ) : in t G a m e F r a m e T e n t h F r a m e - p in s : in t R o ll1 0 1 . . 2 1 The tenth frame has two or three rolls. It is different from all the other frames.
  • 8. A quick design session + r o ll( p in s : in t ) + s c o r e ( ) : in t G a m e + s c o r e ( ) : in t F r a m e T e n t h F r a m e - p in s : in t R o ll1 0 1 . . 2 1 The score function must iterate through all the frames, and calculate all their scores.
  • 9. A quick design session + r o ll( p in s : in t ) + s c o r e ( ) : in t G a m e + s c o r e ( ) : in t F r a m e T e n t h F r a m e - p in s : in t R o ll1 0 1 . . 2 1 n e x t f r a m e The score for a spare or a strike depends on the frame’s successor
  • 10. Begin. • Create a project named BowlingGame • Create a unit test named BowlingGameTest import junit.framework.TestCase; public class BowlingGameTest extends TestCase { }
  • 11. Begin. • Create a project named BowlingGame • Create a unit test named BowlingGameTest 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
  • 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. 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. -ugly comment in conditional. -i is a bad name for this variable
  • 44. 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; } } -ugly comment in test. -ugly comment in conditional.
  • 45. 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; } } -ugly comment in test.
  • 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. 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; } } -ugly comment in testOneStrike. -ugly comment in conditional. -ugly expressions.
  • 49. 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; } } -ugly comment in testOneStrike. -ugly comment in conditional.
  • 50. 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; } } -ugly comment in testOneStrike.
  • 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