Unformatted text preview:

1Bowling Game KataObject Mentor, Inc.fitnesse.orgCopyright  2005 by Object Mentor, IncAll copies must retain this page unchanged.www.junit.orgwww.objectmentor.comblog.objectmentor.comScoring Bowling.The game consists of 10 frames as shown above. In each frame the player hastwo opportunities to knock down 10 pins. The score for the frame is the totalnumber 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 forthat frame is the number of pins knocked down by the next roll. So in frame 3above, the score is 10 (the total number knocked down) plus a bonus of 5 (thenumber of pins knocked down on the next roll.)A strike is when the player knocks down all 10 pins on his first try. The bonusfor 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 extraballs to complete the frame. However no more than three balls can be rolled intenth frame.The Requirements.+ roll(pins : int)+ score() : intGame• 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.2A quick design session+ roll(pins : int)+ score() : intGameClearly we need the Game class.A quick design session+ roll(pins : int)+ score() : intGame Frame10A game has 10 frames.A quick design session+ roll(pins : int)+ score() : intGame Frame- pins : intRoll10 1..2A frame has 1 or two rolls.3A quick design session+ roll(pins : int)+ score() : intGame FrameTenth Frame- pins : intRoll10 1..21The tenth frame has two or three rolls.It is different from all the other frames.A quick design session+ roll(pins : int)+ score() : intGame+ score() : intFrameTenth Frame- pins : intRoll10 1..21The score function mustiterate through all theframes, and calculateall their scores.A quick design session+ roll(pins : int)+ score() : intGame+ score() : intFrameTenth Frame- pins : intRoll10 1..21next frameThe score for a spare or a strike depends on the frame’s successor4Begin.• Create a project named BowlingGame• Create a unit test named BowlingGameTestimport junit.framework.TestCase;public class BowlingGameTest extends TestCase {}Begin.• Create a project named BowlingGame• Create a unit test named BowlingGameTestimport junit.framework.TestCase;public class BowlingGameTest extends TestCase {}Execute this program and verify that you get the following error:No tests found in BowlingGameTestThe first test.import junit.framework.TestCase;public class BowlingGameTest extends TestCase {public void testGutterGame() throws Exception {Game g = new Game();}}5The 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 {}6The 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>7The 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 duplicated8The 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 duplicatedexpected:<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


View Full Document

UNM CS 580 - CS 580 Bowling Game Kata

Download CS 580 Bowling Game Kata
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view CS 580 Bowling Game Kata and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view CS 580 Bowling Game Kata 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?