-- 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") print\_r.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 = {} --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 ----------end Up\_Right 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()