Chess in codea

Hi,

I’m new to codea and Lua, I’m hoping to develop a chess game using codea. I’d like to initially develop a very basic game that you can play between two humans. Then at some point I would like to try developing a chess AI. I’m also interested in making the game available to play online, I realise the last two are a lot more complex so just want to try designing a game that plays first.

There are several examples I’ve seen of chess games however I’m slightly lost in the code, and also I would like to try coding it myself. can anyone help in terms of starting points or some useful examples that might help me to understand how to start building this?

I have experience of c++, JavaScript, vb etc though starting from scratch with Lua.

I also wondered, can you transfer something you’ve built in codea into an app and how do you do this? Does it have to be put into x-code on a Mac?

Thanks
Andrew

For starters, you need to convert to x-code and I think it only works on a Mac.

As for the chess game, it should be kind of easy. The AI shouldn’t be too bad, but I don’t know how competitive it would be. Probably a lot harder to make it smarter.

@abeswick Here’s a start. Tap on a square to select it.

viewer.mode=FULLSCREEN

function setup() 
    size=math.min(WIDTH,HEIGHT)//10 
end

function draw()
    background(255, 207, 0)
    drawBoard()
end

function drawBoard()
    a=true
    for x=1,8 do
        a=not a
        for y=1,8 do
            a=not a
            fill(223, 149, 196)
            if a then
                fill(155, 220, 188)
            end
            if tx==x and ty==y then
                fill(255)
            end
            rect(x*size,y*size,size+1)
        end
    end
end

function touched(t)
    if t.state==BEGAN then
        for x=1,8 do
            for y=1,8 do
                if t.x>x*size and t.x<x*size+size and t.y>y*size and t.y<y*size+size then
                    tx=x
                    ty=y
                end
            end
        end
    end
end

I added a little bit more code. Here’s a playable chess game. Tap the piece you want to move, then tap where you want it. There are no edits, so you have to know the valid moves for each piece. A white square shows the last move.

@dave1707 - I found the same Love2D project, but no time to do anything with it.

@dave1707 - didn’t like the colour scheme so modified and centred board in either orientation. Mainly so I could get decent project icon.

Edit: don’t like the way Codea redraws the screen after rotation.

Thanks both, that’s really helpful and a great start

I noticed yours Dave the pieces were on their side so I edited it slightly

pieces()
pieceTab={{wc,wp,“”,“”,“”,“”,bp,bc},{wn,wp,“”,“”,“”,“”,bp,bn},
{wb,wp,“”,“”,“”,“”,bp,bb},{wq,wp,“”,“”,“”,“”,bp,bq},
{wk,wp,“”,“”,“”,“”,bp,bk},{wb,wp,“”,“”,“”,“”,bp,bb},
{wn,wp,“”,“”,“”,“”,bp,bn},{wc,wp,“”,“”,“”,“”,bp,bc}}

I was wondering about centering the board, what is the code for doing this?

The only issue is the pieces seem to disappear off the board now after a couple of moves, I’ll need to build in the piece movement and look at that in more detail though so can experiment a bit with that. It’s looking good though, the big thing will be developing an AI but I need to have a think through exactly how I want to do that as I’d like it to be slightly different to other engines, that’s the part I’m most interested in though

@abeswick - I put code in my post that roughly centres the board, it uses the vec2() offset. It may need tickling to be properly centred by adjusting the x and y values of offset. Note the screen is even numbered 1024x768 so you can’t truly centre it - it will be out by 1 pixel.

@abeswick - the first thing you need to do is check for legal moves. Then you need to either accept latest move, reject move and return piece to original position or confirm interaction and remove opponents piece.

Then you need to analyze the board looking for check, or checkmate. Or possibly unable to move.

Once you have the basic system present then you can think of AI. Lot to do before that.

@Bri_G Ran your zip and the touches are off.

@dave1707 - doooh, hmm - thanks will amend and repost.

@Bri_G @abeswick I didn’t want to do too much. I just wanted to get the basics done so more work could be done by abeswick with movement checks and then move on to AI.

@dave1707 - basics there now, link above updated with new file. Let’s see how this rolls out now.

@Bri_G That works a lot better.

Here’s an update to my code. I added edits for the knight moves just to see how easy it would be. Tap a knight and it’s valid moves will be shown. This could also be used for the AI. If a valid move lands on an opponent’s piece, a value for that piece could be made and the highest value could result in that move.

Thanks both:). That’s perfect and just what I need to make a start on this project. Can I ask a couple of questions to get a grasp of the code and how it works?

Could you explain the knightMoves function/ what this does? For instance what is the tab= and what is the z variable? Does that relate to number of squares in each direction?

Also what is the resetValidMoves function and how does this work?

I was also curious about this bit of text:

if validMoves[x][y]==“.” then
sprite(asset.builtin.Space_Art.UFO,xsize+offsetW,ysize+offsetH,30)

I presume this creates the dots on the knight when you select the piece?

Thanks again :slight_smile: