othello

please…help me how to capture chips in the game othello…
 

Hi @mojric8888, I think before anyone can really help you, we are going to need to know more about how you’re tracking your pieces. So showing us some code about how your managing your pieces would be helpful.  Beyond that about all we can do is suggest you do an Internet search on “programming Othello”.

When posting code, please use code formatting (the blue <> button in the row with Bold, Italic etc. and paste your code into the popup window.

Rob

-- Your code here display.setStatusBar(display.HiddenStatusBar) screenLeft = display.screenOriginX screenWidth = display.contentWidth - screenLeft \* 2 screenRight = screenLeft + screenWidth screenTop = display.screenOriginY screenHeight = display.contentHeight - screenTop \* 2 screenBottom = screenTop + screenHeight display.contentWidth = screenWidth display.contentHeight = screenHeight screenTopSB = screenTop + display.topStatusBarContentHeight -- when status bar is showing screenHeightSB = display.contentHeight - screenTopSB screenBottomSB = screenTopSB + screenHeightSB -- display.tl = display.TopLeftReferencePoint display.tc = display.TopCenterReferencePoint display.tr = display.TopRightReferencePoint display.cl = display.CenterLeftReferencePoint display.c = display.CenterReferencePoint display.cr = display.CenterRightReferencePoint display.bl = display.BottomLeftReferencePoint display.bc = display.BottomCenterReferencePoint display.br = display.BottomRightReferencePoint function startGame( event) local background2= display.newImageRect("background1.jpg",360, 650) background2.x=display.contentCenterX background2.y=display.contentCenterY background2.alpha=0 local cbs2= display.newImageRect("cbs2.png",320, 150) cbs2.x=display.contentCenterX cbs2.y=display.contentCenterY-220 local board1= display.newImageRect("board1.png",320,320) board1.x=display.contentCenterX board1.y=display.contentCenterY board1.alpha=0 -- 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 function grid() clicked=0 clicked1=0 local chip function chipTapped(event) local button = event.target local button1=event.target clicked=clicked+1 if (button.state=="blank") then 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}) button = display.newImage("black.png") button.x=event.target.x button.y=event.target.y button.width=event.target.width button.height=event.target.height clicked=1 chip="black" return blackpiece end if (clicked==2) then button.state=0 endtxt1 = display.newText( clicked, 0, 0, native.systemFont, 24 ) endtxt1.x=display.contentCenterX endtxt1.y=display.contentCenterY transition.to( endtxt1, {time=1500,alpha=0}) button1 = display.newImage("white.png") button1.x=event.target.x button1.y=event.target.y button1.width=event.target.width button1.height=event.target.height clicked=clicked-2 chip="white" return whitepiece end else if (chip=="white") then clicked=0 end if (chip=="black") then clicked=1 end end end 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 local chip1={} for col=1,numCols do for row=1, numRows do chip1= display.newImage("square.png") chip1.width = chipWidth -- for testing only chip1.height = chipHeight -- for testing only chip1.x = xPos + col \* (chipWidth + colSpace) - chipWidth/2 - colSpace chip1.y = yPos + row\* (chipHeight + rowSpace) - chipHeight/2 - rowSpace chip1.alpha=0.1 chip1.gridPos = {x=col, y=row} chip1.state="blank" chip1:addEventListener("tap",chipTapped) end end end startGame()

that’s my code Sir…

i can’t finish it…since i am new in this language and besides I’m just a student :slight_smile:

thank you in advance :slight_smile:

mojric,

Curious here, are you a high school student or college?  If college, what year and what is your major?

Also curious, are the images posted screenshots from your app or grabbed from the web as examples of what you’d like to do?

It would help others if you changed your display.newImage to display.newRect so others can run your code as they don’t  have your .pngs.   You can change back later when your code works.

I would personally try spawning your game board squares (they will hold all the variables needed to track game play) into a table so you can iterate over the squares adjacent to the square that is being selected and having a disc placed to determine if there are any discs adjacent to be captured and flipped.  It will also determine if the selected square is a legal play.

Hope this helps,

Nail

I’m a 4th year BSMathematics major in CIT…

my school dont teach us how to so we have to study by ourselves

those images that I attached is from my project… what I like to do is to capture the white chip from [5][4] and turn into black…but I can’t
 

I’m sorry for using display.newImage … I just copy and paste it here…
 

I’m just a newbie in this corona sdk…

thank you for the help in advance

mojric,

Thanks for the info.

I fully  understand you want to “Capture” the white disc, I’m sure there are several ways to do it. You need a systematic plan, algorithm, to work on every square in the grid to determine if adjacent discs can be captured.  Think of the possibilities.  this is the “engine” of the game. The engine needs to iterate over all the squares which means every square needs to be identifiable. We put this information in a table and is why I suggest spawning the squares into a table, the same table you will iterate over to determine if the White disc can be captured or even exists for that matter.

I’m curious, where are you getting those black and white discs from shown in your posted images?  How are they being spawned? I don’t see them being spawned in your posted code. How do they show up in the squares?

In order to get help, it would be best if you act on my suggestions , if you don’t want to take the time to re-write your code, then neither do I. Fair?

Change your images to rects and post the code again so I can help you spawn your objects into a table which will have many uses.

FWIW, there may be a method to “Capture” the white disc from your existing code, I just don’t see it.

Hope this helps,

Nail

-- Your code here display.setStatusBar(display.HiddenStatusBar) screenLeft = display.screenOriginX screenWidth = display.contentWidth - screenLeft \* 2 screenRight = screenLeft + screenWidth screenTop = display.screenOriginY screenHeight = display.contentHeight - screenTop \* 2 screenBottom = screenTop + screenHeight display.contentWidth = screenWidth display.contentHeight = screenHeight screenTopSB = screenTop + display.topStatusBarContentHeight -- when status bar is showing screenHeightSB = display.contentHeight - screenTopSB screenBottomSB = screenTopSB + screenHeightSB -- display.tl = display.TopLeftReferencePoint display.tc = display.TopCenterReferencePoint display.tr = display.TopRightReferencePoint display.cl = display.CenterLeftReferencePoint display.c = display.CenterReferencePoint display.cr = display.CenterRightReferencePoint display.bl = display.BottomLeftReferencePoint display.bc = display.BottomCenterReferencePoint display.br = display.BottomRightReferencePoint cy=display.contentCenterY cx=display.contentCenterX function startGame( event) local background2= display.newRect(cx,cy,360, 650) background2.alpha=0 background2:setFillColor( 0 ) local cbs2= display.newRect(cx, cy-220,320, 150) cbs2:setFillColor( 0.5 ) local board1= display.newRect(cx,cy,320,320) board1.alpha=0 board1:setFillColor( 0 ) -- 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 function grid() clicked=0 clicked1=0 local chip function chipTapped(event) local button = event.target local button1=event.target clicked=clicked+1 if (button.state=="blank") then 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}) button = display.newCircle(cx,cy,5) button:setFillColor(0) button.x=event.target.x button.y=event.target.y button.width=event.target.width button.height=event.target.height clicked=1 chip="black" return blackpiece end if (clicked==2) then button.state=0 endtxt1 = display.newText( clicked, 0, 0, native.systemFont, 24 ) endtxt1.x=display.contentCenterX endtxt1.y=display.contentCenterY transition.to( endtxt1, {time=1500,alpha=0}) button1 = display.newCircle(cx,cy,5) button1:setFillColor(1) button1.x=event.target.x button1.y=event.target.y button1.width=event.target.width button1.height=event.target.height clicked=clicked-2 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 = display.newText( "{x=" ..event.target.gridPos.x .. ", y=" .. event.target.gridPos.y .. "}", 0, 0, "Helvetica", 24 ) text.x = display.contentCenterX-60 text.y = display.contentCenterY-60 text.alpha=1 text:setTextColor(255, 254, 185) 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 local chip1={} for col=1,numCols do for row=1, numRows do chip1= display.newRect(0,0,chipWidth,chipHeight) chip1.x = xPos + col \* (chipWidth + colSpace) - chipWidth/2 - colSpace chip1.y = yPos + row\* (chipHeight + rowSpace) - chipHeight/2 - rowSpace chip1.alpha=0.1 chip1.gridPos = {x=col, y=row} chip1.state="blank" chip1:addEventListener("tap",chipTapped) end end end startGame()

I’m sorry…but I really don’t know how to do what you had say

I just Change it to newRect

once the board is click, the chip appeared.

I just manipulate the chips to look like that, please help and teach me how to put the starting chips in the board and how to capture,

I saw a lua program that runs othello, but that’s just for console, I don’t know how to do it in sdk :frowning:

take a look at this code, I cleaned up a few things, added variables to each square,you can remove the commented lines as I don’t think they are needed.

You can see each square now has a table indexed value corresponding to it’s game board location  T[1][1]  - top left squar, T[8][8] - bottom right square.

Now you can reference the center 4 squares to spawn them to begin the game and set their fill colors, states, row, col.

T[4][4]:setFillColor(1)  --not sure what color it should be to start the game

--[[] --Comment these out as they aren't used and not needed' screenLeft = display.screenOriginX screenWidth = display.contentWidth - screenLeft \* 2 screenRight = screenLeft + screenWidth screenTop = display.screenOriginY screenHeight = display.contentHeight - screenTop \* 2 screenBottom = screenTop + screenHeight display.contentWidth = screenWidth display.contentHeight = screenHeight screenTopSB = screenTop + display.topStatusBarContentHeight -- when status bar is showing screenHeightSB = display.contentHeight - screenTopSB screenBottomSB = screenTopSB + screenHeightSB --]] -- --[[] display.tl = display.TopLeftReferencePoint display.tc = display.TopCenterReferencePoint display.tr = display.TopRightReferencePoint display.cl = display.CenterLeftReferencePoint display.c = display.CenterReferencePoint display.cr = display.CenterRightReferencePoint display.bl = display.BottomLeftReferencePoint display.bc = display.BottomCenterReferencePoint display.br = display.BottomRightReferencePoint --]] 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 text = display.newText( "Void", 0, 0, "Helvetica", 24 ) text.x = display.contentCenterX-60 text.y = display.contentCenterY-60 text.alpha=1 text:setTextColor(255, 254, 185) transition.to(text,{time=1500, alpha=0}) --]] 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, 150) 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 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 playCount = playCount + 1 clicked=clicked+1 print("\*clicked == ",clicked) if (button.state=="blank") then 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}) --[[] --spawn when table is created button = display.newCircle(cx,cy,5) button:setFillColor(0) button.x=event.target.x button.y=event.target.y button.width=event.target.width button.height=event.target.height --]] T[col][row].state=clicked --need to set state to player color, 1 = black 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}) --[[] --the discs are now spawned when the board is created in grid() button1 = display.newCircle(cx,cy,5) button1:setFillColor(1) button1.x=event.target.x button1.y=event.target.y button1.width=event.target.width button1.height=event.target.height --]] T[col][row].state=clicked --need to set state to player color, 2 = white 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 = "col= "..col.." / row= "..row.."" --[[text = display.newText( "{x=" ..event.target.gridPos.x .. ", y=" .. event.target.gridPos.y .. "}", 0, 0, "Helvetica", 24 ) text.x = display.contentCenterX-60 text.y = display.contentCenterY-60 text.alpha=1 text:setTextColor(255, 254, 185) --]] 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]: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 = "Void" --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}) end

this should give you a start, you need to create a function that looks at each square “T[col][row]” adjacent to the square that is played to determine if the play is legal and if there are any Chips to flip.  for instance, if you select the top left square on your first move, your function should NOT allow the Chip to be displayed.

Nail

