Skip to content

Instantly share code, notes, and snippets.

@corani
Created January 11, 2018 08:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save corani/d529f77a4f98859cfb60302c25c9b9c9 to your computer and use it in GitHub Desktop.
Save corani/d529f77a4f98859cfb60302c25c9b9c9 to your computer and use it in GitHub Desktop.
Bowling Game Kata in ABAP
CLASS zcl_bosd_bowling_kata DEFINITION
PUBLIC
FINAL
CREATE PUBLIC.
PUBLIC SECTION.
TYPES gtt_rolls TYPE STANDARD TABLE OF int1.
CLASS-METHODS score_game
IMPORTING it_rolls TYPE gtt_rolls
RETURNING VALUE(rv_score) TYPE int2.
ENDCLASS.
CLASS ZCL_BOSD_BOWLING_KATA IMPLEMENTATION.
METHOD score_game.
CLEAR rv_score.
DATA(lv_start) = 0.
" A game consists of 10 frames
DO 10 TIMES.
rv_score = rv_score + it_rolls[ lv_start + 1 ].
IF it_rolls[ lv_start + 1 ] = 10.
" Strike Bonus
rv_score = rv_score + it_rolls[ lv_start + 2 ] + it_rolls[ lv_start + 3 ].
" Strike frame has only one roll
lv_start = lv_start + 1.
ELSE.
rv_score = rv_score + it_rolls[ lv_start + 2 ].
IF it_rolls[ lv_start + 1 ] + it_rolls[ lv_start + 2 ] = 10.
" Spare Bonus
rv_score = rv_score + it_rolls[ lv_start + 3 ].
ENDIF.
" Open and Spare frames have two rolls
lv_start = lv_start + 2.
ENDIF.
ENDDO.
ENDMETHOD.
ENDCLASS.
```
```abap
CLASS zcl_bosd_bowling_aunit DEFINITION FOR TESTING
DURATION SHORT
RISK LEVEL HARMLESS.
PRIVATE SECTION.
DATA:
mt_rolls TYPE zcl_bosd_bowling_kata=>gtt_rolls,
mv_score TYPE int2.
METHODS:
setup,
all_misses FOR TESTING,
all_ones FOR TESTING,
all_open FOR TESTING,
one_spare FOR TESTING,
one_strike FOR TESTING,
all_strikes FOR TESTING,
all_spares FOR TESTING.
ENDCLASS.
CLASS zcl_bosd_bowling_aunit IMPLEMENTATION.
METHOD setup.
CLEAR: mt_rolls, mv_score.
ENDMETHOD.
METHOD all_misses.
DO 20 TIMES.
APPEND 0 TO mt_rolls.
ENDDO.
mv_score = zcl_bosd_bowling_kata=>score_game( mt_rolls ).
cl_aunit_assert=>assert_equals( msg = 'All misses'
exp = 0
act = mv_score ).
ENDMETHOD.
METHOD all_ones.
DO 20 TIMES.
APPEND 1 TO mt_rolls.
ENDDO.
mv_score = zcl_bosd_bowling_kata=>score_game( mt_rolls ).
cl_aunit_assert=>assert_equals( msg = 'All ones'
exp = 20
act = mv_score ).
ENDMETHOD.
METHOD all_open.
DO 10 TIMES.
APPEND 9 TO mt_rolls.
APPEND 0 TO mt_rolls.
ENDDO.
mv_score = zcl_bosd_bowling_kata=>score_game( mt_rolls ).
cl_aunit_assert=>assert_equals( msg = 'All 9 -'
exp = 90
act = mv_score ).
ENDMETHOD.
METHOD one_spare.
APPEND 5 TO mt_rolls.
APPEND 5 TO mt_rolls.
APPEND 3 TO mt_rolls.
DO 17 TIMES.
APPEND 0 TO mt_rolls.
ENDDO.
mv_score = zcl_bosd_bowling_kata=>score_game( mt_rolls ).
cl_aunit_assert=>assert_equals( msg = 'One Spare'
exp = 16
act = mv_score ).
ENDMETHOD.
METHOD one_strike.
APPEND 10 TO mt_rolls.
APPEND 5 TO mt_rolls.
APPEND 3 TO mt_rolls.
DO 16 TIMES.
APPEND 0 TO mt_rolls.
ENDDO.
mv_score = zcl_bosd_bowling_kata=>score_game( mt_rolls ).
cl_aunit_assert=>assert_equals( msg = 'One Strike'
exp = 26
act = mv_score ).
ENDMETHOD.
METHOD all_strikes.
DO 12 TIMES.
APPEND 10 TO mt_rolls.
ENDDO.
mv_score = zcl_bosd_bowling_kata=>score_game( mt_rolls ).
cl_aunit_assert=>assert_equals( msg = 'All Strikes'
exp = 300
act = mv_score ).
ENDMETHOD.
METHOD all_spares.
DO 21 TIMES.
APPEND 5 TO mt_rolls.
ENDDO.
mv_score = zcl_bosd_bowling_kata=>score_game( mt_rolls ).
cl_aunit_assert=>assert_equals( msg = 'All Spares'
exp = 150
act = mv_score ).
ENDMETHOD.
ENDCLASS.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment