othello

In the “Up” for loop above we are building a table of cells starting from the cell in play, let"s say it’s T[4][4].
Since we want to add all the cells in order in the vertical direction, we need to loop the indexes in reverse/descending order.
In this case from row 3, 2, 1. The Col value stays the the same as we are going vertical.
We are looping from 3 to 1, that’s what I consider reverse as oppossed to. Looping from 1 to 3, foward loop which is more typical.
A reverse loop looks like this: for i = 3, 1, -1 do , it will iterate 3,2,1 descending
A typical loop goes positive. for i = 1, 3 do , it will iterate 1,2,3 ascending

This is key to building the CellTable in the direction the vector traveling.

So in the UpRight vector loop, the indexes for the cells we want, the indexes we want will be Row - 1, and Col +1.
The loop will be reverse, same as the Up vector loop, but Col values need to increase.
Hint: you need to implement the tempCol variable, similar to the tempRow variable.

The Right vector loop will build a CellTable to the right horizontally, so what indexes in the loop will be changing.
Will it be a forward or reverse loop and what coordinate will it loop through? Row or Col?

You have to understand what we’re doing here to modify the other vector loops.
Hope this helps,
Nail

Yes, that is correct. The errors should now go away for print_r

Can you see the tables in the debug console now?

Aaargh, I edited 2 posts above. Col values need to increase in UpRight vector loop not decrease as I mistakenly wrote

attempt to index upvalue ‘print_r’(a boolean value)

that’s what it says, in error 

have you changed my calls?  PrintReadableTables.print_r()  to print_r.print_r() ?

The error will have a code line showing where the error occurred.  What’s wrong at that line?

that’s what I wrote, but error appears

by the way, I had finish the code for Up, upright, right, downright, down, downleft, left and upLeft

what is wrong on line 98?  you’ve got to fix it.  paste it here so I can see it

