Skip to Content
As you may realized…I love games…And of course I love ABAP…So I like to mix them everytime I can… Today in the morning, we hadn’t Internet access…So I couldn’t request my transport order to be send to QAS…So, I was bored with nothing else to do…I remember that yesterday I start thinking about Master Mind game…I used to play it when I was a boy…Play it on PC when I was younger…And still like it, so I decided to make up my own ABAP version of the game… First, I developed a simple Dynpro screen…imageThen, I wrote the source code…   REPORT ZABAP_MIND_GAME.  *&——————————————————– * * CONSTANTS                                                * *&——————————————————– * CONSTANTS: line_length TYPE i VALUE 254.  *&——————————————————– * * TYPES                                                    * *&——————————————————– * TYPES: BEGIN OF ty_rand_table,        value(1) TYPE c,        END OF ty_rand_table.  *&——————————————————– * * INTERNAL TABLES                                          * *&——————————————————– * DATA: t_rand_table TYPE STANDARD TABLE OF ty_rand_table,       t_user_table TYPE STANDARD TABLE OF ty_rand_table,       t_game_lines TYPE TABLE OF tline-tdline,       data_rand TYPE REF TO data,       data_lines TYPE REF TO data.  *&——————————————————– * * FIELD-SYMBOLS                                            * *&——————————————————– * FIELD-SYMBOLS:  LIKE LINE OF t_rand_table.  *&——————————————————– * * VARIABLES                                                * *&——————————————————– * DATA: custom_container TYPE REF TO cl_gui_custom_container,       text_editor TYPE REF TO cl_gui_textedit,       w_ucomm TYPE sy-ucomm,       ran_int TYPE qf00-ran_int,       flag TYPE c,       counter TYPE c,       gv_line TYPE string,       button_count TYPE c,       xtext TYPE tline-tdline,       game_win TYPE string,       game_counter TYPE string.  DATA: message_one TYPE string,       message_two TYPE string,       message_three TYPE string,       message_four TYPE string,       message_five TYPE string.  DATA: gv_one TYPE c,       gv_two TYPE c,       gv_three TYPE c,       gv_four TYPE c,       gv_five TYPE c.  DATA: gv_flag_one TYPE c,       gv_flag_two TYPE c,       gv_flag_three TYPE c,       gv_flag_four TYPE c,       gv_flag_five TYPE c.  DATA: gv_user_one TYPE c,       gv_user_two TYPE c,       gv_user_three TYPE c,       gv_user_four TYPE c,       gv_user_five TYPE c.  *——————————————————— * *   START-OF-SELECTION                                     * *——————————————————— * START-OF-SELECTION.   CALL SCREEN 0100.  *&——————————————————– * *&      Form  call_editor                                  * *&——————————————————– * FORM call_editor.    IF text_editor IS INITIAL.     CREATE OBJECT custom_container       EXPORTING         container_name              = ‘CUSTOM_CONTROL’       EXCEPTIONS         cntl_error                  = 1         cntl_system_error           = 2         create_error                = 3         lifetime_error              = 4         lifetime_dynpro_dynpro_link = 5.      CREATE OBJECT text_editor       EXPORTING         wordwrap_mode              = cl_gui_textedit=>wordwrap_at_fixed_position         wordwrap_position          = line_length         wordwrap_to_linebreak_mode = cl_gui_textedit=>true         parent                     = custom_container       EXCEPTIONS         error_cntl_create          = 1         error_cntl_init            = 2         error_cntl_link            = 3         error_dp_create            = 4         gui_type_not_supported     = 5         others                     = 6.      CALL METHOD text_editor->set_readonly_mode       EXPORTING readonly_mode = 1.   ENDIF.    PERFORM get_randoms.  ENDFORM.  *&——————————————————– * *&      Form  get_randoms                                  * *&——————————————————– * FORM get_randoms.    CREATE DATA data_rand TYPE ty_rand_table.   ASSIGN data_rand->* TO  TO t_rand_table.       counter = counter + 1.       IF counter EQ 5.         flag = ‘X’.       ENDIF.     ENDIF.   ENDWHILE.    game_counter = 1.  ENDFORM.                    ” get_randoms  *&——————————————————– * *&      Form  validate_game                                * *&——————————————————– * FORM validate_game.    LOOP AT t_rand_table ASSIGNING -value.     ENDIF.   ENDLOOP.    CLEAR: game_win.    IF gv_one EQ gv_user_one.     message_one = ‘First is Ok!’.     game_win = game_win + 1.   ELSE.     message_one = ‘First is Wrong!’.     game_win = game_win – 1.   ENDIF.   IF gv_two EQ gv_user_two.     message_two = ‘Second is Ok!’.     game_win = game_win + 1.   ELSE.     message_two = ‘Second is Wrong!’.     game_win = game_win – 1.   ENDIF.   IF gv_three EQ gv_user_three.     message_three = ‘Third is Ok!’.     game_win = game_win + 1.   ELSE.     message_three = ‘Third is Wrong!’.     game_win = game_win – 1.   ENDIF.   IF gv_four EQ gv_user_four.     message_four = ‘Forth is Ok!’.     game_win = game_win + 1.   ELSE.     message_four = ‘Forth is Wrong!’.     game_win = game_win – 1.   ENDIF.   IF gv_five EQ gv_user_five.     message_five = ‘Fifth is Ok!’.     game_win = game_win + 1.   ELSE.     message_five = ‘Fifth is Wrong!’.     game_win = game_win – 1.   ENDIF.    IF game_win EQ 5.     CLEAR: message_one,message_two,message_three,            message_four,message_five.     message_one = ‘You win!’.     CONCATENATE ‘In’ game_counter ‘tries’     INTO message_two SEPARATED BY space.   ELSE.     game_counter = game_counter + 1.     CLEAR: gv_flag_one,gv_flag_two,gv_flag_three,            gv_flag_four,gv_flag_five,sy-ucomm.   ENDIF.  ENDFORM.                    ” validate_game  *&——————————————————– * *&      Module  STATUS_0100  OUTPUT                        * *&——————————————————– * MODULE status_0100 OUTPUT.    SET PF-STATUS ‘MAIN’.   SET TITLEBAR ‘TITLE’.    IF flag EQ space.     PERFORM call_editor.   ELSE.      LOOP AT SCREEN.       IF screen-name EQ ‘ONE’ AND gv_flag_one EQ ‘X’.         screen-invisible = 1.         MODIFY SCREEN.       ENDIF.       IF screen-name EQ ‘TWO’ AND gv_flag_two EQ ‘X’.         screen-invisible = 1.         MODIFY SCREEN.       ENDIF.       IF screen-name EQ ‘THREE’ AND gv_flag_three EQ ‘X’.         screen-invisible = 1.         MODIFY SCREEN.       ENDIF.       IF screen-name EQ ‘FOUR’ AND gv_flag_four EQ ‘X’.         screen-invisible = 1.         MODIFY SCREEN.       ENDIF.       IF screen-name EQ ‘FIVE’ AND gv_flag_five EQ ‘X’.         screen-invisible = 1.         MODIFY SCREEN.       ENDIF.     ENDLOOP.      IF button_count EQ 5.        CLEAR button_count.        SHIFT gv_line LEFT DELETING LEADING space.        SPLIT gv_line AT space INTO TABLE t_user_table.        PERFORM validate_game.        xtext = gv_line.       APPEND xtext TO t_game_lines.        CLEAR gv_line.        CALL METHOD text_editor->set_text_as_r3table       EXPORTING table = t_game_lines.      ENDIF.   ENDIF.  ENDMODULE.                 ” STATUS_0100  OUTPUT  *&——————————————————– * *&      Module  USER_COMMAND_0100  INPUT                   * *&——————————————————– * MODULE user_command_0100 INPUT.    w_ucomm = sy-ucomm.    CASE w_ucomm.     WHEN ‘BACK’ OR ‘CANCEL’ OR ‘EXIT’.       SET SCREEN 0.       EXIT.     WHEN ‘ONE’.       CONCATENATE gv_line ‘1’ INTO       gv_line SEPARATED BY space.       button_count = button_count + 1.       gv_flag_one = ‘X’.     WHEN ‘TWO’.       CONCATENATE gv_line ‘2’ INTO       gv_line SEPARATED BY space.       button_count = button_count + 1.       gv_flag_two = ‘X’.     WHEN ‘THREE’.       CONCATENATE gv_line ‘3’ INTO       gv_line SEPARATED BY space.       button_count = button_count + 1.       gv_flag_three = ‘X’.     WHEN ‘FOUR’.       CONCATENATE gv_line ‘4’ INTO       gv_line SEPARATED BY space.       button_count = button_count + 1.       gv_flag_four = ‘X’.     WHEN ‘FIVE’.       CONCATENATE gv_line ‘5’ INTO       gv_line SEPARATED BY space.       button_count = button_count + 1.       gv_flag_five = ‘X’.   ENDCASE.  ENDMODULE.                 ” USER_COMMAND_0100  INPUT     I’m sure you want to see some pictures…Here they are…imageimageimage
To report this post you need to login first.

