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