Marbles Game in ABAP

By | March 10, 2009 | Fun | 4,988 | 6

Marbles Game, a fun and simple variation of the famous Chinese Checkers Game!

Aim:

Try to remove all but one marble from the board. You can remove a marble by skipping over it to an empty spot. Select a marble by clicking on it and move it by selecting the spot that you want to move to.

Look:

It looks like this:

Here is the code snippet for Marbles Game:

 
REPORT  ZTEST_NP_MARBLE NO STANDARD PAGE HEADING.
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
* I N C L U D E
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
INCLUDE <ICON>.
 
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
* D A T A   D E F I N I T I O N
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
*....Types
TYPES: BEGIN OF TY_SCORE,
  SRL   TYPE I,
  UNAME TYPE SY-UNAME,
  SCORE TYPE I,
  TIME  TYPE I,
END   OF TY_SCORE.
*....Internal Tables
DATA: BEGIN OF ITAB OCCURS 0,
  C1,  C2,  C3,
  C4,  C5,  C6,
  C7,  C8,  C9,
END   OF ITAB.
DATA: BEGIN OF IT_PRINT OCCURS 0,
  C1(4), C2(4), C3(4),
  C4(4), C5(4), C6(4),
  C7(4), C8(4), C9(4),
END   OF IT_PRINT.
DATA: IT_SCORE TYPE STANDARD TABLE OF TY_SCORE.
*....Work Areas
DATA: WA_ITAB  LIKE ITAB,
      WA_PRINT LIKE IT_PRINT,
      W_SCORE  TYPE TY_SCORE.
*....Globle Variables
DATA: L_NO_M      TYPE I,
      L_NO_S      TYPE I,
      L_NO_C      TYPE CHAR10,
      L_HALF      TYPE I,
      L_HALF_HALF TYPE I,
      L_PRINT     TYPE FLAG,
      L_NAME      TYPE CHAR20,
      L_MOD       TYPE I.
DATA: W_ON        TYPE FLAG,
      W_SEL_LINE  TYPE I,
      W_SEL_COL   TYPE CHAR1,
      W_DEST_OK   TYPE FLAG,
      W_DEST_LINE TYPE I,
      W_DEST_COL  TYPE CHAR1,
      W_FIELD     TYPE CHAR20,
      W_LINE      TYPE I,
      W_GAME_OVER TYPE FLAG,
      W_TOTAL     TYPE I,
      W_REM       TYPE I,
      W_GONE      TYPE I,
      W_ST_TIME   TYPE I,
      W_END_TIME  TYPE I,
      W_EXPORTED  TYPE FLAG.
*....Field symbols
FIELD-SYMBOLS: <F> TYPE ANY.
*....Constants
CONSTANTS: ICON_0(40) TYPE C VALUE ICON_WD_RADIO_BUTTON_EMPTY,
ICON_1(40) TYPE C VALUE ICON_RADIOBUTTON,
ICON_2(40) TYPE C VALUE ICON_COLOR.
*.. Some systems don't have above listed ICONs You can use:
**....Constants
*CONSTANTS: ICON_0(40) TYPE C VALUE ICON_AVERAGE,      " ICON_WD_RADIO_BUTTON_EMPTY,
*           ICON_1(40) TYPE C VALUE ICON_POSITIVE,     " ICON_RADIOBUTTON,
*           ICON_2(40) TYPE C VALUE ICON_COLOR.
 
*....Ranges
RANGES: R_NOT_GREY FOR ABDOCMODE-FLAG.
*....Macros
DEFINE CONV_I_C.
  &2 = &1.
  CONDENSE &2.
END-OF-DEFINITION.
 
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
* S E L E C T I O N   S C R E E N
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
PARAMETERS: P_NUM TYPE I DEFAULT 7.
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
* A T   S E L E C T I O N - S C R E E N .
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
AT SELECTION-SCREEN.
IF  P_NUM GT 9
OR  P_NUM LT 5.
  MESSAGE E398(00) WITH 'Currently allowed only: 5, 7, 9'.
ENDIF.
L_MOD = P_NUM MOD 2.
IF L_MOD = 0.
  MESSAGE E398(00) WITH 'Only odd numbers are allowed'.
