Where Can I Move?

Sameed Ahmed
Email: sahmed110@qc.cuny.edu
Math 213 -- Mathematics with Mathematica, Fall 2009 
December 3, 2009

Abstract

Have you ever played chess and sit there engaged in deep thought, wondering where can you move your piece without being eaten by the opponent's piece the following turn? That is the question that my projects seeks to answer. I use Mathematica to figure out where one's chess piece can move, given the type of piece and location of that piece and the opponent's. In order to do this, I use various commands in Mathematica, which include Select, Flatten, Table, Compliment, If, GraphicsGrid, and Manipulate. I learned many interesting applications of Mathematica in the process of creating this project, but what I gained knowledge most about is list manipulation. I also gained a lot of knowledge of how to integrate data into graphics.

Section 1. Introduction

1.1 Background

Chess is a two player game that involves different pieces which have distinct movements placed on a board. The object of the game is to remove the opponent's king from play. Pieces can be removed from play, or "eaten," when an opponent's piece lands on the same spot that they are on. Whenever I play chess, I always come to a point where I stare at the chessboard while contemplating where I can move my piece without being eaten. I picture my piece in a certain spot and check if it is in the line of motion of any of my opponent's pieces. Just as how computers can emulate other processes, thereby making them easier, I wondered if I can use Mathematica to emulate his process of deciding where to move a chess piece safely.

1.2 Problemt Statement

The problem statement that I answer is:

Given my and my opponent's chess piece and position, where can I move my piece without being eaten by the opponent's  piece the following turn?

1.3 The Run-Down

I start off by defining a function for each piece that outputs a list of coordinates where one' s piece can move given the input of the type of piece and location of that piece. I then check these possibilities by comparing them to where another piece on the board can move, eliminating the outputs that appear in both lists. The user's piece and position and the opponent's piece and position can be selected by the user.

1.4 Commands that Make this Possible

Different commands are essential to the proper functioning of this project. Table is used to generate a list of the possible movements. For instance, a rook can move one, two, three, etc. spaces left, right, up, or down, and in order to output each possible spot, Table can repeatedly enter values into the function. I need Flatten to eliminate nested lists so that they can be compared to each other. Select is used to only get outcomes that are within the chess board. Complement compares lists and throws out values that are in common, which is useful in getting rid of spots where the movement of two pieces intersects. To allow the user to select the pieces and their positions, Manipulate is used to make these aspects of the code variable. GraphicsGrid produces the picture which depicts the different scenarios. If is used multiple times to restrict the outcomes, such as locations in the output which are not on the board and if the user places two pieces in the same spot.

1.4 By the Way, What does a Chess Board look like?

In case you have never played chess or seen a chess board, here is what it looks like:

GraphicsGrid[Table[Graphics[{If[OddQ[i+j],Black,White],Rectangle[]}],{i,0,7},{j,0,7}],Frame->All]
(*Table iterates the graphics enough times to make this board. If decides each square's color based on if it is odd or even.*)

Each square is a spot where a piece can be placed. The coordinates (x,y) correspond to each square, starting with the bottom left one, which is (0,0) and ending with the top right one, which is (7,7)

Section 2. The Nuts and Bolts

2.1 Piece Movement Functions

Here all the functions that calculate the possible spots of are defined for each peace.

MyPawn[{x_,y_}]:=Select[Flatten[Table[{{x,y+i}},{i,1}],1],(0≤First[#]≤7&&0≤Last[#]≤7)&]
OpponentPawn[{x_,y_}]:=Select[Flatten[Table[{{x+i,y-i},{x-i,y-i}},{i,1}],1],(0≤First[#]≤7&&0≤Last[#]≤7)&]
(*Although it may seem unnecessary to create a table for "Opponent Pawn" that goes from i=1 to 1 when I could have simply put "y+1" instead, I need to use Table because it creates a different kind of list which allows for it to be compared when using Compliment. Also, unlike for other pieces, the pawn is defined for me and the opponent because it is the only piece for which there are different movements for each player. From my perspective, my pawn can only move up, and my opponent's pawn can only move down. Moreover, one may notice that the two pawns have different movements. This is as such because my project is only concerned with not being eaten. Since a pawn can only move forward one space and only eat diagonally one space, my pawn is concerned with moving without being eaten, and my opponent's pawn is concerned with eating another piece.*)

Rook[{x_,y_}]:=Select[Flatten[Table[{{x+i,y},{x-i,y},{x,y+i},{x,y-i}},{i,1,7}],1],(0≤First[#]≤7&&0≤Last[#]≤7)&]

Knight[{x_,y_}]:=Select[{{x+1,y+2},{x-1,y+2},{x+1,y-2},{x-1,y-2},{x+2,y+1},{x-2,y+1},{x+2,y-1},{x-2,y-1}},(0≤First[#]≤7&&0≤Last[#]≤7)&]

Bishop[{x_,y_}]:=Select[Flatten[Table[{{x+i,y+i},{x-i,y+i},{x+i,y-i},{x-i,y-i}},{i,1,7}],1],(0≤First[#]≤7&&0≤Last[#]≤7)&]

Queen[{x_,y_}]:=Select[Flatten[{Rook[{x,y}],Bishop[{x,y}]},1],(0≤First[#]≤7&&0≤Last[#]≤7)&]
(*A queen's movement is the combination of the movement of a rook and a bishop.*)

ExtraKing[{x_,y_}]:=Flatten[Table[{{x+i,y},{x-i,y},{x+i,y+i},{x-i,y+i},{x,y-i}},{i,1}],1]
King[{x_,y_}]:=Select[Flatten[{MyPawn[{x,y}],OpponentPawn[{x,y}],ExtraKing[{x,y}]},1],(0≤First[#]≤7&&0≤Last[#]≤7)&]
(*A king's movement is the combination of the movemnt of the pawns that I defined and some extra spots covered by "ExtraKing."*)

2.2 Finding the Intersection of Points

Complement finds elements in the the first list which appear in the second list and removes them. Here's an example of finding where a bishop at (4,4) can move when there is a queen at (4,6):

a=Bishop[{4,4}]
b=Queen[{4,6}]

Complement[a,b]

2.3 Determining Where One Can Move

Here is the code combining Complement with the above functions and placing them within Manipulate, allowing the user to adjust the pieces and their positions. If was added to ensure that the user does not choose the same spot for both pieces, which, if occurs, results in an error message.

Manipulate[If[(x1==x2&&y1==y2),"Two pieces cannot simultaneously occupy the same spot!",Complement[MyPiece[{x1,y1}],OpponentPiece[{x2,y2}]]],{MyPiece,{MyPawn,Rook,Knight,Bishop,Queen,King}},{{x1,0,"My Position X"},0,7,1},{{y1,0,"My Position Y"},0,7,1},{OpponentPiece,{OpponentPawn,Rook,Knight,Bishop,Queen,King}},{{x2,0,"Opponent Position X"},0,7,1},{{y2, 0,"Opponenet Position Y"},0,7,1}]

2.4 What it Looks Like

Here is the final result that finds the coordinates of where one can move combined with the chess board above to give a visual of the data. This includes two nested GraphicsGrid commands. One contains the chess board and the key. The other contains the three lines in the key. More If' commands were added. One places green circles on the board where the player can possibly move. One places a red circle on the if it the opponent's piece, and another places a blue one if is the player's piece.

Manipulate[
GraphicsGrid[
{{If[(x1==x2&&y1==y2),Style["Two pieces cannot simultaneously occupy the same spot!",FontSize->24],GraphicsGrid[
Table[
Graphics[
{If[OddQ[i+j],Black,White],Rectangle[],
If[MemberQ[Complement[MyPiece[{x1,y1}],OpponentPiece[{x2,y2}]],{j,i}],{Green,Disk[{0.5,0.5},.4]},{}],
If[{x1,y1}=={j,i},{Blue,Disk[{0.5,0.5},.4]},{}],
If[{x2,y2}=={j,i},{Red,Disk[{0.5,0.5},.4]},{}]},ImageSize->50],
{i,0,7},{j,0,7}],
Frame->All]]},
{GraphicsGrid[
{{Graphics[{Blue,Disk[{0.2,0.2},.2]},ImageSize->{100,50}], Style["My piece.",FontSize->16]},{Graphics[{Green,Disk[{0.2,0.2},.2]},ImageSize->50], Style["Where I can move.",FontSize->16]},{Graphics[{Red,Disk[{0.2,0.2},.2]},ImageSize->50], Style["The opponent's piece.",FontSize->16]}}]}}],
{MyPiece,{MyPawn,Rook,Knight,Bishop,Queen,King}},{{x1,0,"My Position X"},0,7,1},{{y1,0,"My Position Y"},0,7,1},{OpponentPiece,{OpponentPawn,Rook,Knight,Bishop,Queen,King}},{{x2,0,"Opponent Position X"},0,7,1},{{y2, 0,"Opponenet Position Y"},0,7,1}]
(*Spaces were added to make this congested code readable. One may notice that Table, which iterates this boxes on the board, is in the form {i,j}. However, the If statement is in the form {j,i}. This as such because Table makes the first row i, and each column j, but the output list is in the form {x,y}, where x is the horizontal axis, and y is the vertical axis. Thus the i and j had to be switched. In this board, the top left box is {0,0} and the bottom right is {7,7}. To make the picture neat, font and image sizes were edited.*)

Section 3. Conclusion and Future Work

3.1 Summary

Mathematica can simplify and perform actions which we do in our regular lives. In this case we can see how a chess piece's movement is limited due to the presence of another piece and determine where to move. This program also has great ways of displaying data visually.

3.2 Discussion

The hardest part of this project was defining the functions. Each function had to output a nested list of level one in order for Compliment to be able to compare different lists. Also, each function had to have different results within the function. A rook function, for example, needed to output where is can move left, right, up, and down, meaning the function had four separate operations to do with the input. I tried using the "or" symbol, but the resulting list was not compatible with Compliment. I learned new functions Flatten, which came in handy in reducing nested lists, and Compliment, which came in handy for removing elements common in different lists. Additionally, what was difficult was to portray the information in a chess board like picture. I tried to do so by labeling the points in a ShowGraph plot. The problem with that was that the output data is a list of coordinates, but ShowGraph refers to each point not with coordinates but as rather with numbers. So the bottom left point is 1, above that is 2, and so on. To deal with this I tired creating a "CoordinateConverter" function which would take in coordinates and output the number of the point that it corresponds to in ShowGraph. Unsuccessful, I attempted to use GraphicsGrid to show the data. The hardship I faced here was the correct placement of the coordinates in the analogous boxes. Some work was also needed to make this picture visually pleasing while accommodating a key.

3.3 Future Work

Had I more time, I would figure out how to create a code which would allow the user to decided how many of the opponent's pieces there are and which type is each piece. I would also try to have an actual chess piece placed on the board as opposed to circles.