othello

The scheme basically works. It’s not complete and even the Up block needs more work, but it’s a good start.

Here’s the start of the validPlay(col, row, state) , paste it above the grid() so it is in scope.

Right now it only works on the Up vector, it works when you select T[5][6] after startup. This block of code will still need to be modified to make sure there are no cells in the table that are beyond the game board.  It will also need to make sure a vector is bound by chip with the color in play at the opposite end of the vector.

Your project now is up to you to complete the validPlay() for the remaining 7 possible vectors using the Up code as a template.

Remember, work in a clockwise direction through the vectors, it will be easier for me to follow your code that way.

Post your code as you complete each vector please.

Here ya go…

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 Col \> 0 and Col \< 9 and Row \> 0 and Row \< 9 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") 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 vector code --next vector code goes here and so on ------- return CaptureTable -- return the CaptureTable back to gridTapped(event) end

Nail

In the vector code above I use a module labeled: PrintReadableTables and call it’s function print_r.

PrintReadableTables.print_r(Table)

You need to add this module to your project, it is invaluable and my MOST used module.  It prints out the values of a table so you can easily read the table and debug.

This module is labeled print_r.lua.  Get it, you need it to see wihat’s going on

Nail

there is an error, 

thank you Sir, I have just Tried it, and it work for up

thank you so Much  :)  :)  :slight_smile:

Sir, when I start up to code for the “Right” , an Error occur  :frowning:

did you add the module print_r to your project?

your next vector clockwise should be UpRight, then Right

post your current validPlay()

Nail

local function print\_r ( t ) local print\_r\_cache={} local function sub\_print\_r(t,indent) if (print\_r\_cache[tostring(t)]) then print(indent.."\*"..tostring(t)) else print\_r\_cache[tostring(t)]=true if (type(t)=="table") then local tLen = #t for i = 1, tLen do local val = t[i] if (type(val)=="table") then print(indent.."#["..i.."] =\> "..tostring(t).." {") sub\_print\_r(val,indent..string.rep(" ",string.len(i)+8)) print(indent..string.rep(" ",string.len(i)+6).."}") elseif (type(val)=="string") then print(indent.."#["..i..'] =\> "'..val..'"') else print(indent.."#["..i.."] =\> "..tostring(val)) end end for pos,val in pairs(t) do if type(pos) ~= "number" or math.floor(pos) ~= pos or (pos \< 1 or pos \> tLen) then if (type(val)=="table") then print(indent.."["..pos.."] =\> "..tostring(t).." {") sub\_print\_r(val,indent..string.rep(" ",string.len(pos)+8)) print(indent..string.rep(" ",string.len(pos)+6).."}") elseif (type(val)=="string") then print(indent.."["..pos..'] =\> "'..val..'"') else print(indent.."["..pos.."] =\> "..tostring(val)) end end end else print(indent..tostring(t)) end end end if (type(t)=="table") then print(tostring(t).." {") sub\_print\_r(t," ") print("}") else sub\_print\_r(t," ") end print() end

is this the print_r?

I create print_r.lua then save it to the folder of othello

and I put the local print_r= require(“print_r”) on the top

this is my new ValidPlay

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") 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 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\_Right 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 ------- return CaptureTable -- return the CaptureTable back to gridTapped(event) end

Yes, that’s the print_r module and that is how you add it to your project and call it from your code.

I labelled the module printReadableTables.lua so at the top of my code I have…

local PrintReadableTables = require(“printReadableTables”)

to call function…   PrintReadableTables.print_r(Table to read)

You want to change my references to the module to print_r.print_r(Table to read) since that’s how you’ve declared it at the top of your file

You should now see your tables printed out cleanly in the console/terminal.

Nail

Edited:

Your UpRight Vector code has an issue that didn’t show up in the Up vector code because the Col value never changes going vertical.

 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

hint: you have to introduce the tempCol variable and use it.

BTW, do you understand what is going on in the line below?  Do you understand what is happening the the code block above?  Explain it to me.

for i = Row, 1 , -1 do -- add all cells above cell in play to CellTable

You have to understand the above line as it is critical when building the CellTable for each vector.

Tell me what is happening in the line of code above.

I like how each vector code block can be reused as a template where only the Col and Row values have to be modified and used.

There’s probably a cleanier way to iterate over the 8 vectors, but this works.

What do you think about that print_r module?  How’s it working for you? It’s nice to be able to see what cell indexes are included in CaptureTable, eh?

Nail

 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)

            

is that right?

in “up”, the Col = col and Row = row -1

then  for i = Row means it will start in “Row” up to  1 , -1 and as you say it will add all cells above cell in play to cell table

that’s what I understand but i’m not sure, sorry about that

I had finish in Up, Up_Right, Right, Right_Down and Down

The print_r function calls are throwing the error. To call a function in a separate module the first Part of the call is the label you declared for the midule. In your case, local print_r This is followed by a . (Period) Followed by the function call itself in the module, which is print_r The lua module and function in this case are the same Your call should be: print_r.print_r(the table you want to use) You need to change my calls from PrintReadableTables.print_r(table) to print_r.print_r(table) Nail

local print_r=require (“print_r”) on the top of the main lua then

print_r.print_r(CellTable) and print_r.print_r(CaptureTable) on the validPlay

the name of the module is print_r.lua

is this right?

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