ENDIF.
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
* S T A R T   O F   S E L E C T I O N
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
START-OF-SELECTION.
GET TIME FIELD W_ST_TIME.
PERFORM FILL_MARBLES.
PERFORM FILL_PRINT_TABLE.
PERFORM WRITE_MARBLES.
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
* A T   L I N E   S E L E C T I O N
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
AT LINE-SELECTION.
GET CURSOR FIELD W_FIELD.
PERFORM CALCULATE_MARBLES.
PERFORM FILL_PRINT_TABLE.
SY-LSIND = 0.
PERFORM WRITE_MARBLES.
PERFORM CHECK_GAME_OVER.
*&---------------------------------------------------------------------*
*&      Form  fill_marbles
*&---------------------------------------------------------------------*
*       Fillup the initial table for the marbles
*----------------------------------------------------------------------*
FORM FILL_MARBLES .
* Grey cells
  L_HALF = FLOOR( P_NUM / 2 ).
  L_HALF_HALF = L_HALF / 2.
  R_NOT_GREY-SIGN   = 'I'.
  R_NOT_GREY-OPTION = 'BT'.
  R_NOT_GREY-LOW  = ( L_HALF - L_HALF_HALF ) + 1.
  R_NOT_GREY-HIGH = ( L_HALF + L_HALF_HALF ) - 1.
  APPEND R_NOT_GREY.
  CLEAR  R_NOT_GREY.
* Filling up the table
  DO P_NUM TIMES.
    L_NO_M = SY-INDEX.
    NEW-LINE.
    DO P_NUM TIMES.
      L_NO_S = SY-INDEX.
      CLEAR: L_PRINT.
      IF L_NO_M IN R_NOT_GREY.
        L_PRINT = 'X'.
      ENDIF.
      IF L_NO_S IN R_NOT_GREY.
        L_PRINT = 'X'.
      ENDIF.
      IF L_PRINT = 'X'.
        CONV_I_C L_NO_S L_NO_C.
        CONCATENATE 'WA_ITAB-C' L_NO_C INTO L_NAME.
        ASSIGN (L_NAME) TO <F>.
        IF  L_NO_S = L_HALF
        AND L_NO_M = L_HALF.
          <F> = '0'.
          W_TOTAL = W_TOTAL - 1.
        ELSE.
          <F> = '1'.
          W_TOTAL = W_TOTAL + 1.
        ENDIF.
      ELSE.
        WRITE: ' '.
      ENDIF.
    ENDDO.
    APPEND WA_ITAB TO ITAB.
    CLEAR  WA_ITAB.
  ENDDO.
  W_REM = W_TOTAL.
ENDFORM.                    " fill_marbles
*&---------------------------------------------------------------------*
*&      Form  fill_print_Table
*&---------------------------------------------------------------------*
*       Convert ITAB value to PRINT table value .
*----------------------------------------------------------------------*
FORM FILL_PRINT_TABLE .
  FIELD-SYMBOLS: <F1> TYPE ANY.
  REFRESH IT_PRINT.
  LOOP AT ITAB INTO WA_ITAB.
    L_NO_M = SY-INDEX.
    DO P_NUM TIMES.
      CONV_I_C SY-INDEX L_NO_C.
      CLEAR L_NAME.
      CONCATENATE 'WA_ITAB-C' L_NO_C INTO L_NAME.
      ASSIGN (L_NAME) TO <F>.
      CLEAR L_NAME.
      CONCATENATE 'WA_PRINT-C' L_NO_C INTO L_NAME.
      ASSIGN (L_NAME) TO <F1>.
      CASE <F>.
      WHEN '1'.
        <F1> = ICON_1.
      WHEN '0'.
        <F1> = ICON_0.
      WHEN '2'.
        <F1> = ICON_2.
      WHEN OTHERS.
      ENDCASE.
    ENDDO.
    APPEND WA_PRINT TO IT_PRINT.
    CLEAR  WA_PRINT.
  ENDLOOP.
ENDFORM.                    " fill_print_Table
*&---------------------------------------------------------------------*
*&      Form  write_marbles
*&---------------------------------------------------------------------*
*       Write marbles from the PRINT table
*----------------------------------------------------------------------*
FORM WRITE_MARBLES .
  FIELD-SYMBOLS: <F1> TYPE ANY.
  IF W_GAME_OVER IS INITIAL.
    LOOP AT IT_PRINT INTO WA_PRINT.
      SKIP 1.
      W_LINE = SY-TABIX.
      WRITE: (2) W_LINE.
      HIDE  W_LINE.
      DO P_NUM TIMES.
        CONV_I_C SY-INDEX L_NO_C.
        CLEAR L_NAME.
        CONCATENATE 'WA_PRINT-C' L_NO_C INTO L_NAME.
        ASSIGN (L_NAME) TO <F1>.
        IF NOT <F1> IS INITIAL.
          WRITE: (2) <F1> AS ICON HOTSPOT ON, (2) ' '.
        ELSE.
          WRITE: (2) ' ', (2) ' '.
        ENDIF.
      ENDDO.
    ENDLOOP.
  ENDIF.
  SKIP 4.
  WRITE: /(30) 'Total Marbles:',    W_TOTAL.
  WRITE: /(30) 'Remaining Marbles', W_REM.
  SKIP 4.
  PERFORM WRITE_5_HIGH_SCORE.
ENDFORM.                    " write_marbles
*&---------------------------------------------------------------------*
*&      Form  calculate_marbles
*&---------------------------------------------------------------------*
*       Calculate the marbles after the user input in line selection
*----------------------------------------------------------------------*
FORM CALCULATE_MARBLES .
* No marble has been selected
  IF W_ON IS INITIAL.
    PERFORM VALIDATE_INPUT.
  ELSE.
* remove the seleced marble
    PERFORM DESELECT_MARBLE.
    IF W_ON = 'X'.
*     Check destination cell, if the same marble has not been selected
      PERFORM CHECK_DESTINATION.
    ENDIF.
  ENDIF.
* Destination is ok ..? rearrange the marbles in ITAB
  IF W_DEST_OK = 'X'.
    PERFORM REARRANGE_MARBLES.
  ENDIF.
ENDFORM.                    " calculate_marbles
*&---------------------------------------------------------------------*
*&      Form  validate_input
*&---------------------------------------------------------------------*
*       Validating the selected marble, is it movable or not
*       if marble is movable, highlight it
*----------------------------------------------------------------------*
FORM VALIDATE_INPUT .
  DATA: L_SEL_FIELD(20),
        L_TMP_FIELD(20),
        L_SEL_COL(1),
        L_TMP_COL(2),
        L_TMP_LINE TYPE I,
        L_OK TYPE FLAG.
  FIELD-SYMBOLS: <F1> TYPE ANY,
  <F2> TYPE ANY.
  READ TABLE ITAB INTO WA_ITAB INDEX W_LINE.
  L_SEL_FIELD = W_FIELD.
  L_SEL_COL   = W_FIELD+10(1).
  REPLACE 'PRINT' INTO L_SEL_FIELD WITH 'ITAB'.
  CONDENSE L_SEL_FIELD.
  ASSIGN (L_SEL_FIELD) TO <F1>.
* value = 0 >> No marble
  IF <F1> = '0'.
    MESSAGE S398(00) WITH 'No marble to select.!'.
    EXIT.
  ENDIF.
* Check right
  L_TMP_COL = L_SEL_COL + 2.
  IF L_TMP_COL < 9.
    CONCATENATE 'WA_ITAB-C' L_TMP_COL INTO L_TMP_FIELD.
    CONDENSE L_TMP_FIELD.
    ASSIGN (L_TMP_FIELD) TO <F2>.
    IF <F2> = '0'.
      L_OK = 'X'.
    ENDIF.
  ENDIF.
* Check left
  IF L_OK IS INITIAL.
    L_TMP_COL = L_SEL_COL - 2.
    IF L_TMP_COL > 0.
      CONCATENATE 'WA_ITAB-C' L_TMP_COL INTO L_TMP_FIELD.
      CONDENSE L_TMP_FIELD.
      ASSIGN (L_TMP_FIELD) TO <F2>.
      IF <F2> = '0'.
        L_OK = 'X'.
      ENDIF.
    ENDIF.
  ENDIF.
* check Above
  IF L_OK IS INITIAL.
    L_TMP_LINE = W_LINE - 2.
    IF L_TMP_LINE > 0.
      READ TABLE ITAB INTO WA_ITAB INDEX L_TMP_LINE.
      IF <F1> = '0'.
        L_OK = 'X'.
      ENDIF.
      READ TABLE ITAB INTO WA_ITAB INDEX W_LINE.
    ENDIF.
  ENDIF.
* Check underneath
  IF L_OK IS INITIAL.
    L_TMP_LINE = W_LINE + 2.
    IF L_TMP_LINE < 9.
      READ TABLE ITAB INTO WA_ITAB INDEX L_TMP_LINE.
      IF <F1> = '0'.
        L_OK = 'X'.
      ENDIF.
      READ TABLE ITAB INTO WA_ITAB INDEX W_LINE.
    ENDIF.
  ENDIF.
* Ok .. than ON
  IF L_OK = 'X'.
    W_ON = 'X'.
    <F1> = '2'.
    MODIFY ITAB FROM WA_ITAB INDEX W_LINE.
    W_SEL_LINE = W_LINE.
    W_SEL_COL  = L_SEL_COL .
  ELSE.
    MESSAGE S398(00) WITH 'No marble to select.!' ' ' ' ' ' '.
  ENDIF.
ENDFORM.                    " validate_input
*&---------------------------------------------------------------------*
*&      Form  deselect_marble
*&---------------------------------------------------------------------*
*       Deselect the marble if the same marble is selected again
*----------------------------------------------------------------------*
FORM DESELECT_MARBLE .
  DATA: L_SEL_FIELD(20).
  FIELD-SYMBOLS: <F1> TYPE ANY.
  READ TABLE ITAB INTO WA_ITAB INDEX W_LINE.
  L_SEL_FIELD = W_FIELD.
  REPLACE 'PRINT' INTO L_SEL_FIELD WITH 'ITAB'.
  CONDENSE L_SEL_FIELD.
  ASSIGN (L_SEL_FIELD) TO <F1>.
  IF <F1> = '2'.
    <F1> = '1'.
    MODIFY ITAB FROM WA_ITAB INDEX W_LINE.
    MESSAGE S398(00) WITH 'Marble was deselected..!!'.
    CLEAR: W_ON, W_SEL_LINE, W_SEL_COL.
  ENDIF.
ENDFORM.                    " deselect_marble
*&---------------------------------------------------------------------*
*&      Form  check_destination
*&---------------------------------------------------------------------*
*       Check the destination cell, it should not be empty and distnce
*       between selected cell and destination cell must be 2
*----------------------------------------------------------------------*
FORM CHECK_DESTINATION .
  DATA: L_DEST_FIELD(20),
        L_DEST_COL(1),
        L_DEST_NOT_OK TYPE FLAG,
        L_TMP_LINE TYPE I,
        L_TMP_COL  TYPE C.
  DATA: L_ITAB_DEST LIKE ITAB.
  FIELD-SYMBOLS: <F1> TYPE ANY.
  READ TABLE ITAB INTO L_ITAB_DEST INDEX W_LINE.
  L_DEST_FIELD = W_FIELD.
  L_DEST_COL   = W_FIELD+10(1).
  REPLACE 'WA_PRINT' INTO L_DEST_FIELD WITH 'L_ITAB_DEST'.
  CONDENSE L_DEST_FIELD.
  ASSIGN (L_DEST_FIELD) TO <F1>.
* Destination should be empty
  IF <F1> <> '0'.
    L_DEST_NOT_OK = 'X'.
  ENDIF.
* Calcualate the distance between selected marble and destination
  IF L_DEST_NOT_OK IS INITIAL.
    IF W_SEL_LINE <> W_LINE.
      L_TMP_LINE = ABS( W_SEL_LINE - W_LINE ).
      IF L_TMP_LINE <> '2'.
        L_DEST_NOT_OK = 'X'.
      ENDIF.
    ENDIF.
  ENDIF.
  IF L_DEST_NOT_OK IS INITIAL.
    IF W_SEL_COL <> L_DEST_COL.
      L_TMP_COL = ABS( W_SEL_COL - L_DEST_COL ).
      IF L_TMP_COL <> '2'.
        L_DEST_NOT_OK = 'X'.
      ENDIF.
    ENDIF.
  ENDIF.
* destination not ok
  IF L_DEST_NOT_OK = 'X'.
    MESSAGE S398(00) WITH 'Destination is not GOOD' ' ' ' ' ' '.
  ELSE.
    W_DEST_OK = 'X'.
    W_DEST_LINE = W_LINE.
    W_DEST_COL  = L_DEST_COL.
  ENDIF.
