Chess in codea

I see the last part is one of the built in assets

@abeswick Hope this explaines what I’m doing for the knight. I’m also working on the valid moves of the other pieces. I’ll hold off posting those just in case you want to work them out.

function knightMoves(x,y,p)

    tab={-1,2, 1,2, 2,1, 2,-1, 1,-2, -1,-2, -2,-1, -2,1,}

— this is how the knight moves in 8 directions, it’s x,y values. The first pair -1,2, is 1 square left and 2 squares up. The second pair 1,2 is 1 square right and 2 squares up. The other values are the other 6 moves. 

    for z=1,16,2 do
        x1=x+tab[z]
        y1=y+tab[z+1]

— this iterates thru the tab table, getting every 2 positions to put the pair of values in x1 and y1 and adds those to the current knights position.


        if x1>0 and x1<9 and y1>0 and y1<9 then
            pos=pieceTab[x1][y1]
            if pos=="" then
                validMoves[x1][y1]="."
            end

— this makes sure to only use the x1, y1 values if it’s on the board 1 to 8 in either direction. If it is, it moves whatever is on the board at the x1,y1 square to the variable pos. If pos is blank, then that’s a possible move.

            if p==wn then
                if pos==bk or pos==bq or pos==bb or
                pos==bn or pos==bc or pos==bp then
                    validMoves[x1][y1]="."
                end
            end

— if we’re using a white knight (wn) and what’s at pos is a black piece, then those are valid moves. 

            if p==bn then
                if pos==wk or pos==wq or pos==wb or 
                pos==wn or pos==wc or pos==wp then
                    validMoves[x1][y1]="."
                end
            end

— if we’re using a black knight (bn) and what’s at pos is a white piece, then those are valid moves. 

        end
    end
end

Thanks, yes that all makes sense I think :). No worries I’m going to try to have a go at coding the other pieces, will let you know if I get stuck