Author Topic: Tiny Tetris  (Read 6141 times)

0 Members and 1 Guest are viewing this topic.

Offline Ed Davis

  • Newbie
  • Posts: 40
    • View Profile
Tiny Tetris
« on: June 25, 2021, 09:33:11 am »
On the Reddit tinycode programming forum:
https://www.reddit.com/r/tinycode/comments/o6m72h/tentris_a_10_line_x_72_characters_tetris_game_in/

Someone posted a GWBASIC version of Tetris:

Code: QB64: [Select]
  1. 0 P$="BJ@@QJI@JB@@IJQ@IIII@D@@JJ@@JJ@@QQJ@AC@@JII@CQ@@IIJ@CA@@JQQ@QC@@"
  2. 1 CLS:B$=CHR$(219):FOR Y=1 TO 19:PRINT,B$,B$:NEXT:PRINT,STRING$(15,B$)
  3. 2 X=19:C=X:R=C:WHILE R:D=13:FOR E=16 TO 28:D=D+(SCREEN(R,E)=219):NEXT
  4. 3 IF D THEN FOR E=16 TO 28:LOCATE C,E:PRINT CHR$(SCREEN(R,E)):NEXT:C=C-1
  5. 4 R=R-1:WEND:Y=0:Z=INT(RND*6)*8:Z=Z-(Z>39)*8:I=8-(Z>31)*8:WHILE 1:C$=""
  6. 5 H=0:GOSUB 8:C$=B$:IF H THEN R=Q:X=U:IF A$=""THEN Y=Y-1:GOSUB 8:GOTO 2
  7. 6 GOSUB 8:Q=R:U=X:A$=INKEY$:IF""=A$ THEN IF TIMER<S THEN 6 ELSE S=TIMER+.5
  8. 7 C$=" ":GOSUB 8:X=X+(A$="A")-(A$="D"):Y=Y-(A$=""):R=R-(A$="W")*4:WEND
  9. 8 FOR C=1 TO 4:D=ASC(MID$(P$,Z+R MOD I+C))-64:W=D\8:WHILE D:D=D-1 AND 7
  10. 9 H=H+(SCREEN(Y+C,X+W+D)>32):LOCATE Y+C,X+W+D:PRINT C$:WEND:NEXT:RETURN
  11.  

With a minor tweak (applied in the above posted code) it compiles and runs with QB64!  Cool!

FellippeHeitor

  • Guest
Re: Tiny Tetris
« Reply #1 on: June 25, 2021, 10:17:24 am »
Whoa!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Tiny Tetris
« Reply #2 on: June 25, 2021, 04:39:50 pm »
Cap lock on
  W
A  D

Nice!

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Tiny Tetris
« Reply #3 on: June 25, 2021, 08:05:44 pm »
What! No aliens?  Pretty cool. All in 10 lines of code! Nicely done!
Logic is the beginning of wisdom.

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Tiny Tetris
« Reply #4 on: June 25, 2021, 10:59:11 pm »
Oh pooie, and here I thought it was going to be truly tiny tetris using single pixel tetranomes on a 1920x1080 window. I wonder just how hard that would be to play?

My prototype Truly Tiny Tetris screen cap below.
Truly_Tiny_Tetris.jpg
* Truly_Tiny_Tetris.jpg (Filesize: 15.74 KB, Dimensions: 1920x1080, Views: 211)
Granted after becoming radioactive I only have a half-life!

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Tiny Tetris
« Reply #5 on: June 26, 2021, 02:28:03 am »
and then someone invented   END IF

half the code is :

can't even reverse engineer it.

In the days when 640k was too much

way too many comments
« Last Edit: June 26, 2021, 02:39:02 am by NOVARSEG »

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Tiny Tetris
« Reply #6 on: June 26, 2021, 03:07:23 am »
Cobalt,

Cannot wait to see the score and level indicators....
Logic is the beginning of wisdom.

Offline Ed Davis

  • Newbie
  • Posts: 40
    • View Profile
Re: Tiny Tetris
« Reply #7 on: June 26, 2021, 09:13:58 am »
and then someone invented   END IF
half the code is :
can't even reverse engineer it.

I tried my hand at cleaning it up.  I did not intentionally change any logic.  Here it is:
Code: QB64: [Select]
  1. p$ = "BJ@@QJI@JB@@IJQ@IIII@D@@JJ@@JJ@@QQJ@AC@@JII@CQ@@IIJ@CA@@JQQ@QC@@"
  2. b$ = chr$(219)
  3. for y = 1 TO 19
  4.   print,b$,b$
  5. print , string$(15, b$)
  6.  
  7. label2:
  8.  
  9. x = 19
  10. c = x: r = c
  11.   d = 13
  12.   for e = 16 TO 28
  13.     d = d + (screen(r, e) = 219)
  14.   next
  15.   if d then
  16.     for e = 16 TO 28
  17.       locate c, e
  18.       print chr$(screen(r, e))
  19.     next
  20.   end if
  21.   c = c - 1
  22.   r = r - 1
  23. y = 0
  24. z = int(rnd * 6) * 8
  25. z = z - (z > 39) * 8
  26. I = 8 - (z > 31) * 8
  27.   c$ = ""
  28.   h = 0
  29.   gosub label8
  30.   c$ = b$
  31.   if h then
  32.     r = q: x = u
  33.     if a$ = "" then
  34.       y = y - 1
  35.       gosub label8
  36.       goto label2
  37.     end if
  38.   end if
  39.  
  40. label6:
  41.  
  42.   gosub label8
  43.   q = r:  u = x
  44.   a$ = inkey$
  45.   if "" = a$ then
  46.     if timer < s then
  47.       goto label6
  48.     else
  49.       s = timer + .5
  50.     end if
  51.   end if
  52.   c$ = " "
  53.   gosub label8
  54.   x = x + (a$ = "A") - (a$ = "D")
  55.   y = y - (a$ = "")
  56.   r = r - (a$ = "W") * 4
  57.  
  58. label8:
  59.   for c = 1 TO 4
  60.     d = asc(mid$(p$, z + r mod I + c)) - 64
  61.     w = d \ 8
  62.     while d
  63.       d = d - 1 and 7
  64.       h = h + (screen(y + c, x + w + d) > 32)
  65.       locate y + c, x + w + d
  66.       print c$
  67.     wend
  68.   next
  69.  

All I did was convert the referenced line numbers to labels, and got rid of most of the multiple statements per line.  There were only two goto's in the original code, so that was nice.  And of course, would be nice to get rid of them :)

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Tiny Tetris
« Reply #8 on: June 27, 2021, 06:03:35 am »
On the Reddit tinycode programming forum:
https://www.reddit.com/r/tinycode/comments/o6m72h/tentris_a_10_line_x_72_characters_tetris_game_in/

Someone posted a GWBASIC version of Tetris:

Code: QB64: [Select]
  1. 0 P$="BJ@@QJI@JB@@IJQ@IIII@D@@JJ@@JJ@@QQJ@AC@@JII@CQ@@IIJ@CA@@JQQ@QC@@"
  2. 1 CLS:B$=CHR$(219):FOR Y=1 TO 19:PRINT,B$,B$:NEXT:PRINT,STRING$(15,B$)
  3. 2 X=19:C=X:R=C:WHILE R:D=13:FOR E=16 TO 28:D=D+(SCREEN(R,E)=219):NEXT
  4. 3 IF D THEN FOR E=16 TO 28:LOCATE C,E:PRINT CHR$(SCREEN(R,E)):NEXT:C=C-1
  5. 4 R=R-1:WEND:Y=0:Z=INT(RND*6)*8:Z=Z-(Z>39)*8:I=8-(Z>31)*8:WHILE 1:C$=""
  6. 5 H=0:GOSUB 8:C$=B$:IF H THEN R=Q:X=U:IF A$=""THEN Y=Y-1:GOSUB 8:GOTO 2
  7. 6 GOSUB 8:Q=R:U=X:A$=INKEY$:IF""=A$ THEN IF TIMER<S THEN 6 ELSE S=TIMER+.5
  8. 7 C$=" ":GOSUB 8:X=X+(A$="A")-(A$="D"):Y=Y-(A$=""):R=R-(A$="W")*4:WEND
  9. 8 FOR C=1 TO 4:D=ASC(MID$(P$,Z+R MOD I+C))-64:W=D\8:WHILE D:D=D-1 AND 7
  10. 9 H=H+(SCREEN(Y+C,X+W+D)>32):LOCATE Y+C,X+W+D:PRINT C$:WEND:NEXT:RETURN
  11.  

With a minor tweak (applied in the above posted code) it compiles and runs with QB64!  Cool!

This is good for any Basic Interpreter... few code few read again..

The other version is better for compiler and human coders but worse for Basic Interpreter
:-)  Human / Machine two poles in opposition.
Programming isn't difficult, only it's  consuming time and coffee

Offline MasterGy

  • Seasoned Forum Regular
  • Posts: 327
  • people lie, math never lies
    • View Profile
Re: Tiny Tetris
« Reply #9 on: June 27, 2021, 11:18:21 am »
this code is brilliant !!! :)

Offline Juan Tamarit

  • Newbie
  • Posts: 53
    • View Profile
Re: Tiny Tetris
« Reply #10 on: June 27, 2021, 01:12:34 pm »
I agree with NOVARSEC: half the code is ":". This should not be called a 10 line program. Otherwise anybody can make a 1 line program... just : every single line LOL

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: Tiny Tetris
« Reply #11 on: June 29, 2021, 12:56:58 pm »
This should not be called a 10 line program. Otherwise anybody can make a 1 line program... just : every single line LOL

yeah, it's clearly not a 10-liner -- the code is an optical illusion.

Now, the question bothering me:  is a true 10 line tetris possible?

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Tiny Tetris
« Reply #12 on: June 29, 2021, 10:20:55 pm »
yeah, it's clearly not a 10-liner -- the code is an optical illusion.

Now, the question bothering me:  is a true 10 line tetris possible?
Ummm…. Probably?

Pieces could be defined by

“SRU\” — Start, move Right, move Up, move diagional down and right.  Produces this piece:
   @
@@@

Other 4 command combos could define the rest of the pieces in a similar manner.  Just put them all in a single string to store them and manage them with MID$.  <—- All in 1 line of code.

Then it’s a DO…

   IF _KEYDOWN = (left arrow) then move to next entry in MID$ list MOD 4 to rotate ELSEIF key down….   ELSEIF key right…..   <— All in 1 line of code.

check screen to see if characters below are filled…. YES, place on top, check for full blocks NO… Move down

LOOP until no place to place block at top

5 lines for the base core (screen 0 to track pieces), so that gives a little wiggle room to try and do a minimalistic version in 10 lines.

https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: Tiny Tetris
« Reply #13 on: June 30, 2021, 06:55:53 pm »
Ummm…. Probably?

Pieces could be defined by

“SRU\” — Start, move Right, move Up, move diagional down and right.  Produces this piece:
   @
@@@

Other 4 command combos could define the rest of the pieces in a similar manner.  Just put them all in a single string to store them and manage them with MID$.  <—- All in 1 line of code.

Then it’s a DO…

   IF _KEYDOWN = (left arrow) then move to next entry in MID$ list MOD 4 to rotate ELSEIF key down….   ELSEIF key right…..   <— All in 1 line of code.

check screen to see if characters below are filled…. YES, place on top, check for full blocks NO… Move down

LOOP until no place to place block at top

5 lines for the base core (screen 0 to track pieces), so that gives a little wiggle room to try and do a minimalistic version in 10 lines.

QED

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Tiny Tetris
« Reply #14 on: June 30, 2021, 07:03:45 pm »
Quote
QED

??? where was the demo?

More like SS (Steve Says!) ;-)
« Last Edit: June 30, 2021, 07:05:37 pm by bplus »