I’m assuming your lines are numbered in your editor, I don’t see them in your thumbnail image

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- Your code here ----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- Your code here local DisplayGroup = display.newGroup() --declare display group to hold display objects local T = {} --replace Chips1 table below with a Table labeled "T" local text = {} local print\_r=require ("print\_r") cy=display.contentCenterY cx=display.contentCenterX function startGame( event) --remove all display objects and respawn the game board, this is an easy way to restart the game display.remove(DisplayGroup) DisplayGroup = nil DisplayGroup = display.newGroup() local background2= display.newRect(cx,cy,360, 650) background2.alpha=0 background2:setFillColor( 0 ) DisplayGroup:insert(background2) local cbs2= display.newRect(cx, cy-220,320, 100) cbs2:setFillColor( 0.5 ) local board1= display.newRect(cx,cy,320,320) board1.alpha=0 board1:setFillColor( 0 ) DisplayGroup:insert(cbs2) -- body transition.to(background2,{time=1000, alpha=1, x=centerX,y=200}) -- transition.to(board1,{time=1000, alpha=1, x=centerX,y=centerY}) grid() end local function validPlay(col, row, state) print("validPlay("..col..", "..row..", "..state..") is called") local Col local Row local State = state --color of current play local CaptureTable = {} --create a table to hold Chips that are to be capture local CellTable = {} -- table to hold coordinates of all Cellls in the Up direction local \_index --this will be an Integer use for a temporary index local tempCol local tempRow local validVector = "false" --------- each vector will have similar to this print("#1 --Start Up Vector iteration") --check UP vetor, so Row is row -1 Col = col Row = row -1 print("col == ",col) print("row == ",row) print("Col == ",Col) print("Row == ",Row) print("T[Col][Row].state == ",T[Col][Row].state) if T[Col][Row] ~= nil then --check to see if Cell above exists, if not, do nothing if T[Col][Row].state == "blank" then --do nothing print("Up vector-- Cell has no Chip") elseif T[Col][Row].state ~= State then --check to see if Cell above is opposite color --since cell above is valid, create table of all cells above to row 1 CellTable = nil --clear the CellTable CellTable = {} tempRow = row --store the original row value in play for i = Row, 1 , -1 do -- add all cells above cell in play to CellTable tempRow = tempRow -1 \_index = #CellTable + 1 CellTable[\_index] = {} CellTable[\_index].col = Col CellTable[\_index].row = tempRow end print("\*\*\*Up vector / CellTable prints below") print\_r.print\_r(CellTable) for i = 1, #CellTable do --starting near cell in play, put all Cells above of opposite color into a CaptureTable if T[CellTable[i].col][CellTable[i].row].state ~= State and T[CellTable[i].col][CellTable[i].row].state ~= "blank" then \_index = #CaptureTable + 1 CaptureTable[\_index] = {} CaptureTable[\_index].col = Col CaptureTable[\_index].row = Row elseif T[CellTable[i].col][CellTable[i].row].state == State then --when a cell with the same color is found, stop adding cells to CaptureTable print("break--cell matches State color") break end end print("\*\*\*Up vector / CellTable prints below") -- PrintReadableTables.print\_r(CaptureTable) end end ----------end Up vector code --next vector code goes here and so on ------- print("#2 --Start Up\_Right Vector iteration") --check UP\_Right vetor, so Row is row -1 Col = col +1 Row = row -1 print("col == ",col) print("row == ",row) print("Col == ",Col) print("Row == ",Row) print("T[Col][Row].state == ",T[Col][Row].state) if T[Col][Row] ~= nil then --check to see if Cell above exists, if not, do nothing if T[Col][Row].state == "blank" then --do nothing print("Up\_Right vector-- Cell has no Chip") elseif T[Col][Row].state ~= State then --check to see if Cell above is opposite color --since cell above is valid, create table of all cells above to row 1 CellTable = nil --clear the CellTable CellTable = {} tempRow = row --store the original row value in play for i = Row, 1 , -1 do -- add all cells above cell in play to CellTable tempRow = tempRow -1 \_index = #CellTable + 1 CellTable[\_index] = {} CellTable[\_index].col = Col CellTable[\_index].row = tempRow end --[[print("\*\*\*Up vector / CellTable prints below") PrintReadableTables.print\_r(CellTable)]] for i = 1, #CellTable do --starting near cell in play, put all Cells above of opposite color into a CaptureTable if T[CellTable[i].col][CellTable[i].row].state ~= State and T[CellTable[i].col][CellTable[i].row].state ~= "blank" then \_index = #CaptureTable + 1 CaptureTable[\_index] = {} CaptureTable[\_index].col = Col CaptureTable[\_index].row = Row elseif T[CellTable[i].col][CellTable[i].row].state == State then --when a cell with the same color is found, stop adding cells to CaptureTable print("break--cell matches State color") break end end --[[print("\*\*\*Up vector / CellTable prints below") PrintReadableTables.print\_r(CaptureTable)]] end end ----------end Up\_Right vector code --next vector code goes here and so on ------- print("#3 --Start Right Vector iteration") --check Right vetor, so Row is row -1 Col = col +1 Row = row print("col == ",col) print("row == ",row) print("Col == ",Col) print("Row == ",Row) print("T[Col][Row].state == ",T[Col][Row].state) if T[Col][Row] ~= nil then --check to see if Cell above exists, if not, do nothing if T[Col][Row].state == "blank" then --do nothing print("Up\_Right vector-- Cell has no Chip") elseif T[Col][Row].state ~= State then --check to see if Cell above is opposite color --since cell above is valid, create table of all cells above to row 1 CellTable = nil --clear the CellTable CellTable = {} tempRow = row --store the original row value in play for i = Row, 1 , -1 do -- add all cells above cell in play to CellTable tempRow = tempRow \_index = #CellTable + 1 CellTable[\_index] = {} CellTable[\_index].col = Col CellTable[\_index].row = tempRow end --[[print("\*\*\*Up vector / CellTable prints below") PrintReadableTables.print\_r(CellTable)]] for i = 1, #CellTable do --starting near cell in play, put all Cells above of opposite color into a CaptureTable if T[CellTable[i].col][CellTable[i].row].state ~= State and T[CellTable[i].col][CellTable[i].row].state ~= "blank" then \_index = #CaptureTable + 1 CaptureTable[\_index] = {} CaptureTable[\_index].col = Col CaptureTable[\_index].row = Row elseif T[CellTable[i].col][CellTable[i].row].state == State then --when a cell with the same color is found, stop adding cells to CaptureTable print("break--cell matches State color") break end end --[[print("\*\*\*Up vector / CellTable prints below") PrintReadableTables.print\_r(CaptureTable)]] end end ----------end Right vector code --next vector code goes here and so on ------- print("#4 --Start Down\_Right Vector iteration") --check Down\_Right vetor, so Row is row -1 Col = col +1 Row = row +1 print("col == ",col) print("row == ",row) print("Col == ",Col) print("Row == ",Row) print("T[Col][Row].state == ",T[Col][Row].state) if T[Col][Row] ~= nil then --check to see if Cell above exists, if not, do nothing if T[Col][Row].state == "blank" then --do nothing print("Up\_Right vector-- Cell has no Chip") elseif T[Col][Row].state ~= State then --check to see if Cell above is opposite color --since cell above is valid, create table of all cells above to row 1 CellTable = nil --clear the CellTable CellTable = {} tempRow = row --store the original row value in play for i = Row, 1 , -1 do -- add all cells above cell in play to CellTable tempRow = tempRow \_index = #CellTable + 1 CellTable[\_index] = {} CellTable[\_index].col = Col CellTable[\_index].row = tempRow end tempCol = col --store the original row value in play for i = Col, 1 , -1 do -- add all cells above cell in play to CellTable tempCol = tempCol \_index = #CellTable + 1 CellTable[\_index] = {} CellTable[\_index].row = Row CellTable[\_index].col = tempCol end --[[print("\*\*\*Up vector / CellTable prints below") PrintReadableTables.print\_r(CellTable)]] for i = 1, #CellTable do --starting near cell in play, put all Cells above of opposite color into a CaptureTable if T[CellTable[i].col][CellTable[i].row].state ~= State and T[CellTable[i].col][CellTable[i].row].state ~= "blank" then \_index = #CaptureTable + 1 CaptureTable[\_index] = {} CaptureTable[\_index].col = Col CaptureTable[\_index].row = Row elseif T[CellTable[i].col][CellTable[i].row].state == State then --when a cell with the same color is found, stop adding cells to CaptureTable print("break--cell matches State color") break end end --[[print("\*\*\*Down\_Right vector / CellTable prints below") PrintReadableTables.print\_r(CaptureTable)]] end end ----------end Down\_Right vector code --next vector code goes here and so on ------- print("#5 --Start Down Vector iteration") --check Down\_Right vetor, so Row is row -1 Col = col Row = row +1 print("col == ",col) print("row == ",row) print("Col == ",Col) print("Row == ",Row) print("T[Col][Row].state == ",T[Col][Row].state) if T[Col][Row] ~= nil then --check to see if Cell above exists, if not, do nothing if T[Col][Row].state == "blank" then --do nothing print("Down vector-- Cell has no Chip") elseif T[Col][Row].state ~= State then --check to see if Cell above is opposite color --since cell above is valid, create table of all cells above to row 1 CellTable = nil --clear the CellTable CellTable = {} tempRow = row --store the original row value in play for i = Row, 1 do -- add all cells above cell in play to CellTable tempRow = tempRow \_index = #CellTable + 1 CellTable[\_index] = {} CellTable[\_index].col = Col CellTable[\_index].row = tempRow end tempCol = col --store the original row value in play for i = Col, 1 , -1 do -- add all cells above cell in play to CellTable tempCol = tempCol \_index = #CellTable + 1 CellTable[\_index] = {} CellTable[\_index].row = Row CellTable[\_index].col = tempCol end --[[print("\*\*\*Up vector / CellTable prints below") PrintReadableTables.print\_r(CellTable)]] for i = 1, #CellTable do --starting near cell in play, put all Cells above of opposite color into a CaptureTable if T[CellTable[i].col][CellTable[i].row].state ~= State and T[CellTable[i].col][CellTable[i].row].state ~= "blank" then \_index = #CaptureTable + 1 CaptureTable[\_index] = {} CaptureTable[\_index].col = Col CaptureTable[\_index].row = Row elseif T[CellTable[i].col][CellTable[i].row].state == State then --when a cell with the same color is found, stop adding cells to CaptureTable print("break--cell matches State color") break end end --[[print("\*\*\*Down\_Right vector / CellTable prints below") PrintReadableTables.print\_r(CaptureTable)]] end end ----------end Down vector code --next vector code goes here and so on ------- ------- print("#6 --Start Down\_Left Vector iteration") --check Down\_Left vetor, so Row is row -1 Col = col -1 Row = row +1 print("col == ",col) print("row == ",row) print("Col == ",Col) print("Row == ",Row) print("T[Col][Row].state == ",T[Col][Row].state) if T[Col][Row] ~= nil then --check to see if Cell above exists, if not, do nothing if T[Col][Row].state == "blank" then --do nothing print("Down vector-- Cell has no Chip") elseif T[Col][Row].state ~= State then --check to see if Cell above is opposite color --since cell above is valid, create table of all cells above to row 1 CellTable = nil --clear the CellTable CellTable = {} tempRow = row --store the original row value in play for i = Row, 1 , -1 do -- add all cells above cell in play to CellTable tempRow = tempRow \_index = #CellTable + 1 CellTable[\_index] = {} CellTable[\_index].col = Col CellTable[\_index].row = tempRow end tempCol = col --store the original row value in play for i = Col, 1 , -1 do -- add all cells above cell in play to CellTable tempCol = tempCol \_index = #CellTable + 1 CellTable[\_index] = {} CellTable[\_index].row = Row CellTable[\_index].col = tempCol end --[[print("\*\*\*Up vector / CellTable prints below") PrintReadableTables.print\_r(CellTable)]] for i = 1, #CellTable do --starting near cell in play, put all Cells above of opposite color into a CaptureTable if T[CellTable[i].col][CellTable[i].row].state ~= State and T[CellTable[i].col][CellTable[i].row].state ~= "blank" then \_index = #CaptureTable + 1 CaptureTable[\_index] = {} CaptureTable[\_index].col = Col CaptureTable[\_index].row = Row elseif T[CellTable[i].col][CellTable[i].row].state == State then --when a cell with the same color is found, stop adding cells to CaptureTable print("break--cell matches State color") break end end --[[print("\*\*\*Down\_Left vector / CellTable prints below") PrintReadableTables.print\_r(CaptureTable)]] end end ----------end Down\_left vector code --next vector code goes here and so on ------- ------- print("#7 --Start Left Vector iteration") --check Left vetor, so Row is row -1 Col = col-1 Row = row print("col == ",col) print("row == ",row) print("Col == ",Col) print("Row == ",Row) print("T[Col][Row].state == ",T[Col][Row].state) if T[Col][Row] ~= nil then --check to see if Cell above exists, if not, do nothing if T[Col][Row].state == "blank" then --do nothing print("Down vector-- Cell has no Chip") elseif T[Col][Row].state ~= State then --check to see if Cell above is opposite color --since cell above is valid, create table of all cells above to row 1 CellTable = nil --clear the CellTable CellTable = {} tempRow = row --store the original row value in play for i = Row, 1 , -1 do -- add all cells above cell in play to CellTable tempRow = tempRow \_index = #CellTable + 1 CellTable[\_index] = {} CellTable[\_index].col = Col CellTable[\_index].row = tempRow end tempCol = col --store the original row value in play for i = Col, 1 , -1 do -- add all cells above cell in play to CellTable tempCol = tempCol \_index = #CellTable + 1 CellTable[\_index] = {} CellTable[\_index].row = Row CellTable[\_index].col = tempCol end --[[print("\*\*\*Up vector / CellTable prints below") PrintReadableTables.print\_r(CellTable)]] for i = 1, #CellTable do --starting near cell in play, put all Cells above of opposite color into a CaptureTable if T[CellTable[i].col][CellTable[i].row].state ~= State and T[CellTable[i].col][CellTable[i].row].state ~= "blank" then \_index = #CaptureTable + 1 CaptureTable[\_index] = {} CaptureTable[\_index].col = Col CaptureTable[\_index].row = Row elseif T[CellTable[i].col][CellTable[i].row].state == State then --when a cell with the same color is found, stop adding cells to CaptureTable print("break--cell matches State color") break end end --[[print("\*\*\*Down\_Right vector / CellTable prints below") PrintReadableTables.print\_r(CaptureTable)]] end end ----------end Left vector code --next vector code goes here and so on ------- ------- print("#8 --Start Up\_Left Vector iteration") --check Up\_Left vetor, so Row is row -1 Col = col -1 Row = row -1 print("col == ",col) print("row == ",row) print("Col == ",Col) print("Row == ",Row) print("T[Col][Row].state == ",T[Col][Row].state) if T[Col][Row] ~= nil then --check to see if Cell above exists, if not, do nothing if T[Col][Row].state == "blank" then --do nothing print("Down vector-- Cell has no Chip") elseif T[Col][Row].state ~= State then --check to see if Cell above is opposite color --since cell above is valid, create table of all cells above to row 1 CellTable = nil --clear the CellTable CellTable = {} tempRow = row --store the original row value in play for i = Row, 1 , -1 do -- add all cells above cell in play to CellTable tempRow = tempRow \_index = #CellTable + 1 CellTable[\_index] = {} CellTable[\_index].col = Col CellTable[\_index].row = tempRow end tempCol = col --store the original row value in play for i = Col, 1 , -1 do -- add all cells above cell in play to CellTable tempCol = tempCol \_index = #CellTable + 1 CellTable[\_index] = {} CellTable[\_index].row = Row CellTable[\_index].col = tempCol end --[[print("\*\*\*Up vector / CellTable prints below") PrintReadableTables.print\_r(CellTable)]] for i = 1, #CellTable do --starting near cell in play, put all Cells above of opposite color into a CaptureTable if T[CellTable[i].col][CellTable[i].row].state ~= State and T[CellTable[i].col][CellTable[i].row].state ~= "blank" then \_index = #CaptureTable + 1 CaptureTable[\_index] = {} CaptureTable[\_index].col = Col CaptureTable[\_index].row = Row elseif T[CellTable[i].col][CellTable[i].row].state == State then --when a cell with the same color is found, stop adding cells to CaptureTable print("break--cell matches State color") break end end --[[print("\*\*\*Down\_Right vector / CellTable prints below") PrintReadableTables.print\_r(CaptureTable)]] end end ----------end Up\_Left vector code --next vector code goes here and so on ------- return CaptureTable -- return the CaptureTable back to gridTapped(event) end function grid() local playCount = 0 local clicked=0 local clicked1=0 local chip function chipTapped(event) local button = event.target local button1=event.target local col = button.col local row = button.row local State = "Void" --we will change the state to a string, "ehite" or "black" playCount = playCount + 1 clicked=clicked+1 print("\*clicked == ",clicked) if clicked == 1 then State = "black" elseif clicked == 2 then State = "white" else clicked=0 end if (button.state=="blank") then ------------ get table of Chips to flip local FlipTable = validPlay(col, row, State) print("#FlipTable == ",#FlipTable) if #FlipTable \>= 1 then print("\*#FlipTable == ",#FlipTable) playCount = playCount + 1 local FlipColor --New variable to hold the FlipColor to be used in transition.to below if clicked == 1 then --State = "black" FlipColor = 0 elseif clicked == 2 then --State = "white" FlipColor = 1 end for i = 1, #FlipTable do T[FlipTable[i].col][FlipTable[i].row].state = State -- T[FlipTable[i].col][FlipTable[i].row]:setFillColor(clicked) transition.to( T[FlipTable[i].col][FlipTable[i].row].Chip.fill, { r=FlipColor, g=FlipColor, b=FlipColor, a=1, time=500, transition=easing.inCubic }) end else print("\*\*\*return / NOT a valid move") return end ------------ if (clicked==1) then button.state=1 endtxt = display.newText( clicked, 0, 0, native.systemFont, 24 ) endtxt.x=display.contentCenterX endtxt.y=display.contentCenterY transition.to( endtxt, {time=1500,alpha=0}) T[col][row].state = State T[col][row].originColor = "black" --need to set color T[col][row].originPlayCount = playCount T[col][row].Chip:setFillColor(0) T[col][row].Chip.isVisible = true clicked=1 chip="black" print("\*\*clicked == ",clicked) --return blackpiece end if (clicked==2) then button.state=0 --set state below, this works fine, just setting variable in 1 area endtxt1 = display.newText( clicked, 0, 0, native.systemFont, 24 ) endtxt1.x=display.contentCenterX endtxt1.y=display.contentCenterY transition.to( endtxt1, {time=1500,alpha=0}) T[col][row].state = State T[col][row].originColor = "white" T[col][row].originPlayCount = playCount T[col][row].Chip:setFillColor(1) T[col][row].Chip.isVisible = true clicked=clicked-2 print("\*\*clicked == ",clicked) chip="white" --return whitepiece end else if (chip=="white") then clicked=0 end if (chip=="black") then clicked=1 end end print("{x=" ..event.target.gridPos.x .. ", y=" .. event.target.gridPos.y .. "}") text.alpha = 1 -- text.text = "{x=" ..event.target.gridPos.x .. ", y=" .. event.target.gridPos.y .. "}" text.text = "State= "..State.." / col= "..col.." / row= "..row.."" transition.to(text,{time=1500, alpha=0}) end --======================================== -- grid code --======================================== local chipWidth = 38 local chipHeight = 38 local colSpace =0 local rowSpace =0 local numCols = 8 local numRows = 8 local centerY=display.contentCenterY local centerX= display.contentCenterX local xPos = centerX- (numCols \* chipWidth + numCols \* colSpace) / 2 local yPos = centerY - (numRows \* chipHeight + numRows \* rowSpace) / 2 --clear table and respawn, display objects have already been removed in "startGame" when the DisplayGroup was cleared/removed T = nil T = {} for col=1,numCols do T[col] = {} --creates an indexed table for each col for row=1, numRows do T[col][row] = {} --creates an indexed table for each row in each col, look familiar? --T[1][1] will be the upper left square, T[8][8] will be the lower right square T[col][row]= display.newRect(0,0,chipWidth,chipHeight) T[col][row].x = xPos + col \* (chipWidth + colSpace) - chipWidth/2 - colSpace T[col][row].y = yPos + row\* (chipHeight + rowSpace) - chipHeight/2 - rowSpace T[col][row].strokeWidth = 1 --add grid lines T[col][row]:setStrokeColor(1) --sets grid color black T[col][row]:setFillColor(0.3, 0.6, 0.8) T[col][row].alpha=0.5 T[col][row].gridPos = {x=col, y=row} -- I'm going to seperate the gridPos references, it may be easier, not sure yet' T[col][row].col = col T[col][row].row = row T[col][row].state="blank" T[col][row].originColor = "Void" --will store the player color when first played T[col][row].originPlayCount = 0 --will store the index when first played T[col][row]:addEventListener("tap",chipTapped) DisplayGroup:insert(T[col][row]) --add object to the display group T[col][row].Chip = display.newCircle(cx,cy,15) T[col][row].Chip:setFillColor(1) T[col][row].Chip.x = T[col][row].x T[col][row].Chip.y = T[col][row].y --T[col][row].Chip.width=event.target.width --T[col][row].Chip.height=event.target.height T[col][row].Chip.isVisible = false -- hide the Chip until played DisplayGroup:insert(T[col][row].Chip) end end text = display.newText( "Void", 0, 0, "Helvetica", 24 ) text.x = display.contentCenterX-60 --text.y = display.contentCenterY-60 text.y = 60 text.alpha=1 text:setTextColor(255, 254, 185) DisplayGroup:insert(text) transition.to(text,{time=1500, alpha=0}) local function start() T[4][5].originColor="black" T[4][5].originPlayCount = playCount T[4][5].Chip:setFillColor(0) T[4][5].Chip.isVisible = true T[4][5].state="black" T[4][5].originColor = "black" T[4][5].originPlayCount = 0 T[5][4].originColor="black" T[5][4].originPlayCount = playCount T[5][4].Chip:setFillColor(0) T[5][4].Chip.isVisible = true T[5][4].state="black" T[5][4].originColor = "black" T[5][4].originPlayCount = 0 T[5][5].originColor="white" T[5][5].originPlayCount = playCount T[5][5].Chip:setFillColor(1) T[5][5].Chip.isVisible = true T[5][5].state="white" T[5][5].originColor = "white" T[5][5].originPlayCount = 0 T[4][4].originColor="white" T[4][4].originPlayCount = playCount T[4][4].Chip:setFillColor(1) T[4][4].Chip.isVisible = true T[4][4].state="white" T[4][4].originColor = "white" T[4][4].originPlayCount = 0 end function vector() local Up= T[col][row-1] Up\_Right= T[col+1][row-1] Right=T[col+1][row] Down\_Right=T[col+1][row+1] Down=T[col][row+1] Down\_Left=T[col-1][row+1] Left=T[col-1][row] Up\_Left=T[col-1][row-1] Center=T[col][row] end function get\_neighbor(col, row, vector) local obj = nil local vx, vy = self:get\_vector(vector) if vx ~= nil then col = col + vx row = row + vy if self:is\_valid(col, row) then obj = self:get\_cell(col, row) end end return obj end start() end startGame()

