SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Conway's Life



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Submit a new program | Новые материалы, программы для сайта
View previous topic :: View next topic  
Author Message
John Doe
Модератор
Модератор


Age: 46
Joined: 05 Nov 2007
Posts: 725
Location: КраснАдар

PostPosted: Wed Jan 20, 2010 2:27 pm    Post subject: Conway's Life Reply with quote

Порт игры Life.

Для запуска игры необходимо создать гуи статус MAIN:
Стандартная строка инструментов
BACK, CANCEL, EXIT для выхода
Строка кнопок
CLEAR "Clear" - статический текст
REFRESH "Re-generate" - статический текст
BUTTON01 - динамический текст на поле программы BUTTON01

Кроме этого нужно создать html шаблон через транзакцию SMW0 с названием ZLIFE. Для этого нужно будет импортировать приложенный файл. Данный файл необходим для запуска события on_sapevent по таймеру.

Code:
REPORT  zlife.

TYPE-POOLS: icon, cntl, swww, vrm, sdydo.

CONSTANTS: cs_min_neighbours TYPE i VALUE 2,
           cs_max_neighbours TYPE i VALUE 3,
           cs_pause TYPE smp_dyntxt VALUE 'Pause',
           cs_start TYPE smp_dyntxt VALUE 'Start'.

DATA: fcat     TYPE lvc_t_fcat,
      hcat     TYPE lvc_s_fcat,
      layo     TYPE lvc_s_layo,
      dref     TYPE REF TO data,
      dref1    TYPE REF TO data,
      header   TYPE REF TO data,
      curr_col TYPE i,
      curr_row TYPE i,
      position TYPE i,
      rndvalue TYPE i,
      colname  TYPE string.

DATA: button01 TYPE smp_dyntxt,
      vrm_id   TYPE vrm_id,
      vrm_list TYPE vrm_values,
      value    LIKE LINE OF vrm_list.

DATA: dock        TYPE REF TO cl_gui_docking_container,
      rules_cont  TYPE REF TO cl_gui_docking_container,
      rules       TYPE REF TO cl_dd_document,
      html        TYPE REF TO cl_gui_html_viewer,
      html_table  TYPE TABLE OF w3html WITH HEADER LINE,
      merge_table TYPE swww_t_merge_table WITH HEADER LINE,
      html_events TYPE cntl_simple_events,
      html_event  TYPE cntl_simple_event.
DATA  frame_url(255).
DATA  grid TYPE REF TO cl_gui_alv_grid.

FIELD-SYMBOLS: <outtab>  TYPE table,
               <temptab> TYPE table,
               <header>  TYPE ANY,
               <cell>    TYPE ANY.

DEFINE move_to_html.
  move &1 to html_table-line.
  append html_table.
END-OF-DEFINITION.

*----------------------------------------------------------------------*
*       CLASS lcl_sapevent_receiver DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_sapevent_receiver DEFINITION.

  PUBLIC SECTION.
    METHODS:  on_sapevent
                FOR EVENT sapevent OF cl_gui_html_viewer
                  IMPORTING
                    action frame getdata postdata query_table    .

ENDCLASS.                    "lcl_sapevent_receiver DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_sapevent_receiver IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_sapevent_receiver IMPLEMENTATION.
  METHOD on_sapevent.
    CHECK button01 EQ cs_pause.

    CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
      IMPORTING
        e_grid = grid.

    PERFORM next_wave.
    grid->refresh_table_display( ).

  ENDMETHOD.                    "on_sapevent
ENDCLASS.                    "lcl_sapevent_receiver IMPLEMENTATION

DATA: handler TYPE REF TO lcl_sapevent_receiver.

PARAMETERS: p_cols TYPE i DEFAULT 32,
            p_rows TYPE i DEFAULT 24,
            p_speed(14) AS LISTBOX VISIBLE LENGTH 14
              DEFAULT '1000' OBLIGATORY.

INITIALIZATION.
  REFRESH vrm_list.
  vrm_id = 'P_SPEED'.
  value-key = '1500'.
  value-text = 'Low'.
  APPEND value TO vrm_list.
  value-key = '1000'.
  value-text = 'Medium'.
  APPEND value TO vrm_list.
  value-key = '500'.
  value-text = 'High'.
  APPEND value TO vrm_list.

  CALL FUNCTION 'VRM_SET_VALUES'
    EXPORTING
      id     = vrm_id
      values = vrm_list.

AT SELECTION-SCREEN.
  REFRESH: html_table, merge_table.
  APPEND p_speed TO html_table.

  merge_table-name = '<!speed!>'.
  merge_table-html = html_table[].
  APPEND merge_table.

START-OF-SELECTION.
  PERFORM initialization.

END-OF-SELECTION.
  " Create all objects and handlers
  CREATE OBJECT:
    dock EXPORTING repid = 'SAPLSLVC_FULLSCREEN'
                   dynnr = '0500'
                   side  = dock->dock_at_top
                   ratio = 12,
    html EXPORTING parent = dock,
    rules_cont EXPORTING repid = 'SAPLSLVC_FULLSCREEN'
                   dynnr = '0500'
                   side  = dock->dock_at_right
                   ratio = 50,
    rules,
    handler.

  REFRESH html_events.
  html_event-eventid = cl_gui_html_viewer=>m_id_sapevent.
  html_event-appl_event = 'X'.
  APPEND html_event TO html_events.

  html->set_registered_events( events = html_events ).
  SET HANDLER handler->on_sapevent FOR html.

  " Load html template with timer script into HTML Viewer
  html->load_html_document( EXPORTING document_id  = 'ZLIFE'
                            IMPORTING assigned_url = frame_url
                            CHANGING  merge_table  = merge_table[]
                            EXCEPTIONS OTHERS      = 1 ).

  html->show_data( url = frame_url ).

  " Create dynamic document with game rules
  PERFORM prepare_rules.
  rules->display_document( parent = rules_cont ).

  MOVE 'X' TO : layo-no_toolbar, layo-no_rowmark, layo-no_headers,
                layo-no_hgridln, layo-no_vgridln.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      i_callback_program       = sy-cprog
      i_callback_pf_status_set = 'SET_PF_STATUS'
      i_callback_user_command  = 'USER_COMMAND'
      is_layout_lvc            = layo
      it_fieldcat_lvc          = fcat
    TABLES
      t_outtab                 = <outtab>
    EXCEPTIONS
      program_error            = 1
      OTHERS                   = 2.
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.


*&---------------------------------------------------------------------*
*&      Form  set_pf_status
*&---------------------------------------------------------------------*
FORM set_pf_status USING extab TYPE slis_t_extab.
  SET PF-STATUS 'MAIN'.
ENDFORM.                    "set_pf_status

*&---------------------------------------------------------------------*
*&      Form  user_command
*&---------------------------------------------------------------------*
FORM user_command USING okcode TYPE sy-ucomm
                        selfield TYPE slis_selfield.
  CASE okcode.
    WHEN 'BACK' OR 'EXIT' OR 'CANCEL'.
      LEAVE TO SCREEN 0.

    WHEN 'BUTTON01'.
      IF button01 EQ cs_pause.
        button01 = cs_start.
      ELSE.
        button01 = cs_pause.
      ENDIF.

    WHEN 'CLEAR' OR 'REFRESH'.
      CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
        IMPORTING
          e_grid = grid.

      IF okcode EQ 'CLEAR'.
        REFRESH <outtab>.
        DO p_rows TIMES.
          APPEND INITIAL LINE TO <outtab>.
        ENDDO.
      ELSE.
        PERFORM initialize_content.
      ENDIF.

      grid->refresh_table_display( ).

    WHEN '&IC1'.
      IF button01 EQ cs_pause.
        MESSAGE 'Please set game pause first!' TYPE 'I'.
        EXIT.
      ELSE.
        CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
          IMPORTING
            e_grid = grid.

      ENDIF.

      READ TABLE <outtab> ASSIGNING <header> INDEX selfield-tabindex.
      ASSIGN COMPONENT selfield-fieldname OF STRUCTURE <header>
        TO <cell>.
      IF <cell> IS INITIAL.
        <cell> = icon_led_green.
      ELSE.
        CLEAR <cell>.
      ENDIF.
      UNASSIGN <cell>.

      grid->refresh_table_display( ).

  ENDCASE.
ENDFORM.                    "user_command

