Tetris/Julia: Difference between revisions
m
Fixed syntax highlighting.
m (Fixed syntax highlighting.) |
|||
(4 intermediate revisions by 2 users not shown) | |||
Line 1:
<span style='font-family: "Linux Libertine",Georgia,Times,serif;font-size:150%;'>[[Julia]]</span><hr>
This is a fairly complete, configurable pure Julia implementation of Tetris, played in the standard Julia terminal. No external packages are used.
Line 6:
{{works with|Julia|1.0}}
<syntaxhighlight lang="julia">struct B4 # Tetromino made of 4 Blocks
bg::Int # ANSI code of tetromino color (bground of " ")
d::Array{Int,2} # d[b,1:2] = row,col/2 offset of block b
# constructor:
# print tetromino on playfield▼
for i=1:4 print("\e[$(r+b.d[i,1]);$(c+2b.d[i,2])H ") end▼
end
#
Base.iterate(B::B4,i=1) = i<5 ? (B.d[i,:],i+1) : nothing
drw(B,r,c)=(for(i,j)=B print("\e[$(B.bg)m\e[$(r+i);$(c+2j)H ") end)
# move tetrominos left/right/down, if no hit▼
TMv || (global tmr = Timer(tm)); return (r,c)▼
hit(B,r+x,c+y) || (clr(B,r,c); drw(B,r+=x,c+=y))
end
▲ # rotate tetrominos left/right, if no hit
function rot(B,r,c,rt=0) # ROTATE tetrominos left/right, if no hit
▲ TMv || (global tmr = Timer(tm))
for j = c.+(0,+2,-2) # try shifted positions
hit(A,r,j) || (
end; return (B,c) # cannot rotate: all 3 positions hit
end
function mrk(B,r,c) # RECORD
global lines,
for
for i in r.+sort(unique(B.d[:,1])) #
if all(BD[i,
for j
print("\e[0m\e[$i;2H$s20"); sleep(0.3)
end end # update changed display from board data ->
for i=1:
lines+=n; level=isqrt(lines>>3)
end
▲ # function for dialog confirmation
function
end
#
#
ccall(:jl_tty_set_mode,Cint,(Ptr{Cvoid},Cint),stdin.handle,1)==0 ||
throw("Terminal cannot enter raw mode.") # raw terminal mode to catch keystrokes
const chnl = Channel{Array{UInt8,1}}(0) # unbuffered channel for key codes
@async while true put!(chnl,readavailable(stdin)) end # task, catching keystrokes
I = B4(106,[0 0; 0 1; 0 2; 0 3]) # define the 7 tetrominos
Line 72 ⟶ 66:
begin @label RESTART # @label - @goto: require begin..end
global lines,score,level,s20,eq = 0,0,0," "^20,"="^6
BD = fill(0,
print("\e[0m\e[2J\e[?25l\e[1H") #
print("\n\n$s20\n$s20"); X0 = (I,T,O,S,Z,J,L)[rand(1:7)]▼
while true # random B4,
global lines,score,level,tm,tmr,LDr,TMv,Ifo
global X0,X,r,c = (I,T,O,S,Z,J,L)[rand(1:7)],X0,2,10
print("\n Level =\t$level\n Lines filled =\t$lines\n Score =\t$score")
tm=(.8-level*.007)^level; tmr=Timer(0.5) #
hit(X,r,c) && (while true
while isready(chnl) take!(chnl) end # flush queued keystrokes, max 1
while true
global X,r,c,tm,tmr
if !isopen(tmr) # time to drop tetromino by a line
hit(X,r+1,c) && (
end
global X,r,c,score
ch = take!(chnl) # take keys
Line 105 ⟶ 96:
elseif ch==[0x61] X,c=rot(X,r,c) # a
elseif ch==[0x64] X,c=rot(X,r,c,1) # d
elseif ch==[0x20] r0=r;
while !hit(X,r+=1,c) end;
elseif ch==[0x1b,0x5b,0x31,0x7e] # HOME
elseif ch==[0x1b,0x5b,0x34,0x7e] # END
elseif ch
elseif ch
elseif ch
else print("\e[0m\e[32H\e[J") # erase txt
end end end
sleep(0.01) # not to take all CPU
end end end</
The following long comment block has to be appended to program code:
<pre>#====== Julia-TETRIS by Laszlo Hars, Version 1.
-- Key assignments --
======
The game is played in the Julia standard Unicode character based Terminal
Line 160 ⟶ 144:
Number of LINES cleared
SCORE
Keys for short Info, Locked-drop-, and Timed
Game LEVEL: 0,1...= [sqrt(LINES/8)]
Increase
SCORE: Cleared_Line values: *(LEVEL+1)
Line 201 ⟶ 185:
White 107 L ██████
Julia
Terminal is switched to RAW mode to catch keystrokes.
Non-blocking
Keystrokes are transferred to the main program through a CHANNEL.
STRUCTs (B4) describe Tetrominos, with color code and coordinates of blocks.
A TIMER controls the dropping speed of tetrominos.▼
At construction the coordinates get centralized.
therefore many GLOBAL variables have to be declared and used▼
Memory used by orphan instance is cleared by internal GARBAGE COLLECTION.
Out of range indices are caught with TRY..CATCH, to keep tetrominos in board.
ITERATE is defined for tetrominos, looping over the coordinates of blocks.
In loops when variables get assigned before other use, Julia 1.0 assumes
For simplicity Int variables are used; in 64-bit OS versions: 8 Bytes.
In most places 1 Byte Int8 (or Int16, Int32) would work.
Only
▲ Assigning Struct to a variable only creates a reference.
▲ Label - GoTo (for restart) require the main loop enclosed in Begin..End.
Several instructions are written in single lines of at most 84 chars,
to keep the program
#.. Comments denote tasks, explain important points.
\u{hex_digits}
======#</pre>
|