Chess Game How To Check For Pawn Capturables

I’m making a Chess game, and running into an issue with my pawns. Every time it moves it generates all pieces’ legal moves. Of course, most pieces are fine because they can just move straight into the enemy, but pawns can only move diagonally if there is a piece in that spot, so I made a rather convoluted method for checking if there is a piece there.

So here’s my code,

    private void CheckForPawnCapturables()
    {
        if (team == TeamColor.White)
        {
            if (board.pieces[1 + posOnBoard.x, 1 + posOnBoard.y] != null && board.pieces[1 + posOnBoard.x, 1 + posOnBoard.y].team != team)
            {
                legalMoves.Add(new Vector2Int(1, 1) + posOnBoard);
            }
            if (board.pieces[-1 + posOnBoard.x, 1 + posOnBoard.y] != null && board.pieces[-1 + posOnBoard.x, 1 + posOnBoard.y].team != team)
            {
                legalMoves.Add(new Vector2Int(-1, 1) + posOnBoard);
            }
        }
        else
        {
            if (board.pieces[1 + posOnBoard.x, -1 + posOnBoard.y] != null && board.pieces[1 + posOnBoard.x, -1 + posOnBoard.y].team != team)
            {
                legalMoves.Add(new Vector2Int(1, -1) + posOnBoard);
            }
            if (board.pieces[-1 + posOnBoard.x, -1 + posOnBoard.y] != null && board.pieces[-1 + posOnBoard.x, -1 + posOnBoard.y].team != team)
            {
                legalMoves.Add(new Vector2Int(-1, -1) + posOnBoard);
            }
        }
      
    }

So the pieces array is a 2D array that contains all the spots on the board and each piece has a reference to that spot in the script, basically if I move a piece it nullifies the spot it was in, and occupies the spot it moves to. So, logically this method should work(sloppy as it is lol) but it’s throwing errors because as you can imagine, the edge pieces are checking spots that are out of the arrays bounds, such as -1 or 8 on the x axis. I tried checking if it was null thinking that would prevent it, but it doesn’t

Does anyone know how I could fix this? I imagine I could add a check if the number returns -1 or 8 before checking if the spot is null, but this method is sloppy enough as it is and that would just worsen the issue lol.

You need to do a bounds check before you access the array, e.g. like you said check if the index is less than 0 or greater than 7. That’s all there is to it.

However, since a pawn should promote when it reaches the final rank, shouldn’t that be happening first anyway? :stuck_out_tongue:

1 Like

Ahh, I guess I could add it to where I do my team check at the start there.

My pieces are all sharing the same script, just sprites are different and the moves are in an array of arrays, it checks which one to return from using a piece index and then gets the moves from there, so promotion should be super easy to implement :smile: I’ll just have another check if the team is white and the y position is 7 or if black and the position is 0 :slight_smile:

Hmm, maybe I’m just being dumb, but I’m still getting 2 errors. Weird that I’m getting 2 instead of 4 now, but here’s my current script.

    private void CheckForPawnCapturables()
    {
        if (team == TeamColor.White && 1 + posOnBoard.x < 8 && 1 + posOnBoard.x > -1)
        {
            if (board.pieces[1 + posOnBoard.x, 1 + posOnBoard.y] != null && board.pieces[1 + posOnBoard.x, 1 + posOnBoard.y].team != team)
            {
                legalMoves.Add(new Vector2Int(1, 1) + posOnBoard);
            }
            if (board.pieces[-1 + posOnBoard.x, 1 + posOnBoard.y] != null && board.pieces[-1 + posOnBoard.x, 1 + posOnBoard.y].team != team)
            {
                legalMoves.Add(new Vector2Int(-1, 1) + posOnBoard);
            }
        }
        else if(1 + posOnBoard.x < 8 && 1 + posOnBoard.x > -1)
        {
            if (board.pieces[1 + posOnBoard.x, -1 + posOnBoard.y] != null && board.pieces[1 + posOnBoard.x, -1 + posOnBoard.y].team != team)
            {
                legalMoves.Add(new Vector2Int(1, -1) + posOnBoard);
            }
            if (board.pieces[-1 + posOnBoard.x, -1 + posOnBoard.y] != null && board.pieces[-1 + posOnBoard.x, -1 + posOnBoard.y].team != team)
            {
                legalMoves.Add(new Vector2Int(-1, -1) + posOnBoard);
            }
        }
      
    }

I never thought Chess would be so difficult to program lol

Wait till you try to make a computer that plays Chess.

If I were you I would make a function that checks if a position is on the gird and just call that to reduce the reptition in your code:

public static bool IsOnBoard(this Vector2Int position) {
  return position.x > 0 && position.x < 8
    && position.y > 0 && position.y < 8;
}
1 Like

I have to make a basic AI, this is for a client lol, this is hell XD

I fixed it, but not before creating an infinite loop that ate up 16gb of RAM, that was fun! lol, thanks for the help

This may be helpful to watch

I love Sebastian Lague, I’ve watched this, but it’s way above my paygrade to make such a complex AI, lol. Thanks though.