I put as comment that print.r_print.r() and not edited yet except for up*

that’s all the code I have
 

thanks Sir Nail

eeeek! my mistake on the print_r error

I didn’t realized the print_r .lua  was supposed to be pasted in your main.lua as a function.

So, to fix your print_r.lua

Paste this at the very top of code in print_r.lua , replace the function declartion.

local M = {} function M.print\_r ( t ) --end --at the very bottom of print\_r.lua paste this return M

now the module should work, sorry about that

fix this and get the print_r.print_r() working

Nail

I see you have been busy pasting.  That’s a start.

I’m going to paste my dribble from above so you read it again and again.  You have to understand how iterations work to find the cells in the vector direction from the cell in play.

In the “Up” for loop above we are building a table of cells starting from the cell in play, let"s say it’s T[4][4].

Since we want to add all the cells in order in the vertical direction, we need to loop the indexes in reverse/descending order.
In this case from row 3, 2, 1. The Col value stays the the same as we are going vertical.

We are looping from 3 to 1, that’s what I consider reverse as oppossed to. Looping from 1 to 3, foward loop which is more typical.

A reverse loop looks like this: for i = 3, 1, -1 do , it will iterate 3,2,1 descending
A typical loop goes positive. for i = 1, 3 do , it will iterate 1,2,3 ascending

