Chess and MAPLE

For the most recent version, see the FAQ. This page describes version 0.6.

This page describes a little package I wrote which lets you display chess games and diagrams in maple. This was motivated by a desire to have a program which prints diagrams in linux (and also as an excuse to do some fun programming:). It does not play chess, though I hope it one day to at least be able to play some endgames.

If you want to print chess diagrams in linux, you have several choices:

The "emulator" solution didn't work for me because I couldn't get printing to configure correctly. (By the way, I could not get chessmaster 4000 or 5500 to work under wine, though chessbaselight worked fine.) The "java" solution, though pretty, required internet access which I don't always have. The "latex" solution wasn't easy and flexible enough for what I had in mind. This motivated chess.mpl.

There are other (much better) chess programs for linux and/or unix, crafty for example, but they do not print diagrams. (By the way, some good links for chess files for linux/unix are at here.)

The package chess (version 0.6) will

  1. display graphical chess diagrams associated to a given position (entered as a suitable 8x8 array, as explained below),
  2. draw the chess diagram associated to a move (entered as a list, as described below) from a given position,
  3. draw the chess diagram associated to a sequence of moves (entered as a list of lists, as described below) from a given position,
  4. animate the chess diagrams associated to a game (entered as a list of lists, as described below),
  5. convert from FEN (positional notation) to 8x8 array notation (which can then be displayed),
  6. has a "king in check?" test,
  7. move a piece (the position as an 8x8 array and a move as input and the new position as an 8x8 array as output),
  8. list all legal moves of the position,
  9. display a chess problem (and its solution) from a limited, but growing, list of pre-entered positions,
  10. display a "classic" chess game from a limited, but growing, list of pre-entered games,
  11. translate a (slightly modified - see the example below) chessmaster game which was exported from ChessMaster 4000 or above to long algebraic notation into maple/chess notation, which can then be displayed or animated.

This page explains using examples how to use this package. This package was written for maple6 (the latest release) but most seems to work for maple5. (The fen_parser seems to be the only one with a problem.)


First, download the (ascii text) file chess.mpl, start maple, load the plots package and read in the chess.mpl file. The maple command below assumes that the directory containing chess.mpl is "/home/wdj/maplestuff". You will of course have to modify that command. In windows, for example, it might be something like "c:/maplestuff" (Since Maple might have trouble with spaces, so I try to stay away from things like "c:/Program Files/...".)

> restart;
with(plots):

Warning, the name changecoords has been redefined

> read(`/home/wdj/maplestuff/chess.mpl`);

`chess.mpl version 0.6 loaded. commands:`

`move_piece, move_game, convert_board,game_animatio...

`convert_from_array_notation, legal_white_pawn_move...

`legal_white_bishop_moves, legal_black_bishop_moves...

`whiteking_incheck, blackking_incheck, array_to_alg...

`translate_chessmaster_game, read_in_file_to_arrays...

`chess_problems, solutions_to_chess_problems, fen_p...

` `