ENDFORM.                    " check_destination
*&---------------------------------------------------------------------*
*&      Form  rearrange_marbles
*&---------------------------------------------------------------------*
*       Rearrange marbles
*     1 Remove the marble which is inbetween the selected & destination
*     2 Remove the marble from the selected cell
*     3 Put marble on the destination cell
*----------------------------------------------------------------------*
FORM REARRANGE_MARBLES .
  DATA: L_FIELD(20),
        L_TMP_LINE TYPE I,
        L_TMP_COL  TYPE CHAR1,
        L_NO_MOVE  TYPE FLAG,
        L_ITAB LIKE ITAB.
 
  FIELD-SYMBOLS: <F1> TYPE ANY.
* Make the inbetween column as 0 if both lines are same
  IF W_SEL_LINE = W_DEST_LINE.
    IF W_SEL_COL > W_DEST_COL.
      L_TMP_COL = W_DEST_COL + 1.
    ELSE.
      L_TMP_COL = W_SEL_COL + 1.
    ENDIF.
    READ TABLE ITAB INTO L_ITAB INDEX W_SEL_LINE.
    L_FIELD = W_FIELD.
    REPLACE 'WA_PRINT' INTO L_FIELD WITH 'L_ITAB'.
    CONDENSE L_FIELD.
    L_FIELD+8(1) = L_TMP_COL.
    ASSIGN (L_FIELD) TO <F1>.
    IF <F1> = 1.
      <F1> = '0'.
      W_GONE = W_GONE + 1.
      MODIFY ITAB FROM L_ITAB INDEX W_SEL_LINE.
    ELSE.
      L_NO_MOVE = 'X'.
    ENDIF.
    CLEAR  L_ITAB.
  ENDIF.
* Make the inbetween line as 0 if both lines are same
  IF W_SEL_COL = W_DEST_COL.
    IF W_SEL_LINE > W_DEST_LINE.
      L_TMP_LINE = W_DEST_LINE + 1.
    ELSE.
      L_TMP_LINE = W_SEL_LINE + 1.
    ENDIF.
    READ TABLE ITAB INTO L_ITAB INDEX L_TMP_LINE.
    L_FIELD = W_FIELD.
    REPLACE 'WA_PRINT' INTO L_FIELD WITH 'L_ITAB'.
    CONDENSE L_FIELD.
    L_FIELD+8(1) = W_SEL_COL.
    ASSIGN (L_FIELD) TO <F1>.
    IF <F1> = 1.
      <F1> = '0'.
      W_GONE = W_GONE + 1.
      MODIFY ITAB FROM L_ITAB INDEX L_TMP_LINE.
    ELSE.
      L_NO_MOVE = 'X'.
    ENDIF.
    CLEAR  L_ITAB.
  ENDIF.
  IF L_NO_MOVE IS INITIAL.
*   Make Destination = 1
    READ TABLE ITAB INTO L_ITAB INDEX W_DEST_LINE.
    L_FIELD = W_FIELD.
    REPLACE 'WA_PRINT' INTO L_FIELD WITH 'L_ITAB'.
    CONDENSE L_FIELD.
    L_FIELD+8(1) = W_DEST_COL.
    ASSIGN (L_FIELD) TO <F1>.
    <F1> = '1'.
    MODIFY ITAB FROM L_ITAB INDEX W_DEST_LINE.
    CLEAR  L_ITAB.
*   Make Selected = 0.
    READ TABLE ITAB INTO L_ITAB INDEX W_SEL_LINE.
    L_FIELD = W_FIELD.
    REPLACE 'WA_PRINT' INTO L_FIELD WITH 'L_ITAB'.
    CONDENSE L_FIELD.
    L_FIELD+8(1) = W_SEL_COL.
    ASSIGN (L_FIELD) TO <F1>.
    <F1> = '0'.
    MODIFY ITAB FROM L_ITAB INDEX W_SEL_LINE.
    CLEAR  L_ITAB.
  ELSE.
*   Make Selected = 1 when no movement
    READ TABLE ITAB INTO L_ITAB INDEX W_SEL_LINE.
    L_FIELD = W_FIELD.
    REPLACE 'WA_PRINT' INTO L_FIELD WITH 'L_ITAB'.
    CONDENSE L_FIELD.
    L_FIELD+8(1) = W_SEL_COL.
    ASSIGN (L_FIELD) TO <F1>.
    <F1> = '1'.
    MODIFY ITAB FROM L_ITAB INDEX W_SEL_LINE.
    CLEAR  L_ITAB.
  ENDIF.
  W_REM = W_TOTAL - W_GONE.
  CLEAR: W_DEST_OK, W_DEST_LINE, W_DEST_COL,
  W_ON,      W_SEL_LINE,  W_SEL_COL.
 
