Tetris/Julia: Difference between revisions

Content added Content deleted
Line 12: Line 12:
# constructor: Z = B4(101,[0 0; 0 1; 1 1; 1 2])
# constructor: Z = B4(101,[0 0; 0 1; 1 1; 1 2])
function B4(bg,A::Array{Int,2}) # - fills in centered offsets for tetromino
function B4(bg,A::Array{Int,2}) # - fills in centered offsets for tetromino
new(bg, A .- sum.(extrema(A,dims=1)).>>1)
new(bg,A.-sum.(extrema(A,dims=1)).>>1)
end end
end end
# print tetromino on playfield
# print tetromino on playfield
Line 21: Line 21:
# hit other B4s or border?
# hit other B4s or border?
function hit(B,r,c)
function hit(B,r,c)
for i=1:4 BD[r+B.d[i,1],c>>1+B.d[i,2]+2]>0 && return true end
for i=1:4 BD[r+B.d[i,1],c>>1+B.d[i,2]+2]>0 && return true end; false
return false
end
end
# move tetrominos left/right/down, if no hit
# move tetrominos left/right/down, if no hit
Line 37: Line 36:
for j = c.+(0,+2,-2) # try shifted positions
for j = c.+(0,+2,-2) # try shifted positions
hit(A,r,j) || (draw(B,r,c,0); draw(A,r,j); return (A,j))
hit(A,r,j) || (draw(B,r,c,0); draw(A,r,j); return (A,j))
end; return (B,c) # cannot rotate: all 3 positions hit
end
return (B,c) # cannot rotate: all 3 positions hit
end
end
# record-place,clear-full-lines,drop-above,score
# record-place,clear-full-lines,drop-above,score
Line 118: Line 116:
if (Ifo = !Ifo) io = open(Base.source_path()); readuntil(io,"="^6)
if (Ifo = !Ifo) io = open(Base.source_path()); readuntil(io,"="^6)
println("\e[0m\e[32H$(readuntil(io,"="^6))"); close(io)
println("\e[0m\e[32H$(readuntil(io,"="^6))"); close(io)
else
else print("\e[0m\e[32H\e[J") # erase txt
print("\e[0m\e[32H\e[J") # erase txt
end end end
end end end
sleep(0.01) # not to take all CPU core time
sleep(0.01) # not to take all CPU core time
Line 219: Line 216:
Label - GoTo (for restart) require the main loop enclosed in Begin..End.
Label - GoTo (for restart) require the main loop enclosed in Begin..End.
Several instructions are written in single lines of at most 84 chars,
Several instructions are written in single lines of at most 84 chars,
to keep the program less than 110 non-comment lines!
to keep the program around 100 non-comment lines long!
#.. Comments denote tasks, explain important points.
#.. Comments denote tasks, explain important points.
The program has embedded Unicode characters. They can be replaced with
The program has embedded Unicode characters. They can be replaced with