1

Originally posted on math.stackexchange.com

I am trying to verify whether moving a chess-piece is valid, where the moves are calculated in one dimension.

Instead of identifying the squares on a chess-board as {A1, B1, C1, ...} they are identified as {1, 2, 3, ...}, respectively.

Calculating valid moves is done by addition/subtraction, but it is not very reliable, as it may suggest some very erroneous moves.

I first looked at how the pawn behaves:

  • It moves one square at a time (or two if in starting position) forward.
  • It strikes diagonally, one square.

So a pawn's movement possibilities is simply it's current position, plus 8 (or 16), i.e.:

C2 = 11,

11 + 8 = 19 = C3

Now, striking should be equally easy; Just by adding/subtracting 7 or 9, but this is not entirely correct...

If a pawn is on H2, which is identified as 16, then striking at:

16 + 9 = 35 = A4,

is clearly an invalid move.

To avoid this, I checked (for white pawns) if the current position of the pawn, denoted P, is:

((P + 7) % 8 != 0) && (P % 8 != 0)

Now I am really having big troubles with figuring out how to verify the moves of the remaining pieces in general...

Is there an effective way to validate the moves of the remaining pieces?

Community
  • 1
  • 1
JohnWO
  • 203
  • 3
  • 13
  • 1
    Have a look at this post : http://stackoverflow.com/questions/494721/what-are-some-good-resources-for-writing-a-chess-engine – Jérémy Dutheil Nov 15 '13 at 10:46
  • Your need to do modulus calculations on the one-dimensional vector shows that it was not a good choice to do it this way. The moves are way easier to validate in a 8x8 matrix. Why are you doing it this way? – Oliver Nov 15 '13 at 10:57
  • I can't imagine what the expression to validation castling is going to look like but I doubt it'll be pretty. – Damien_The_Unbeliever Nov 15 '13 at 11:27

1 Answers1

0

Are you trying to:

  • Generate moves for a given piece on a populated chessboard.
  • Generate moves for a given piece on an otherwise unpopulated chessboard.
  • Validate a specific piece move on a populated chessboard
  • Validate a specific piece move on an otherwise unpopulated chessboard

Your question doesn't make this context clear, and without that context it's hard to give a good answer. Move generation/validation in chess depends heavily on the board representation, and (as you've discovered) a one-dimensional array representation isn't very easy to manipulate.

Unless the one-dimensional array approach is mandated, I would consider either a 10 x 10 matrix representation or a bitboard representation. The former is simpler to visualise. Then the answer depends on which of the 4 contexts I've mentioned is correct.

EDIT: Based on your comment, the one-dimensional representation is mandated. In that case, I would start by pre-calculating the possible moves for each piece type/colour (king, queen, rook, bishop, knight, pawn) for each square on the board.

So for the example that you gave of a White pawn on h2 (16), the possible moves are 23 (g3), 24 (h3), and 32 (h4). Then you can store all of the combinations indexed in such a way that you can access them at execution time using the square number and piece type/colour located on that square.

Of course, at execution time, some of these moves may be illegal. If 23 is not occupied by an enemy piece, then that move can't be made. If 24 is occupied by any piece, then that move can't be made. If 24 or 32 is occupied by any piece, then the pawn can't be moved to 32. And so on.

HTTP 410
  • 17,300
  • 12
  • 76
  • 127
  • Short answer: All of the above. It is a requirement to do this one-dimensionally. Figuring out how to validate moved on an otherwise empty board would be a good place to start. – JohnWO Nov 15 '13 at 12:10