= Notes for A Chess Program NOTES A string data structure could be used like this XXXXXXXXXX XrnbqkbnrX XppppppppX X00000000X X00000000X X00000000X X00000000X XPPPPPPPPX XRNBQKBNRX XXXXXXXXXX So lower case letters are used for the black pieces, and upper case letters for the White pieces. The number (or character) zero is used for empty squares. The idea of the X's surrounding the board is that they make it easier to check if a particular move is 'off the board' and therefor illegal. This could be done by checking the current position and the move length, but that may take time consuming calculations. The text string above actually wouldn't contain any new-lines or spaces. Why a text string do I hear you ask. Well, just because maybe I could write this is the unix / linux 'bash' shell and use programs like 'tr' and 'sed' to perform the move calculations. Why? Because this is in keeping with my idea that the unix shell is a very good 'prototyping' device. The board squares could be numbered like this And so on (up the board) [11][12][13][14][15][16][17][18][19][20] [01][02][03][04][05][06][07][08][09][10] So move calculations would not involve 'cartesian' arithmetic, with two co-ordinates, but only one cordinate for each square (the square number). The move calculations would involve some kind of modulus arithmetic. I could number the possible move directions as 8 1 2 \|/ 7- -3 /|\ 6 5 4 So each time that a direction of moves for a piece was checked the direction would be incremented. And the move length would be incremented as well in an inner loop There may well be a better way to order the moves but I dont know what it is. So a move would be defined by its 'starting square' 'direction' and 'length'. Knight moves are going to complicate this a bit. In an OO world I suppose this would define the Move object. The Object Oriented solution may well be conceptually easy to write, but I feel that it would be hopelessly slow and not particularly edifying. Obviously this is not going to be particularly fast or efficient, but it may demonstrate the necessary algorithms in a clear and simple way. The most important and difficult function is going to be getNextLegalMove() This is because the legal chess moves need to be ordered in some way, and there has to be some reasonably quick way of finding the next one. Data variables needed (all text strings of course) $sCurrentBoardPosition $sCurrentSquare The square which contains the piece that is the 'current piece'. That is: the piece that is being currently checked for legal moves. $sCurrentMoveDirection $sCurrentMoveLength $sCurrentPiece Here below is a kind of 'stack trace' to help me think about the problem sCurrentPiece == PAWN sCurrentDirecion == 1 sCurrentMoveLength == 1 sCurrentSquare == ? What is the target square? These if statements really should be 'case' statements. I wonder if there is a way to do this arithmetic with string substitutions, since we are using a text shell function isMoveLegal(LENGTH, DIRECTION, START-SQUARE) { TARGET-SQUARE=getTargetSquare() TARGET-SQUARE-PIECE=getTargetSquarePiece(TARGET-SQUARE) if [ TARGET-SQUARE-PIECE == 'X' ] then return false fi if [ CURRENT-PIECE == 'Pawn' ] && [ CURRENT-DIRECTION == diagonal-forward ] && [ Current- } # function isMoveLegal function getTargetSquare(START-SQUARE, MOVE-LENGTH, MOVE-DIRECTION) { if [ $sCurrentDirection == 1 ] then if [ $sCurrentMoveLength == 1 ] then sTargetSquare=$sCurrentSquare + 10 elif [ $sCurrentMoveLength == 2 ] then sTargetSquare=$sCurrentSquare + 20 elif [ $sCurrentMoveLength == 3 ] then sTargetSquare=$sCurrentSquare + 30 fi elif [ $sCurrentDirection == 2 ] then ... fi } # function getTargetSquare / Piece