*&---------------------------------------------------------------------*
*&  Include           z_lcl_lines
*&---------------------------------------------------------------------*
include <list>.
*----------------------------------------------------------------------*
*       CLASS z_lcl_lines DEFINITION
*----------------------------------------------------------------------*
*       Created by Garipov S.A. 2008
*----------------------------------------------------------------------*
class z_lcl_lines definition.
  public section.
    methods redraw.
    methods draw_info.
    methods draw_field importing x type i y type i.
    methods initialize.

    methods useraction importing col type i row type i.
    methods addnewballs.

    methods deletelines.
    methods scan_for_lines
      importing
        start_x type i
        start_y type i
        dx type i
        dy type i
        tms type i.
    methods del_line
      importing
        x type i
        y type i
        num type i
        dx type i
        dy type i.

    methods step_route
      importing
        start_x type i
        start_y type i
        x type i
        y type i
        finish_x type i
        finish_y type i
      returning value(ret) type string.

    data fields type table of i initial size 100.
    data: step type i,
          from type i,
          fromcol type i,
          from_x type i,
          from_y type i.

    data ispoints type c.
    data rec_cnt type i.
  private section.
    data dummy type i.
    methods getval importing x type i y type i returning value(ret)
                   type i.
    data points type i.

endclass.                    "z_lcl_lines DEFINITION

