Qbasicnews.com
February 25, 2020, 03:41:34 AM
 Pages: [1]
 Author Topic: Smallest Tetris Clone?  (Read 6064 times)
Antoni Gual
Na_th_an

Posts: 1434

 « on: August 03, 2007, 10:48:11 AM »

Program a tetris clone in the smallest possible source size.
The only condition is it must be playable, no fancy graphics required
ASCII or graphics, QB or FB.
Only built-in keywords allowed, no libraries!
Some people says it can be done in 100 lines or less..

 Logged

Antoni
Deleter
Na_th_an

Posts: 1293

 « Reply #1 on: August 06, 2007, 03:36:59 PM »

pshhhhh 100, 79 is all you need:
(fb code)
Code:
screen 12
dim myblock(-1 to 16,31) as integer, score as integer ,px as integer=7 ,py as integer=3 , gameover as integer ,myblockdata(6,2,2) as integer={{{0,1,0},{0,1,0},{0,1,0}},{{0,0,0},{0,1,0},{1,1,1}},{{1,1,0},{1,0,0},{1,0,0}},{{0,1,1},{0,0,1},{0,0,1}},{{0,0,0},{1,1,0},{0,1,1}},{{0,0,0},{0,1,1},{1,1,0}},{{0,0,0},{1,1,0},{1,1,0}}},tm as double=timer,godown as integer,newblock as integer
for tmpy as integer  = 15 to 0 step -1
for tmpx as integer =0 to 4
if tmpx<3 and tmpy < 3 then if myblockdata(2,tmpy,tmpx) then myblock(px+tmpx,py+tmpy)=-1
myblock(iif(tmpx<2,-1,iif(tmpx<4,16,tmpy)),iif(tmpx=1 or tmpx=3,tmpy+16,iif(tmpx=4,30,tmpy)))=1
next tmpx,tmpy
do
locate 1,1
? "Level:"+str((int(score/2500)+1))+chr(13)+chr(10)+"Score:"+str(score)
for tmpx as integer = 0 to 30*16-1
if myblock((tmpx mod 16),int(tmpx/16))<>0 then line((tmpx mod 16)*20+160,(int(tmpx/16)-6)*20)-((tmpx mod 16)*20+20+158,(int(tmpx/16)-6)*20+20-2),7,bf else line((tmpx mod 16)*20+160,(int(tmpx/16)-6)*20)-((tmpx mod 16)*20+20+159,(int(tmpx/16)-6)*20+20-1),8,bf
next tmpx
sleep 50,1
sleep 500-(int(score/2500)+1)*50
newblock=int(rnd*5)
if timer-tm>(.5-(int(score/2500)+1)*.05) or multikey(80) then godown=-1 else godown=0
dim canrotate as integer =-1, donttouch as integer =0,canmove as integer =-1, rotdata(2,2,1) as integer=>{{{2,0},{1,0},{0,0}},{{2,1},{1,1},{0,1}},{{2,2},{1,2},{0,2}}},tmpblock(-1 to 3,2) as integer=>{{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}},myxdir as integer=multikey(75)*2+1
if multikey(72) then
for tmpx as integer=0 to 9-1
if myblock(px+(tmpx mod 3),py+int(tmpx/3))<> 0 then
if myblock(px+(tmpx mod 3),py+int(tmpx/3))=-1 then    tmpblock((tmpx mod 3),int(tmpx/3))=myblock(px+(tmpx mod 3),py+int(tmpx/3))
if myblock(px+(tmpx mod 3),py+int(tmpx/3))=-1 then    if myblock(px+rotdata((tmpx mod 3),int(tmpx/3),0) , py+rotdata((tmpx mod 3),int(tmpx/3),1)  )<>0 then if myblock(px+rotdata((tmpx mod 3),int(tmpx/3),0),py+rotdata((tmpx mod 3),int(tmpx/3),1))=1 then canrotate=0
end if
next tmpx
if canrotate=-1 then
for tmpx as integer=0 to 9-1
if myblock(px+rotdata((tmpx mod 3),int(tmpx/3),0),py+rotdata((tmpx mod 3),int(tmpx/3),1))<> 0 then if myblock(px+rotdata((tmpx mod 3),int(tmpx/3),0),py+rotdata((tmpx mod 3),int(tmpx/3),1))=1 then donttouch=-1
if donttouch=0 then myblock(px+rotdata((tmpx mod 3),int(tmpx/3),0),py+rotdata((tmpx mod 3),int(tmpx/3),1))=tmpblock((tmpx mod 3),int(tmpx/3)) else donttouch=0
tmpblock((tmpx mod 3),int(tmpx/3))=0
next tmpx
end if
elseif (multikey(75) xor multikey(77)) and (px+multikey(75)*2+1 >-2 ) and (px + multikey(75)*2+1 < 15) then
for tmpx as integer = 0 to 9-1
if myblock(px+(tmpx mod 3),py+int(tmpx/3))<>0 then if myblock(px+(tmpx mod 3),py+int(tmpx/3))=-1 then if myblock(px+(tmpx mod 3)+myxdir,py+int(tmpx/3))<>0 then if myblock(px+(tmpx mod 3)+myxdir,py+int(tmpx/3))=1 then canmove=0
if myblock(px+(tmpx mod 3),py+int(tmpx/3))<>0 then if myblock(px+(tmpx mod 3),py+int(tmpx/3))=-1 then tmpblock((tmpx mod 3),int(tmpx/3))=myblock(px+(tmpx mod 3),py+int(tmpx/3))
next tmpx
if canmove then
px+=myxdir
for tmpx as integer = iif(myxdir=-1,0,-1) to iif(myxdir=-1,3,2)
for tmpy as integer =0 to 2
if myblock(px+tmpx,py+tmpy)<>0 then if myblock(px+tmpx,py+tmpy)=1 then donttouch=-1
if donttouch=0 then myblock(px+tmpx,py+tmpy)=tmpblock(tmpx,tmpy) else donttouch=0
next tmpx,tmpy
end if
end if
if godown=-1 then
canmove=-1
for tmpx as integer=0 to 9-1
if myblock(px+(tmpx mod 3),py+int(tmpx/3))<>0 then if myblock(px+(tmpx mod 3),py+int(tmpx/3))=-1 then if myblock(px+(tmpx mod 3),py+int(tmpx/3)+1)<>0 then if myblock(px+(tmpx mod 3),py+int(tmpx/3)+1)=1 then canmove=0
next tmpx
for tmpx as integer = 4*3-1 to 0 step -1
if canmove=0 then    if int(tmpx/3)<3 then if myblock(px+(tmpx mod 3),py+int(tmpx/3))<>0 then myblock(px+(tmpx mod 3),py+int(tmpx/3))=1
if canmove=0 then    if int(tmpx/3)<3 then if myblockdata(newblock,int(tmpx/3),(tmpx mod 3)) then myblock(7+(tmpx mod 3),int(tmpx/3)+3)=-1
if canmove<>0 then    if myblock((tmpx mod 3)+px,int(tmpx/3)+py)<>0 then if myblock((tmpx mod 3)+px,int(tmpx/3)+py)=1 then donttouch=-1
if canmove<>0 then    if myblock((tmpx mod 3)+px,int(tmpx/3)+py-1)<> 0 then if myblock((tmpx mod 3)+px,int(tmpx/3)+py-1)=1 then donttouch=-1
if canmove<>0 then    if myblock((tmpx mod 3)+px,int(tmpx/3)+py-1)<> 0 then if myblock((tmpx mod 3)+px,int(tmpx/3)+py-1)=1 then if myblock((tmpx mod 3)+px,int(tmpx/3)+py)<>0 then if myblock((tmpx mod 3)+px,int(tmpx/3)+py)=-1 then myblock((tmpx mod 3)+px,int(tmpx/3)+py)=0
if canmove<>0 then    if donttouch=0 then myblock((tmpx mod 3)+px,int(tmpx/3)+py)=myblock((tmpx mod 3)+px,int(tmpx/3)+py-1) else donttouch=0
next tmpx
if canmove=0 then py=3 else py+=1
if canmove=0 then px=7
end if
if timer-tm>(.5-(int(score/2500)+1)*.05) then tm=timer
dim exists as integer , shiftdown as integer =0
for tmpx as integer= 30*16-1 to 0 step -1
if (tmpx mod 16)=15 then exists=-1
if (myblock((tmpx mod 16),int(tmpx/16))<>0) then    if myblock((tmpx mod 16),int(tmpx/16))=-1 then exists=0 else if int(tmpx/16)=6 then gameover=-1
if (myblock((tmpx mod 16),int(tmpx/16))<>0) then    if int(tmpx/16)<30-shiftdown then if myblock((tmpx mod 16),int(tmpx/16))=1 and shiftdown>0 then myblock((tmpx mod 16),int(tmpx/16)+shiftdown)=myblock((tmpx mod 16),int(tmpx/16))
if (myblock((tmpx mod 16),int(tmpx/16))<>0) then    if int(tmpx/16)<30-shiftdown then if myblock((tmpx mod 16),int(tmpx/16))=1 and shiftdown>0 then myblock((tmpx mod 16),int(tmpx/16))=0
if (myblock((tmpx mod 16),int(tmpx/16))=0) then    exists=0
if (tmpx mod 16)=0 and exists=-1 then
for tmpx2 as integer =0 to 15
myblock(tmpx2,int(tmpx/16)+shiftdown)=0
score+=10
next
shiftdown+=1
end if
next
loop until multikey(1) or gameover

not a single #define or colon....

edit: modified, now only 79 lines...
 « Last Edit: August 06, 2007, 03:49:15 PM by Deleter » Logged

Antoni Gual
Na_th_an

Posts: 1434

 « Reply #2 on: August 09, 2007, 06:25:22 PM »

Great work, Deleter!
Tetris is a game that I always was afraid of coding, it looked too complicated to me. If it can be reduced to 79 lines I will grasp it one of these days.

BTW: I must change that avatar of mine, I guess It's  scaring young people..
 Logged

Antoni
Dav