othello

thank you Sir Nail :slight_smile:

--[[Traversal Vector Constants]] GRID\_TOP\_LEFT = 1 GRID\_TOP = 2 GRID\_TOP\_RIGHT = 3 GRID\_LEFT = 4 GRID\_CENTER = 5 GRID\_RIGHT = 6 GRID\_BOTTOM\_LEFT = 7 GRID\_BOTTOM = 8 GRID\_BOTTOM\_RIGHT = 9 function g:get\_vector(vector) if vector == GRID\_TOP\_LEFT then return -1, -1 elseif vector == GRID\_TOP then return -1, 0 elseif vector == GRID\_TOP\_RIGHT then return -1, 1 elseif vector == GRID\_LEFT then return 0, -1 elseif vector == GRID\_CENTER then return 0, 0 elseif vector == GRID\_RIGHT then return 0, 1 elseif vector == GRID\_BOTTOM\_LEFT then return 1, -1 elseif vector == GRID\_BOTTOM then return 1, 0 elseif vector == GRID\_BOTTOM\_RIGHT then return 1, 1 else --[[Oops.]] return nil end end

is this helps?

I just noticed that

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]

yes, that’s the basis for the scheme how we are going to look at all the Chips around the Chip in play to see if the play is valid and if there are Chips that need to be captured.

One thing to consider though, we can’t just look at the cells adjacent to the Chip in play, we have to look at each vector and determine if there are more chips along that vector of the opposite color (could be up to 6 chips that need to be captured/flipped) and see if the opposite end of the vector, or series of chips are bound by a chip with the color in play in order to capture them.

like this:

X  --chip in play

 O

   O

     O

       X    the O’s on the DownRight vector would all be captured or flipped.

or:

XOOOOOX on the Right vector.

the vector below would NOT be valid as is is NOT bound on the opposite end by a chip with the same color in play

X  --chip in play

O

O
O

in the validPlay(), which needs to be created, we have to look at each vector, determine if the vector is bound by a chip with the same color in play and save in a table the coordinates (which are the indexes)  of all the chips along the vector that need to be flipped.  We will then iterate with a loop over the saved table of Captured Chip Coordinates/Indexes and change the color of each chip/cell in the “T” table that is display as our game board.

Do you understand the basic scheme we are going to use?  This was my original idea and I’m finding it difficult to describe in words without getting to confusing.

I am working on the validatePlay() now concentrating of the Up vector.  I’ll post what I’ve got later today.

when we call the validatePlay() in the chipTapped(event) we are going to pass along 3 parameters of tap/cell in play with the function call. It will look like this:  validatePlay(col, row, State)

We are going to use and modify tables extensively in the function.  The use of indexed tables in lua is probably one of the most powerful and useful features of lua.  You are going to get another good lesson using tables here.  I think you can see the value of tables as we’ve already spawned the game board into the table “T”.

Nail

paste this in the chipTapped(event) function where you find it.

This will fetch and flip the Chips returned.

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=0

Nail

how it will look like? I really dont know how,

 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=0 --if (clicked==1) then button.state=0 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

thank you Sir Nail

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?