`written for maple6 (the only command which does no...

`written 3-2000 by d joyner,wdj@usna.edu`

>

The plot structure for each piece was created using polygonplot. They have already been typed into chess.mpl. When these plot structures are put into an 8x8 array, you get a chess position. The list of plot structures associated to the starting position in chess.mpl is board0 . It can be displayed by typing the following command.

> display(array(board0),scaling=constrained,axes=none);

[Maple Plot]

game_immortal is a sample game which has already been entered into chess.mpl. The notation chess.mpl uses is a modification of the long algebraic notation: you must indicate the piece, its color, the starting square and the final square. For example,

Note the lower case letters for the black pieces and the upper case letters for the white pieces. By the way, the chess.mpl command move_game understands castling notation but not the move_piece command.

In PGN notation, an example of a chess game (Dutch Defense) is:
1. d4 f5 2. g3 Nf6 3. Bg2 g6 4. Nf3 Bg7 5. c4 O-O 6. O-O d6 7. Nc3 Qe8 .... .

In chess.mpl notation, this game would be written:
[[Pd2,Pd4],[pf7,pf5],[Pg2,Pg3],[ng8,nf6],[Bf1,Bg2],[pg7,pg6],[Ng8, Nf3], [bf8,bg7],[Pc2,Pc4],[ke8,kg8],[rh8,rf8],[Ke1,Kg1],[Rh1,Rf1],[pd7,pd6], [Nb1, Nc3],[qd8,qe8],...]:
Equivalent to this is:
[[Pd2,Pd4],[pf7,pf5],[Pg2,Pg3],[ng8,nf6], [Bf1,Bg2],[pg7,pg6],[Ng8, Nf3],[bf8,bg7],[Pc2,Pc4],["o-o"],["O-O"], [pd7,pd6],[Nb1, Nc3],[qd8,qe8],...]:
Notice move numbers are ignored.

To have MAPLE display the position after the last move of g ame4 , type the following commands. The first command returns the position in array notation and the second command displays the position using the graphical structures in chess.mpl .

> board_in:=move_game(board_start,game_immortal);
display(convert_board(board_in),axes=none);

board_in := matrix([[r, 0, b, k, 0, 0, 0, r], [p, 0...

[Maple Plot]

The 3rd argument print in move_game will print out the score in a slightly more readable form:

> board_in:=move_game(board_start,game_immortal,print);
display(convert_board(board_in),axes=none);

1, `  `, Pe2, `--`, Pe4, `         `, pe7, `--`, pe...

2, `  `, Pf2, `--`, Pf4, `         `, pe5, `--`, pf...

3, `  `, Bf1, `--`, Bc4, `         `, pb7, `--`, pb...

4, `  `, Bc4, `--`, Bb5, `         `, qd8, `--`, qh...

5, `  `, Ke1, `--`, Kf1, `         `, ng8, `--`, nf...

6, `  `, Ng1, `--`, Nf3, `         `, qh4, `--`, qh...

7, `  `, Pd2, `--`, Pd3, `         `, nf6, `--`, nh...

8, `  `, Nf3, `--`, Nh4, `         `, pc7, `--`, pc...

9, `  `, Nh4, `--`, Nf5, `         `, qh6, `--`, qg...

10, `  `, Pg2, `--`, Pg4, `         `, nh5, `--`, n...

11, `  `, Rh1, `--`, Rg1, `         `, pc6, `--`, p...

12, `  `, Ph2, `--`, Ph4, `         `, qg5, `--`, q...

13, `  `, Ph4, `--`, Ph5, `         `, qg6, `--`, q...

14, `  `, Qd1, `--`, Qf3, `         `, nf6, `--`, n...

15, `  `, Bc1, `--`, Bf4, `         `, qg5, `--`, q...

16, `  `, Nb1, `--`, Nc3, `         `, bf8, `--`, b...

17, `  `, Nc3, `--`, Nd5, `         `, qf6, `--`, q...

18, `  `, Bf4, `--`, Bd6, `         `, bc5, `--`, b...

19, `  `, Pe4, `--`, Pe5, `         `, qb2, `--`, q...

20, `  `, Kf1, `--`, Ke2, `         `, nb8, `--`, n...

21, `  `, Nf5, `--`, Ng7, `         `, ke8, `--`, k...

22, `  `, Qf3, `--`, Qf6, `         `, ng8, `--`, n...

23, `  `, Bd6, `--`, Be7

board_in := matrix([[r, 0, b, k, 0, 0, 0, r], [p, 0...

[Maple Plot]

To see an animation of the game, use the following game_animation command. The program will ask you to kindly be patient while the files load and then when a diagram eventually displays on the screen, you click once with the mouse on the diagram and then several times on the << symbol on the new toolbar to slow it down. The play button is the triangle > .

> game_animation(game_immortal);

`loading positions; depending on the length of the ...

[Maple Plot]

chess can convert from "matrix notation" to algebraic notation:

> array_to_algebraic_notation(K,5,8);

Ke1

chess can also list all legal piece moves using the command legal_color_piece_moves(position); For example, the black king moves in the position board_test4 are as follows. We use the "display" option to print the board as well:

> legal_black_king_moves(board_test4,"display");

