'From Squeakland 3.8-05 of 7 September 2005 [latest update: #527] on 22 November 2006 at 1:09:58 pm'! Object subclass: #Game instanceVariableNames: 'itsCurrentFrame itsScorer firstThrowInFrame' classVariableNames: '' poolDictionaries: '' category: 'BowlingScore-TDD'! Object subclass: #Scorer instanceVariableNames: 'itsThrows itsCurrentThrow ball' classVariableNames: '' poolDictionaries: '' category: 'BowlingScore-TDD'! TestCase subclass: #TestGame instanceVariableNames: 'g' classVariableNames: '' poolDictionaries: '' category: 'BowlingScore-TDD-Test'! !Game methodsFor: 'accessing' stamp: 'sumim 11/22/2006 11:59'! score ^ self scoreForFrame: itsCurrentFrame! ! !Game methodsFor: 'accessing' stamp: 'sumim 11/22/2006 12:53'! scoreForFrame: theFrame ^ itsScorer scoreForFrame: theFrame! ! !Game methodsFor: 'adding' stamp: 'sumim 11/22/2006 12:00'! add: pins itsScorer addThrow: pins. self adjustCurrentFrame: pins! ! !Game methodsFor: 'initialization' stamp: 'sumim 11/22/2006 12:49'! initialize itsCurrentFrame _ 1. firstThrowInFrame _ true. itsScorer _ Scorer new! ! !Game methodsFor: 'private' stamp: 'sumim 11/22/2006 12:42'! adjustCurrentFrame: pins (self lastBallInFrame: pins) ifTrue: [self advanceFrame] ifFalse: [firstThrowInFrame _ false]! ! !Game methodsFor: 'private' stamp: 'sumim 11/22/2006 13:09'! advanceFrame itsCurrentFrame _ 10 min: itsCurrentFrame + 1! ! !Game methodsFor: 'private' stamp: 'sumim 11/22/2006 12:43'! lastBallInFrame: pins ^ (self strike: pins) or: [firstThrowInFrame not]! ! !Game methodsFor: 'private' stamp: 'sumim 11/22/2006 12:43'! strike: pins ^ firstThrowInFrame and: [pins = 10]! ! !Scorer methodsFor: 'accessing' stamp: 'sumim 11/22/2006 12:48'! scoreForFrame: theFrame | score | ball _ 1. score _ 0. 1 to: theFrame do: [:currentFrame | self strike ifTrue: [ score _ score + 10 + self nextTwoBallsForStrike. ball _ ball + 1] ifFalse: [ self spare ifTrue: [ score _ score + 10 + self nextBallForSpare. ball _ ball + 2] ifFalse: [ score _ score + self twoBallsInFrame. ball _ ball + 2]]]. ^ score! ! !Scorer methodsFor: 'adding' stamp: 'sumim 11/22/2006 12:55'! addThrow: pins itsThrows at: itsCurrentThrow put: pins. itsCurrentThrow _ itsCurrentThrow + 1! ! !Scorer methodsFor: 'initialization' stamp: 'sumim 11/22/2006 13:08'! initialize itsThrows _ Array new: 21 withAll: 0. itsCurrentThrow _ 1! ! !Scorer methodsFor: 'private' stamp: 'sumim 11/22/2006 12:46'! nextBallForSpare ^ itsThrows at: ball + 2! ! !Scorer methodsFor: 'private' stamp: 'sumim 11/22/2006 12:15'! nextTwoBallsForStrike ^ (itsThrows at: ball + 1) + (itsThrows at: ball + 2)! ! !Scorer methodsFor: 'private' stamp: 'sumim 11/22/2006 12:14'! spare ^ (itsThrows at: ball) + (itsThrows at: ball + 1) = 10! ! !Scorer methodsFor: 'private' stamp: 'sumim 11/22/2006 12:13'! strike ^ (itsThrows at: ball) = 10! ! !Scorer methodsFor: 'private' stamp: 'sumim 11/22/2006 12:16'! twoBallsInFrame ^ (itsThrows at: ball) + (itsThrows at: ball + 1)! ! !TestGame methodsFor: 'tests' stamp: 'sumim 11/22/2006 12:26'! testEndOfArray 9 timesRepeat: [g add: 0. g add: 0]. g add: 2. g add: 8. g add: 10. self assert: 20 = g score! ! !TestGame methodsFor: 'tests' stamp: 'sumim 11/22/2006 12:21'! testFourThrowsNoMark g add: 5. g add: 4. g add: 7. g add: 2. self assert: 18 = g score. self assert: 9 = (g scoreForFrame: 1). self assert: 18 = (g scoreForFrame: 2)! ! !TestGame methodsFor: 'tests' stamp: 'sumim 11/22/2006 12:29'! testHeartBreak 11 timesRepeat: [g add: 10]. g add: 9. self assert: 299 = g score! ! !TestGame methodsFor: 'tests' stamp: 'sumim 11/22/2006 12:26'! testPerfectGame 12 timesRepeat: [g add: 10]. self assert: 300 = g score! ! !TestGame methodsFor: 'tests' stamp: 'sumim 11/22/2006 12:28'! testSampleGame #(1 4 4 5 6 4 5 5 10 0 1 7 3 6 4 10 2 8 6) do: [:each | g add: each]. self assert: 133 = g score! ! !TestGame methodsFor: 'tests' stamp: 'sumim 11/22/2006 12:23'! testSimpleFrameAfterSpare g add: 3. g add: 7. g add: 3. g add: 2. self assert: 13 = (g scoreForFrame: 1). self assert: 18 = (g scoreForFrame: 2). self assert: 18 = g score! ! !TestGame methodsFor: 'tests' stamp: 'sumim 11/22/2006 12:22'! testSimpleSpare g add: 3. g add: 7. g add: 3. self assert: 13 = (g scoreForFrame: 1)! ! !TestGame methodsFor: 'tests' stamp: 'sumim 11/22/2006 12:24'! testSimpleStrike g add: 10. g add: 3. g add: 6. self assert: 19 = (g scoreForFrame: 1). self assert: 28 = g score! ! !TestGame methodsFor: 'tests' stamp: 'sumim 11/22/2006 12:29'! testTenthFrameSpare 9 timesRepeat: [g add: 10]. g add: 9. g add: 1. g add: 1. self assert: 270 = g score! ! !TestGame methodsFor: 'tests' stamp: 'sumim 11/22/2006 12:19'! testTwoThrowsNoMark g add: 5. g add: 4. self assert: 9 = g score! ! !TestGame methodsFor: 'running' stamp: 'sumim 11/22/2006 12:18'! setUp g _ Game new! !