That's what I'm doing now, but it's pretty heavy. Especially since I need to perform this check after every move (about every second).
ive been working on an app that does something similar to this when i get home today ill send you some sample code. it goes though a grid of 7x9 and find matches of 2x2 or bigger
ive changed it to work with your 5x6 grid and 4+ in a row match
have not tested but if it doesnt work it should be close
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
| -- set a random game board each number stands different color block
gameData = {}
for a = 1, 30 do
gameData[a] = math.random( 1, 3 )
end
function fnCheckBlocks()
print( "fnCheckBlocks" )
popTable = {}
for ct = 1, 30 do
if ct%5 < 3 then -- no need to check pass 2nd column for row match
if gameData[ct] == gameData[ct+1] and
gameData[ct] == gameData[ct+2] and
gameData[ct] == gameData[ct+3] then
pop1 = { 0, 1, 2, 3 }
for a = 1, #pop1 do
table.insert( popTable, #popTable+1, ct+pop1[a] )
end
end
end
if ct < 16 then --no need to check pass 3rd row for column match
if gameData[ct] == gameData[ct+5] and
gameData[ct] == gameData[ct+10] and
gameData[ct] == gameData[ct+15] then
pop2 = { 0, 5, 10, 15 }
for a = 1, #pop2 do
table.insert( popTable, #popTable+1, ct+pop2[a] )
end
end
end
end
--print table to terminal
for a = 1, #popTable do
print("popTable: "..popTable[a])
end
--sort table
table.sort( popTable )
--print sorted table to terminal
for a = 1, #popTable do
print("sorted popTable: "..popTable[a])
end
--unique table, removes double entries from table
local newTable = {}
local ct = nil
for k,v in pairs(popTable) do
if ct ~= v then
table.insert( newTable, #newTable+1, v )
end
ct = v
end
--print table to terminal without double entries
for a = 1, #popTable do
print("unique entry popTable: "..popTable[a])
end
popTable = nil
popTable = {}
popTable = newTable
-- if #popTable > 0 then
-- fnRemoveBlocks()
-- end
end
-------------------------------------------------------------------
fnCheckBlocks()
-- now just make function to go though the popTable and remove the blocks |
@thomas,
You are on the right track , but believe me that is Massive redundant code right there. You can modularize the checking into a function/s and reduce that into lesser lines of code.
Have a look at my game RetroBalls here (iTunes) or a video about it here and see that a lot of calculations have to be made on every level for every colour of the ball and also considering that there are blocks in between.
cheers,
?:)
Hi Jstrahan and Jayantv,
Thanks for the help! I just got to work (morning in Belgium now) but will check in detail tonight!
Jayantv, that game looks cool! Is it out yet?
ok if you use my code and have problems just let me know
@thomas,
The game was released in July and is universal, the same one runs on all iOS devices, iPhones or iPads.
you can find it here iTunes App Store link
cheers,
?:)
made a few changes to my code above
Thanks!
I just tried your code, took some tiny tiny tiny errors out of it and now it works like a charm.
Here's my adapted code, for detecting groups of 3 blocks (yep, I went from 4 to 3) in a horizontal row or vertical column:
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
| -- set a random game board each number stands different color block
gameData = {}
for a = 1, 30 do
gameData[a] = math.random(5)
end
for y = 0,5 do
print(gameData[1+y*5].." "..gameData[2+y*5].." "..gameData[3+y*5].." "..gameData[4+y*5].." "..gameData[5+y*5])
end
print("---------")
function fnCheckBlocks()
popTable = {}
for ct = 1, 30 do
if (ct-1)%5 < 3 then -- no need to check pass 3rd column for row match
if (gameData[ct] == gameData[ct+1]) and (gameData[ct] == gameData[ct+2]) then
pop1 = { 0, 1, 2}
for a = 1, 3 do
table.insert( popTable, #popTable+1, ct+pop1[a] )
end
end
end
if (ct < 21) then --no need to check pass 4th row for column match
if (gameData[ct] == gameData[ct+5]) and (gameData[ct] == gameData[ct+10]) then
pop2 = { 0, 5, 10}
for a = 1, 3 do
table.insert( popTable, #popTable+1, ct+pop2[a] )
end
end
end
end
--sort table
table.sort(popTable)
---unique table, removes double entries from table
local newTable = {}
local lastNumber = 0
for ct = 1, #popTable do
nextNumber = popTable[ct]
if (nextNumber ~= lastNumber) then
newTable[#newTable+1] = nextNumber
end
lastNumber = nextNumber
end
-- build Array with 1s where blocks match
local popGrid = {}
for i = 1, 30 do
popGrid[i] = 0
end
for i = 1, #newTable do
popGrid[newTable[i]] = 1
end
for y = 0,5 do
print(popGrid[1+y*5].." "..popGrid[2+y*5].." "..popGrid[3+y*5].." "..popGrid[4+y*5].." "..popGrid[5+y*5])
end
print("---------")
end
fnCheckBlocks() |
glad you got it to work it works great with my 2x2 system also
I figured the 4 in a row would be a bit large for the grid you were using
like I said I didn't try it out just adopted it to fit for you
views:1796 update:2011/9/26 8:07:09