I need my code reviewed.

OK, so im going on my 2 week in trying to learn CODEA and after some help on the fourms and youtube i come up with this…

– tic tac toe –
– Created by Evan Davis on September the 1st –
– I was just fooling around and made this, i have no idea about the “board” class, and after i made this code i started inserting random numbers trying to get it to work. :slight_smile: Please dont put too much effort into fixing it, and thanks for any help!!

function setup()
setup:init

   board={
{"TL",1,w20,h60,w40,h80,0},
{"TM",2,w40,h60,w60,h80,0},
{"TR",3,w60,h60,w80,h80,0},
{"ML",4,w20,h40,w40,h60,0},
{"MM",5,w40,h40,w60,h60,0},
{"MR",6,w60,h40,w80,h60,0},
{"BL",7,w20,h20,w40,h40,0},
{"BM",8,w40,h20,w60,h40,0},
{"BR",9,w60,h20,w80,h40,0}

}
end

function draw()
background(0, 0, 0, 255)
stroke(255, 255, 255, 255)
strokeWidth(2)
– left line –
line(w40,h20,w40,h80)
– right line –
line(w60,h20,w60,h80)
– bottom –
line(w20,h40,w80,h40)
– top –
line(w20,h60,w80,h60)
for t=1,9 do
if CurrentTouch.x>board[t][3] and CurrentTouch.x<board[t][5] then
if CurrentTouch.y>board[t][4] and CurrentTouch.y<board[t][6] then
if CurrentTouch.tapCount==1 then
board[t][7]=1
elseif CurrentTouch.tapCount==2 then
board[t][7]=1
end
end
end
end
end

           -- This is where the problem is

for i=7,9 do
board[i][1]=1
text(“x”,board[i][3],board[i][4])
text(“0”,board[i][3],board[i][4])
end

I hve no idea if this is even close to making a tic tac toe game, any advice will be greatly appreciated! Thanks

I haven’t looked at your code yet, but if you want the code to display correctly on the forums, then put three ~'s before and after your code

@EvanDavis There are a lot of things wrong with your code, but I don’t have time right now to explain them. The main things are formatting your code so you match the end statements with the if and for and function statements. Plus you’re using a lot of variable names but they don’t have values assigned to them.

@EvanDavis Here’s an example to get you started. You can look at this code and make corrections to yours, or take this and finish it.

function setup()
    rectMode(CENTER)
    size=140
    fontSize(size)
    board={}
    for x=1,3 do
        for y=1,3 do
            table.insert(board,square(x*size,y*size,size))            
        end
    end
    xo="O"
end

function draw()
    background(40, 40, 50)
    for a,b in pairs(board) do
        b:draw()
    end
end

function touched(t)
    for a,b in pairs(board) do
        b:touched(t)
    end
end

square=class()

function square:init(x,y,s)
    self.x=x
    self.y=y
    self.s=s
    self.xo=""
end

function square:draw()
    fill(255)
    rect(self.x,self.y,self.s,self.s)
    fill(255,0,0)
    text(self.xo,self.x,self.y)
end

function square:touched(t)
    if t.state==BEGAN then
        if t.x>self.x-self.s/2 and t.x<self.x+self.s/2 and
                t.y>self.y-self.s/2 and t.y <self.y+self.s/2 then
            if self.xo=="" then
                self.xo=xo
                if xo=="O" then
                    xo="X"
                else
                    xo=("O")
                end
            end
        end        
    end
end

Haha I was so far off, thanks though I’ll definitely work on yours!

It’s quite normal to feel you’re drowning at first, so just keep practising and you’ll find it gets easier and more fun, quite quickly