Chess Centvol Caiculatiens Using APL2 --by Shriram H. B i y a n i Hawthorne, New York WAS BROWSING T H R O U G H A PROGRAMMING EDUCATION BOOK The locations controlled are given by these expressions: ¢ Locations controlled by rook: R+Eo . ( ¢ o .¢)E Locations controlled by bishop: B X#Do . as a possible gift for a young engineer, when the following problem caught my eye: "It is known that a chessboard can be completely covered by five queens. Is it feasible to determine by exhaustive testing whether it can be covered using only four queens?" (A location is said to be "covered" if it is occupied by or is under attack by a queen.) The number of ways to place four queens on the chessboard is the number of combinations of 4 items out of 64 ( 4 ! 6 4 in APL), which is 6 3 5 3 7 5 . So the problem looked entirely feasible, using an average PC. To solve this problem, one would need to efficiendy generate the combinations of positions, and determine which locations are covered by a queen placed at any given location. For the first part --generating the combinations--there are some well known solutions. For the second part, it turns out that there are some concise expressions in APL2, (or any APL with nested arrays) that give the locations controlled by the queen as well as other chess pieces, from every location on the board. The locations controlled from one specific location on the board can be represented by an 8-by-8 binary matrix, where i represents a controlled location, and 0 represents an uncontrolled location. The locations controlled from all locations can be collectively represented as an 8-by-8 matrix of binary matrices. The location within the outer matrix represents the location of the piece under consideration. The inner matrix represents the locations controlled by the piece from the given location. These nested matrices can be generated as follows: ¢ I ( o .= )D ¢ Locations controlled by queen: Q~BvR While we are at it, here are the expressions for the locations controlled by a king and a knight: ¢ Locations controlled by king: K,-X¢( D_<i ) o. ( o. A ) D<i ¢ Locations controlled by knight: N+( ( D<2 ) o. ( o. A ) D_<2 )A,--,QvX ¢ To complete the set, we need two expressions for the pawns --one for white and one for black. These are given by: PW+(S:i)o.(o.h) D = i PB~-(S=-i)o.( o.A ) D : i The expressions for pawns include some entries corresponding to "impossible" locations for pawns. While they are not logically incorrect, one may wish to zero them out, for esthetic reasons. In APL2, one can use: PW PW A [DIO] (7pl),O PB PB A [DIO] 0,7pl The common idea among all expressions is the following. The left argument of the double outer product corresponds to the change in row number, while the right argument corresponds to the change in column number. The relationship between the two determines whether or not a location is controlled by a given piece. The inner (right)jot-dot calculations the controlled positions from one piece location, while the outer outer product repeats the calculation for all piece locations. As to the original problem, I will omit the remaining details of the calculations, but the final answer is: no, you can't cover the board with four queens. In fact, the maximum number of locations that can be covered is 62 out of 64, and there are 41 ways to do that, counting only one arrangement from mirror-image alternatives. ¢ Shriram H. Biyani works at the IBM T. j . Watson Research Center in Hawthorne, New York. He may be reached at "biyani@watson.ibm .C01n ~. Row/column#s: ¢ Signed change in row#/column#: S~I~I ¢ Absolute change: D'-IS ¢ Is row#/column# unchanged? E~S=O ¢ Location occupied by the piece: X*-Eo . ( o.A )E X is like an "identity matrix," with a 3. at the location of the piece, and @elsewhere. SEPTEMBER 1997 - - VOLUME 28, NUMBER 1
/lp/association-for-computing-machinery/chess-control-calculations-using-apl2-0JcMaUiPPM