chess_maple With Maple
by David Joyner, Prof Math Dept, USNA, Annapolis, MD USA; wdj@usna.edu, http://tinah.mslab.usna.edu/
© 2000 David Joyner
NOTE: This worksheet demonstrates the use of Maple for developing a fun chess_maple game package. It illustrates how Maple can handle and present data in a meaningful way, such as the moves on a chess_maple board.
Introduction: The chess_maple Package and its Capabilities
The package chess_maple (version 0.6) will
1. display graphical chess_maple diagrams associated to a given position (entered as a suitable 8x8 array, as explained below),
2. draw the chess_maple diagram associated to a move (entered as a list, as described below) from a given position,
3. draw the chess_maple diagram associated to a sequence of moves (entered as a list of lists, as described below) from a given position,
4. animate the chess_maple 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_maple problem (and its solution) from a limited, but growing, list of pre-entered positions,
10. translate a (slightly modified - see the example below) chess_maplemaster game which was exported from chess_mapleMaster 4000 or above to long algebraic notation into maple/chess_maple notation, which can then be displayed or animated.
Maple Worksheet Examples Using the chess_maple Package
First, load the plots package and read in the chess_maple.mpl file.
>
restart;
with(plots):
> read(`c:/Program Files/Maple 6/mpls/chess_maple_v5.mpl`); << Indicate your directory here.
>
The plot structure for each piece was created using polygonplot. They have already been typed into chess_maple.mpl. When these plot structures are put into an 8x8 array, you get a chess_maple position. The list of plot structures associated to the starting position in chess_maple.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_maple.mpl. The notation chess_maple.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,
(i) 1. e4 (in algebraic) is 1. PK4 (in descriptive) is (1. "pawn to king 4") is ([Pe2,Pe4] in chess_maple.mpl,
(ii) 1. ... e5 (in algebraic) is 1. ... PK4 (in descriptive) is (1. ... "pawn to king 4") is ([pe7,pe5] in chess_maple.mpl,
(iii) O-O (castling kingside) is ["O-O"] for white (here O is a capital letter O not a zero, also the quotes are important since otherwise MAPLE will automatically think - is the minus sign and cancel them) , and is ["o-o"] for black ,
(iv) O-O-O (castling queenside) is ["O-O-O"] for white (here O is a capital letter O not a zero) , and is ["o-o-o"] for black.
Note the lower case letters for the black pieces and the upper case letters for the white pieces. By the way, the chess_maple.mpl command move_game understands castling notation but not the move_piece command.
In PGN notation, an example of a chess_maple 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_maple.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_maple.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);
Animating the Game
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_maple can convert from "matrix notation" to algebraic notation:
> array_to_algebraic_notation(K,5,8);
chess_maple 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_maple problem? Try
> chess_maple_problems(6);
What a solution? Try
> solutions_to_chess_maple_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_maple can "almost" take any game exported from chess_mapleMaster 4000 or better in long algebraic notation and translate it into the maple/chess_maple notation (which can then be displayed). chess_maplemaster will create text files in the format
(stuff deleted)
28. Re1-e2 ... ;In an unclear position the e-file proves
;to be White's strenghth.
... Ke8-d7
(more stuff deleted)
each line like this must be edited to look like
(stuff deleted)
28. Re1-e2 Ke8-d7 ;In an unclear position the e-file proves
;to be White's strength.
(more stuff deleted)
(Correcting chess_mapleMaster's spelling is optional:-). Otherwise chess_maple.mpl won't know which move is black's. Also, it is important to keep the moves aligned in the rows as chess_mapleMaster has them.
In more detail: the chess_mapleMaster output file (call it Game_Larsen-Spassky_1970.txt)
must be edited to the new file (call it Game_Larsen-Spassky_1970b.txt)
Now, read in the file, translate it, and display it as follows (proper txt file is required here):
> gamefile:=read_in_file_to_arrays("/home/wdj/chess_maple/games/Game_Larsen-Spassky_1970b.txt"):
";Title: Larsen vs Spassky, USSR-Rest of the World Match, 1970\r"
> game:=translate_chess_maplemaster_game(gamefile);
>
board_in:=move_game(board_start,game);
display(convert_board(board_in),axes=none);