This is key to building the CellTable in the direction the vector traveling.

So in the UpRight vector loop, the indexes for the cells we want, the indexes we want will be Row - 1, and Col +1.
The loop will be reverse, same as the Up vector loop, but Col values need to increase.
Hint: you need to implement the tempCol variable, similar to the tempRow variable.

The Right vector loop will build a CellTable to the right horizontally, so what indexes in the loop will be changing.
Will it be a forward or reverse loop and what coordinate will it loop through? Row or Col?

You have to understand what we’re doing here to modify the other vector loops.
Hope this helps,
Nail

read it again.

Now let’s review your UpRight vector code next

Nail

This is from your UpRight vector code.

Where idid you implement my Hint: Hint: you need to implement the tempCol variable, similar to the tempRow variable.

Let’s say T{3}[5] is the cell in play.

What are the indexes/coordinates (they are the same) for the cells in the UpRight vector?  Write them down.  The Rows decrease -1 but the Cols increase +1

Your loop has to look for these values.

--check UP\_Right vetor, so Row is row -1 tempRow = row --store the original row value in play for i = Row, 1 , -1 do -- add all cells above cell in play to CellTable tempRow = tempRow -1 \_index = #CellTable + 1 CellTable[\_index] = {} CellTable[\_index].col = Col CellTable[\_index].row = tempRow end