ENDFORM.                    " rearrange_marbles
*&---------------------------------------------------------------------*
*&      Form  check_game_over
*&---------------------------------------------------------------------*
*       Check game over when the remaining marbles are half than
*         the original marbles. Check for all cells with the marbles and
*         check adjacent cells (right, left, above and underneath cells)
*         with the value. If the marble found in any adjacent cell than
*         GAME is NOT OVER
*----------------------------------------------------------------------*
FORM CHECK_GAME_OVER .
  STATICS: L_TOT_HALF TYPE I.
  DATA: L_ITAB     LIKE ITAB,
        L_ITAB_TMP LIKE ITAB.
  DATA: L_TMP_FIELD(20),
        L_SEL_COL(1),
        L_TMP_COL(2),
        L_OK       TYPE FLAG,
        L_LINE     TYPE I,
        L_TMP_LINE TYPE I.
  CLEAR: L_OK.
  FIELD-SYMBOLS: <F1> TYPE ANY,
  <F2> TYPE ANY.
  CHECK W_ON IS INITIAL.
  L_TOT_HALF = ABS( W_TOTAL / 2 ).
  CHECK W_REM < L_TOT_HALF.
  LOOP AT ITAB INTO L_ITAB.
    L_LINE = SY-TABIX.
    L_ITAB_TMP = L_ITAB.
    DO P_NUM TIMES.
      L_ITAB = L_ITAB_TMP.
      L_SEL_COL = SY-INDEX.
      CONV_I_C SY-INDEX L_NO_C.
      CLEAR L_NAME.
      CONCATENATE 'L_ITAB-C' L_NO_C INTO L_NAME.
      ASSIGN (L_NAME) TO <F1>.
      IF <F1> IS INITIAL
      OR <F1> = '0'.
        CONTINUE.
      ENDIF.
*      IF <F1> = '1'.
*        l_ok = 'X'.
*        exit.
*      ENDIF.
*     right neighbour
      L_TMP_COL = L_SEL_COL + 1.
      IF L_TMP_COL < 9.
        CONCATENATE 'L_ITAB-C' L_TMP_COL INTO L_TMP_FIELD.
        CONDENSE L_TMP_FIELD.
        ASSIGN (L_TMP_FIELD) TO <F2>.
        IF <F2> = '1'.
          L_TMP_COL = L_SEL_COL + 2.
          IF L_TMP_COL < 9.
            CONCATENATE 'L_ITAB-C' L_TMP_COL INTO L_TMP_FIELD.
            CONDENSE L_TMP_FIELD.
            ASSIGN (L_TMP_FIELD) TO <F2>.
            IF <F2> = '0'.
              L_OK = 'X'.
              EXIT.
            ENDIF.
          ENDIF.
        ENDIF.
      ENDIF.
*     Check left
      L_TMP_COL = L_SEL_COL - 1.
      IF L_TMP_COL > 0.
        CONCATENATE 'L_ITAB-C' L_TMP_COL INTO L_TMP_FIELD.
        CONDENSE L_TMP_FIELD.
        ASSIGN (L_TMP_FIELD) TO <F2>.
        IF <F2> = '1'.
          L_TMP_COL = L_SEL_COL - 2.
          IF L_TMP_COL > 0.
            CONCATENATE 'L_ITAB-C' L_TMP_COL INTO L_TMP_FIELD.
            CONDENSE L_TMP_FIELD.
            ASSIGN (L_TMP_FIELD) TO <F2>.
            IF <F2> = '0'.
              L_OK = 'X'.
              EXIT.
            ENDIF.
          ENDIF.
        ENDIF.
      ENDIF.
      CONCATENATE 'L_ITAB-C' L_NO_C INTO L_TMP_FIELD.
      CONDENSE L_TMP_FIELD.
      ASSIGN (L_TMP_FIELD) TO <F2>.
*     check Above
      L_TMP_LINE = L_LINE - 1.
      IF L_TMP_LINE > 0.
        CLEAR L_ITAB.
        READ TABLE ITAB INTO L_ITAB INDEX L_TMP_LINE.
        IF <F2> = '1'.
          L_TMP_LINE = L_LINE - 2.
          IF L_TMP_LINE > 0.
            CLEAR L_ITAB.
            READ TABLE ITAB INTO L_ITAB INDEX L_TMP_LINE.
            IF <F2> = '0'.
              L_OK = 'X'.
              EXIT.
            ENDIF.
          ENDIF.
        ENDIF.
      ENDIF.
*     Check underneath
      L_TMP_LINE = L_LINE + 1.
      IF L_TMP_LINE < 9.
        CLEAR L_ITAB.
        READ TABLE ITAB INTO L_ITAB INDEX L_TMP_LINE.
        IF <F2> = '1'.
          L_TMP_LINE = L_LINE + 2.
          IF L_TMP_LINE < 9.
            CLEAR L_ITAB.
            READ TABLE ITAB INTO L_ITAB INDEX L_TMP_LINE.
            IF <F2> = '0'.
              L_OK = 'X'.
              EXIT.
            ENDIF.
          ENDIF.
        ENDIF.
      ENDIF.
    ENDDO.
    IF L_OK = 'X'.
      EXIT.
    ENDIF.
  ENDLOOP.
  IF L_OK IS INITIAL.
    W_GAME_OVER = 'X'.
    MESSAGE S398(00) WITH 'Game Over. Socre:' W_REM.
    PERFORM EXPORT_HIGH_SCORE.
*    leave program.
  ENDIF.
ENDFORM.                    " check_game_over
*&---------------------------------------------------------------------*
*&      Form  export_high_score
*&---------------------------------------------------------------------*
*       Export High Score to memory when the game is over
*----------------------------------------------------------------------*
FORM EXPORT_HIGH_SCORE .
  DATA: L_TIME TYPE I.
  CHECK W_EXPORTED IS INITIAL.
  GET TIME FIELD W_END_TIME.
  L_TIME = W_END_TIME - W_ST_TIME.
  W_SCORE-UNAME = SY-UNAME.
  W_SCORE-SCORE = W_REM.
  W_SCORE-TIME  = L_TIME.
  APPEND W_SCORE TO IT_SCORE.
  SORT IT_SCORE BY SCORE TIME.
  LOOP AT IT_SCORE INTO W_SCORE.
    W_SCORE-SRL = SY-TABIX.
    MODIFY IT_SCORE FROM W_SCORE.
    CLEAR  W_SCORE.
  ENDLOOP.
  DELETE IT_SCORE WHERE SRL > 5.
  EXPORT IT_SCORE = IT_SCORE TO DATABASE INDX(ZZ)
  ID 'ZGAME_MAR'.
  W_EXPORTED = 'X'.
ENDFORM.                    " export_high_score
*&---------------------------------------------------------------------*
*&      Form  write_5_high_score
*&---------------------------------------------------------------------*
*       Write 5 high scores
*----------------------------------------------------------------------*
FORM WRITE_5_HIGH_SCORE .
  IMPORT IT_SCORE = IT_SCORE FROM DATABASE INDX(ZZ)
  ID 'ZGAME_MAR'.
  WRITE: /(12) 'User',
  (10) 'Score' RIGHT-JUSTIFIED ,
  (10) 'Time'  RIGHT-JUSTIFIED.
  WRITE: /(34) SY-ULINE.
  LOOP AT IT_SCORE INTO W_SCORE.
    WRITE: /(12) W_SCORE-UNAME,
    (10) W_SCORE-SCORE,
    (10) W_SCORE-TIME.
  ENDLOOP.
ENDFORM.                    " write_5_high_score
 

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{274 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 274 articles.

Load comments

6 Comments

  • Anonymous

    Good one. My score.

    User Score Time

    KUMAR.P 2 181
    KUMAR.P 4 930
    KUMAR.P 5 756
    KUMAR.P 6 80

  • Hello Anonymous,

    Congrats for the good score. Keep it up.

    Regards,
    Naimesh Patel

  • This is the best code I have ever seen in Abap era. Fun to play, thank you for sharing.

  • U R great

  • Anonymous

    User Score Time

    RBABU 3 99

  • Anonymous

    User Score Time

    RBABU 1 58
    RBABU 3 99
    RBABU 5 36
    RBABU 7 135

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.