*&---------------------------------------------------------------------*
*&      Form  INITIALIZATION
*&---------------------------------------------------------------------*
FORM initialization .
  " Create dynamic table & header
  DO p_cols TIMES.
    CLEAR hcat.
    hcat-col_pos = sy-index.
    MOVE hcat-col_pos TO hcat-fieldname.
    hcat-inttype = 'C'.
    hcat-intlen  = '4'.
    hcat-icon    = 'X'.
    hcat-outputlen = 1.
    APPEND hcat TO fcat.
  ENDDO.

  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog = fcat
    IMPORTING
      ep_table        = dref.
  ASSIGN  dref->* TO <outtab>.
  CHECK <outtab> IS ASSIGNED.

  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog = fcat
    IMPORTING
      ep_table        = dref1.
  ASSIGN  dref1->* TO <temptab>.
  CHECK <temptab> IS ASSIGNED.

  CREATE DATA header LIKE LINE OF <outtab>.

  " Initialize table by first life wave
  PERFORM initialize_content.

  button01 = cs_pause.

ENDFORM.                    " INITIALIZATION

*&---------------------------------------------------------------------*
*&      Form  initialize_content
*&---------------------------------------------------------------------*
FORM initialize_content.
  REFRESH: <outtab>, <temptab>.

  ASSIGN header->* TO <header>.

  DO p_rows TIMES.
    CLEAR curr_col.
    DO p_cols TIMES.
      curr_col = curr_col + 1.
      MOVE curr_col TO colname.
      ASSIGN COMPONENT colname OF STRUCTURE <header>  TO <cell>.
      CHECK <cell> IS ASSIGNED.
      CALL FUNCTION 'RANDOM_I4'
        EXPORTING
          rnd_min   = 0
          rnd_max   = 100
        IMPORTING
          rnd_value = rndvalue.
      IF rndvalue < 80. " Dead cell
        CLEAR <cell>.
      ELSE.             " Alive cell
        <cell> = icon_led_green.
      ENDIF.
      UNASSIGN <cell>.
    ENDDO.
    APPEND <header> TO <outtab>.
  ENDDO.

ENDFORM.                    "initialize_content

*&---------------------------------------------------------------------*
*&      Form  NEXT_WAVE
*&---------------------------------------------------------------------*
FORM next_wave .
  DATA: n TYPE i,
        tmpheader TYPE REF TO data.

  FIELD-SYMBOLS: <tmpheader> TYPE ANY,
                 <tmpcell> TYPE ANY.

  IF <temptab> IS ASSIGNED.
    REFRESH <temptab>.
  ENDIF.

  CREATE DATA tmpheader LIKE LINE OF <outtab>.
  ASSIGN tmpheader->* TO <tmpheader>.

  CLEAR: curr_row, curr_col.

  LOOP AT <outtab> ASSIGNING <header>.
    curr_row = sy-tabix.
    CLEAR: <tmpheader>, curr_col.

    DO p_cols TIMES.
      curr_col = curr_col + 1.
      PERFORM calculate_neighbours USING curr_row curr_col CHANGING n.

      MOVE curr_col TO colname.
      ASSIGN COMPONENT colname OF STRUCTURE <header>  TO <cell>.
      ASSIGN COMPONENT colname OF STRUCTURE <tmpheader> TO <tmpcell>.

      <tmpcell> = <cell>.

      IF <cell> IS NOT INITIAL. " Is alive
        IF n < cs_min_neighbours OR n > cs_max_neighbours.
          CLEAR <tmpcell>.
        ENDIF.
      ELSE.                     " Dead cell
        IF n = cs_max_neighbours.
          <tmpcell> = icon_led_green.
        ENDIF.
      ENDIF.
    ENDDO.
    APPEND <tmpheader> TO <temptab>.
  ENDLOOP.

  <outtab> = <temptab>.

ENDFORM.                    " NEXT_WAVE

