Taking turns - Naughts and Crosses gamee

I have code to create the scene for taking turns. First scene/file is for X first then O and then I need to do another for O first then X. I have followed the instructions given but cannot work out how to make this code work. Any advice would be greatly appreciated. 

Thanks!

local composer = require( "composer" ) local scene = composer.newScene() d = display w20 = d.contentWidth \* .2 h20 = d.contentHeight \* .2 w40 = d.contentWidth \* .4 h40 = d.contentHeight \* .4 w60 = d.contentWidth \* .6 h60 = d.contentHeight \* .6 w80 = d.contentWidth \* .8 h80 = d.contentHeight \* .8 ----DRAW LINES FOR BOARD local lline = d.newLine(w40,h20,w40,h80 ) lline.strokeWidth = 5 local rline = d.newLine(w60,h20,w60,h80 ) rline.strokeWidth = 5 local bline = d.newLine(w20,h40,w80,h40 ) bline.strokeWidth = 5 local tline = d.newLine(w20,h60,w80,h60 ) tline.strokeWidth = 5 --PLACE BOARD COMPARTMENT DIMENSIONS IN TABLE board ={ {"tl",1,w20,h40,w40,h20,0}, {"tm",2,w40,h40,w60,h40,0}, {"tr",3,w60,h40,w80,h20,0}, {"ml",4,w20,h60,w40,h40,0}, {"mm",5,w40,h60,w60,h40,0}, {"mr",6,w60,h60,w80,h40,0}, {"bl",7,w20,h80,w40,h60,0}, {"bm",8,w40,h80,w60,h60,0}, {"br",9,w60,h80,w80,h60,0} } -- local EMPTY, X, O = 0, 1, 2 local whichTurn = X -- X is starting game --FILL COMPARTMENT W/ COLOUR WHEN TOUCHED local function fill(event) if (event.phase == "began") then for t = 1,9 do if event.x \> board[t][3] and event.x \< board[t][5] then if event.y \< board[t][4] and event.y \> board[t][6] then&nbsp; if board[t][7] == EMPTY then board[t][7] = whichTurn whichTurn = whichTurn == X and O or X end &nbsp; end end end end end Runtime:addEventListener ("touch", fill) return scene

You would not do another scene/file for when O goes first. 

Why not just have the turn value be an integer that is 1 or 2, and depending on what that value is choose the right ‘mark’.

[lua]

local whoseTurn = math.random(1,2)  – choose a random player to go first

local playerMarks = {“X”, “O”}

– switch turns

if whoseTurn == 1 then

  whoseTurn = 2

else

  whoseTurn = 1

end

[/lua]

Thank you for the reply. However, the requirements I have been given for this game says that the players must be able to choose which icon goes first. So I have created a menu scene that has an X and an O button and the selected one goes first. I just can’t work out how to make the game itself work within that constraint. 

All you do is change the whoseTurn value when entering the game scene depending on what was selected in the menu. 

Look at the composer API docs for information on how to pass variables between scenes. 

Ok I have added what I think was along the right lines. And it somewhat works. Only problem is that when I tap a square the X only shows occasionally and the O not at all. What have I done wrong?

The code - 

local composer = require( "composer" ) local scene = composer.newScene() d = display w20 = d.contentWidth \* .2 h20 = d.contentHeight \* .2 w40 = d.contentWidth \* .4 h40 = d.contentHeight \* .4 w60 = d.contentWidth \* .6 h60 = d.contentHeight \* .6 w80 = d.contentWidth \* .8 h80 = d.contentHeight \* .8 ----DRAW LINES FOR BOARD local lline = d.newLine(w40,h20,w40,h80 ) lline.strokeWidth = 5 local rline = d.newLine(w60,h20,w60,h80 ) rline.strokeWidth = 5 local bline = d.newLine(w20,h40,w80,h40 ) bline.strokeWidth = 5 local tline = d.newLine(w20,h60,w80,h60 ) tline.strokeWidth = 5 --PLACE BOARD COMPARTMENT DIMENSIONS IN TABLE board ={ {"tl",1,w20,h40,w40,h20,0}, {"tm",2,w40,h40,w60,h40,0}, {"tr",3,w60,h40,w80,h20,0}, {"ml",4,w20,h60,w40,h40,0}, {"mm",5,w40,h60,w60,h40,0}, {"mr",6,w60,h60,w80,h40,0}, {"bl",7,w20,h80,w40,h60,0}, {"bm",8,w40,h80,w60,h60,0}, {"br",9,w60,h80,w80,h60,0} } -- local EMPTY, X, O = 0, 1, 2 local whichTurn = 1 -- X is starting game --FILL COMPARTMENT W/ COLOUR WHEN TOUCHED local function fill(event) if (event.phase == "ended") then for t = 1,9 do if event.x \> board[t][3] and event.x \< board[t][5] then if event.y \< board[t][4] and event.y \> board[t][6] then&nbsp; if board[t][7] == EMPTY then &nbsp; if whichTurn == 1 then &nbsp; &nbsp; whichTurn = 2 &nbsp; else &nbsp; &nbsp; whichTurn = 1 board[t][7] = whichTurn if board[t][7] == 1 then &nbsp; &nbsp;display.newText("X", board[t][3], board[t][4], "Arial", 100) elseif board[t][7] == 2 then&nbsp; &nbsp; &nbsp;display.newText("O", board[t][3], board[t][4], "Arial", 100) end end end &nbsp; end end end end end Runtime:addEventListener ("touch", fill) return scene

You would not do another scene/file for when O goes first. 

Why not just have the turn value be an integer that is 1 or 2, and depending on what that value is choose the right ‘mark’.

[lua]

local whoseTurn = math.random(1,2)  – choose a random player to go first

local playerMarks = {“X”, “O”}

– switch turns

if whoseTurn == 1 then

  whoseTurn = 2

else

  whoseTurn = 1

end

[/lua]

Thank you for the reply. However, the requirements I have been given for this game says that the players must be able to choose which icon goes first. So I have created a menu scene that has an X and an O button and the selected one goes first. I just can’t work out how to make the game itself work within that constraint. 

All you do is change the whoseTurn value when entering the game scene depending on what was selected in the menu. 

Look at the composer API docs for information on how to pass variables between scenes. 

Ok I have added what I think was along the right lines. And it somewhat works. Only problem is that when I tap a square the X only shows occasionally and the O not at all. What have I done wrong?

The code - 

local composer = require( "composer" ) local scene = composer.newScene() d = display w20 = d.contentWidth \* .2 h20 = d.contentHeight \* .2 w40 = d.contentWidth \* .4 h40 = d.contentHeight \* .4 w60 = d.contentWidth \* .6 h60 = d.contentHeight \* .6 w80 = d.contentWidth \* .8 h80 = d.contentHeight \* .8 ----DRAW LINES FOR BOARD local lline = d.newLine(w40,h20,w40,h80 ) lline.strokeWidth = 5 local rline = d.newLine(w60,h20,w60,h80 ) rline.strokeWidth = 5 local bline = d.newLine(w20,h40,w80,h40 ) bline.strokeWidth = 5 local tline = d.newLine(w20,h60,w80,h60 ) tline.strokeWidth = 5 --PLACE BOARD COMPARTMENT DIMENSIONS IN TABLE board ={ {"tl",1,w20,h40,w40,h20,0}, {"tm",2,w40,h40,w60,h40,0}, {"tr",3,w60,h40,w80,h20,0}, {"ml",4,w20,h60,w40,h40,0}, {"mm",5,w40,h60,w60,h40,0}, {"mr",6,w60,h60,w80,h40,0}, {"bl",7,w20,h80,w40,h60,0}, {"bm",8,w40,h80,w60,h60,0}, {"br",9,w60,h80,w80,h60,0} } -- local EMPTY, X, O = 0, 1, 2 local whichTurn = 1 -- X is starting game --FILL COMPARTMENT W/ COLOUR WHEN TOUCHED local function fill(event) if (event.phase == "ended") then for t = 1,9 do if event.x \> board[t][3] and event.x \< board[t][5] then if event.y \< board[t][4] and event.y \> board[t][6] then&nbsp; if board[t][7] == EMPTY then &nbsp; if whichTurn == 1 then &nbsp; &nbsp; whichTurn = 2 &nbsp; else &nbsp; &nbsp; whichTurn = 1 board[t][7] = whichTurn if board[t][7] == 1 then &nbsp; &nbsp;display.newText("X", board[t][3], board[t][4], "Arial", 100) elseif board[t][7] == 2 then&nbsp; &nbsp; &nbsp;display.newText("O", board[t][3], board[t][4], "Arial", 100) end end end &nbsp; end end end end end Runtime:addEventListener ("touch", fill) return scene