Program to Relax Yourself when you code in Abap i.e Play games in Abap with this code
REPORT zplay_games.
* Declarations.
DATA: v_first TYPE xfeld,
v_won TYPE xfeld.
DATA: BEGIN OF v_name,
name TYPE char4,
num TYPE char1,
END OF v_name.
DATA: v_blank_push LIKE v_name.
DATA: v_name_tmp LIKE v_name.
DATA: v_fieldname TYPE fieldname.
DATA: BEGIN OF s_pos,
s TYPE char1,
t TYPE char1,
END OF s_pos,
t_pos LIKE STANDARD TABLE OF s_pos.
FIELD-SYMBOLS:,.
* selection-screen objects.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE v_text.
SELECTION-SCREEN PUSHBUTTON /10(3) push1 USER-COMMAND push1.
SELECTION-SCREEN PUSHBUTTON 14(3) push2 USER-COMMAND push2.
SELECTION-SCREEN PUSHBUTTON 18(3) push3 USER-COMMAND push3.
SELECTION-SCREEN PUSHBUTTON /10(3) push4 USER-COMMAND push4.
SELECTION-SCREEN PUSHBUTTON 14(3) push5 USER-COMMAND push5.
SELECTION-SCREEN PUSHBUTTON 18(3) push6 USER-COMMAND push6.
SELECTION-SCREEN PUSHBUTTON /10(3) push7 USER-COMMAND push7.
SELECTION-SCREEN PUSHBUTTON 14(3) push8 USER-COMMAND push8.
SELECTION-SCREEN PUSHBUTTON 18(3) push9 USER-COMMAND push9.
SELECTION-SCREEN END OF BLOCK b1.
SELECTION-SCREEN PUSHBUTTON /10(10) output USER-COMMAND output.
SELECTION-SCREEN PUSHBUTTON 22(10) start USER-COMMAND start.
* Initialization event.
INITIALIZATION.
* Move initial text.
PERFORM move_initial_text.
* At selection-screen output.
AT SELECTION-SCREEN OUTPUT.
LOOP AT SCREEN.
* First time.
IF v_first IS INITIAL.
IF screen-name = 'PUSH9'.
screen-invisible = 1.
MODIFY SCREEN.
ENDIF.
ELSE.
IF screen-name = v_name.
screen-invisible = 1.
MODIFY SCREEN.
ENDIF.
ENDIF.
ENDLOOP.
IF v_won = 'X'.
MESSAGE i208(00) WITH 'You won'.
ENDIF.
* Set the first flag.
IF v_first IS INITIAL.
v_first = 'X'.
ENDIF.
* At selection-screen.
AT SELECTION-SCREEN.
* Check the sy-ucomm.
CASE sy-ucomm.
WHEN 'OUTPUT'.
* Show the output.
PERFORM show_output.
WHEN 'START'.
* Move the initial text.
CLEAR v_first.
PERFORM move_initial_text.
ENDCASE.
* Check if the sy-ucomm is one of the push buttons.
CHECK sy-ucomm = 'PUSH1' OR
sy-ucomm = 'PUSH2' OR
sy-ucomm = 'PUSH3' OR
sy-ucomm = 'PUSH4' OR
sy-ucomm = 'PUSH5' OR
sy-ucomm = 'PUSH6' OR
sy-ucomm = 'PUSH7' OR
sy-ucomm = 'PUSH8' OR
sy-ucomm = 'PUSH9'.
* move the sy-ucomm.
v_name_tmp = sy-ucomm.
* Check valid button.
CLEAR: s_pos.
READ TABLE t_pos INTO s_pos WITH KEY s = v_name_tmp-num
t = v_blank_push-num.
IF sy-subrc <> 0.
v_name = v_blank_push.
EXIT.
ENDIF.
* Move the user command.
v_name = sy-ucomm.
* Get the text.
CLEAR v_fieldname.
CONCATENATE 'PUSH' v_name-num INTO v_fieldname.
ASSIGN (v_fieldname) TO.
* Get the blank push value.
CLEAR v_fieldname.
CONCATENATE 'PUSH' v_blank_push-num INTO v_fieldname.
ASSIGN (v_fieldname) TO.
* Move the values.
= .
* success.
IF push1 = '1' AND
push2 = '2' AND
push3 = '3' AND
push4 = '4' AND
push5 = '5' AND
push6 = '6' AND
push7 = '7' AND
push8 = '8'.
v_won = 'X'.
ENDIF.
* Move the blank push to the current.
v_blank_push = v_name.
DEFINE pop.
clear: s_pos.
s_pos-s = &1.
s_pos-t = &2.
append s_pos to t_pos.
END-OF-DEFINITION.
*---------------------------------------------------------------------*
* FORM move_initial_text *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
FORM move_initial_text.
* Move frame title.
MOVE 'Game' TO v_text.
MOVE 'Output' TO output.
MOVE 'Start' TO start.
* Move values to the push button.
MOVE '4' TO push1.MOVE '3' TO push2.
MOVE '2' TO push3.MOVE '7' TO push4.
MOVE '5' TO push5.MOVE '6' TO push6.
MOVE '8' TO push7.MOVE '1' TO push8.
MOVE ' ' TO push9.
* Set the blank push.
v_blank_push = 'PUSH9'.
* Prepare the possible values for each place.
CLEAR: t_pos.
pop '1' '2'.pop '1' '4'.
pop '2' '1'.pop '2' '3'.pop '2' '5'.
pop '3' '2'.pop '3' '6'.
pop '4' '1'.pop '4' '5'.pop '4' '7'.
pop '5' '2'.pop '5' '4'.pop '5' '6'.pop '5' '8'.
pop '6' '3'.pop '6' '5'.pop '6' '9'.
pop '7' '4'.pop '7' '8'.
pop '8' '5'.pop '8' '7'.pop '8' '9'.
pop '9' '6'.pop '9' '8'.
ENDFORM.
*---------------------------------------------------------------------*
* FORM show_output *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
FORM show_output.
* Move values to the push button.
MOVE '1' TO push1.MOVE '2' TO push2.
MOVE '3' TO push3.MOVE '4' TO push4.
MOVE '5' TO push5.MOVE '6' TO push6.
MOVE '7' TO push7.MOVE '8' TO push8.
MOVE ' ' TO push9.
* Set the blank push.
v_blank_push = 'PUSH9'.
CLEAR v_first.
ENDFORM.
REPORT zplay_games.
* Declarations.
DATA: v_first TYPE xfeld,
v_won TYPE xfeld.
DATA: BEGIN OF v_name,
name TYPE char4,
num TYPE char1,
END OF v_name.
DATA: v_blank_push LIKE v_name.
DATA: v_name_tmp LIKE v_name.
DATA: v_fieldname TYPE fieldname.
DATA: BEGIN OF s_pos,
s TYPE char1,
t TYPE char1,
END OF s_pos,
t_pos LIKE STANDARD TABLE OF s_pos.
FIELD-SYMBOLS:
* selection-screen objects.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE v_text.
SELECTION-SCREEN PUSHBUTTON /10(3) push1 USER-COMMAND push1.
SELECTION-SCREEN PUSHBUTTON 14(3) push2 USER-COMMAND push2.
SELECTION-SCREEN PUSHBUTTON 18(3) push3 USER-COMMAND push3.
SELECTION-SCREEN PUSHBUTTON /10(3) push4 USER-COMMAND push4.
SELECTION-SCREEN PUSHBUTTON 14(3) push5 USER-COMMAND push5.
SELECTION-SCREEN PUSHBUTTON 18(3) push6 USER-COMMAND push6.
SELECTION-SCREEN PUSHBUTTON /10(3) push7 USER-COMMAND push7.
SELECTION-SCREEN PUSHBUTTON 14(3) push8 USER-COMMAND push8.
SELECTION-SCREEN PUSHBUTTON 18(3) push9 USER-COMMAND push9.
SELECTION-SCREEN END OF BLOCK b1.
SELECTION-SCREEN PUSHBUTTON /10(10) output USER-COMMAND output.
SELECTION-SCREEN PUSHBUTTON 22(10) start USER-COMMAND start.
* Initialization event.
INITIALIZATION.
* Move initial text.
PERFORM move_initial_text.
* At selection-screen output.
AT SELECTION-SCREEN OUTPUT.
LOOP AT SCREEN.
* First time.
IF v_first IS INITIAL.
IF screen-name = 'PUSH9'.
screen-invisible = 1.
MODIFY SCREEN.
ENDIF.
ELSE.
IF screen-name = v_name.
screen-invisible = 1.
MODIFY SCREEN.
ENDIF.
ENDIF.
ENDLOOP.
IF v_won = 'X'.
MESSAGE i208(00) WITH 'You won'.
ENDIF.
* Set the first flag.
IF v_first IS INITIAL.
v_first = 'X'.
ENDIF.
* At selection-screen.
AT SELECTION-SCREEN.
* Check the sy-ucomm.
CASE sy-ucomm.
WHEN 'OUTPUT'.
* Show the output.
PERFORM show_output.
WHEN 'START'.
* Move the initial text.
CLEAR v_first.
PERFORM move_initial_text.
ENDCASE.
* Check if the sy-ucomm is one of the push buttons.
CHECK sy-ucomm = 'PUSH1' OR
sy-ucomm = 'PUSH2' OR
sy-ucomm = 'PUSH3' OR
sy-ucomm = 'PUSH4' OR
sy-ucomm = 'PUSH5' OR
sy-ucomm = 'PUSH6' OR
sy-ucomm = 'PUSH7' OR
sy-ucomm = 'PUSH8' OR
sy-ucomm = 'PUSH9'.
* move the sy-ucomm.
v_name_tmp = sy-ucomm.
* Check valid button.
CLEAR: s_pos.
READ TABLE t_pos INTO s_pos WITH KEY s = v_name_tmp-num
t = v_blank_push-num.
IF sy-subrc <> 0.
v_name = v_blank_push.
EXIT.
ENDIF.
* Move the user command.
v_name = sy-ucomm.
* Get the text.
CLEAR v_fieldname.
CONCATENATE 'PUSH' v_name-num INTO v_fieldname.
ASSIGN (v_fieldname) TO
* Get the blank push value.
CLEAR v_fieldname.
CONCATENATE 'PUSH' v_blank_push-num INTO v_fieldname.
ASSIGN (v_fieldname) TO
* Move the values.
* success.
IF push1 = '1' AND
push2 = '2' AND
push3 = '3' AND
push4 = '4' AND
push5 = '5' AND
push6 = '6' AND
push7 = '7' AND
push8 = '8'.
v_won = 'X'.
ENDIF.
* Move the blank push to the current.
v_blank_push = v_name.
DEFINE pop.
clear: s_pos.
s_pos-s = &1.
s_pos-t = &2.
append s_pos to t_pos.
END-OF-DEFINITION.
*---------------------------------------------------------------------*
* FORM move_initial_text *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
FORM move_initial_text.
* Move frame title.
MOVE 'Game' TO v_text.
MOVE 'Output' TO output.
MOVE 'Start' TO start.
* Move values to the push button.
MOVE '4' TO push1.MOVE '3' TO push2.
MOVE '2' TO push3.MOVE '7' TO push4.
MOVE '5' TO push5.MOVE '6' TO push6.
MOVE '8' TO push7.MOVE '1' TO push8.
MOVE ' ' TO push9.
* Set the blank push.
v_blank_push = 'PUSH9'.
* Prepare the possible values for each place.
CLEAR: t_pos.
pop '1' '2'.pop '1' '4'.
pop '2' '1'.pop '2' '3'.pop '2' '5'.
pop '3' '2'.pop '3' '6'.
pop '4' '1'.pop '4' '5'.pop '4' '7'.
pop '5' '2'.pop '5' '4'.pop '5' '6'.pop '5' '8'.
pop '6' '3'.pop '6' '5'.pop '6' '9'.
pop '7' '4'.pop '7' '8'.
pop '8' '5'.pop '8' '7'.pop '8' '9'.
pop '9' '6'.pop '9' '8'.
ENDFORM.
*---------------------------------------------------------------------*
* FORM show_output *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
FORM show_output.
* Move values to the push button.
MOVE '1' TO push1.MOVE '2' TO push2.
MOVE '3' TO push3.MOVE '4' TO push4.
MOVE '5' TO push5.MOVE '6' TO push6.
MOVE '7' TO push7.MOVE '8' TO push8.
MOVE ' ' TO push9.
* Set the blank push.
v_blank_push = 'PUSH9'.
CLEAR v_first.
ENDFORM.
No comments:
Post a Comment