SlideShare uma empresa Scribd logo
1 de 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

Mais conteúdo relacionado

Mais procurados

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
 

Mais procurados (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
 

Destaque (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
 

Semelhante a 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
 
You will write a multi-interface version of the well-known concentra.pdf
You will write a multi-interface version of the well-known concentra.pdfYou will write a multi-interface version of the well-known concentra.pdf
You will write a multi-interface version of the well-known concentra.pdfFashionColZone
 
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
 
This is Java,I am currently stumped on how to add a scoreboard for.pdf
This is Java,I am currently stumped on how to add a scoreboard for.pdfThis is Java,I am currently stumped on how to add a scoreboard for.pdf
This is Java,I am currently stumped on how to add a scoreboard for.pdfanjandavid
 
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
 

Semelhante a 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
 
You will write a multi-interface version of the well-known concentra.pdf
You will write a multi-interface version of the well-known concentra.pdfYou will write a multi-interface version of the well-known concentra.pdf
You will write a multi-interface version of the well-known concentra.pdf
 
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
 
This is Java,I am currently stumped on how to add a scoreboard for.pdf
This is Java,I am currently stumped on how to add a scoreboard for.pdfThis is Java,I am currently stumped on how to add a scoreboard for.pdf
This is Java,I am currently stumped on how to add a scoreboard for.pdf
 
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++
 

Último

Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Scott Andery
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 

Último (20)

Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 

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