This block of code will have to be modified for each vector so it “adds” the indexes for each cell along the direction of the vector

Try to fix it and just post this code block when you modify it.

Nail

23:19:52.481  Loading project from:   C:\Users\ASUS\Documents\Corona Projects\help1

23:19:52.481  Project sandbox folder: C:\Users\ASUS\AppData\Local\Corona Labs\Corona Simulator\Sandbox\help1-06B64305D225D542AC3826F5DBF26B8B\Documents

23:19:55.581  *clicked == 1

23:19:55.581  validPlay(5, 6, black) is called

23:19:55.581  #1 --Start Up Vector iteration

23:19:55.581  col == 5

23:19:55.581  row == 6

23:19:55.581  Col == 5

23:19:55.581  Row == 5

23:19:55.581  T[Col][Row].state == white

23:19:55.581  break–cell matches State color

23:19:55.581  ***Up vector / CellTable prints below

23:19:55.581  #FlipTable == 1

23:19:55.581  *#FlipTable == 1

23:19:55.581  **clicked == 1

23:19:55.581  {x=5, y=6}

23:20:29.117  

23:20:29.117  Copyright © 2009-2017  C o r o n a   L a b s   I n c .

23:20:29.117  Version: 3.0.0

23:20:29.117  Build: 2017.3135

23:20:29.117  Platform: iPhone / x64 / 10.0 / Intel® HD Graphics 505 / 4.4.0 - Build 21.20.16.4534 / 2017.3135 / fil_PH | PH | fil_PH | fil

23:20:29.127  Loading project from:   C:\Users\ASUS\Documents\Corona Projects\help1

23:20:29.127  Project sandbox folder: C:\Users\ASUS\AppData\Local\Corona Labs\Corona Simulator\Sandbox\help1-06B64305D225D542AC3826F5DBF26B8B\Documents

23:20:38.413  *clicked == 1

23:20:38.413  validPlay(5, 6, black) is called

23:20:38.413  #1 --Start Up Vector iteration

23:20:38.413  col == 5

23:20:38.413  row == 6

23:20:38.413  Col == 5

23:20:38.413  Row == 5

23:20:38.413  T[Col][Row].state == white

23:20:38.413  break–cell matches State color

23:20:38.413  ***Up vector / CellTable prints below

23:20:38.413  #FlipTable == 1

23:20:38.413  *#FlipTable == 1

23:20:38.413  **clicked == 1

23:20:38.413  {x=5, y=6}

it has the same output, when I try this code, but the error gone

the first part before I refresh the corona, the last part starting copyright. I add that code print_r.print_r… and I refresh it

tempCol = col --store the original col value in play for i = Col, 1 , -1 do -- add all cells above cell in play to CellTable tempCol = tempCol \_index = #CellTable + 1 CellTable[\_index] = {} CellTable[\_index].row = Row CellTable[\_index].col = tempCol end

I put that in some of the 8 possibilities, and it runs well, which part should I change?

thank you Sir Nail, now I learned how to iterate “backward” , what’s next?

can we go now "how to find its adjacent?

mojric,

hmmm, the CellTable should print out, did you remove the comments from the print_r.print_r(CellTable) calls?

