You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While working on the Minesweeper exercise i noticed, that instead of throwing the expected exception, replace just adds to the string, when the offset is out of bounds. Substring seems to ignore it.
Below is the code i was running:
CLASSzcl_minesweeper DEFINITION PUBLIC FINAL CREATE PUBLIC.
PUBLIC SECTION.
METHODS annotate
IMPORTING
!input TYPE string_table
RETURNINGVALUE(result) TYPE string_table.
PRIVATE SECTION.
ENDCLASS.
CLASSzcl_minesweeper IMPLEMENTATION.
METHODannotate.
FIELD-SYMBOLS<other_line> TYPE string.
FIELD-SYMBOLS<current_line> TYPE string.
result=input.
* Working from each mine outwards - adding 1 to each adjacent field that is not a mine LOOP ATresultASSIGNING<current_line>.
CHECK<current_line>CA'*'.
DATA(current_row) =sy-tabix.
* start one row up DOstrlen( <current_line> ) TIMES.
DATA(row) = current_row -1.
DATA(current_off) =sy-index-1.
CHECKsubstring( val =<current_line> off = current_off len =1 ) EQ'*'.
DO3TIMES.
* for each row, start one character to the left from currentDATA(off) = current_off -1.
* "one row up, same row and one row down - if row existsREAD TABLEresult INDEX row ASSIGNING<other_line>.
IFsy-subrcEQ0.
DO3TIMES.
* except current character IF row NE current_row OR off NE current_off.
* check bounds by catching exception try.
* Expecting to throw exception, when offset out of bounds. DATA(current_field) =substring( val =<other_line> off = off len =1 ). "<<<<< IF current_field NE'*'.
DATA(cn_mines) =CONV i( substring( val =<other_line> off = off len =1 ) ). "<<<<<
cn_mines = cn_mines +1.
<other_line>=replace( val =<other_line> off = off len =1 with=|{ cn_mines }| ). "<<<<< ENDIF.
catch CX_SY_RANGE_OUT_OF_BOUNDS.
* out of bounds - nothing to do endtry.
ENDIF.
off = off +1.
ENDDO.
ENDIF.
* row does not exist - try the next
row = row +1.
ENDDO.
ENDDO.
ENDLOOP.
ENDMETHOD.
ENDCLASS.
The text was updated successfully, but these errors were encountered:
While working on the Minesweeper exercise i noticed, that instead of throwing the expected exception, replace just adds to the string, when the offset is out of bounds. Substring seems to ignore it.
Below is the code i was running:
The text was updated successfully, but these errors were encountered: