-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.lua
116 lines (105 loc) · 2.35 KB
/
board.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
--[[
Squaricles
Copyright (C) 2017 ITR
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
]]--
Board = {width = 6, height = 10}
for y=-4,Board.height+3 do
Board[y] = {}
for x=-1,Board.width+2 do
if y>Board.height or x<1 or x>Board.width then
Board[y][x] = -1
else
Board[y][x] = 0
end
end
end
function Board:new()
local o = clone(self)
return o
end
function Board:findSquares()
local squares = {}
for size=math.min(self.width,self.height),1,-1 do
for y=1,self.height-size do
for x=1,self.width-size do
if self:isSquare(x,y,size) then
squares[#squares+1] = {
x = x,
y = y,
size = size,
color = self[y][x],
}
end
end
end
end
return squares
end
function Board:isSquare(x,y,size)
local color = self[y][x]
if color < 1 then
return false
end
local dx, dy = size,0
for i=1,4 do
if self[y+dy][x+dx] ~= color then
return false
end
dx,dy = dy,size-dx
end
return true
end
function Board:gravBoard()
for x=1,self.width do
local distance = 0
for y=self.height,-1,-1 do
if self[y][x]==0 then
distance = distance+1
elseif distance ~= 0 then
self[y+distance][x] = self[y][x]
if y+distance<1 then
self[y+distance][x] = 0
end
self[y][x] = 0
elseif y<1 then
self[y][x] = 0
end
end
end
end
function Board:removeSquares(squares)
for i=1,#squares do
local x,y = squares[i].x,squares[i].y
local dx,dy = 0,squares[i].size
for j=1,4 do
self[y+dy][x+dx] = 0
dx,dy = dy,squares[i].size-dx
end
end
end
function Board:findColor(color)
local found = {}
for y=1,self.height do
for x=1,self.width do
if self[y][x] == color then
found[#found+1] = {
x = x,
y = y,
size = 0,
color = color,
}
end
end
end
return found
end