Sunday, 13 May 2012

Board representation and piece movement.

There are several ways of representing a chess board in a chess program. One such method is the use of bit boards, but considering the fact that I'm using VB6 I have decided to implement a simple single dimensional array. I'm assuming that visual Basic 6 is quite efficient at handling array and to try to utilise bit-boards would be more complicated to program in VB and maybe less efficient. I may be wrong, we'll just have to see how it goes.

I am representing the board by a single dimensional array of 36 element indexed 0 to 35.

Although it's a single dimensional array, as far as human thinking is concerned, I tend to think of the structure in two dimensions. That way it is easy to see which cells pieces move to etc.

The pieces are represented by positive numbers for white and negative numbers for black with vacant squares represented by zeros as follows:

pawn = 1, Knight = 2, Bishop = 3, Rook = 4, Queen = 5, King = 6

You will notice I have included a value for a bishop when the game does not include bishops. The reason for this is that I intend to give the user the option of setting up their own board position and I'm giving them the option to use bishops if they so desire.

To enable the computer to generate piece moves I have set up two dimensional arrays for each of the pieces which list all the possible moves available from any given square. If you examine the table below you can see for example that a knight on board square 10 has possibly 4 move to squares 2, 14, 21 and 23. Obviously not all of these moves may be legal. The program will have to check the destination square is not occupied by a piece of the same colour, or that it is not moving into check.

I've created similar tables for each of the chess pieces. For example, there are four tables which represent the rook moves. One table for each move direction N, S, E and W. If we take a rook on square 0, the table will show that it can move to squares 1, 2, 3, 4 and 5. The computer will look at each of these in turn until it reaches the end of the list or reaches an occupied square. If it reaches an occupies square with a piece of the same colour then this final move is not playable. If the occupied square has a piece of the opposite colour then that will be the final move available in that direction.

I have created two separate tables (arrays) for the pawn, as, unlike the other pieces which all move the same, the black and white pawns move in opposite directions.

1 comment: