001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003: package eg.bowling;
004:
005: import junit.framework.TestCase;
006:
007: public class BowlingGameTest extends TestCase {
008: private Bowling g;
009:
010: protected void setUp() throws Exception {
011: g = new BowlingGame();
012: }
013:
014: public void testGutterGame() throws Exception {
015: rollMany(20, 0);
016: assertEquals(0, g.score(10));
017: }
018:
019: public void testAllOnes() throws Exception {
020: rollMany(20, 1);
021: assertEquals(20, g.score(10));
022: }
023:
024: public void testSpare() throws Exception {
025: g.roll(5);
026: g.roll(5); // spare
027: g.roll(3);
028: rollMany(17, 0);
029: assertEquals(16, g.score(10));
030: }
031:
032: public void testStrike() throws Exception {
033: g.roll(10); // strike
034: g.roll(3);
035: g.roll(5);
036: rollMany(16, 0);
037: assertEquals(26, g.score(10));
038: }
039:
040: public void testPerfectGame() throws Exception {
041: rollMany(12, 10);
042: assertEquals(300, g.score(10));
043: }
044:
045: private void rollMany(int n, int pins) {
046: for (int i = 0; i < n; i++)
047: g.roll(pins);
048: }
049:
050: public void testCurrentFrameStartsAtOne() throws Exception {
051: assertEquals(1, g.currentFrame());
052: }
053:
054: public void testCurrentFrameNoStrike() throws Exception {
055: g.roll(1);
056: assertEquals(1, g.currentFrame());
057: g.roll(2);
058: assertEquals(2, g.currentFrame());
059: }
060:
061: public void testCurrentFrameNeverExceedsTen() throws Exception {
062: rollMany(20, 0);
063: assertEquals(10, g.currentFrame());
064: }
065:
066: public void testCurrentBallStartsAtOne() throws Exception {
067: assertEquals(1, g.currentBall());
068: }
069:
070: public void testCurrentBallNoStrike() throws Exception {
071: g.roll(1);
072: assertEquals(2, g.currentBall());
073: g.roll(1);
074: assertEquals(1, g.currentBall());
075: }
076:
077: public void testCurrentBallZeroWhenGameOver() throws Exception {
078: rollMany(20, 0);
079: assertEquals(0, g.currentBall());
080: }
081:
082: public void testScoreableFrameStartsAtZero() throws Exception {
083: assertEquals(0, g.scoreableFrame());
084: }
085:
086: public void testScoreableFrameNoMarks() throws Exception {
087: g.roll(1);
088: assertEquals(0, g.scoreableFrame());
089: g.roll(1);
090: assertEquals(1, g.scoreableFrame());
091: rollMany(18, 1);
092: assertEquals(10, g.scoreableFrame());
093: }
094:
095: public void testScoreableFrameForSpare() throws Exception {
096: g.roll(5);
097: g.roll(5);
098: assertEquals(0, g.scoreableFrame());
099: g.roll(3);
100: assertEquals(1, g.scoreableFrame());
101: g.roll(4);
102: assertEquals(2, g.scoreableFrame());
103: }
104:
105: public void testScoreableFrameForTenthFrameSpare() throws Exception {
106: rollMany(18, 0);
107: g.roll(5);
108: g.roll(5); //tenth frame spare
109: assertEquals(10, g.currentFrame());
110: assertEquals(3, g.currentBall());
111: assertEquals(9, g.scoreableFrame());
112: assertEquals(false, g.gameOver());
113: g.roll(5); // last ball;
114: assertEquals(10, g.currentFrame());
115: assertEquals(0, g.currentBall());
116: assertEquals(10, g.scoreableFrame());
117: assertTrue(g.gameOver());
118: }
119:
120: public void testGameNotOver() throws Exception {
121: assertEquals(false, g.gameOver());
122: }
123:
124: public void testGameOverNoMarks() throws Exception {
125: for (int i = 0; i < 20; i++) {
126: assertEquals(false, g.gameOver());
127: g.roll(0);
128: }
129: assertEquals(true, g.gameOver());
130: }
131:
132: public void testCurrentFrameForStrike() throws Exception {
133: g.roll(10); // strike
134: assertEquals(2, g.currentFrame());
135: assertEquals(1, g.currentBall());
136: assertEquals(0, g.scoreableFrame());
137: g.roll(3);
138: assertEquals(2, g.currentFrame());
139: assertEquals(2, g.currentBall());
140: assertEquals(0, g.scoreableFrame());
141: g.roll(3);
142: assertEquals(3, g.currentFrame());
143: assertEquals(1, g.currentBall());
144: assertEquals(2, g.scoreableFrame());
145: }
146:
147: public void testStrikeAfterSpare() throws Exception {
148: g.roll(5);
149: g.roll(5);
150: g.roll(10);
151: assertEquals(3, g.currentFrame());
152: assertEquals(1, g.currentBall());
153: assertEquals(1, g.scoreableFrame());
154: }
155:
156: public void testManyStrikes() throws Exception {
157: g.roll(10);
158: g.roll(10);
159: assertEquals(3, g.currentFrame());
160: assertEquals(1, g.currentBall());
161: assertEquals(0, g.scoreableFrame());
162: rollMany(7, 10); // ninth frame
163: assertEquals(10, g.currentFrame());
164: assertEquals(1, g.currentBall());
165: assertEquals(7, g.scoreableFrame());
166: g.roll(10);
167: assertEquals("", 10, g.currentFrame());
168: assertEquals("", 2, g.currentBall());
169: assertEquals("", 8, g.scoreableFrame());
170: g.roll(10);
171: assertEquals("", 3, g.currentBall());
172: assertEquals("", 9, g.scoreableFrame());
173: g.roll(10);
174: assertEquals("", 0, g.currentBall());
175: assertEquals("", 10, g.scoreableFrame());
176: assertTrue(g.gameOver());
177: }
178:
179: public void testFinalStrike() throws Exception {
180: rollMany(18, 0);
181: g.roll(5);
182: g.roll(5);
183: g.roll(10);
184: assertEquals("", true, g.gameOver());
185: assertEquals("", 0, g.currentBall());
186: assertEquals("", 10, g.scoreableFrame());
187: }
188:
189: public void testFourFinalStrikes() throws Exception {
190: rollMany(16, 0);
191: g.roll(10);
192: assertEquals("", 10, g.currentFrame());
193: assertEquals("", 1, g.currentBall());
194: assertEquals("", 8, g.scoreableFrame());
195: g.roll(10);
196: assertEquals("", 10, g.currentFrame());
197: assertEquals("", 2, g.currentBall());
198: assertEquals("", 8, g.scoreableFrame());
199: g.roll(10);
200: assertEquals("", 10, g.currentFrame());
201: assertEquals("", 3, g.currentBall());
202: assertEquals("", 9, g.scoreableFrame());
203: g.roll(10);
204: assertEquals("", 10, g.currentFrame());
205: assertEquals("", 0, g.currentBall());
206: assertEquals("", 10, g.scoreableFrame());
207: assertEquals("", true, g.gameOver());
208: }
209:
210: public void testThreeFinalStrikes() throws Exception {
211: rollMany(18, 0);
212: g.roll(10);
213: assertEquals("", 10, g.currentFrame());
214: assertEquals("", 2, g.currentBall());
215: assertEquals("", 9, g.scoreableFrame());
216: g.roll(10);
217: assertEquals("", 10, g.currentFrame());
218: assertEquals("", 3, g.currentBall());
219: assertEquals("", 9, g.scoreableFrame());
220: g.roll(10);
221: assertEquals("", 10, g.currentFrame());
222: assertEquals("", 0, g.currentBall());
223: assertEquals("", 10, g.scoreableFrame());
224: assertEquals("", true, g.gameOver());
225: }
226: }
|