-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path2048.lua
130 lines (130 loc) · 1.98 KB
/
2048.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
local component=require("component")
local term=require("term")
local event=require("event")
local gpu=component.gpu
local map={
{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
}
local function up()
local valid=false
local rep
local function shift(x)
for y=2,4 do
if map[y][x]>0 and map[y-1][x]==0 then
map[y-1][x]=map[y][x]
map[y][x]=0
rep=true
valid=true
end
end
end
for x=1,4 do
rep=true
while rep do
rep=false
shift(x)
end
local y=1
while y<4 do
if map[y+1][x]==map[y][x] then
valid=true
map[y][x]=map[y][x]*2
map[y+1][x]=0
shift(x)
end
y=y+1
end
end
if not valid then
os.exit()
end
end
local function rightrotate(dir)
local nmap={{},{},{},{}}
for y,d in pairs(map) do
for x,n in pairs(d) do
nmap[x][5-y]=n
end
end
for k,v in pairs(map) do
map[k]=nmap[k]
end
end
local function leftrotate(dir)
local nmap={{},{},{},{}}
for y,d in pairs(map) do
for x,n in pairs(d) do
nmap[5-x][y]=n
end
end
for k,v in pairs(map) do
map[k]=nmap[k]
end
end
local function down()
rightrotate()
rightrotate()
up()
rightrotate()
rightrotate()
end
local function left()
rightrotate()
up()
leftrotate()
end
local function right()
leftrotate()
up()
rightrotate()
end
local function step()
term.clear()
local sp={}
for y=1,4 do
for x=1,4 do
if map[y][x]==0 then
table.insert(sp,{y,x})
end
end
end
if #sp==1 then
local y,x=table.unpack(sp[1])
map[y][x]=2
elseif #sp>0 then
local y,x=table.unpack(sp[math.random(1,#sp)])
map[y][x]=2
end
for y=1,4 do
for x=1,4 do
local n=tostring(map[y][x])
if n~="0" then
gpu.set(((x*5)-4)+(3-math.floor(#n/2)),(y*5)-2,n)
end
end
end
end
step()
print(xpcall(function()
while true do
local _,_,_,key=event.pull("key_down")
if key==200 then
up()
step()
elseif key==205 then
right()
step()
elseif key==208 then
down()
step()
elseif key==203 then
left()
step()
elseif key==16 then
break
end
end
end,debug.traceback))