This page describes a little package I wrote which lets you display chess games and diagrams in maple and play a simple game. This was partially 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 well, though it does know the moves. An example is given in the html maple worksheet.
If you want to print chess diagrams in linux, you have several choices:
There are other (much better) chess programs for linux and/or unix, crafty for example, but they do not print diagrams.
The package chess (version 1.0) will
This page explains using examples how to use this package. This package was written for maple6 (the latest release at the time) but most seems to work for maple5. (The fen_parser seems to be the only one with a problem.) I have not tested it with maple7 or maple8.
>
restart;
with(plots):
Warning, the name changecoords has been redefined
> read(`/home/wdj/maplestuff/chess.mpl`);
>
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);
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);
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);
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);
chess can convert from "matrix notation" to algebraic notation:
> array_to_algebraic_notation(K,5,8);
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");
Here are the white knight moves:
> legal_white_knight_moves(board_test4);
Here are all legal moves which black can make in the position board_test3:
> legal_moves(board_test3,"black","display");
Here is the "king in check?" test in action:
>
display(convert_board(board_test2),axes=none);
evalm(board_test2);
whiteking_incheck(board_test2);
Want a chess problem? Try
> chess_problems(6);
What a solution? Try
> solutions_to_chess_problems(6);
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);
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);
>
board_in:=move_game(board_start,game);
display(convert_board(board_in),axes=none);
>