*----------------------------------------------------------------------*
*       CLASS z_lcl_lines IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class z_lcl_lines implementation.
  method getval.
    data tmp type i.
    tmp = ( y - 1 ) * 10 + x.
    read table fields index tmp into ret.
  endmethod.                    "getval

  method initialize.
    "Generate balls
    "Legend: 1 - Red, 2 - Green, 3 - Blue, 4 - Yellow, 5 - etc
    data: rnd_field type i, rnd_col type i.

    ispoints = ' '.

    do 100 times.
      append 0 to fields.
    enddo.

    do 5 times.
      call function 'GENERAL_GET_RANDOM_INT'
        exporting
          range  = 99
        importing
          random = rnd_field.
      rnd_field = rnd_field + 1.

      call function 'GENERAL_GET_RANDOM_INT'
        exporting
          range  = 7
        importing
          random = rnd_col.
      rnd_col = rnd_col + 1.

      modify fields index rnd_field from rnd_col.
    enddo.
  endmethod.                    "initialize

  method draw_field.

    "x,y
    data: scr_x type i, scr_y type i.
    data str(3) type c.
    scr_x = x * 5.
    scr_y = y * 3.

    skip to line scr_y.
    position scr_x.

    "Top line
    write line_top_left_corner as line no-gap.
    do 3 times.
      write line_horizontal_line as line no-gap.
    enddo.
    write line_top_right_corner as line no-gap.

    "Left & Right
    dummy = scr_x + 4.
    new-line.
    position scr_x.
    write line_vertical_line as line no-gap.
    position dummy.
    write line_vertical_line as line no-gap.

    "Bottom Line
    new-line.
    position scr_x.
    write line_bottom_left_corner as line no-gap.
    do 3 times.
      write line_horizontal_line as line no-gap.
    enddo.
    write line_bottom_right_corner as line no-gap.

    "TODO: output value
    dummy = scr_y + 1.
    skip to line dummy.
    dummy = scr_x + 1.
    position dummy.

    data ttt(1) type c.

    dummy = ( y - 1 ) * 10 + x.
    read table fields index dummy into dummy.
    str = dummy.
    if dummy > 0.
      if dummy = 2.
        ttt = '>'.
        format color = 6 intensified on.
        write ttt no-gap hotspot.
        ttt = '&'.
        format color = 5.
        write ttt no-gap intensified on hotspot.
        ttt = '<'.
        format color = 4.
        write ttt no-gap intensified on hotspot.
        format reset.
        exit.
      elseif dummy = 8.
        ttt = '>'.
        format color = 7 intensified on.
        write ttt no-gap hotspot.
        ttt = '#'.
        format color = 3.
        write ttt no-gap intensified on hotspot.
        ttt = '<'.
        format color = 1.
        write ttt no-gap intensified on hotspot.
        format reset.
        exit.
      else.
        format color = dummy intensified on.
      endif.
      write str no-gap hotspot.
      format reset.
    else.
      write '__' hotspot.
    endif.
  endmethod.                    "draw_field

  method draw_info.
    skip to line 3.
    position 60.
    write: line_top_left_corner as line no-gap.
    do 30 times.
      write line_horizontal_line as line no-gap.
    enddo.
    write: line_top_right_corner as line no-gap.

    do 5 times.
      dummy = sy-index + 3.
      skip to line dummy.
      position 60.
      write: line_vertical_line as line no-gap.
      do 30 times.
        write line_space as line no-gap.
      enddo.
      write: line_vertical_line as line no-gap.
    enddo.

    new-line.
    position 60.
    write: line_bottom_left_corner as line no-gap.
    do 30 times.
      write line_horizontal_line as line no-gap.
    enddo.
    write: line_bottom_right_corner as line no-gap.

    skip to line 4.
    position 62.
    write: 'Wellcome to ZLines!'.
    skip to line 6.
    position 62.
    write: 'Your points: ', points.
    skip to line 7.
    position 62.
    format reset.
    write: / 'PS: ZLines 2 is coming soon!'.
  endmethod.                    "draw_info

  method redraw.
    deletelines( ).
    ispoints = space.

    if sy-lsind > 0.
      "  write: sy-title. uline.
      sy-lsind = sy-lsind - 1.
    endif.

    "Game zone
    data: _x type i, _y type i.
    do 100 times.
      _y = ( ( sy-index - 1 ) div 10 ) + 1.
      _x = ( ( sy-index - 1 ) mod 10 ) + 1.
      draw_field( exporting x = _x y = _y ).
    enddo.

    draw_info( ).
  endmethod.                    "redraw

  method useraction.
    data x type i.
    data y type i.
    data ret type c.
    data tmp type i.

    x = ( col + 1 ) / 5 - 1.
    y = row / 3.

    tmp = x + ( y - 1 ) * 10.
    data str type string.
    str = tmp.
    read table fields index tmp into dummy.
    if dummy > 0.
      message s002.
      step = 1.
      from_x = x.
      from_y = y.
      fromcol = dummy.
    else.
      if step = 1.
        ret = step_route(

          start_x = from_x
          start_y = from_y
          x = from_x
          y = from_y
          finish_x = x
          finish_y = y ).

        do 100 times.
          read table fields index sy-index into dummy.
          if dummy = -5.
            modify fields index sy-index from 0.
          endif.
        enddo.
        "Check route
        if ret = 'X'.
          message s003.
        else.
          message s004.
          exit.
        endif.
        "Go))
        modify fields index tmp from fromcol.
        tmp = from_x + ( from_y - 1 ) * 10.
        modify fields index tmp from 0.

        step = 0.

        deletelines( ).
        addnewballs( ).
      endif.
    endif.
    redraw( ).
  endmethod.                    "useraction

  method addnewballs.
    if ispoints = 'X'.
      ispoints = space.
      exit.
    endif.

    data spacecnt type i.
    do 100 times.
      read table fields index sy-index into dummy.
      if dummy = 0.
        spacecnt = spacecnt + 1.
      endif.
    enddo.
    if spacecnt <= 3.
      message i005.
      exit.
    endif.

    data: cnt type i, rnd_field type i, rnd_col type i.
    cnt = 0.
    do.
      if cnt = 3.
        exit.
      endif.

      call function 'GENERAL_GET_RANDOM_INT'
        exporting
          range  = 99
        importing
          random = rnd_field.
      rnd_field = rnd_field + 1.

      call function 'GENERAL_GET_RANDOM_INT'
        exporting
          range  = 7
        importing
          random = rnd_col.
      rnd_col = rnd_col + 1.

      read table fields index rnd_field into dummy.
      if dummy = 0.
        modify fields index rnd_field from rnd_col.
        cnt = cnt + 1.
      endif.
    enddo.



  endmethod.                    "addnewballs

  method del_line."Delete line
    "x,y - start pos. of line
    "num - number of line balls
    "dx,dy - delta's

    data:
          _x type i,
          _y type i,
          tmp type i,
          curpos type i.

    _x = x.
    _y = y.
    do num times.
      curpos = ( _y - 1 ) * 10 + _x.
      modify fields index curpos from 0.
      _x = _x + dx.
      _y = _y + dy.
    enddo.
    ispoints = 'X'.
    points = points + 10 + ( num - 5 ) * 10.
  endmethod.                    "del_line

  method scan_for_lines."Search lines
    "x,y - start pos. of line
    "dx,dy - delta's
    data:
          _x type i,
          _y type i,
          tmp type i,
          tmp2 type i,
          del_dx type i,
          del_dy type i.
    data:
          prevclr type i,
          color type i,
          cnt type i.
    _x = start_x.
    _y = start_y.

    del_dx = - dx.
    del_dy = - dy.

    prevclr = -1.
    cnt = -1.
    do tms times.
      tmp = ( _y - 1 ) * 10 + _x.
      read table fields index tmp into color.
      if prevclr = -1.
        prevclr = color.
        cnt = 1.
      else.
        if prevclr = color.
          cnt = cnt + 1.
        else.
          if cnt >= 5 and prevclr <> 0.
            tmp = _x - dx.
            tmp2 = _y - dy.
            del_line( x = tmp y = tmp2 num = cnt dx = del_dx dy = del_dy
).
          endif.
          prevclr = color.
          cnt = 1.
        endif.
      endif.
      _x = _x + dx.
      _y = _y + dy.
    enddo.
    if cnt >= 5  and prevclr <> 0.
      tmp = _x - dx.
      tmp2 = _y - dy.
      del_line( x = tmp y = tmp2 num = cnt dx = del_dx dy = del_dy ).
    endif.
  endmethod.                    "scan_for_lines

  method deletelines.
    data: d1 type i, d2 type i, d3 type i.
    do 10 times.
      scan_for_lines( start_x = 1 start_y = sy-index dx = 1 dy = 0 tms
= 10 ).
    enddo.

    do 10 times.
      scan_for_lines( start_x = sy-index start_y = 1 dx = 0 dy = 1 tms
= 10 ).
    enddo.

    do 10 times.
      scan_for_lines( start_x = sy-index start_y = 1 dx = -1 dy = 1 tms
= sy-index ).
    enddo.

    do 10 times.
      d1 = 11 - sy-index.
      scan_for_lines( start_x = 10 start_y = sy-index dx = -1 dy = 1 tms
= d1 ).
    enddo.

    do 10 times.
      d1 = 11 - sy-index.
      scan_for_lines( start_x = sy-index start_y = 1 dx = 1 dy = 1 tms
= d1 ).
    enddo.

    do 10 times.
      d1 = 11 - sy-index.
      scan_for_lines( start_x = 1 start_y = sy-index dx = 1 dy = 1 tms
= d1 ).
    enddo.
  endmethod.                    "deletelines

  method step_route.
    "x,y - current position
    "finish_x,finish_y
    "ret - return value
    data tmp type i.
    data: _x type i, _y type i.

    if ( x = finish_x ) and ( y = finish_y ).
      ret = 'X'."Successful search!
      exit.
    endif.

    if x <> start_x or y <> start_y.
      tmp = getval( x = x y = y ).
      if tmp > 0.
        ret = space.
        exit.
      elseif tmp = 0.
        tmp = x + ( y - 1 ) * 10.
        modify fields index tmp from -5.
      else.
        exit.
      endif.
    endif.

    _x = x.
    _y = y.
    "Up
    _y = y - 1.
    if _y > 0.
      ret = step_route( start_x = start_x start_y = start_y x = _x y =
_y finish_x = finish_x finish_y =
finish_y ).
      if ret = 'X'.
        exit.
      endif.
    endif.
    _y = y.

    "Right
    _x = x + 1.
    if _x <= 10.
      ret = step_route( start_x = start_x start_y = start_y x = _x y =
_y finish_x = finish_x finish_y =
finish_y ).
      if ret = 'X'.
        exit.
      endif.
    endif.
    _x = x.

    "Left
    _x = x - 1.
    if _x > 0.
      ret = step_route( start_x = start_x start_y = start_y x = _x y =
_y finish_x = finish_x finish_y =
finish_y ).
      if ret = 'X'.
        exit.
      endif.
    endif.
    _x = x.

    "Bottom
    _y = y + 1.
    if _y <= 10.
      ret = step_route( start_x = start_x start_y = start_y x = _x y =
_y finish_x = finish_x finish_y =
finish_y ).
      if ret = 'X'.
        exit.
      endif.
    endif.
    _y = y.

    ret = space."Search is unsuccessful
  endmethod.                    "step_route

endclass.                    "z_lcl_lines IMPLEMENTATION