You still don’t see how the for loop gets the cells in the UpRight vector loop and what those indexes/coordinates should be.

Your last modification posted above is completely wrong, delete it from the UpRight code block.

Let’s try this, You should take the time to sketch on paper the game board and in each cell write the col / row coordinates.  These will be the same as the indexes in the T table.

This is the upper left corner of the game board, correct? with the coordinates col/row which match the T[col][row] values.

1,1   2,1   3,1   4,1

1,2   2,2   3,2   4,2

1,3   2,3   3,3   4,3

1,4    2,4   3,4   4,4

if the cell 1,4 (bold) is selected, it is the “cell in play”.

the UpjRight vector loop needs to get the 3 cells in the UpRight diagonal (underlined) correct?

at the top of your UpRight vector code block, you’ve adjusted your Col and Row correctly. These values travel UpRight.

print("#2 --Start Up\_Right Vector iteration") --check UP\_Right vetor, so Row is row -1 Col = col +1 Row = row -1

so, for this line in the UpRight for loop:  for i = Row, 1 , -1 do   (this is how it should be written, we are traveling UP the rows - 1 each iteration, but we need to also travel RIGHT  +1 along the Col’s to get cell coordinates we need.  So how do we do that? We must implement the tempCol variable and +1 to each value as the loop moves UpRight, similar to the tempRow variable as it moves UpRight by subracting 1 through each iteration of the loop.

The loop should look like this for the UpRight vector.

 --cell in play example coordinate (1,4) col = 1, row = 4 tempCol = col --store the original col value of the cell in play from the params --tempCol = 1 because it is the col value of the cell in play tempRow = row --store the original row value of the cell in play from the params --tempRow = 4 because it is the original row value of the play for i = Row, 1 , -1 do -- add all cells above cell in play to CellTable as we iterate in reverse --Row = 3 which was set at the top of UpRight vector code, it iterates in reverse 3,2,1 because those are the row values of the cells traveling along the vector tempCol = tempCol + 1 --we need this variable now so each time the loop gets the row value for the cell above the previous cell, it also moves RIGHT 1 cell coordinate --tempCol = 2 on first iteration, 3 on second iteration, 4 on third iteration, it references the col values in order of the cell col coordinates we want to save tempRow = tempRow -1 --tempRow = 3 on first iteration, 2 on second iteration, 1 on third iteration, it references the row values in order of the cell row coordinates we want to save \_index = #CellTable + 1 --#CellTable return the number of indexed tables in the table, we add 1 to that number as our next indexed table -- #CellTable starts at 0 because we set it to nil above, there are no indexed tables saved in it yet -- \_index = 1 on first iteration because the code added 0 + 1, 2 on second iteration because we added 1 + 1, 3 on third iteration because we added 2 = 1 CellTable[\_index] = {} --we create a new indexed table for each iteration -- CellTable[1] on first iteration, CellTable[2] on second iteration, CellTable[3] on third iteration CellTable[\_index].col = tempCol --we store the tempCol coordinate of the cell along the vector CellTable[\_index].row = tempRow --we store the tempRow coordinate of the cell along the vector end

so in the example where the cell in play has the coordinates 1,4,  we store the original coordinates in 2 variables

tempCol = col  (col value of the cell in play, in our example is 1)

tempRow = row  (row value of the cell in play , in our example is 4)

so in our loop which iterates in reverse from rows 3 to 1

tempCol = tempCol + 1  (we need to add 1 to the col value as we move UpRight,

tempRow = tempRow - 1  (we need to subtract 1 from the row value as move UpRight

You will see the cell coordinates  col / row print in the console when you call print_r.print_r(CellTable)

Post the complete UpRight vector code when you have it and hopefully you can see the CellTable print in the console.

Nail

 print("#2 --Start Up\_Right Vector iteration") --check UP\_Right vetor, so Row is row -1 Col = col +1 Row = row -1 print("col == ",col) print("row == ",row) print("Col == ",Col) print("Row == ",Row) print("T[Col][Row].state == ",T[Col][Row].state) if T[Col][Row] ~= nil then --check to see if Cell above exists, if not, do nothing if T[Col][Row].state == "blank" then --do nothing print("Up\_Right vector-- Cell has no Chip") elseif T[Col][Row].state ~= State then --check to see if Cell above is opposite color --since cell above is valid, create table of all cells above to row 1 CellTable = nil --clear the CellTable CellTable = {} --cell in play example coordinate (1,4) col = 1, row = 4 tempCol = col tempRow = row for i = Row, 1 , -1 do tempCol = tempCol + 1 tempRow = tempRow -1 \_index = #CellTable + 1 CellTable[\_index] = {} CellTable[\_index].col = tempCol CellTable[\_index].row = tempRow end print("\*\*\*Up\_Right vector / CellTable prints below") print\_r.print\_r(CellTable) for i = 1, #CellTable do --starting near cell in play, put all Cells above of opposite color into a CaptureTable if T[CellTable[i].col][CellTable[i].row].state ~= State and T[CellTable[i].col][CellTable[i].row].state ~= "blank" then \_index = #CaptureTable + 1 CaptureTable[\_index] = {} CaptureTable[\_index].col = Col CaptureTable[\_index].row = Row elseif T[CellTable[i].col][CellTable[i].row].state == State then --when a cell with the same color is found, stop adding cells to CaptureTable print("break--cell matches State color") break end end print("\*\*\*Up\_Right vector / CellTable prints below") print\_r.print\_r(CaptureTable) end end

is this right?

print("#2 --Start Up\_Right Vector iteration") --check UP\_Right vetor, so Row is row -1 Col = col +1 Row = row -1 print("col == ",col) print("row == ",row) print("Col == ",Col) print("Row == ",Row) print("T[Col][Row].state == ",T[Col][Row].state) if T[Col][Row] ~= nil then --check to see if Cell above exists, if not, do nothing if T[Col][Row].state == "blank" then --do nothing print("Up\_Right vector-- Cell has no Chip") elseif T[Col][Row].state ~= State then --check to see if Cell above is opposite color --since cell above is valid, create table of all cells above to row 1 CellTable = nil --clear the CellTable CellTable = {} --cell in play example coordinate (1,4) col = 1, row = 4 tempCol = col tempRow = row for i = Row, 1 , -1 do tempRow = tempRow -1 \_index = #CellTable + 1 CellTable[\_index] = {} CellTable[\_index].col = tempCol CellTable[\_index].row = tempRow end print("\*\*\*Up\_Right vector / CellTable prints below") print\_r.print\_r(CellTable) for i = 1, #CellTable do --starting near cell in play, put all Cells above of opposite color into a CaptureTable if T[CellTable[i].col][CellTable[i].row].state ~= State and T[CellTable[i].col][CellTable[i].row].state ~= "blank" then \_index = #CaptureTable + 1 CaptureTable[\_index] = {} CaptureTable[\_index].col = Col CaptureTable[\_index].row = Row elseif T[CellTable[i].col][CellTable[i].row].state == State then --when a cell with the same color is found, stop adding cells to CaptureTable print("break--cell matches State color") break end end print("\*\*\*Up\_Right vector / CellTable prints below") print\_r.print\_r(CaptureTable) end end ----------end Up\_Right vector code --next vector code goes here and so on

If I use 

    tempCol = tempCol +1 

an error occur but if I remove it, no error appear, and the one upper right turns white

I don’t know why that error says 

"attempt to index field ‘?’ (a nil value)

but if i remove tempCol=tempCol+1 this happens

first, put this line back in the UpRight vector loop, it is correct:  tempCol=tempCol+1

the error you are seeing is because the table/cell the code at the error line specified is looking for does NOT exist.

Did you get the print_r.print_r(CellTable) call to print to console?  You need this to work in order to debug

I have the print_r.print_r(CellTable) call below the loop in the UpRight vector block.

 tempCol = col --check UP\_Right vector, so Row is row -1 tempRow = row --store the original row value in play for i = Row, 1 , -1 do -- add all cells above cell in play to CellTable tempCol = tempCol + 1 tempRow = tempRow -1 \_index = #CellTable + 1 CellTable[\_index] = {} CellTable[\_index].col = tempCol CellTable[\_index].row = tempRow end print("\*\*\*UpRight vector / CellTable prints below") print\_r.print\_r(CellTable)

This is the CellTable that prints in my console.

\*\*\*UpRight vector / CellTable prints below table: 0x7fc9c95eec50 { #[1] =\> table: 0x7fc9c95eec50 { [row] =\> 5 [col] =\> 5 } #[2] =\> table: 0x7fc9c95eec50 { [row] =\> 4 [col] =\> 6 } #[3] =\> table: 0x7fc9c95eec50 { [row] =\> 3 [col] =\> 7 } #[4] =\> table: 0x7fc9c95eec50 { [row] =\> 2 [col] =\> 8 } #[5] =\> table: 0x7fc9c95eec50 { [row] =\> 1 [col] =\> 9 } } ERROR: Runtime error

This is why print_r() is so important when working with tables to debug.

Do you see anything wrong? Anything that should not exist?

Look at index #[5], the [col] value is 9.  There is NO col 9 on our game board, it is out of range. 8 is our last col.

So we need to add some code to our loop to break/stop the loop when we get either a col out of range or a row out of range.

 if tempCol \> 8 or tempCol \< 1 or tempRow \> 8 or tempRow \< 1 then --verify values are not out of range print("\*\*UpRight -- Cell out of Ranged") print("\*\*UpRight break") break --calling break will stop the loop, so no more cells will be added to the CellTable end

The loop will now look like this now

 tempCol = col tempRow = row --store the original row value in play for i = Row, 1 , -1 do -- add all cells above cell in play to CellTable tempCol = tempCol + 1 tempRow = tempRow -1 --new cell validator --This is needed in ALL vector blocks if tempCol \> 8 or tempCol \< 1 or tempRow \> 8 or tempRow \< 1 then --verify values are not out of range print("\*\*UpRight -- Cell out of Ranged") print("\*\*UpRight break") break --calling break will stop the loop when a condition is met, so no more cells will be added to the CellTable end \_index = #CellTable + 1 CellTable[\_index] = {} CellTable[\_index].col = tempCol CellTable[\_index].row = tempRow end print("\*\*\*UpRight vector / CellTable prints below") print\_r.print\_r(CellTable)

learning to debug tables is key, without the print_r(), I may never realize there was a cell off the game board being added to the CellTable

The code is evolving, we are programming.

Nail