-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.lua
155 lines (142 loc) · 3.3 KB
/
logic.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
function resetGame()
field = {
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
}
gameOver = false
field = insertNumber(field)
platform.window:invalidate()
end
function areFreeFields(grid)
for row = 1, 4 do
for col = 1, 4 do
if grid[row][col] == 0 then
return true
end
end
end
return false
end
function gameLost(grid)
if areFreeFields(grid) then return false end
for row = 1, 4 do
for cell = 1, 3 do
if(grid[row][cell] == grid[row][cell+1]) then
return false
end
end
end
grid = rotateClockwise(grid)
for row = 1, 4 do
for cell = 1, 3 do
if(grid[row][cell] == grid[row][cell+1]) then
return false
end
end
end
return true
end
function insertNumber(f)
local i = math.random(1, 4)
local j = math.random(1, 4)
while (f[i][j] ~= 0) do
i = math.random(1, 4)
j = math.random(1, 4)
end
f[i][j] = math.random(1, 2)
return f
end
-- left
function move(f)
for i=1, 4 do
for j=1, 4 do
if f[i][j] == 0 then
for k=j, 4 do
if f[i][k] ~=0 then
f[i][j] = f[i][k]
f[i][k] = 0
break
end
end
end
end
end
return f
end
-- left
function merge(grid)
local new = grid
for row = 1, 4 do
for cell = 1, 3 do
if(grid[row][cell] == new[row][cell+1] and grid[row][cell] ~= 0) then
new[row][cell] = new[row][cell] + 1
new[row][cell+1] = 0
end
end
end
return grid
end
function rotateClockwise(f)
local new = {{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}}
for i=1, 4 do
for j=1, 4 do
new[j][i] = f[i][j]
end
end
return new
end
function mirrorHoriz(f)
local new = {{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}}
for i=1, 4 do
for j=1, 4 do
new[i][5-j] = f[i][j]
end
end
return new
end
function fieldChanged(old, new)
for i=1, 4 do
for j=1, 4 do
if old[i][j] ~= new[i][j] then
return true
end
end
end
return false
end
function handleMove(dir)
if dir == "up" then
field = rotateClockwise(field)
field = move(field)
field = merge(field)
field = move(field)
field = rotateClockwise(field)
field = rotateClockwise(field)
field = rotateClockwise(field)
end
if dir == "down" then
field = rotateClockwise(field)
field = mirrorHoriz(field)
field = move(field)
field = merge(field)
field = move(field)
field = mirrorHoriz(field)
field = rotateClockwise(field)
field = rotateClockwise(field)
field = rotateClockwise(field)
end
if dir == "left" then
field = move(field)
field = merge(field)
field = move(field)
end
if dir == "right" then
field = mirrorHoriz(field)
field = move(field)
field = merge(field)
field = move(field)
field = mirrorHoriz(field)
end
end