Skip to end of metadata
Go to start of metadata

Author: Thomas Schilling
Submitted: 30.05.2018
Related Links:

  • Near by the DEC PDP Basic Code from 1973. See and feel how to deal with SY-LSIND (wink)

Description

*&---------------------------------------------------------------------*
*& Report  ZS_TTT
*&
*&---------------------------------------------------------------------*
*& Thomas Schilling 2018
*& thomas@schilling-bontkirchen.de
*&---------------------------------------------------------------------*

REPORT  zs_ttt.

TYPES: BEGIN OF s_ttt,
  f1 TYPE i,
  f2 TYPE i,
  f3 TYPE i,
  END OF s_ttt.

TYPES: BEGIN OF s_win,
  z1 TYPE i,
  z2 TYPE i,
  z3 TYPE i,
  s1 TYPE i,
  s2 TYPE i,
  s3 TYPE i,
  d1 TYPE i,
  d2 TYPE i,
  END OF s_win.

DATA: it_ttt TYPE TABLE OF s_ttt,
      wa_win TYPE s_win,
      wa_ttt TYPE s_ttt.

FIELD-SYMBOLS: <fs_ttt> TYPE s_ttt,
               <fs_win> TYPE s_win.

DATA: a TYPE n LENGTH 1,
      x TYPE n LENGTH 1,
      y TYPE n LENGTH 1,
      rndx TYPE int4,
      rndy TYPE int4,
      zug TYPE i VALUE 0,
      wer TYPE i VALUE 5,
      pruef TYPE i VALUE 0,
      win TYPE i VALUE 0.

*DATA c_rnd TYPE REF TO cl_abap_random_int.
*c_rnd = cl_abap_random_int=>create( min = 1 max = 3 ).
*Fehler
*auch mit Interface seed = 2 immer die selben Zahlen

ASSIGN wa_win TO <fs_win>.

MOVE 1 TO a.
WRITE 'Klassisches Tic Tac Toe Programm'.
WRITE 'Vorlage geschrieben für DEC PDP11 1973'.
ULINE.SKIP.
MOVE 10 TO sy-linno.
WRITE 5 'Spielparamter'.SKIP.
WRITE: 5 'Anzahl der Spieler ',a INPUT.
SKIP.
WRITE: 5 'Spiel beginn' HOTSPOT.
SET CURSOR FIELD a LINE 12 OFFSET 0.


AT LINE-SELECTION.
  IF sy-lisel = '    Spiel beginn'.
    READ LINE 12 FIELD VALUE a INTO a.
    IF a = 0.
      WRITE 25 'Anzahl Spieler 0. Der Computer spielt alleine'.
    ELSE.
      MOVE 1 TO a.
      WRITE 25 'Spielen wir'.
    ENDIF.
    ULINE.
  ENDIF.

  PERFORM nullstellung.

  PERFORM liko.


AT pf05.
  CLEAR x.
  CLEAR y.
  MOVE 1 TO pruef.
*  Wenn ein Spieler spielt lese den Zug
  IF a NE 0 AND wer EQ 5.
    READ LINE 15 FIELD VALUE x INTO x.
    READ LINE 15 FIELD VALUE y  INTO y.
    IF x = 0 OR x > 3 OR y = 0 OR y > 3.
      MESSAGE 'Falsche Eingabe' TYPE 'E'.
    ENDIF.

  ENDIF.

  PERFORM spielen.
  PERFORM taktik.
  PERFORM ttt_feld.
  PERFORM liko.

FORM strategie USING wert TYPE i.
  CHECK wert = 10 OR wert = 2.
  CASE wert.
    WHEN <fs_win>-d1.
*          Sonderfall Diagonale li nach re
      DO 3 TIMES.
        READ TABLE it_ttt INDEX sy-index ASSIGNING <fs_ttt>.
        CASE sy-index.
          WHEN 1.
            IF <fs_ttt>-f1 = 0.
              MOVE sy-index TO x.
              MOVE 1 TO y.
            ENDIF.
          WHEN 2.
            IF <fs_ttt>-f2 = 0.
              MOVE sy-index TO x.
              MOVE 2 TO y.
            ENDIF.
          WHEN 3.
            IF <fs_ttt>-f3 = 0.
              MOVE sy-index TO x.
              MOVE 3 TO y.
            ENDIF.
        ENDCASE.
      ENDDO.

    WHEN <fs_win>-d2.
