The Purpose of this program:
To utilize the event capturing properties
of JButton objects for creating a Chess program. The Sun ONE 4
Studio has sample programs you can download, including a Chess
program. However, Sun's program and others like it (i.e., written
in Java) are written like old fashioned programs; i.e., the code
is similar to what you'd find in C or compiled BASIC. More to
the point, these programs capture the x and y co-ordinates of
mouse events and calculate what rank and file they are at from
these co-ordinates. After determining rank and file they use
arrays containing board (and/or game) information to find out
what piece is moved to what square, from what square, etc. This
program uses the information in the squares (JButton objects)
themselves to determin what is happening and uses the event
trapping in the same squares to move the pieces, update the board, etc.
The following code can be compiled with the JDK from Sun, using the
the following command line: javac -g:none JChess.java
The -g:none option isn't necessary but it should make a smaller footprint
and result in better program performance. After compiling you can run the
program using the command: java JChess
You will also need to download some Chess piece gifs or jpegs from the
Internet and rename them to the names used in this program or change the
names used here. There are lots of Chess pieces out there; e.g., look at
http://pages.prodigy.com/Art/Chess/
I just finished the legal move generator, minus pawn promotion and en passant captures (in
the works - the code for en-passant is there, I just have to debug it); my next (and last)
additions to the legal move generator. (It's also missing a color check, but this allows
multiple moves by one side - very helpful while developing the program). The legal move
generator follows my OOD in that legal moves are a property of each ChessSquare. When you
move a square, the makeMove method asks the square if it's legal to move.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
/**
*
* @author David Katelansky
*/
public class JChess extends JFrame
{
//January 12, 2013: Fixed horizontal move calculations for the legalmoves list
//January 16, 2013: Fixed it correctly; that last fix was a quick hack.
//January 19, 2013: Added color to move checking to the legal move generator
//flags for double moves
boolean WOO; // White Kingside castling
boolean WOOO; // White queenside castling
boolean WPP; // White Pawn Promotion
boolean BOO; // Black Kingside castling
boolean BOOO; // Black queenside castling
boolean BPP; // Black Pawn Promotion
//we need to know when the mouse is being pressed
boolean pressedFlag = false;
boolean grayem = false; //gray squares we pass over
//create container for our board and pieces
Container c = this.getContentPane();
//dark squares will be green, light squares will be yellow
Color light = Color.yellow;
Color dark = Color.green;
//flag to alternate colors of squares
boolean flipflop = true;
//flags to determine when we have a from and to square
boolean toFound = false, fromFound = false;
ChessSquare fromMT; //an emptry square that's stored in a linked list
ChessSquare fromSquare; //the square we are moving from
ChessSquare toSquare; //the square we are moving to
ChessSquare enteredSquare; //the square we are entering
Color enteredColor; //and it's color
Color colorToMove = dark;
//store piece location here
int Board[][] = new int[8][8];
//array to hold pieces
ChessSquare square[][] = new ChessSquare[8][8];
//variables to store rank and file
int fromRank, fromFile, toRank, toFile, enteredRank, enteredFile;
//Create a linked list to hold available dark empty squares
LinkedList darkList = new LinkedList();
//Create empty squares to cover up from squares
ChessSquare MTd[][] = new ChessSquare[8][8];
//Create a linked list to hold available light empty squares
LinkedList lightList = new LinkedList();
//Create empty squares to cover up from squares
ChessSquare MTl[][] = new ChessSquare[8][8];
public static void main(String[] args)
{
new JChess("TakeNote Chess Study Utility").setVisible(true);
}
//** Creates new form JChess */
public JChess(String heading) {
super(heading);
initComponents();
setMTl();
setMTd();
}
private ImageIcon shrinkIcon(String piece)
{
ImageIcon chessIcon = new ImageIcon(JChess.class.getResource(piece));
Image img = chessIcon.getImage();
Image newimg = img.getScaledInstance(80, 80, java.awt.Image.SCALE_SMOOTH);
ImageIcon newIcon = new ImageIcon(newimg);
return newIcon;
}
private void initComponents()
{
// set flafs that denote double moves
WOO = false;
WOOO = false;
WPP = false;
BOO = false;
BOOO = false;
BPP = false;
//fill up empty square array and linked list
for(int i = 0; i < 8; i++)
for(int j = 0; j < 8; j++)
{
MTd[i][j] = new ChessSquare();
MTd[i][j].setBackground(dark);
MTd[i][j].setVisible(true);
MTd[i][j].setName(Integer.toString(-10 * i - j - 1));
darkList.addLast(MTd[i][j]);
}
//fill up empty square array and linked list
for(int i = 0; i < 8; i++)
for(int j = 0; j < 8; j++)
{
MTl[i][j] = new ChessSquare();
MTl[i][j].setBackground(light);
MTl[i][j].setVisible(true);
MTl[i][j].setName(Integer.toString(10 * i + j + 78));
lightList.addLast(MTl[i][j]);
}
//color the squares and set up the initial board position
for(int i = 0; i < 8; i++)
for(int j = 0; j < 8; j++)
{
//this is a good place to populate the Board
Board[i][j] = 10 * i + j;
square[i][j] = new ChessSquare();
square[i][j].moved = false; //hasn't moved yet
//use the string (rank)(file) to name the squares
square[i][j].setName(Integer.toString(Board[i][j]));
if (flipflop)
{
square[i][j].setBackground(light);
}
else
{
square[i][j].setBackground(dark);
}
flipflop = !flipflop;
if (j == 7)
flipflop = !flipflop;
if ((i > 1) && (i < 6))
square[i][j].setOpaque(true);
}
//create 64 square grid to place squares on
getContentPane().setLayout(new GridLayout(8, 8));
//when we close the screen we exit the program
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
});
//put the pieces on the squares
square[0][0].setIcon(shrinkIcon("pics/br.png"));
square[0][1].setIcon(shrinkIcon("pics/bn.png"));
square[0][2].setIcon(shrinkIcon("pics/bb.png"));
square[0][3].setIcon(shrinkIcon("pics/bq.png"));
square[0][4].setIcon(shrinkIcon("pics/bk.png"));
square[0][5].setIcon(shrinkIcon("pics/bb.png"));
square[0][6].setIcon(shrinkIcon("pics/bn.png"));
square[0][7].setIcon(shrinkIcon("pics/br.png"));
square[1][0].setIcon(shrinkIcon("pics/bp.png"));
square[1][1].setIcon(shrinkIcon("pics/bp.png"));
square[1][2].setIcon(shrinkIcon("pics/bp.png"));
square[1][3].setIcon(shrinkIcon("pics/bp.png"));
square[1][4].setIcon(shrinkIcon("pics/bp.png"));
square[1][5].setIcon(shrinkIcon("pics/bp.png"));
square[1][6].setIcon(shrinkIcon("pics/bp.png"));
square[1][7].setIcon(shrinkIcon("pics/bp.png"));
square[6][0].setIcon(shrinkIcon("pics/wp.png"));
square[6][1].setIcon(shrinkIcon("pics/wp.png"));
square[6][2].setIcon(shrinkIcon("pics/wp.png"));
square[6][3].setIcon(shrinkIcon("pics/wp.png"));
square[6][4].setIcon(shrinkIcon("pics/wp.png"));
square[6][5].setIcon(shrinkIcon("pics/wp.png"));
square[6][6].setIcon(shrinkIcon("pics/wp.png"));
square[6][7].setIcon(shrinkIcon("pics/wp.png"));
square[7][0].setIcon(shrinkIcon("pics/wr.png"));
square[7][1].setIcon(shrinkIcon("pics/wn.png"));
square[7][2].setIcon(shrinkIcon("pics/wb.png"));
square[7][3].setIcon(shrinkIcon("pics/wq.png"));
square[7][4].setIcon(shrinkIcon("pics/wk.png"));
square[7][5].setIcon(shrinkIcon("pics/wb.png"));
square[7][6].setIcon(shrinkIcon("pics/wn.png"));
square[7][7].setIcon(shrinkIcon("pics/wr.png"));
//put all the squares on the board with initial piece configuration
//set mouse handler for each square
for(int i = 0; i < 8; i++)
for(int j = 0; j < 8; j++)
{
//add mouse listener
square[i][j].addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent evt)
{
if (!pressedFlag)
{
grayem = true;
fromSquare = (ChessSquare) evt.getSource();
//the rest of this block can't hurt anything
//but there is no reason to waste cpu time
String boardStr = fromSquare.getName();
for(int i = 0; i < 8; i++)
for(int j = 0; j < 8; j++)
{
if (Board[i][j] == Integer.parseInt(boardStr))
{
if (fromFound != true)
{
fromRank = i;
fromFile = j;
fromFound = true;
}
}
}
}
pressedFlag = true;
}
public void mouseReleased(MouseEvent e)
{
grayem = false;
if (toFound != true)
{
toSquare = enteredSquare;
toRank = enteredRank;
toFile = enteredFile;
toFound = true;
}
//we just made a move, so record it and change graphics
enteredSquare.setBackground(enteredColor);
makeMove();
//check for double moves; e.g., castling
if (WOO)
{
//kingside castling - need to move rook
int num = Board[7][5] - 78;
int rank = num/10;
int file = num - (10 * rank);
toSquare = MTl[rank][file];
toRank = 7;
toFile = 5;
fromSquare = square[7][7];
fromRank = 7;
fromFile = 7;
makeMove();
WOO = false; //allows makeMove() to run
}
if (WOOO)
{
//queenside castling - need to move rook
int num = Board[7][3] - 78;
int rank = num/10;
int file = num - (10 * rank);
toSquare = MTl[rank][file];
toRank = 7;
toFile = 3;
fromSquare = square[7][0];
fromRank = 7;
fromFile = 0;
makeMove();
WOOO = false; //allows makeMove() to run
}
if (BOO)
{
//kingside castling - need to move rook
int num = -Board[0][5] + 1;
int rank = num/10;
int file = num - (10 * rank);
toSquare = MTd[rank][file];
toRank = 0;
toFile = 5;
fromSquare = square[0][7];
fromRank = 0;
fromFile = 7;
makeMove();
BOO = false; //allows makeMove() to run
}
if (BOOO)
{
//queenside castling - need to move rook
int num = -Board[0][3] + 1;
int rank = num/10;
int file = num - (10 * rank);
toSquare = MTd[rank][file];
toRank = 0;
toFile = 3;
fromSquare = square[0][0];
fromRank = 0;
fromFile = 0;
makeMove();
BOOO = false; //allows makeMove() to run
}
}
public void mouseExited(MouseEvent e)
{
if (pressedFlag)
enteredSquare.setBackground(enteredColor);
}
public void mouseEntered(MouseEvent evt)
{
enteredSquare = (ChessSquare) evt.getComponent();
String boardStr = enteredSquare.getName();
for(int i = 0; i < 8; i++)
for(int j = 0; j < 8; j++)
{
if (Board[i][j] == Integer.parseInt(boardStr))
{
enteredRank = i;
enteredFile = j;
int squareRank = 0;
int squareFile = 0;
if (boardStr.length() > 1)
{
squareRank = Integer.parseInt(boardStr.substring(0,1));
squareFile = Integer.parseInt(boardStr.substring(1,2));
}
else
squareFile = Integer.parseInt(boardStr.substring(0,1));
enteredColor = square[squareRank][squareFile].getBackground();
if (pressedFlag)
{
if (grayem)
square[squareRank][squareFile].setBackground(Color.gray);
}
}
}
}
});
c.add(square[i][j]);
}
pack();
}
private void setMTl()
{
for(int i = 0; i < 8; i++)
for(int j = 0; j < 8; j++)
{
//add mouse listener
MTl[i][j].addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent evt)
{
if (!pressedFlag)
{
grayem = true;
fromSquare = (ChessSquare) evt.getSource();
String boardStr = fromSquare.getName();
for(int i = 0; i < 8; i++)
for(int j = 0; j < 8; j++)
{
if (Board[i][j] == Integer.parseInt(boardStr))
{
if (fromFound != true)
{
fromRank = i;
fromFile = j;
fromFound = true;
}
}
}
}
pressedFlag = true;
}
public void mouseReleased(MouseEvent e)
{
grayem = false;
if (toFound != true)
{
toSquare = enteredSquare;
toRank = enteredRank;
toFile = enteredFile;
toFound = true;
}
//we just made a move, so record it and change graphics
toFound = false;
fromFound = false;
pressedFlag = false;
}
public void mouseExited(MouseEvent e)
{
if (pressedFlag)
enteredSquare.setBackground(enteredColor);
}
public void mouseEntered(MouseEvent evt)
{
enteredSquare = (ChessSquare) evt.getComponent();
String boardStr = enteredSquare.getName();
for(int i = 0; i < 8; i++)
for(int j = 0; j < 8; j++)
{
if (Board[i][j] == Integer.parseInt(boardStr))
{
enteredRank = i;
enteredFile = j;
int squareNum = Integer.parseInt(boardStr) - 78;
int squareRank = squareNum/10;
int squareFile = squareNum - (10 * squareRank);
enteredColor = light;
if (pressedFlag)
{
if (grayem)
MTl[squareRank][squareFile].setBackground(Color.gray);
}
}
}
}
});
}
}
private void setMTd()
{
for(int i = 0; i < 8; i++)
for(int j = 0; j < 8; j++)
{
//add mouse listener
MTd[i][j].addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent evt)
{
if (!pressedFlag)
{
grayem = true;
fromSquare = (ChessSquare) evt.getSource();
String boardStr = fromSquare.getName();
for(int i = 0; i < 8; i++)
for(int j = 0; j < 8; j++)
{
if (Board[i][j] == Integer.parseInt(boardStr))
{
if (fromFound != true)
{
fromRank = i;
fromFile = j;
fromFound = true;
}
}
}
}
pressedFlag = true;
}
public void mouseReleased(MouseEvent e)
{
grayem = false;
if (toFound != true)
{
toSquare = enteredSquare;
toRank = enteredRank;
toFile = enteredFile;
toFound = true;
}
//we just made a move, so record it and change graphics
toFound = false;
fromFound = false;
pressedFlag = false;
}
public void mouseExited(MouseEvent e)
{
if (pressedFlag)
enteredSquare.setBackground(enteredColor);
}
public void mouseEntered(MouseEvent evt)
{
enteredSquare = (ChessSquare) evt.getComponent();
String boardStr = enteredSquare.getName();
for(int i = 0; i < 8; i++)
for(int j = 0; j < 8; j++)
{
if (Board[i][j] == Integer.parseInt(boardStr))
{
enteredRank = i;
enteredFile = j;
int squareNum = -Integer.parseInt(boardStr) - 1;
int squareRank = squareNum/10;
int squareFile = squareNum - (10 * squareRank);
enteredColor = dark;
if (pressedFlag)
{
if (grayem)
MTd[squareRank][squareFile].setBackground(Color.gray);
}
}
}
}
});
}
}
private void makeMove()
{
if (fromSquare.getLegal())
{
int num = Integer.parseInt(toSquare.getName());
int fromNum = Integer.parseInt(fromSquare.getName());
//don't move empty squares
if (((fromNum > 77) || (fromNum < 0) || ((fromNum > 17) && (fromNum < 60))))
{
toFound = false;
fromFound = false;
pressedFlag = false;
grayem = false;
return;
}
//The move is legal, so we should switch the side to move
if (colorToMove == light)
{
colorToMove = dark;
}
else
{
colorToMove = light;
}
//First, get a blank square to replace the fromSquare with
Color fromColor = fromSquare.getBackground();
Color toColor = toSquare.getBackground();
if (fromColor == light)
{
fromMT = (ChessSquare) lightList.removeFirst();
}
else
{
fromMT = (ChessSquare) darkList.removeFirst();
}
//Make the move on our board used for chess calculations
Board[fromRank][fromFile] = Integer.parseInt(fromMT.getName());
Board[toRank][toFile] = Integer.parseInt(fromSquare.getName());
//remove the fromSquare from the grid
c.remove(fromRank * 8 + fromFile);
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++)
{
String aStr = square[i][j].getName();
square[i][j].setText(Integer.toString(Board[i][j]));
}
//replace it with the MT square we got from the linked list
c.add(fromMT, fromRank * 8 + fromFile);
//remove the to square from the grid
c.remove(toRank * 8 + toFile);
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++)
{
String aStr = square[i][j].getName();
square[i][j].setText(Integer.toString(Board[i][j]));
}
//replace it with the from square
//first, set the color of the from square to the to square
fromSquare.setBackground(toColor);
c.add(fromSquare, toRank * 8 + toFile);
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++)
{
String aStr = Integer.toString(Board[i][j]);
square[i][j].setText("");
}
//update the screen image
c.repaint();
//put the square we removed from the grid on the end of the linked list
if (fromColor == light)
{
if (((num > 77) || (num < 0) || ((num > 17) && (num < 60))) && (toColor == light))
lightList.addLast(toSquare);
}
else
{
if (((num > 77) || (num < 0) || ((num > 17) && (num < 60))) && (toColor == dark))
darkList.addLast(toSquare);
}
fromSquare.moved = true;
} //the above block only performed if to square is legal
//set the flags that allow our mouse methods to execute
toFound = false;
fromFound = false;
pressedFlag = false;
}
class ChessSquare extends JButton
{
// flag for determining if piece moved yet
public boolean moved;
//linked list for holding legal moves
//each node holds the legal toSquares at the start of a call to makeMove()
public LinkedList legalMoves = new LinkedList();
public boolean returnVal = false;
public String material = "MT"; //To be reset by initComponents() and makeMove()
//method to populate legalMoves with legal moves
//returns false if the current toSquare isn't in the list
public boolean getLegal()
{
// check to see if this is a return from a double move
if (WOO || WOOO || WPP || BOO || BOOO || BPP)
{
if (colorToMove == light)
{
colorToMove = dark;
}
else
{
colorToMove = light;
}
return true;
}
//if not we need reload in the list of legal to squares
legalMoves.clear(); //clear out any moves in list
int orig = fromRank * 10 + fromFile;
int dest = toRank * 10 + toFile;
int num;
int toNum;
int rank;
int file;
int squareNum = Integer.parseInt(this.getName());
boolean notAQueen = true;
if ((squareNum < 18) && (colorToMove != light))
{
return false;
}
if ((squareNum > 59) && (colorToMove != dark))
{
return false;
}
switch (squareNum)
{
//queen
case 3:
case 73:
notAQueen = false;
//rook
case 0:
case 7:
case 70:
case 77: //calculate vertical moves
for (toNum = orig + 10, rank = toNum/10; rank < 8; toNum += 10, rank = toNum/10)
{
legalMoves.add(new Integer(toNum));
num = Board[toNum/10][fromFile];
if (((num > 77) || (num < 0) || ((num > 17) && (num < 60))))
num = 0;
else
break;
}
for (toNum = orig - 10, rank = toNum/10; rank > -1; toNum -= 10, rank = toNum/10)
{
legalMoves.add(new Integer(toNum));
num = Board[toNum/10][fromFile];
if (((num > 77) || (num < 0) || ((num > 17) && (num < 60))))
num = 0;
else
{
break;
}
}
//calculate horizontal moves
toNum = orig;
while (true)
{
toNum++;
rank = toNum/10;
file = toNum - rank * 10;
if ((file < 8) && (rank == fromRank))
{
legalMoves.add(new Integer(toNum));
}
else
break;
num = Board[rank][file];
if (((num > 77) || (num < 0) || ((num > 17) && (num < 60))))
{
num = 0;
}
else
break;
}
toNum = orig;
while (true)
{
toNum--;
rank = toNum/10;
file = toNum - rank * 10;
if ((file > -1) && (rank == fromRank))
{
legalMoves.add(new Integer(toNum));
}
else
break;
num = Board[rank][file];
if (((num > 77) || (num < 0) || ((num > 17) && (num < 60))))
{
num = 0;
}
else
break;
}
if (notAQueen)
{
break;
}
//bishop
case 2:
case 5:
case 72:
case 75:
//up the board
toNum = orig;
while (true)
{
toNum += 11;
rank = toNum/10;
file = toNum - (10 * rank);
try
{
num = Board[rank][file];
}
catch (Exception e) {break;}
if ((rank < 8) && (file < 8))
legalMoves.add(new Integer(toNum));
else
break;
if (((num > 77) || (num < 0) || ((num > 17) && (num < 60))))
num = 0;
else
break;
}
//down the board
toNum = orig;
while (true)
{
toNum -= 11;
rank = toNum/10;
file = toNum - (10 * rank);
try
{
num = Board[rank][file];
}
catch (Exception e) {break;}
if ((rank > -1) && (file > -1))
legalMoves.add(new Integer(toNum));
else
break;
if (((num > 77) || (num < 0) || ((num > 17) && (num < 60))))
num = 0;
else
break;
}
//up the board
toNum = orig;
while (true)
{
toNum += 9;
rank = toNum/10;
file = toNum - (10 * rank);
try
{
num = Board[rank][file];
}
catch (Exception e) {break;}
if ((rank < 8) && (file > -1))
legalMoves.add(new Integer(toNum));
else
break;
if (((num > 77) || (num < 0) || ((num > 17) && (num < 60))))
num = 0;
else
break;
}
//down the board
toNum = orig;
while (true)
{
toNum -= 9;
rank = toNum/10;
file = toNum - (10 * rank);
try
{
num = Board[rank][file];
}
catch (Exception e) {break;}
if ((rank > -1) && (file < 8))
legalMoves.add(new Integer(toNum));
else
break;
if (((num > 77) || (num < 0) || ((num > 17) && (num < 60))))
num = 0;
else
break;
}
notAQueen = true;
break;
//Knight
case 1:
case 6:
case 71:
case 76:
toNum = orig + 19;
rank = toNum/10;
file = toNum - (10 * rank);
if ((rank > -1) && (rank < 8) && (file > -1) && (file < 8))
legalMoves.add(new Integer(toNum));
toNum = orig + 21;
rank = toNum/10;
file = toNum - (10 * rank);
if ((rank > -1) && (rank < 8) && (file > -1) && (file < 8))
legalMoves.add(new Integer(toNum));
toNum = orig + 12;
rank = toNum/10;
file = toNum - (10 * rank);
if ((rank > -1) && (rank < 8) && (file > -1) && (file < 8))
legalMoves.add(new Integer(toNum));
toNum = orig - 8;
rank = toNum/10;
file = toNum - (10 * rank);
if ((rank > -1) && (rank < 8) && (file > -1) && (file < 8))
legalMoves.add(new Integer(toNum));
toNum = orig - 19;
rank = toNum/10;
file = toNum - (10 * rank);
if ((rank > -1) && (rank < 8) && (file > -1) && (file < 8))
legalMoves.add(new Integer(toNum));
toNum = orig - 21;
rank = toNum/10;
file = toNum - (10 * rank);
if ((rank > -1) && (rank < 8) && (file > -1) && (file < 8))
legalMoves.add(new Integer(toNum));
toNum = orig - 12;
rank = toNum/10;
file = toNum - (10 * rank);
if ((rank > -1) && (rank < 8) && (file > -1) && (file < 8))
legalMoves.add(new Integer(toNum));
toNum = orig + 8;
rank = toNum/10;
file = toNum - (10 * rank);
if ((rank > -1) && (rank < 8) && (file > -1) && (file < 8))
legalMoves.add(new Integer(toNum));
break;
//King
case 4:
case 74:
toNum = orig + 10;
rank = toNum/10;
file = toNum - (10 * rank);
if ((rank > -1) && (rank < 8) && (file > -1) && (file < 8))
legalMoves.add(new Integer(toNum));
toNum = orig + 11;
rank = toNum/10;
file = toNum - (10 * rank);
if ((rank > -1) && (rank < 8) && (file > -1) && (file < 8))
legalMoves.add(new Integer(toNum));
toNum = orig + 1;
rank = toNum/10;
file = toNum - (10 * rank);
if ((rank > -1) && (rank < 8) && (file > -1) && (file < 8))
legalMoves.add(new Integer(toNum));
toNum = orig - 9;
rank = toNum/10;
file = toNum - (10 * rank);
if ((rank > -1) && (rank < 8) && (file > -1) && (file < 8))
legalMoves.add(new Integer(toNum));
toNum = orig - 10;
rank = toNum/10;
file = toNum - (10 * rank);
if ((rank > -1) && (rank < 8) && (file > -1) && (file < 8))
legalMoves.add(new Integer(toNum));
toNum = orig - 11;
rank = toNum/10;
file = toNum - (10 * rank);
if ((rank > -1) && (rank < 8) && (file > -1) && (file < 8))
legalMoves.add(new Integer(toNum));
toNum = orig - 1;
rank = toNum/10;
file = toNum - (10 * rank);
if ((rank > -1) && (rank < 8) && (file > -1) && (file < 8))
legalMoves.add(new Integer(toNum));
toNum = orig + 9;
rank = toNum/10;
file = toNum - (10 * rank);
if ((rank > -1) && (rank < 8) && (file > -1) && (file < 8))
legalMoves.add(new Integer(toNum));
//castling
if (this.moved == false)
{
if ((this.getName().equals("74")) && (Board[7][7] == 77) && (square[7][7].moved == false) && (Board[7][5] > 77) && (Board[7][6] < 0))
{
if (toFile == 6)
{
legalMoves.add(new Integer(76));
WOO = true;
}
}
if ((this.getName().equals("74")) && (Board[7][0] == 70) && (square[7][0].moved == false) && (Board[7][3] > 77) && (Board[7][2] < 0))
{
if (toFile == 2)
{
legalMoves.add(new Integer(72));
WOOO = true;
}
}
if ((this.getName().equals("4")) && (Board[0][7] == 7) && (square[0][7].moved == false) && (Board[0][5] < 0) && (Board[0][6] > 77))
{
if (toFile == 6)
{
legalMoves.add(new Integer(6));
BOO = true;
}
}
if ((this.getName().equals("4")) && (Board[0][0] == 0) && (square[0][0].moved == false) && (Board[0][3] < 0) && (Board[0][2] > 77))
{
if (toFile == 2)
{
legalMoves.add(new Integer(2));
BOOO = true;
}
}
}
break;
//black pawns
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
case 16:
case 17:
toNum = orig + 10;
rank = toNum/10;
file = toNum - (10 * rank);
try
{
num = Board[rank][file];
}
catch (Exception e) {break;}
if (((num > 77) || (num < 0) || ((num > 17) && (num < 60))))
num = 0;
else
rank = 9;
if ((rank > -1) && (rank < 8) && (file > -1) && (file < 8))
legalMoves.add(new Integer(toNum));
if (!moved)
{
toNum = orig + 20;
rank = toNum/10;
file = toNum - (10 * rank);
try
{
num = Board[rank][file];
}
catch (Exception e) {break;}
if (((num > 77) || (num < 0) || ((num > 17) && (num < 60))))
num = 0;
else
rank = 9;
if ((rank > -1) && (rank < 8) && (file > -1) && (file < 8))
legalMoves.add(new Integer(toNum));
}
toNum = orig + 9;
rank = toNum/10;
file = toNum - (10 * rank);
try
{
num = Board[rank][file];
}
catch (Exception e) {break;}
if (((num > 77) || (num < 0) || ((num > 17) && (num < 60))))
rank = 9;
else
num = 0;
if ((rank > -1) && (rank < 8) && (file > -1) && (file < 8))
legalMoves.add(new Integer(toNum));
toNum = orig + 11;
rank = toNum/10;
file = toNum - (10 * rank);
try
{
num = Board[rank][file];
}
catch (Exception e) {break;}
if (((num > 77) || (num < 0) || ((num > 17) && (num < 60))))
rank = 9;
else
num = 0;
if ((rank > -1) && (rank < 8) && (file > -1) && (file < 8))
legalMoves.add(new Integer(toNum));
break;
//white pawns
case 60:
case 61:
case 62:
case 63:
case 64:
case 65:
case 66:
case 67:
toNum = orig - 10;
rank = toNum/10;
file = toNum - (10 * rank);
try
{
num = Board[rank][file];
}
catch (Exception e) {break;}
if (((num > 77) || (num < 0) || ((num > 17) && (num < 60))))
num = 0;
else
rank = 9;
if ((rank > -1) && (rank < 8) && (file > -1) && (file < 8))
legalMoves.add(new Integer(toNum));
if (!moved)
{
toNum = orig - 20;
rank = toNum/10;
file = toNum - (10 * rank);
try
{
num = Board[rank][file];
}
catch (Exception e) {break;}
if (((num > 77) || (num < 0) || ((num > 17) && (num < 60))))
num = 0;
else
rank = 9;
if ((rank > -1) && (rank < 8) && (file > -1) && (file < 8))
legalMoves.add(new Integer(toNum));
}
toNum = orig - 9;
rank = toNum/10;
file = toNum - (10 * rank);
try
{
num = Board[rank][file];
}
catch (Exception e) {break;}
if (((num > 77) || (num < 0) || ((num > 17) && (num < 60))))
rank = -1;
else
num = 0;
if ((rank > -1) && (rank < 8) && (file > -1) && (file < 8))
legalMoves.add(new Integer(toNum));
toNum = orig - 11;
rank = toNum/10;
file = toNum - (10 * rank);
try
{
num = Board[rank][file];
}
catch (Exception e) {break;}
if (((num > 77) || (num < 0) || ((num > 17) && (num < 60))))
rank = -1;
else
num = 0;
if ((rank > -1) && (rank < 8) && (file > -1) && (file < 8))
legalMoves.add(new Integer(toNum));
break;
default: break;
}
//if the destination square is in the list, return true
returnVal = false;
for (int i = 0; i < legalMoves.size(); i++)
{
Integer nodeVal = (Integer) legalMoves.get(i);
//System.out.println(nodeVal.intValue());
if (nodeVal.intValue() == dest)
{
returnVal = true;
break;
}
}
//System.out.println(returnVal);
return returnVal;
}
}
}
Return to my program list