6 Comments

You must be Logged on to comment or reply to a post.

  1. Anonymous
    Hi Blag,

    I too share your appreciation of games written in ABAP.  My current ABAP games “portfolio” consists of a Slot Machine game, Video Poker, Car Racing, and a very rudimentary version of Battleship.  I created a global class ZCL_CARDS for the poker game.  For the slot machine, I incorporated some custom tables to keep track of a user’s “points” (instead of coins or tokens). 

    When I used to teach ABAP classes for SAP America, I always ended the week with a nice game of ABAP Jeopardy (written with classic Dynpros).  I used enqueue locking for the class members to buzz in with an answer to an ABAP question.  I still believe that playing that game right before the student evaluations helped improve my teacher ratings…  😉  

    Who says ABAP can’t be fun!

    Cheers,
    Jamie

    (0) 
    1. Alvaro Tejada Galindo Post author
      Jamie:

      Thanks for the comment -:) VideoPoker? I was thinking about doing that (I got a nice one developed in VB6)…But if you already done that…Why don’t you blog it?

      I would like to start a WIKI page with all the games developed in ABAP…You’re would be a great contribution -:D

      Greetings,

      Blag.

      (0) 
  2. Che Eky
    Hi,
    I remember playing Master Mind and from what I recall you were never actually told which columns were right or wrong, you were only ever told the total number you had guessed correctly. Your design makes the game too simple. In the case of your example it should say you have three correct.

    Anyway thats how I remember it, I maybe wrong.

    (0) 
    1. Alvaro Tejada Galindo Post author
      Che:

      I don’t remember the original quite well…So I think the best way for beginners was to make the game easy…As this a “Community Project” anyway is free to made any modifications or improvements to the game -;)

      Greetings,

      Blag.

      (0) 
  3. Anonymous
    I do not formally work in ABAP any more – so this was fun.

    Added a ‘New Game’ button on the application toolbar.

    Mathew.

    (0) 
  4. Sathish R
    Hi Alvaro,

      It’s really interesting to play some games written in ABAP isn’t it.. thanks for that..but when i tried your code, I found that for the second time, am not able to see the numbers push buttons again in the screen, as you are making it invisible…i made a couple of things to make it work for me…
          CALL METHOD text_editor->set_text_as_r3table
            EXPORTING
              table = t_game_lines.
    *****
          IF game_win NE ‘5’.
            LOOP AT SCREEN.
              IF screen-name EQ ‘ONE’. ” AND gv_flag_one EQ ‘X’.
                screen-invisible = 0.
                MODIFY SCREEN.
              ENDIF.
              IF screen-name EQ ‘TWO’. ” AND gv_flag_two EQ ‘X’.
                screen-invisible = 0.
                MODIFY SCREEN.
              ENDIF.
              IF screen-name EQ ‘THREE’. ” AND gv_flag_three EQ ‘X’.
                screen-invisible = 0.
                MODIFY SCREEN.
              ENDIF.
              IF screen-name EQ ‘FOUR’. ” AND gv_flag_four EQ ‘X’.
                screen-invisible = 0.
                MODIFY SCREEN.
              ENDIF.
              IF screen-name EQ ‘FIVE’. ” AND gv_flag_five EQ ‘X’.
                screen-invisible = 0.
                MODIFY SCREEN.
              ENDIF.
            ENDLOOP.
          ENDIF.
    ********
    and commenting this piece of code

    *  game_counter = 1.

    keep posting 🙂

    Regards,
    Sathish

    (0) 

Leave a Reply