*          Sonderfall Diagonale re nach li
      DO 3 TIMES.
        READ TABLE it_ttt INDEX sy-index ASSIGNING <fs_ttt>.
        CASE sy-index.
          WHEN 1.
            IF <fs_ttt>-f3 = 0.
              MOVE sy-index TO x.
              MOVE 3 TO y.
            ENDIF.
          WHEN 2.
            IF <fs_ttt>-f2 = 0.
              MOVE sy-index TO x.
              MOVE 2 TO y.
            ENDIF.
          WHEN 3.
            IF <fs_ttt>-f1 = 0.
              MOVE sy-index TO x.
              MOVE 1 TO y.
            ENDIF.
        ENDCASE.
      ENDDO.

    WHEN <fs_win>-z1.
      MOVE 1 TO x.
      MOVE 10 TO pruef.
    WHEN <fs_win>-z2.
      MOVE 2 TO x.
      MOVE 10 TO pruef.
    WHEN <fs_win>-z3.
      MOVE 3 TO x.
      MOVE 10 TO pruef.
    WHEN <fs_win>-s1.
      MOVE 1 TO y.
      MOVE 11 TO pruef.
    WHEN <fs_win>-s2.
      MOVE 2 TO y.
      MOVE 11 TO pruef.
    WHEN <fs_win>-s3.
      MOVE 3 TO y.
      MOVE 11 TO pruef.
  ENDCASE.
  IF pruef = 10.
* Spalte finden
    READ TABLE it_ttt INDEX x ASSIGNING <fs_ttt>.
    CASE 0.
      WHEN <fs_ttt>-f1.
        MOVE 1 TO y.
      WHEN <fs_ttt>-f2.
        MOVE 2 TO y.
      WHEN <fs_ttt>-f3.
        MOVE 3 TO y.
    ENDCASE.
  ENDIF.
  IF pruef = 11.
* Zeile finden
    LOOP AT it_ttt ASSIGNING <fs_ttt>.
      CASE y.
        WHEN 1.
          IF <fs_ttt>-f1 = 0.
            MOVE sy-tabix TO x.
          ENDIF.
        WHEN 2.
          IF <fs_ttt>-f2 = 0.
            MOVE sy-tabix TO x.
          ENDIF.
        WHEN 3.
          IF <fs_ttt>-f3 = 0.
            MOVE sy-tabix TO x.
          ENDIF.
      ENDCASE.
    ENDLOOP.

  ENDIF.



ENDFORM.

FORM spielen.

  WHILE pruef = 1 AND win = 0.
*  Zug zufällig finden
*  Siehe Heinweis im DATA Bereich
    IF x = 0 OR y = 0.
*      y = c_rnd->get_next( ).
*      x = c_rnd->get_next( ).
      CALL FUNCTION 'QF05_RANDOM_INTEGER'
       EXPORTING
         ran_int_max         = 3
         ran_int_min         = 1
       IMPORTING
         ran_int             = rndy
*       EXCEPTIONS
*         INVALID_INPUT       = 1
*         OTHERS              = 2
                .
      CALL FUNCTION 'QF05_RANDOM_INTEGER'
       EXPORTING
         ran_int_max         = 3
         ran_int_min         = 1
       IMPORTING
         ran_int             = rndx
*       EXCEPTIONS
*         INVALID_INPUT       = 1
*         OTHERS              = 2
                .
      MOVE rndx TO x.
      MOVE rndy TO y.
    ENDIF.
*  Verteidigung

    IF wer = 1.
      PERFORM taktik.
*      Defensivstrategie
      CHECK win EQ 0.
      PERFORM strategie USING 10.
      PERFORM strategie USING 2.
      MOVE 1 TO pruef.
    ENDIF.
* Zug prüfen
    LOOP AT it_ttt ASSIGNING <fs_ttt>.
      IF sy-tabix = x.
        CASE y.
          WHEN 1.
            IF <fs_ttt>-f1 EQ 0.
              MOVE wer TO <fs_ttt>-f1.
              MOVE 0 TO pruef.
            ELSE.
              MOVE 1 TO pruef.
            ENDIF.
          WHEN 2.
            IF <fs_ttt>-f2 EQ 0.
              MOVE wer TO <fs_ttt>-f2.
              MOVE 0 TO pruef.
            ELSE.
              MOVE 1 TO pruef.
            ENDIF.
          WHEN 3.
            IF <fs_ttt>-f3 EQ 0.
              MOVE wer TO <fs_ttt>-f3.
              MOVE 0 TO pruef.
            ELSE.
              MOVE 1 TO pruef.
            ENDIF.
        ENDCASE.
      ENDIF.
    ENDLOOP.
    IF pruef NE 0.
      MOVE 0 TO x.
      MOVE 0 TO y.
    ENDIF.
    IF pruef NE 0 AND wer EQ 5 AND a = 1.

      MESSAGE 'Belegt' TYPE 'E'.
    ENDIF.

