1

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 $\{\text{A}1, \text{B}1, \text{C}1, \dots\}$ they are identified as $\{1,2,3,\dots\}$, 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.: $$\text{C}2 = 11,\\11 + 8 = 19 = \text{C}3$$

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

If a pawn is on $\text{H}2$, which is identified as $16$, then striking at $16 + 9 = 35 = \text{A}4$, is clearly an invalid move.

To avoid this, I checked (for white pawns) if the current position of the pawn, denoted $P$, $$\left (P + 7 \not\equiv 0\quad(\bmod \,8)\right ) \wedge \left (P \not\equiv 0\quad(\bmod \,8)\right )$$

where $\wedge$ denotes logical AND.

Then, I ran into some really big troubles with knights, and 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?

JohnWO
  • 2,089
  • 14
  • 29

1 Answers1

0

Why don't you use a 2D Array to represent your chessboard?. Like this :- $\{(1,1),(1,2),(1,3).....(2,1),(2,2)......(8,8)\} $

$i$---> for rows and $j$----> for columns

now it would be a lot easier for you to manipulate your moves and the boundary conditions will be $0<i<9 $ and $ 0<j<9$

For the pawns it would be $++i $ or( $++i$ and ($++j$ or $--j$) ) and the boundary conditions.

PleaseHelp
  • 761
  • 8
  • 29