function start() T[4][5].originColor="white" T[4][5].originPlayCount = playCount T[4][5].Chip:setFillColor(1) T[4][5].Chip.isVisible = true T[4][5].state=0 T[5][4].originColor="white" T[5][4].originPlayCount = playCount T[5][4].Chip:setFillColor(1) T[5][4].Chip.isVisible = true T[5][4].state=0 T[5][5].originColor="black" T[5][5].originPlayCount = playCount T[5][5].Chip:setFillColor(0) T[5][5].Chip.isVisible = true T[5][5].state=0 T[4][4].originColor="black" T[4][4].originPlayCount = playCount T[4][4].Chip:setFillColor(0) T[4][4].Chip.isVisible = true T[4][4].state=0 end

I add this for starting chips,

I dont know how to make algorithm for getting legal moves, sorry about that

function OB:get\_valid\_moves(player) local x, y, vx, vy, piece local moves = {} local cells = {} local opp = self:get\_opponent(player) if opp == nil then return moves end -- Check every cell of the board... for x=1, self.size\_x do for y=1, self.size\_y do -- ...and if the cell is empty... if self:get\_cell(x, y) == " " then -- ...then check each of it's neighbors... for \_, vector in self.\_vectors do piece = self:get\_neighbor(x, y, vector) -- ...and if the neighbor contains the -- opponents piece... if piece == opp then -- ...traverse that vector looking for our -- player's piece. If we find our player, -- then we note the original blank x,y cell -- in our moves table. cells = self:traverse(x, y, vector) for \_, c in cells do vx, vy, obj = unpack(c) if obj == player then table.insert(moves, {x, y}) break end end end end end end end

I found that algorithm, but I don’t know how to translate it

Whien you start the game and spawn the first 4 Chips, you have to set the “T[4][4].state=0” to the color which is now a string, either “white” or “black”.  You also have to set the other 2 origin variables for each cell.or square.

T[4][4].originColor = “white”

T[4][4].originPlayCount = 0  – since the first 4 chips are established at game start, we will set the originPlayCount to 0 for all 4 start chips.

the originColor and originPlayCount variables will be used if you want to display a “transcript” of the game after it is completed.

I’ve placed the start(() at the bottom of the grid().

replace the grid() in your file with this modified grid() so we can continue with the same code.

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 playCount = playCount + 1 clicked=clicked+1 print("\*clicked == ",clicked) local State = "Void" --we will change the state to a string, "ehite" or "black" if clicked == 1 then State = "black" elseif clicked == 2 then State = "white" end if (button.state=="blank") then 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 --======================================== -- 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]: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 start() end

I changed the  .state variable so it’s value is a string, either “blank”, “white”, or “black” so we can compare  strings to determine the current color of the Chip.  This is the variable we will look at when we determine if a play is valid and if adjacent Chips need to be “captured” or “flipped”.

I also corrected the colors of the start chips, you had them reversed.

Review these changes so you understand the changes and why.

Nail

the function OB:get_valid_moves(player) you posted will have to be totally modified as it calls other functions I have not seen and uses the cells x,y values to determine if other cells are found along the same vectors I believe.  I’m not sure how this works from the code posted so far.

The method I have in mind to determine if a play is valid and if there are Chips to be captured will iterate over the indexes of the adjacent cells.  I’m not sure this is the best method, but I think it will work.

As you see, there are different ways to accomplish things when programming, some are better than others and some turn out ot be dead ends.  this is programming, it’s a game, a puzzle that must be solved. In my opinion, coding is the ultimate game.

I’m imagining the new validPlay() we will write to work like this…

Since we will know the col and row of the cell in play, we will look at the cell vertically above it to see if it is the opposite color and then look at the next cell to see if it is the color in play.  If these conditions are found true, the Chip above will be captured.

We will do this with for loops and they will look at all 8 possible directions from the cell in play.

I suggest the 8 possible directions or vectors around the cell in play should be labeled.

If we start Up from the cell in play and rotate clockwise through the vectors we could use these 8 labels.

Up

UpRight - this is the diagonal

Right

DownRight  - diagonal

Down

DownLeft - diagonal

Left

UpLeft  - diagonal

does this make sense?

Nail

I also added grid lines to the cells to make the game board clear.

Replace these lines in the file.

 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 = 3 --add grid lines T[col][row]:setStrokeColor(0) --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

Nail

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