Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

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

Tagged:

Comments

  • dave1707dave1707 Mod
    Posts: 9,725

    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.

  • dave1707dave1707 Mod
    edited November 16 Posts: 9,725

    @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
    
  • dave1707dave1707 Mod
    Posts: 9,725

    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.

    001j.zip 44.3K
  • Posts: 2,496

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

  • edited November 17 Posts: 2,496

    @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.

    Chess001.zip 94.4K
  • edited November 17 Posts: 9

    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}}

  • edited November 17 Posts: 9

    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

  • edited November 17 Posts: 2,496
    @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.
  • Posts: 2,496
    @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.
  • dave1707dave1707 Mod
    Posts: 9,725

    @Bri_G Ran your zip and the touches are off.

  • edited November 17 Posts: 2,496
    @dave1707 - doooh, hmm - thanks will amend and repost.
  • dave1707dave1707 Mod
    Posts: 9,725

    @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.

  • Posts: 2,496
    @dave1707 - basics there now, link above updated with new file. Let's see how this rolls out now.
  • dave1707dave1707 Mod
    Posts: 9,725

    @Bri_G That works a lot better.

  • dave1707dave1707 Mod
    Posts: 9,725

    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.

    chess1.zip 44.9K
  • edited November 18 Posts: 9

    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?

  • edited November 18 Posts: 9

    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 :)

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

  • dave1707dave1707 Mod
    edited November 18 Posts: 9,725

    @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

Sign In or Register to comment.