Matrix Applications in Chess Douglas Steiner November 18, 2009 Abstract This report illustrates basic applications of linear algebra for solving simple endgame positions in chess. 1 Introduction The game of chess has attracted mathematicians for generations. With its geometric board and vector like piece movement, the proposition that chess can be solved mathematically has caused centuries of man hours to be spent searching for the elusive formula which will reduce chess to a precise formula which if followed, will inevitably result in victory. Deriving this formula by hand is generally accepted as impossible, but it is hoped (by some) or even believed (by a small but vocal minority) that the computer, pointed in the right direction, will one day do just that: derive the formula that will solve chess. The use of linear algebra to achieve this end, specifically in the endgame of chess, is the subject of this paper. 2 2.1 Basic Matrix applications in chess: positions, pieces, and movements Standard Chess notation Figure 1: Starting position for chess Playing chess beyond the occasional game with Grandpa will eventually expose the player to the concept of chess notation. The necessity of chess notation arose from the desire to analyze why one player lost or another won (or failed to win what he thought was a won position) a particular game. Unlike many other games involving piece movement and strategy, chess lends itself well to endless post-mortem analysis, arising from its square board of 64 alternating black and white squares and its rigid rules for the movement of the pieces. There are literally millions of recorded games played by the masters (and grandmasters) of the sport, available for perusal in printed and electronic databases. The need to record these moves systematically eventually gave way to what is know as 1 algebraic notation. This term algebraic notation is misleading, as the system does not use algebraic formulas to describe movements but rather alphanumeric designations which appear to be mathematical equations. Algebraic notation is can be understood this way: the 8x8 chess board is numbered and lettered from the perspective of the player with the white pieces, with the letters from a-h flowing from left to right across the horizontal axis and numbers from 1-8 proceeding from the bottom to the top of the vertical axis, as shown below: Figure 2: chess board with rank and column labels The chess pieces themselves are assigned a letter designation as follows: Figure 3: piece notation Although pawns are in the table, their moves are given by entering only the destination square, based on the corresponding alphanumeric designation of the 2 square. Piece movements (knights and higher) also contain this information, but also include the capital letter given in the above table. How does this all come together? Look at the chess position below: Figure 4 Obviously, the chess pieces are not at their starting positions. The chess notation which explains how the pieces arrived at this position is as follows: 1.e4 e5 2.Nf3 Nc6 3.Bc4 Bc5 The movements are recorded sequentially, with the white move given first and the black move given second. 1.e4 e5 means that white moved his pawn from square e2 to e4, while black responded with the pawn move e7-e5. 2.Nf3 designates white moving his knight on a7 to the f3 square, while black’s response 2...Nc6 is a knight move from b8 to c6. This form of recording moves using only the destination square is called short algebraic notation. While useful for post game analysis by human players, using this form of notation for mathematical analysis and manipulation of chess positions breaks down immediately, as the alpha designators are not variables, but constants which do not change, both for the pieces and for the board. 2.2 Assigning numeric values to the chess board The task of assigning numeric values to specific squares on the chess board (so that it can be more easily manipulated with matrices) can be as simple as using the row column approach, or by assigning binary values with a 10 digit series. 3 Figure 5: Row column chessboard Figure 6: Binary chessboard The row column approach, it should be noted, starts at the top left, while algebraic chess notation starts at the bottom left, which means the natural method of numbering inherent in matrices is upside down and backwards to the chess method. The cartesian coordinate system is more useful for this reason as numbering begins in the lower left and proceeds to the upper right, just like chess notation. In this approach, the x and y axes are off the board. Figure 7: Cartesian coordinate chessboard 4 2.3 Assigning numeric values to the chess pieces Giving distinct numerical values to chess pieces is straightforward. The table below gives a value to each chess piece, beginning with the white pawn and ending with the black king. Remember that these numerical values are constants, and not necessarily a measure of the importance of an individual piece. Also, white is numbered with single digit numbers, and black with double digits, to avoid confusion as to what color piece is on each square. The bishops are each given a distinct number, as one is always on the light squares, and the other always on the dark squares. As rooks and knights can of course be on either color square, individual designations for each rook or knight are not needed. Figure 8: Chess piece ”values” Again, potential confusion with chess norms requires clarification. For chess aficionados, the ”values” assigned to chess pieces are of a strategic nature, and refer to a pieces tactical strength. A queen has a value of 9, a rook 5, a bishop 3, a knight 3, and a pawn 1. There is no value attached to the king, as his imminent captures ends the game, his value is unlimited. However, his tactical strength is usually viewed as between a bishop and a rook. For our purposes, the above chart of values should be seen as piece designators, not actual values 2.4 Matrix representation of chess positions The simple construction of a chess position using an 8x8 matrix can be accomplished by using the values for individual pieces given above, and by using a zero 5 value for unoccupied squares. The starting position on a chess board would be rendered this way: Figure 9 Experienced chess players would see big problems with attempts at analyzing positions represented this way. The obvious lack of dark and light square distinction makes the system unusable, but as it is designed to be manipulated mathematically by a computer, rather than by a human player, this is not an issue. 2.5 Vectors and movements of chess pieces For the author, conceptualization of actual piece movement was the most difficult task. How does one account for both direction and magnitude of all the move options available to a piece on the board? For a pawn, other than its first move, all moves will be one space, but how to account for the variety of moves available to a bishop, for instance, which depending on position can move in 4 separate directions anywhere from 1 to 7 squares? The solution is to use a relative bearing approach, in which the actual square a piece occupies becomes the origin of a cartesian coordinate system. In a sample position with the white king at b2 (chess notation) below moves to the square c3, his move could be represented as (1,1): 6 Figure 10 Figure 11 By this method, each piece would have a separate ”movement matrix” for all 64 squares of the chess board, as the position on the board would dictate how far each piece could move in each direction. The chart below shows movement vectors for each piece and for pawns, for simplicity sake constraints such as the 7 edge of the board and other pieces are omitted. Figure 12: Movement vectors Below are a few sample matrices for different pieces in a few locations around the board, using the movement vectors from above. Figure 13 8 Figure 14: King on b2 with movement vectors Figure 15: Numeric representation of black’s light square bishop on d5, with movement vectors Another approach for expressing piece movements is one used to describe movement vectors in a program developed to ”detect” draw by repetition movements (which is where the same position is arrived at three times in the same game) is to use a variation of long algebraic notation in which the piece designation is omitted, and only the origin and destination squares are called out. In this case, the movement of the white king from b2 to c3, shown in figure 10 and 11 above, which would be Kb2 in shorthand, would be A1B2. The advantage of this approach is that the alpha designator can be removed and replaced with 9 a number, in which case all moves can be described using a 4 digit string. For this technique, the first 3 moves of the position shown in figure 4 would be as follows: 1. 7163 4745 2. 7273 5756 3. 6172 7866 While the simplicity of this system has a certain attraction, en passant, pawn promotion, and castling movements would need special codes, which may not be readily intelligible in a four digit string. It should be noted that in approaching chess with linear algebra, movement of chess pieces by matrix manipulation cannot be accomplished in one operation. The simple opening move 1.e4 (algebraic notation), shown below, involves changing the location of the 1 in row 7 column 5 to row 5 column 5, while leaving all other values constant on the board, and returning a zero to row 7 column 5. Figure 16: Opening position Figure 17: White Pawn on e4 10 If we call the start position A and the pawn at e4 B, there is no matrix such that Ax=B. However, if we proceed with simple matrix addition and subtraction, this move can be accomplished, but the method is highly manual and cannot be integrated into an algorithm to actually solve chess. The mathematical technique used to do this will be sketched out briefly further on. 3 3.1 ”Solving” chess: matrices and the endgame Six piece Tablebases At the start of a chess game there are 32 total pieces on the board, 16 each for white and black. Chess games which continue to the endgame often arrive at a position where there are no pawns on the board and a total of six pieces (including kings). In the quest to ”solve” chess, masters as early as the 17th century would analyze these 6 piece endgame positions for months and months, attempting to determine best play for both sides, and any possible wins or draws. This of course had to be done by hand, but even so the results of these analyses were tabulated into something called tablebases. A tablebase is a database of all possible positions with certain set of pieces, which allows a determination of whether a position is won, lost, or drawn. Chess grandmasters study these table bases and can therefore play perfect moves (theoretically: in a real game move sequences are sometimes forgotten or transposed, turning a win into a draw or worse). These table bases can run into 200 or more moves, although in actual play such a game would be declared a draw due to the fiftymove rule (which declares a draw after 50 moves with no captures or pawn moves). There are thousands of different tablebases, accounting for all possible 6 piece combinations and their myriad starting positions. A example would be the tablebase for KRNKNN which is translated white has king, knight, and rook; while black has king, knight, and knight. The starting position for this example is given below, along with the move sequence in symbolic (the chess piece symbol, rather than the alpha designator, is given) notation: 11 Figure 18: Start position of tablebase below (white to move) 12 Figure 19: Sample tablebase As long as this move sequence appears to be, there are 61 additional moves not shown! Obviously, the 50 move rule would mercifully end this contest, but the point of the exercise is to show how the combination of matrix representation of chess positions and computer processing power can solve positions previously seen as unwinnable. The impact of the computer on table bases was to develop far more of them, and to disprove or prove previous results generated by hand. It produced a subtle yet fundamental shift in chess understanding, and made table bases accessible to millions more chess players. It also allowed the development of 7 and 8 piece table bases, along with piece and pawn configurations. 13 3.2 Use of matrices in Tablebases to solve the endgame As illustrated below, the math involved in solving even basic chess positions is quite complicated. It is interesting to note that although chess is played in 2 dimensional space (or R2), it is actually solved in 3 dimensional space (or R3). The basic concept is that matrices are manipulated in layers, so that a single chess position may have hundreds of submatrices associated with it, in 4 or more layers. The position below has only the white king and rook, so it is far simpler to conceive. Figure 20 The math associated with this position looks like this: Figure 21 The relevant component of this math, the application of which of illustrated above, is the use of tensor products, which in this case involves matrices which are composed of matrices, being multiplied by other matrices composed of matrices. When enough of these tensor products are combined (and multiplied by each other), the correct chess move for the position is produced. For more on 14 this application, see Multilinear Algebra and Chess Endgames by Lewis Stiller, the publication from which the above illustrations was taken. 3.3 Application: an example of matrix treatment of a three piece endgame Below is a typical endgame position, with white having a rook, and black having only the king. This position is a win for white, if time is not an issue. For simplicity, captures, stalemates, checks, and checkmate will be disregarded unless otherwise stated. This will be a very simple process. Figure 22: Sample endgame position Below are the movement matrices for all three pieces in this position, they are labeled according to the cartesian coordinate system, with the numeric piece constants shown earlier. Note, for example the black king on the f2 (chess notation) square has a movement matrice of 6217. Note also, that because the kings have restrictions on their movements due to their proximity, they have single digit -1 values on their movement matrices. This denotes a square under attack by another piece, which of course the king cannot move to. 15 Figure 23 Figure 24 Figure 25 16 White can move to any vector for either piece that does not have a negative value, however, the best choice is the square h2 (chess notation) with the rook (there would also be a checks matrix in a full blown matrix array for chess, which would have let us know about the existence a checking square), which in our case is vector 0,-1 for the 8305 matrice. Figure 26: Position after 1. Rh2+ (chess notation) White has delivered check, and effectively cut the black king off from all but the first rank of the board. Black has no choice but to retreat. Here are the movement matrices for the current position: Figure 27: New matrix 6217 17 Figure 28 Figure 29: Matrix 8206 Notice that both the white king and the black king still have restrictions on their movement (negative values represent squares currently under attack from the other side) due to the proximity of the opposite king, but the black king now has a -5 on both sides of him on the 2d rank, as well as on his own square. 18 This means he is currently in check, and cannot move to any square with a negative value. His available moves went from 5 in the previous position to 3. Depending on the philosophy of the player, black can either threaten the rook with a move to g1, or stay away. Figure 30: Position after 1...Ke1 We see in the above position that rather than indeed attacking the rook with a move to g1 (which is easily thwarted by white), the black king flees towards the middle of the board, hoping white will play imprecisely and let him off the back rank. The updated movement matrices for the new position appear as follows: 19 Figure 31 Figure 32 Figure 33: Matrix 5117 Notice that the white king no longer has any restrictions on his movement, while the black king now has only 2 moves available. Whites best strategy is to move forward towards black with his king (vector 1,0 on board 6417), to provide support for his rook and close in a mating net. A table base search would confirm this plan as best for white. 20 Figure 34: position after 2.Kf2 With this move, white has forced black to flee towards the opposite end of the board, as (2Kf1) leads to an immediate mate (3.Rh1). Here are the movement matrices for all three pieces: Figure 35: Matrix 6307 21 Figure 36 Figure 37: Matrix 5117 Advancing towards the black king did give white less freedom of movement, but also serves to hem black in. Black is at most 6 moves from mate. The one thing he cannot do is move towards the rook, as then he will have nowhere to retreat as any route away from the 1st rank would be cut off by the king directly in front of him. He has only 2 moves available, so the right one is an easy choice: Kd1 (vector -1,0 on board 5117). 22 Figure 38: Position after 2...Kf1 Black has committed chess suicide! He fails to move away from the threat, which means he either he has a previous engagement to get to, or he does not know any better. At any rate, he has just ended the game, as 3. Rh1 is checkmate. The movement matrices are below: Figure 39 23 Figure 40: Matrix 6307 Figure 41 -A quick check reveals black still has no mobility, the white king is still somewhat restricted, and the rook has any move available to him. However, 24 the obvious move is Rh1 which wins the game. Is this indicated on our matrix? Not on this one, but it would be on one of the 14 additional matrices for the white rook, each based on one of the possible moves available to him, and displaying the results of each. These sub matrices get closer to the heart of real chess, as they give values for the rook at each position, based on threats, attacks, captures, checks and checkmate, sacrifices, blunders, etc. Depending on the game situation, those sub matrices will also have sub matrices, and may interact with the many other matrices in the many other layers. In this example, we have greatly simplified the process, but only in the interest of grasping its complexity. White’s move here is 1,0 from matrice 8205. Figure 42: Final position after 3.Rh1 checkmate Game over. Black has nowhere to go, with the white King right in front of him, he cannot escape the wrath of the white rook. Now look at the final 3 ”movement” matrices: 25 Figure 43: Matrix 6307 Figure 44 26 Figure 45: New matrix 6117 Why look at another matrix after the game is over? Because it is identical to one of those matrices referred to just a second agoit is just like one of those fourteen sub matrices of every available move for the white rook. What value would it return that would have helped us to see the right move? A zero value for the square occupied by the black king! It is somewhat analogous to the solution Ax=0all game each player is searching for THIS matrice, the one with the value of the opponents king at 0. This of course means that the king is under attack, and cannot escapeits checkmate. In this case our solution to Ax=0 is matrice 8105, which is the white move that results in the black king matrice shown above. Again, each movement vector submatrice would have its own submatrices, based on outcomes of all of those possible moves. Imagine manipulating the sub-sub matrices for all 32 pieces at the start of the game! This hints at the enormity of the problem with finding a solution to chess. 4 Conclusion To fully understand how matrices are used to efficiently solve endgame positions, one must study a great deal more math at the graduate level. Even so, regardless of how much math has been applied to date, the elusive solution to an entire game of chess has yet to materialize. However, it is certain that if the solution is to be found, it will be rooted in linear combinations of vectors, and matrix manipulation. 27 5 Bibliography [1] Strang, Gilbert. Introduction To Linear Algebra. 3rd ed. Wellesley, MA: Wellesley-Cambridge Press, 2003. 363-378. Print. [2] Siller, Lewis Multilinear Algebra and Chess Endgames. http://users.rcn.com/lstiller/msri-paper.pdf [3] Vuckovic, Vladan and Vidanovic, Djordje A New Approach to Draw Detection by Move Repetition in Computer Chess Programming http://arxiv.org/ftp/cs/papers/0406/0406038.pdf 28