Skip to content

Commit

Permalink
bowling: some updates and more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjbq7 committed Oct 25, 2011
1 parent 634bd5c commit 5eb7275
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
2 changes: 2 additions & 0 deletions bowling/bowling-tests.factor
Expand Up @@ -10,6 +10,8 @@ IN: bowling.tests
[ 10 ] [ "4/" score-frame ] unit-test

[ 0 ] [ "---------------------" score-game ] unit-test
[ 11 ] [ "-------------------X1-" score-game ] unit-test
[ 12 ] [ "----------------X1-" score-game ] unit-test
[ 80 ] [ "-8-7714215X6172183-" score-game ] unit-test
[ 83 ] [ "12X4--3-69/-98/8-8-" score-game ] unit-test
[ 144 ] [ "XXX6-3/819-44X6-" score-game ] unit-test
Expand Down
26 changes: 10 additions & 16 deletions bowling/bowling.factor
Expand Up @@ -27,19 +27,15 @@ ERROR: invalid-throw ;
over throw#>> = [ invalid-throw ] unless
] [ drop ] if ;

: check-strike? ( game -- game )
dup pins>> 10 = [ invalid-throw ] unless ;

: check-pins ( game n -- game n )
over pins>> dupd <= [ invalid-throw ] unless ;

: bonus ( game n -- game n' )
over bonus>> [
2 > [
[ [ 2 - ] change-bonus ] dip 3 *
] [
[ [ 1 - ] change-bonus ] dip 2 *
] if
2 >
[ [ [ 2 - ] change-bonus ] dip 3 * ]
[ [ [ 1 - ] change-bonus ] dip 2 * ]
if
] unless-zero ;

: take-pins ( game n -- game )
Expand All @@ -53,17 +49,13 @@ ERROR: invalid-throw ;
[ + ] curry change-bonus
] [ drop ] if ;

PRIVATE>

: strike ( game -- game )
0 check-throw# check-strike?
10 take-pins 2 add-bonus
next-frame ;
0 check-throw# 10 take-pins
2 add-bonus next-frame ;

: spare ( game -- game )
1 check-throw#
dup pins>> take-pins 1 add-bonus
next-frame ;
1 check-throw# dup pins>> take-pins
1 add-bonus next-frame ;

: hit ( game n -- game )
take-pins next-throw ;
Expand All @@ -76,6 +68,8 @@ PRIVATE>
[ CHAR: 0 - hit ]
} case ;

PRIVATE>

: score-frame ( str -- score )
[ <game> ] dip [ throw-ball ] each
[ frame#>> 1 assert= ] [ score>> ] bi ;
Expand Down

0 comments on commit 5eb7275

Please sign in to comment.