matrix([[r, n, b, q, k, 0, n, r], [0, P, p, p, 0, 0...

[[[k, 5, 1], [k, 6, 1]], [[k, 5, 1], [k, 6, 2]], [[...

Here are the white knight moves:

> legal_white_knight_moves(board_test4);

[[[N, 2, 8], [N, 3, 6]], [[N, 2, 8], [N, 1, 6,

Here are all legal moves which black can make in the position board_test3:

> legal_moves(board_test3,"black","display");

matrix([[r, n, b, q, k, b, n, r], [p, P, p, p, p, 0...

[[[[p, 1, 2], [p, 1, 3]], [[p, 3, 2], [p, 3, 4,
[[[[p, 1, 2], [p, 1, 3]], [[p, 3, 2], [p, 3, 4,
[[[[p, 1, 2], [p, 1, 3]], [[p, 3, 2], [p, 3, 4,

Here is the "king in check?" test in action:

> display(convert_board(board_test2),axes=none);
evalm(board_test2);
whiteking_incheck(board_test2);

[Maple Plot]

matrix([[r, n, b, q, k, b, n, r], [p, P, p, p, p, p...

false

Want a chess problem? Try

> chess_problems(6);

`White to play and draw, by Richard Reti, 1921`

[Maple Plot]

What a solution? Try

> solutions_to_chess_problems(6);

`1. Kg7, Kf6, ...`

Here is the (modified) FEN-to-array converter:

> FEN_Lewitzky_vs_Marshall_Breslau1912_finalposition;
whattype(FEN_Lewitzky_vs_Marshall_Breslau1912_finalposition);
board_pos:=fen_parser(FEN_Lewitzky_vs_Marshall_Breslau1912_finalposition);
display(convert_board(board_pos),axes=none);

string

board_pos := matrix([[0, 0, 0, 0, 0, r, k, 0], [p, ...

[Maple Plot]

chess can "almost" take any game exported from ChessMaster 4000 or better in long algebraic notation and translate it into the maple/chess notation (which can then be displayed). Chessmaster will create text files in the format:

;Title: Lasker vs Steinitz, 2nd Match Game, Moscow, 1896
;White: Lasker
;Black: Steinitz
;Place: 2nd Match Game, Moscow
;Date: 1896
;Lasker-Steinitz, 2nd Match Game, Moscow 1896 A preview of Russian hegemony
;in chess a half-century later: Moscow is the venue for Lasker's easy
;"return match" victory.  The mating combination in this game is
;spectacular, as if Lasker felt that winning was not enough.

  1. e2-e4          e7-e5         
  2. Ng1-f3         Nb8-c6        
  3. Bf1-b5         Bf8-c5        
  4. c2-c3          Ng8-e7        
  5. O-O            Ne7-g6        
  6. d2-d4          e5xd4         
  7. c3xd4          Bc5-b6        
  8. Nb1-c3         O-O           
  9. a2-a4          a7-a6         
 10. Bb5-c4         h7-h6         
 11. h2-h3          d7-d6         
 12. Bc1-e3         Nc6-e7        
 13. Rf1-e1         c7-c6         
 14. Qd1-b3         Bb6-c7        
 15. Nf3-d2         Ra8-b8        
 16. Ra1-c1         b7-b5         
 17. a4xb5          a6xb5         
 18. Bc4-d3         Kg8-h8        
 19. Nc3-e2         f7-f5         
 20. e4xf5          Bc8xf5        
 21. Bd3xf5         Rf8xf5        
 22. Ne2-g3         Rf5-f8        
 23. Qb3-e6         Qd8-c8        
 24. Qe6xc8         Rf8xc8        
 25. Nd2-b3         Kh8-g8        
 26. Ng3-e4         Kg8-f7        
 27. g2-g3          Kf7-e8        
 28. Re1-e2         ...           ;In an unclear position the e-file proves
                                   ;to be White's strenghth.
     ...            Ke8-d7        
 29. Rc1-e1         Bc7-b6        
 30. Be3-f4         Bb6-c7        
 31. h3-h4          h6-h5         ;If 31... Nxf4 32. Nf6+! followed by 33.
                                   ;Rxe7 (+).
 32. Bf4-g5         Bc7-d8        
 33. g3-g4          h5xg4         
 34. h4-h5          Ng6-f8        
 35. Ne4-c5+        d6xc5         
 36. Nb3xc5+        Kd7-d6        ;36...Kc7 avoids mate but is a hopeless
                                   ;endgame after 37. Bxe7.
 37. Bg5-f4+        Kd6-d5        
 38. Re2-e5+        Kd5-c4        
 39. Re1-c1+        Kc4xd4        
 40. Nc5-b3+        Kd4-d3        
 41. Re5-e3++       ...           ;One of the more beautiful mating
                                   ;positions in chess history.
     ...            

Each file like this must be edited to look like:

;Title: Lasker vs Steinitz, 2nd Match Game, Moscow, 1896
;White: Lasker
;Black: Steinitz
;Place: 2nd Match Game, Moscow
;Date: 1896
;Lasker-Steinitz, 2nd Match Game, Moscow 1896 A preview of Russian hegemony
;in chess a half-century later: Moscow is the venue for Lasker's easy
;"return match" victory.  The mating combination in this game is
;spectacular, as if Lasker felt that winning was not enough.

  1. e2-e4          e7-e5         
  2. Ng1-f3         Nb8-c6        
  3. Bf1-b5         Bf8-c5        
  4. c2-c3          Ng8-e7        
  5. O-O            Ne7-g6        
  6. d2-d4          e5xd4         
  7. c3xd4          Bc5-b6        
  8. Nb1-c3         O-O           
  9. a2-a4          a7-a6         
 10. Bb5-c4         h7-h6         
 11. h2-h3          d7-d6         
 12. Bc1-e3         Nc6-e7        
 13. Rf1-e1         c7-c6         
 14. Qd1-b3         Bb6-c7        
 15. Nf3-d2         Ra8-b8        
 16. Ra1-c1         b7-b5         
 17. a4xb5          a6xb5         
 18. Bc4-d3         Kg8-h8        
 19. Nc3-e2         f7-f5         
 20. e4xf5          Bc8xf5        
 21. Bd3xf5         Rf8xf5        
 22. Ne2-g3         Rf5-f8        
 23. Qb3-e6         Qd8-c8        
 24. Qe6xc8         Rf8xc8        
 25. Nd2-b3         Kh8-g8        
 26. Ng3-e4         Kg8-f7        
 27. g2-g3          Kf7-e8        
 28. Re1-e2         Ke8-d7         ;In an unclear position the e-file proves
                                   ;to be White's strenghth.
 29. Rc1-e1         Bc7-b6        
 30. Be3-f4         Bb6-c7        
 31. h3-h4          h6-h5         ;If 31... Nxf4 32. Nf6+! followed by 33.Rxe7 (+).
 32. Bf4-g5         Bc7-d8        
 33. g3-g4          h5xg4         
 34. h4-h5          Ng6-f8        
 35. Ne4-c5+        d6xc5         
 36. Nb3xc5+        Kd7-d6        ;36...Kc7 avoids mate but is a hopeless
                                   ;endgame after 37. Bxe7.
 37. Bg5-f4+        Kd6-d5        
 38. Re2-e5+        Kd5-c4        
 39. Re1-c1+        Kc4xd4        
 40. Nc5-b3+        Kd4-d3        
 41. Re5-e3++       ...             ;One of the more beautiful mating
                                   ;positions in chess history.
 

(Correcting ChessMaster's spelling is optional:-). Otherwise chess.mpl won't know which move is black's. Also, it is important to keep the moves aligned in the rows as ChessMaster has them.

Now, read in the file (I'll use the Larsen-Spassky game instead of the Lasker-Steinitz game in the example below), translate it, and display it as follows:

> gamefile:=read_in_file_to_arrays("/home/wdj/chess/games/Game_Larsen-Spassky_1970b.txt"):

";Title: Larsen vs Spassky, USSR-Rest of the World Match, 1970\r"

> game:=translate_chessmaster_game(gamefile);

game := [[Pb2, Pb3], [pe7, pe5], [Bc1, Bb2], [nb8, ...
game := [[Pb2, Pb3], [pe7, pe5], [Bc1, Bb2], [nb8, ...
game := [[Pb2, Pb3], [pe7, pe5], [Bc1, Bb2], [nb8, ...
game := [[Pb2, Pb3], [pe7, pe5], [Bc1, Bb2], [nb8, ...

> board_in:=move_game(board_start,game);
display(convert_board(board_in),axes=none);

board_in := matrix([[0, 0, k, r, 0, 0, 0, 0], [p, p...

[Maple Plot]

>


Created 3-23-2000 by D. Joyner.
Last updated 4-6-2000.