*&---------------------------------------------------------------------*
*&      Form  CALCULATE_NEIGHBOURS
*&---------------------------------------------------------------------*
FORM calculate_neighbours  USING    curr_row TYPE i
                                    curr_col TYPE i
                           CHANGING n TYPE i.
  DATA: calc_row TYPE i,
        calc_col TYPE i,
        is_alive TYPE i.

  DEFINE is_cell_alive.
    perform is_cell_alive using &1 &2 changing is_alive.
    n = n + is_alive.
  END-OF-DEFINITION.

  CLEAR n.

  calc_row = curr_row - 1. calc_col = curr_col - 1.
  is_cell_alive calc_row calc_col.

  calc_row = curr_row - 1. calc_col = curr_col.
  is_cell_alive calc_row calc_col.

  calc_row = curr_row - 1. calc_col = curr_col + 1.
  is_cell_alive calc_row calc_col.

  calc_row = curr_row. calc_col = curr_col - 1.
  is_cell_alive calc_row calc_col.

  calc_row = curr_row. calc_col = curr_col + 1.
  is_cell_alive calc_row calc_col.

  calc_row = curr_row + 1. calc_col = curr_col - 1.
  is_cell_alive calc_row calc_col.

  calc_row = curr_row + 1. calc_col = curr_col.
  is_cell_alive calc_row calc_col.

  calc_row = curr_row + 1. calc_col = curr_col + 1.
  is_cell_alive calc_row calc_col.


ENDFORM.                    " CALCULATE_NEIGHBOURS

*&---------------------------------------------------------------------*
*&      Form  IS_CELL_ALIVE
*&---------------------------------------------------------------------*
FORM is_cell_alive  USING    p_row TYPE i
                             p_col TYPE i
                    CHANGING is_alive TYPE i.
  FIELD-SYMBOLS: <tmpheader2> TYPE ANY,
                 <tmpcell2> TYPE ANY.

  DATA: tmpcolname TYPE string,
        calc_row TYPE i,
        calc_col TYPE i.

  calc_row = p_row.
  calc_col = p_col.

  IF calc_row = 0.
    calc_row = p_rows.
  ELSEIF calc_row > p_rows.
    calc_row = 1.
  ENDIF.

  IF calc_col = 0.
    calc_col = p_cols.
  ELSEIF calc_col > p_cols.
    calc_col = 1.
  ENDIF.

  READ TABLE <outtab> ASSIGNING <tmpheader2> INDEX calc_row.
  MOVE calc_col TO tmpcolname.
  ASSIGN COMPONENT tmpcolname OF STRUCTURE <tmpheader2> TO <tmpcell2>.
  IF <tmpcell2> IS ASSIGNED.
    IF <tmpcell2> IS NOT INITIAL.
      is_alive = 1.
    ELSE.
      is_alive = 0.
    ENDIF.
  ENDIF.

ENDFORM.                    " IS_CELL_ALIVE

*&---------------------------------------------------------------------*
*&      Form  REPARE_RULES
*&---------------------------------------------------------------------*
FORM prepare_rules .
  DEFINE add_rule.
    rules->add_text( text = &1 ).
    rules->new_line( ).
  END-OF-DEFINITION.

  rules->initialize_document( background_color = rules->col_textarea ).
  rules->add_text( text = 'Rules' sap_emphasis = rules->strong ).
  rules->new_line( ).

  add_rule:
     'The universe of the Game of Life is an infinite two-dimensional',
     'orthogonal grid of square cells, each of which is in one of two',
     'possible states, live or dead. Every cell interacts with its eight',
     'neighbors, which are the cells that are directly horizontally,',
     'vertically, or diagonally adjacent. At each step in time, the ',
     'following transitions occur:',
     '1. Any live cell with fewer than two live neighbours dies, as if',
     'caused by underpopulation.',
    '2. Any live cell with more than three live neighbours dies, as if',
    'by overcrowding.',
    '3. Any live cell with two or three live neighbours lives on to the',
    'next generation.',
    '4. Any dead cell with exactly three live neighbours becomes a live',
    'cell.'.

  rules->new_line( ).
  add_rule:
    'My additional:',
    '1. I set game field without endfields. After last column begin first,',
    'after last row - also begin first row.',
    '2. When game is paused you can manualy kill or grow cells by double-click.',
    '3. 32x24 - optimal resolution of game field on my comp. If field will be',
    'bigger - I catch incorrect grid refreshing. May be it`s low memory or',
    'something else.'.

  rules->merge_document( ).
ENDFORM.                    " PREPARE_RULES



zlife_timer_script.zip
 Description:
HTML шаблон со скриптом таймера

Download
 Filename:  zlife_timer_script.zip
 Filesize:  710 Bytes
 Downloaded:  1371 Time(s)

Back to top
View user's profile Send private message Blog
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Submit a new program | Новые материалы, программы для сайта All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.