Lua,
pasted
on Sep 25:
|
cubes = {}
widthcubes = 6
heightcubes = 12
newheight = 12
newwidth = 6
pstr = ""
rows = {}
taken = 0
z = 0
math.randomseed(os.time())
function newgrid()
for i = 1,newheight do
cubes[i] = {}
for i2 = 1,newwidth do
colv = math.random(1,3)
cubes[i][i2] = colv
--colorv = colorv .. cubes[i][i2]
end
end
widthcubes = newwidth
heightcubes = newheight
--draw out values
zstr = "|"
zstra = {}
print("--CUBES COLOR VALUES:---")
for i = 1,newheight do
for i2 = 1,newwidth do
zstr = zstr .. cubes[i][i2] .. "|"
zstra[i] = zstr
end
--print(zstr)
zstr = "|"
end
x = newheight
for i = 1,newheight do
print(zstra[x])
x = x - 1
end
print("-------------------------------")
print(" ")
print("--BEGIN CHECKING:---")
end
function checkmatches()
taken = 1
takingx = {}
takingy = {}
rows[1] = 0
pstr = ""
z = 1
for cubey = 1, heightcubes do
for cubex = 1, widthcubes do
if (cubes[cubex][cubey] ~= 0) then
checkingcol = cubes[cubex][cubey]
--use dx and dy variables to simulate the directions to be checked
for dx = -1,1 do -- (down, right, downleft, and downright)
for dy = 0,1 do
--special cases that don;t need to be checked
if not (dx==0 and dy == 0) and not (dx == -1 and dy == 0) then
local rowsize = 1 --how many of this color are in a straight line so far
rows[z] = "start: " .. cubex
rows[z] = rows[z] .. "," .. cubey
while true do
--find the cube that is, rowsize untis away in direction dx,dy
local tempx,tempy = cubex+dx*rowsize,cubey+dy*rowsize
--if out of range, stop now
if tempx < 1 or tempy < 1 or tempx > #cubes or tempy > #cubes[1] then
z = 1
break
end
--if same color ,add 1 to rowsize and keep going
if cubes[tempx][tempy] == checkingcol then
if ( z == 1 ) then
print("Start new check:")
--rows[z] = "start: " .. cubex .. "," .. cubey
rows[z] = cubex .. "," .. cubey
z = z + 1
rows[z] = tempx .. "," .. tempy
else
rows[z] = tempx .. "," .. tempy
--print(tempx,tempy)
end
rowsize = rowsize + 1
takingx[z] = tempx
takingy[z] = tempy
--rows[z] = rows[z] .. " : " .. takingx[z] .. "," .. takingy[z]
print("Checked: " .. z)
z = z + 1
taken = taken + 1
--if different color, stop now
else
--print(rows[z])
z = 1
break
end
end
if rowsize >= 3 then
print("Greater than 3: ")
for i = 1, rowsize do
print(rows[i])
end
--found a row of 3 or more items of the same color!
--if takingx[1] ~= "" then --do stuff here
--for z2 = 1, rowsize do
--print(rows[z])////////////////////////////////
--pstr = pstr .. takingx[zx] .. "," .. takingy[zx] .. " : "
--end
--z = 0
--end
end
end
end
end
end
end
end
print("-------------------------------")
end
newgrid()
checkmatches()
print(pstr)
|
Output:
|
--CUBES COLOR VALUES:---
|3|3|3|2|1|2|
|1|1|2|2|1|3|
|2|1|2|2|3|3|
|1|1|2|2|3|1|
|3|1|3|1|2|2|
|2|1|3|1|2|3|
|1|2|1|1|1|1|
|2|2|1|3|2|2|
|3|1|1|2|2|3|
|2|1|2|1|2|1|
|3|2|2|2|1|3|
|1|3|1|3|1|2|
-------------------------------
--BEGIN CHECKING:---
Start new check:
Checked: 2
Start new check:
Checked: 2
Start new check:
Checked: 2
Start new check:
Checked: 2
Start new check:
Checked: 2
Start new check:
Checked: 2
Checked: 3
Greater than 3:
2,2
2,3
2,4
Start new check:
Checked: 2
Checked: 3
Checked: 4
Greater than 3:
2,2
3,3
4,4
5,5
Start new check:
Checked: 2
Start new check:
Checked: 2
Start new check:
Checked: 2
Start new check:
Checked: 2
Checked: 3
Greater than 3:
4,2
5,3
6,4
Start new check:
Checked: 2
Start new check:
Checked: 2
Start new check:
Checked: 2
Start new check:
Checked: 2
Start new check:
Checked: 2
Checked: 3
Greater than 3:
3,3
4,4
5,5
Start new check:
Checked: 2
Checked: 3
Greater than 3:
4,3
3,4
2,5
Start new check:
Checked: 2
Checked: 3
Greater than 3:
4,3
5,3
6,3
Start new check:
Checked: 2
Start new check:
Checked: 2
Start new check:
Checked: 2
Checked: 3
Checked: 4
Greater than 3:
6,3
6,4
6,5
6,6
Start new check:
Checked: 2
Start new check:
Checked: 2
Start new check:
Checked: 2
Start new check:
Checked: 2
Start new check:
Checked: 2
Start new check:
Checked: 2
Start new check:
Checked: 2
Checked: 3
Greater than 3:
6,4
6,5
6,6
Start new check:
Checked: 2
Checked: 3
Greater than 3:
6,4
7,4
8,4
Start new check:
Checked: 2
Start new check:
Checked: 2
Start new check:
Checked: 2
Checked: 3
Greater than 3:
3,5
4,5
5,5
Start new check:
Checked: 2
Start new check:
Checked: 2
Start new check:
Checked: 2
Start new check:
Checked: 2
-------------------------------
|
|