Skip to Content
Author's profile photo Michael Keller

Maze generator

Today, no business topic but it’s play time 🙂 A couple of days ago, I stumbled upon the free book “10 PRINT CHR$(205.5+RND(1)); : GOTO 10“. No joke, that’s the title of the book. It’s about an one-line Commodore 64 BASIC program that prints a maze to the screen. Because I’m a big fan of Retrocomputing, I browsed the book a little bit.

Afterwards I asked myself what ABAP report I could write in one line of 40 characters with a nice output. The answer was sobering: Nothing interesting. Even with 80 characters I just printed vertical lines to the screen 🙂 Here is my approach.

REPORT z.DO 10 TIMES.DO 80 TIMES.WRITE AT sy-index(1) '|'.ENDDO.NEW-LINE.ENDDO.

Finally I decided to give up the length restriction and wrote my own random maze generator as a homage to the original BASIC program. It uses the WRITE instruction and the characters “-” and “|” to build frames. You will find it on GitHub, too.

REPORT z_random_maze.

DATA: lr_random_int TYPE REF TO cl_abap_random_int,
      lv_random_int TYPE i,
      lv_position_x TYPE i,
      lv_position_y TYPE i,
      lv_maze_part  TYPE char2.

lr_random_int = cl_abap_random_int=>create( seed = CONV i( sy-uzeit ) min = 1 max = 9 ).

DO 20 TIMES.
  lv_position_y = sy-index.
  lv_position_x = 1.
  DO 30 TIMES.
    CLEAR lv_maze_part.
    IF lv_position_y = 20.
      lv_maze_part = '--'.
    ELSE.
      lv_random_int = lr_random_int->get_next( ).
      IF lv_random_int < 5.
        lv_maze_part = '|'.
      ELSEIF lv_random_int > 7.
        lv_maze_part = '--'.
      ENDIF.
    ENDIF.
    WRITE AT lv_position_x(2) lv_maze_part.
    lv_position_x = lv_position_x + 2.
  ENDDO.
  NEW-LINE.
ENDDO.

Relax your eyes, try to find your way through the maze and have fun 🙂 Here is a little example of the report output. And if you have a good idea for an interesting one-liner in ABAP, let me know.

Assigned Tags

      23 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Christopher Solomon
      Christopher Solomon

      VERY cool!!!!

       

      Author's profile photo Jelena Perfiljeva
      Jelena Perfiljeva

      Pretty cool! Nice to know ABAP is still good for something. 🙂

      Thanks for sharing!

       

      Author's profile photo Uwe Fetzer
      Uwe Fetzer

      Challenge accepted

      REPORT z.DATA(r) = cl_abap_random_int=>create( min = 1 max = 3 ).DO 10 TIMES.DO 80 TIMES.WRITE SWITCH #( r->get_next( ) WHEN 1 THEN '-' WHEN 2 THEN '|' ELSE ` ` ) NO-GAP.ENDDO.NEW-LINE.ENDDO.ULINE (80).
      Author's profile photo Michael Keller
      Michael Keller
      Blog Post Author

      nice combination of WRITE and SWITCH statement 🙂

       

      Author's profile photo Horst Keller
      Horst Keller

       

      Should I put it as an example into the documentation 😉

      Author's profile photo Uwe Fetzer
      Uwe Fetzer

      🙂

      Author's profile photo Christian Guenter
      Christian Guenter

      Hi Michael,

      nice one 🙂

      Btw. sharing your code with abapGit would make it easier to install

      Author's profile photo Michael Keller
      Michael Keller
      Blog Post Author

      I'm very interested in. Next week I have more free time to look at abapGit. There is a lot of unreleased stuff lying here around. A good chance to start with abapGit :-))

      Author's profile photo Jacques Nomssi Nzali
      Jacques Nomssi Nzali

      make full use of ABAP's strengths 🙂

      SELECT * FROM zmaze INTO @DATA(line) UP TO 10 ROWS.
        WRITE :/ line.
      ENDSELECT.
      ULINE (80).

      JNN

      Author's profile photo Oliver Wahrstötter
      Oliver Wahrstötter

      nice mix of modern ABAP (inline declaration) and old dinosaurs like ENDSELECT and WRITE 😉

       

      Author's profile photo Enno Wulff
      Enno Wulff

      I doubt that my Apfelmännchen can be squezzed into one line, but it can be shorten quite a lot.

      fits to retro programming (at least the graphics...)

      http://www.tricktresor.de/blog/apfelmaennchen/

      Here is a very interesting website that collects 140-character-javascripts: https://www.dwitter.net/

       

       

      Author's profile photo Michelle Crapo
      Michelle Crapo

      What a fun post!!!!   Thank you for the nice break.

      Michelle

      Author's profile photo Michael Keller
      Michael Keller
      Blog Post Author

      You're welcome! I was really surprised of the interest. But perhaps we need more fun blogs to have more nice breaks 😉

      Author's profile photo Julian Phillips
      Julian Phillips

      Thats what we need - more ABAP computer games!

       

      Author's profile photo Michael Keller
      Michael Keller
      Blog Post Author

      I support your request. Between all the business stuff we should have the chance to relax a minute without log out from our favorite erp 🙂 By the way: Enno has a nice collection of ABAP games under "http://www.tricktresor.de/blog/keywords/spiele/".

      Author's profile photo Mike Pokraka
      Mike Pokraka

      https://blogs.sap.com/2016/01/18/three-abap-games/

       

      Author's profile photo Julian Phillips
      Julian Phillips

      Great stuff, I shall do some reading up!

       

      Author's profile photo Richard Harper
      Richard Harper

      Have a look at http://www.richard-harper.me.uk/Kb/default.html  Topic 'Downtime'.

      Author's profile photo Mike Pokraka
      Mike Pokraka

      Nice one. Well it depends on what you define as one-liner. Do we want to differentiate between number of statements or lines? The C64 example is two statements.

      I think the king of one-liners is Horst Keller , with gems like

      https://blogs.sap.com/2015/12/21/merry-happy-etc/ and

      https://blogs.sap.com/2015/04/10/abap-obfuscation-riddle/

       

      Author's profile photo Horst Keller
      Horst Keller

       

      Nice example!

      In the system you find:

      DEMO_JAWBREAKER_HTML_740,

      DEMO_MINESWEEPER_740,

      DEMO_GAME_2048_740

      https://help.sap.com/http.svc/rc/abapdocu_752_index_htm/7.52/en-US/index.htm?file=abenexpressions_abexas.htm

      Author's profile photo Florian Henninger
      Florian Henninger

      Thanks for sharing. Home is getting better.

      Awesome post. Did you see what Christian Drumm can do with mazes?

       

      ~Florian

      Author's profile photo Richard Harper
      Richard Harper

      Hmmm....

       

      Retro computing....  You've set me going again.

      I started a project to collect emulators or simulators of all the systems I have used during my programming career from a UK101 right upto an IBM System 370 running MVS and JES2 which I have managed to do (I(ncluding DGC Novas,  PDP 11/70's,  Vaxes etc etc....)

      One thing I want to do is to get an 'IDES' version of R/2 and host it on the web so the newbies amongst us can see what they are missing....

      I keep on bugging Horst Keller occasionally to see if he can help but I think he thinks I'm mad.....

      Regards

      Rich

      Author's profile photo Michael Keller
      Michael Keller
      Blog Post Author

      I think that's a good idea. Every hardware and software is a little piece of computer history. In my office I have a printout of the Applesoft BASIC sourcecode of Breakout. Every time someone complains about modern development environments and programming languages, I ask if he would prefer to develop again in Applesoft BASIC 🙂 Unfortunately, for many people, there is no comparison of how great our technologies are today.