*    SAP macht Zug direkt nach USR
    IF wer = 5 AND a = 1.
      MOVE 1 TO pruef.
      MOVE 1 TO wer.
      MOVE 0 TO x.
      MOVE 0 TO y.
      zug = zug + 1.
    ENDIF.
  ENDWHILE.
  zug = zug + 1.
  IF  wer = 5. " AND a = 0.
    MOVE 1 TO wer.
  ELSEIF wer = 1. " AND a = 0.
    MOVE 5 TO wer.
  ENDIF.

ENDFORM.


FORM ttt_feld.
* Keine Verweigung
  MOVE 1 TO sy-lsind.
* Felder prüfen
  LOOP AT it_ttt ASSIGNING <fs_ttt>.
    CASE sy-tabix.
      WHEN 1.
        MOVE 22 TO sy-linno.
      WHEN 2.
        MOVE 28 TO sy-linno.
      WHEN 3.
        MOVE 34 TO sy-linno.
    ENDCASE.
    MOVE 13 TO sy-colno.
    IF <fs_ttt>-f1 = 0.
      WRITE '   '.
    ELSEIF <fs_ttt>-f1 = 1.
      WRITE 'SAP'.
    ELSE.
      WRITE 'USR'.
    ENDIF.
    MOVE 26 TO sy-colno.
    IF <fs_ttt>-f2 = 0.
      WRITE '   '.
    ELSEIF <fs_ttt>-f2 = 1.
      WRITE 'SAP'.
    ELSE.
      WRITE 'USR'.
    ENDIF.
    MOVE 43 TO sy-colno.
    IF <fs_ttt>-f3 = 0.
      WRITE '   '.
    ELSEIF <fs_ttt>-f3 = 1.
      WRITE 'SAP'.
    ELSE.
      WRITE 'USR'.
    ENDIF.
  ENDLOOP.
* Feld malen
  SKIP TO LINE 20.
  DO 15 TIMES.
    WRITE 22 '|'.
    WRITE 38 '|'.
    NEW-LINE.
    IF sy-index = 5 OR sy-index = 10.
      ULINE AT 10(40).
      NEW-LINE.
    ENDIF.
  ENDDO.
*  Stretegiedaten
  MOVE 22 TO sy-linno.
  MOVE 60 TO sy-colno.
  WRITE: 'Strategische Daten'.
  NEW-LINE.
  MOVE 60 TO sy-colno. WRITE:  '#######################'.
  NEW-LINE.
  MOVE 60 TO sy-colno. WRITE:  'Zeile  1    ',<fs_win>-z1 .
  NEW-LINE.
  MOVE 60 TO sy-colno. WRITE:  'Zeile  2    ' ,<fs_win>-z2 .
  NEW-LINE.
  MOVE 60 TO sy-colno. WRITE:  'Zeile  3    ' ,<fs_win>-z3 .
  NEW-LINE.
  MOVE 60 TO sy-colno. WRITE:  '#######################'.
  NEW-LINE.
  MOVE 60 TO sy-colno. WRITE:  'Spalte 1    ',<fs_win>-s1 .
  NEW-LINE.
  MOVE 60 TO sy-colno. WRITE:  'Spalte 2    ' ,<fs_win>-s2 .
  NEW-LINE.
  MOVE 60 TO sy-colno. WRITE:  'Spalte 3    ' ,<fs_win>-s3 .
  NEW-LINE.
  MOVE 60 TO sy-colno. WRITE:  '#######################'.
  NEW-LINE.
  MOVE 60 TO sy-colno. WRITE:  'Diag. li re ',<fs_win>-d1 .
  NEW-LINE.
  MOVE 60 TO sy-colno. WRITE:  'Diag. re li ' ,<fs_win>-d2 .

ENDFORM.

