```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 ``` ```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) ```
 ```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 ``` ```--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 ------------------------------- ```