RosettaCodeData/Task/2048/EasyLang/2048.easy
2026-04-30 12:34:36 -04:00

161 lines
2.8 KiB
Text

len brd[] 16
gbackground 987
proc newtile .
for i to 16 : if brd[i] = 0 : break 1
if i > 16 : return
v = 2
if randomf < 0.1 : v = 4
repeat
ind = random 1 16
until brd[ind] = 0
.
brd[ind] = v
.
global pts stat .
proc show .
gclear
gtextsize 5
gcolor 222
gtext 10 94 "2048 Game"
gtext 60 94 "Pts: " & pts
gtextsize 7
gcolor 876
grect 9 9 82 82
gcolor 987
grect 10 10 80 80
for i to 16 : if brd[i] <> 0
x = i mod1 4 * 20 - 9.5
y = i div1 4 * 20 - 9.5
gcolor 765
grect x y 19 19
v = brd[i]
h = 2 * floor log v 10
gcolor 000
gtext x + 7 - h y + 7 brd[i]
.
.
proc init .
for i to 16 : brd[i] = 0
newtile
newtile
stat = 0
pts = 0
show
.
init
#
dir[] = [ -4 -1 4 1 ]
start[] = [ 16 4 1 13 ]
proc domove indk test &moved .
moved = 0
dir = dir[indk]
bdir = dir[(indk + 1) mod1 4]
start = start[indk]
for i to 4
h0 = start + dir
for j to 3
if brd[h0] <> 0
v = brd[h0]
h = h0
while h <> start and brd[h - dir] = 0 : h -= dir
if h <> h0
moved = 1
if test = 1 : return
.
if h <> start and brd[h - dir] = v
moved = 1
if test = 1 : return
v *= 2
pts += v
brd[h - dir] = -v
if v = 2048 : stat = 1
v = 0
.
brd[h0] = 0
brd[h] = v
.
h0 += dir
.
h0 = start
for j to 3
brd[h0] = abs brd[h0]
h0 += dir
.
sleep 0.1
show
start += bdir
.
.
proc handle indk .
if indk = 0 : return
domove indk 0 moved
if moved = 1
newtile
sleep 0.2
show
if stat = 0
stat = 2
for indk to 4
domove indk 1 moved
if moved = 1
stat = 0
break 1
.
.
.
.
if stat <> 0
gtextsize 5
if stat = 1
stat = 0
gtext 10 3 "You got 2048 😊"
else
gtext 10 3 "No more moves 🙁"
.
.
.
on mouse_down
mx = mouse_x
my = mouse_y
.
proc handle_mup .
dx = mouse_x - mx
dy = mouse_y - my
indk = 0
if abs dx > abs dy
if abs dx > 3
indk = 4
if dx > 0 : indk = 2
.
else
if abs dy > 3
indk = 3
if dy > 0 : indk = 1
.
.
if indk <> 0 and stat = 2
init
else
handle indk
.
.
on mouse_up
handle_mup
.
on key_down
if stat = 2
if keybkey = " " : init
return
.
indk = 0
if keybkey = "ArrowUp"
indk = 1
elif keybkey = "ArrowRight"
indk = 2
elif keybkey = "ArrowDown"
indk = 3
elif keybkey = "ArrowLeft"
indk = 4
.
handle indk
.