FORM liko.
* Listenkopf
  MOVE 1 TO sy-lsind.
  MOVE 1 TO sy-linno.
  MOVE 1 TO sy-colno.
  FORMAT COLOR 4.
  WRITE 'Tic Tac Toe'.
  SKIP.
  WRITE: 'Liste ',sy-lsind,' Spieler Anzahl ',a,' Zeit ',sy-uzeit.
  SKIP.
  WRITE: 'Züge ', zug.
  CASE wer.
    WHEN 1.
      WRITE ' SAP ist am Zug.'.
    WHEN 5.
      WRITE ' USR ist am Zug.'.

  ENDCASE.
  FORMAT COLOR OFF.
  SKIP.
  WRITE 'F5 macht Spielzug.'.
  ULINE.
  IF a NE 0.
    MOVE 15 TO sy-linno.
    WRITE: 15 'mein Zug: Reihe ',x INPUT,',Spalte ',y INPUT.
    SET CURSOR FIELD x LINE 15.
  ENDIF.
  IF win NE 0.
    FORMAT COLOR 4.
    ULINE.
    CASE win.
      WHEN 1.
        WRITE 'SAP hat gewonnen'.
      WHEN 2.
        WRITE 'USR hat gewonnen'.
      WHEN 3.
        WRITE 'Unentschieden'.
    ENDCASE.
    win = 0.
    zug = 0.
    PERFORM nullstellung.
    wer = 1.
  ENDIF.
ENDFORM.

FORM nullstellung.

  IF it_ttt IS INITIAL.
    ASSIGN wa_ttt TO <fs_ttt>.
    DO 3 TIMES.
      MOVE '0' TO <fs_ttt>-f1.
      MOVE '0' TO <fs_ttt>-f2.
      MOVE '0' TO <fs_ttt>-f3.
      INSERT <fs_ttt>  INTO it_ttt INDEX sy-index.
    ENDDO.
    UNASSIGN <fs_ttt>.

  ELSE.
    LOOP AT it_ttt ASSIGNING <fs_ttt>.
      MOVE '0' TO <fs_ttt>-f1.
      MOVE '0' TO <fs_ttt>-f2.
      MOVE '0' TO <fs_ttt>-f3.
    ENDLOOP.
  ENDIF.
ENDFORM.

FORM taktik.

*   gewinner und taktische daten feststellen
  LOOP AT it_ttt ASSIGNING <fs_ttt>.
    CASE sy-tabix.
      WHEN 1.
        <fs_win>-z1 = <fs_ttt>-f1 + <fs_ttt>-f2 + <fs_ttt>-f3.
        IF <fs_win>-z1 = 3.
          win = 1.
        ELSEIF <fs_win>-z1 = 15.
          win = 2.
        ENDIF.
        <fs_win>-s1 = <fs_ttt>-f1.
        <fs_win>-s2 = <fs_ttt>-f2.
        <fs_win>-s3 = <fs_ttt>-f3.
        <fs_win>-d1 = <fs_ttt>-f1.
        <fs_win>-d2 = <fs_ttt>-f3.
      WHEN 2.
        <fs_win>-z2 = <fs_ttt>-f1 + <fs_ttt>-f2 + <fs_ttt>-f3.
        IF <fs_win>-z2 = 3.
          win = 1.
        ELSEIF <fs_win>-z2 = 15.
          win = 2.
        ENDIF.
        <fs_win>-s1 = <fs_win>-s1 + <fs_ttt>-f1.
        <fs_win>-s2 = <fs_win>-s2 + <fs_ttt>-f2.
        <fs_win>-s3 = <fs_win>-s3 + <fs_ttt>-f3.
        <fs_win>-d1 = <fs_win>-d1 + <fs_ttt>-f2.
        <fs_win>-d2 = <fs_win>-d2 + <fs_ttt>-f2.
      WHEN 3.
        <fs_win>-z3 = <fs_ttt>-f1 + <fs_ttt>-f2 + <fs_ttt>-f3.
        IF <fs_win>-z3 = 3.
          win = 1.
        ELSEIF <fs_win>-z3 = 15.
          win = 2.
        ENDIF.
        <fs_win>-s1 = <fs_win>-s1 + <fs_ttt>-f1.
        <fs_win>-s2 = <fs_win>-s2 + <fs_ttt>-f2.
        <fs_win>-s3 = <fs_win>-s3 + <fs_ttt>-f3.
        <fs_win>-d1 = <fs_win>-d1 + <fs_ttt>-f3.
        <fs_win>-d2 = <fs_win>-d2 + <fs_ttt>-f1.
        IF <fs_win>-s1 = 3 OR <fs_win>-s2 = 3 OR <fs_win>-s3 = 3.
          win = 1.
        ELSEIF <fs_win>-s1 = 15 OR <fs_win>-s2 = 15 OR <fs_win>-s3 = 15.
          win = 2.
        ENDIF.

        IF <fs_win>-d1 = 3 OR <fs_win>-d2 = 3.
          win = 1.
        ELSEIF <fs_win>-d1 = 15 OR <fs_win>-d2 = 15.
          win = 2.
        ENDIF.
    ENDCASE.
    IF zug = 9 AND win = 0.
      MOVE 3 TO win.
    ENDIF.

  ENDLOOP.
ENDFORM.

  • No labels