Working out chess piece location / piece name
Hi guys,
I'm making a chess game for a project at uni. I have all the pieces laid out on a board and they move freely. I am now trying to get the game to understand what each piece is and the rules behind each piece.
The code so far for the game (layout and movement) is:
Code:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class ChessGame extends JFrame implements MouseListener, MouseMotionListener {
JLayeredPane layeredPane;
JPanel chessBoard;
JLabel chessPiece;
int xAdjustment;
int yAdjustment;
public ChessGame(){
Dimension boardSize = new Dimension(600, 600);
// Use a Layered Pane for this this application
layeredPane = new JLayeredPane();
getContentPane().add(layeredPane);
layeredPane.setPreferredSize(boardSize);
layeredPane.addMouseListener(this);
layeredPane.addMouseMotionListener(this);
//Add a chess board to the Layered Pane
chessBoard = new JPanel();
layeredPane.add(chessBoard, JLayeredPane.DEFAULT_LAYER);
chessBoard.setLayout( new GridLayout(8, 8) );
chessBoard.setPreferredSize( boardSize );
chessBoard.setBounds(0, 0, boardSize.width, boardSize.height);
Color Green = new Color(119,170,119);
Color Cream = new Color(250,235,215);
for (int i = 0; i < 64; i++) {
JPanel square = new JPanel( new BorderLayout() );
chessBoard.add( square );
int row = (i / 8) % 2;
if (row == 0)
square.setBackground( i % 2 == 0 ? Green : Cream );
else
square.setBackground( i % 2 == 0 ? Cream : Green );
}
/** Pieces for the board */
// Image String array - TOP ROWS
String[] ImageArray_TOP = new String[16];
ImageArray_TOP[0] = "pieces/top/1brook.gif";
ImageArray_TOP[1] = "pieces/top/1bknight.gif";
ImageArray_TOP[2] = "pieces/top/1bbishop.gif";
ImageArray_TOP[3] = "pieces/top/1bqueen.gif";
ImageArray_TOP[4] = "pieces/top/1bking.gif";
ImageArray_TOP[5] = "pieces/top/1bbishop.gif";
ImageArray_TOP[6] = "pieces/top/1bknight.gif";
ImageArray_TOP[7] = "pieces/top/1brook.gif";
ImageArray_TOP[8] = "pieces/top/1bpawn.gif";
ImageArray_TOP[9] = "pieces/top/1bpawn.gif";
ImageArray_TOP[10] = "pieces/top/1bpawn.gif";
ImageArray_TOP[11] = "pieces/top/1bpawn.gif";
ImageArray_TOP[12] = "pieces/top/1bpawn.gif";
ImageArray_TOP[13] = "pieces/top/1bpawn.gif";
ImageArray_TOP[14] = "pieces/top/1bpawn.gif";
ImageArray_TOP[15] = "pieces/top/1bpawn.gif";
// Image String Array - BOTTOM ROWS
String[] ImageArray_BOTTOM = new String[16];
ImageArray_BOTTOM[0] = "pieces/bottom/rpawn.gif";
ImageArray_BOTTOM[1] = "pieces/bottom/rpawn.gif";
ImageArray_BOTTOM[2] = "pieces/bottom/rpawn.gif";
ImageArray_BOTTOM[3] = "pieces/bottom/rpawn.gif";
ImageArray_BOTTOM[4] = "pieces/bottom/rpawn.gif";
ImageArray_BOTTOM[5] = "pieces/bottom/rpawn.gif";
ImageArray_BOTTOM[6] = "pieces/bottom/rpawn.gif";
ImageArray_BOTTOM[7] = "pieces/bottom/rpawn.gif";
ImageArray_BOTTOM[8] = "pieces/bottom/rrook.gif";
ImageArray_BOTTOM[9] = "pieces/bottom/rknight.gif";
ImageArray_BOTTOM[10] = "pieces/bottom/rbishop.gif";
ImageArray_BOTTOM[11] = "pieces/bottom/rqueen.gif";
ImageArray_BOTTOM[12] = "pieces/bottom/rking.gif";
ImageArray_BOTTOM[13] = "pieces/bottom/rbishop.gif";
ImageArray_BOTTOM[14] = "pieces/bottom/rknight.gif";
ImageArray_BOTTOM[15] = "pieces/bottom/rrook.gif";
// Add pieces to the board (TOP)
for( int i = 0; i < 16; i++) {
JLabel piece = new JLabel( new ImageIcon(ImageArray_TOP[i]) );
JPanel panel = (JPanel)chessBoard.getComponent(i);
panel.add(piece);
}
// Add pieces to the board (BOTTOM)
int inc = 48;
for( int j = 0; j < 16; j++, inc++) {
JLabel piece = new JLabel( new ImageIcon(ImageArray_BOTTOM[j]) );
JPanel panel = (JPanel)chessBoard.getComponent(inc);
panel.add(piece);
}
}
public void mousePressed(MouseEvent e){
chessPiece = null;
Component c = chessBoard.findComponentAt(e.getX(), e.getY());
int X = e.getX();
int Y = e.getY();
System.out.println(X + Y);
Pieces.workoutloc(X, Y);
if (c instanceof JPanel)
return;
Point parentLocation = c.getParent().getLocation();
xAdjustment = parentLocation.x - e.getX();
yAdjustment = parentLocation.y - e.getY();
chessPiece = (JLabel)c;
chessPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
chessPiece.setSize(chessPiece.getWidth(), chessPiece.getHeight());
layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER);
}
//Move the chess piece around
public void mouseDragged(MouseEvent me) {
if (chessPiece == null) return;
chessPiece.setLocation(me.getX() + xAdjustment, me.getY() + yAdjustment);
}
//Drop the chess piece back onto the chess board
public void mouseReleased(MouseEvent e) {
if(chessPiece == null) return;
chessPiece.setVisible(false);
Component c = chessBoard.findComponentAt(e.getX(), e.getY());
if (c instanceof JLabel){
Container parent = c.getParent();
parent.remove(0);
parent.add( chessPiece );
}
else {
Container parent = (Container)c;
parent.add( chessPiece );
}
chessPiece.setVisible(true);
}
public void mouseClicked(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e) {
}
public static void main(String[] args) {
JFrame frame = new ChessGame();
JMenuBar MenuBar = new JMenuBar();
JMenu File = new JMenu("File" );
JMenu Help = new JMenu( "Help" );
MenuBar.add( File );
MenuBar.add( Help );
JMenuItem newGame = new JMenuItem( "New Game" );
JMenuItem exit = new JMenuItem( "Exit" );
File.add( newGame );
File.add( exit );
JMenuItem HelpInfo = new JMenuItem( "How to play chess" );
JMenuItem About = new JMenuItem( "About" );
Help.add( HelpInfo );
Help.add( About );
// ActionListeners for items
// EXIT PROGRAM
ActionListener ExitGame = new ActionListener() {
public void actionPerformed( ActionEvent actionEvent ) {
System.exit(0);
}
};
// NEW GAME
ActionListener newGameAction = new ActionListener() {
public void actionPerformed( ActionEvent actionEvent ) {
// SOMETHING
}
};
// HOW TO PLAY
ActionListener HowToPlay = new ActionListener() {
public void actionPerformed( ActionEvent actionEvent ) {
String url = "http://www.chess.com/learn-how-to-play-chess.html";
try {
java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
} catch (IOException ex) {
System.err.println(); }
}
};
// ADD LISTENERS
exit.addActionListener( ExitGame );
newGame.addActionListener( newGameAction );
HelpInfo.addActionListener( HowToPlay );
frame.setJMenuBar( MenuBar );
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE );
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo( null );
frame.setVisible(true);
frame.setSize(600, 645);
}
}
Okay so that is the main class to make the board and add pieces / movement.
Here is the code I have so far to make an array of piece names and make a 'pseudoboard' so the game knows where each piece is.
In the ChessGame class there is a method
Code:
Pieces.workoutloc(X, Y);
which I hope will pass the co-ordinates from the mouse click which will work out what piece has been clicked and whether it is allowed to move or not.
workoutloc is inside the piece class which I have here:
Code:
public class Pieces {
public static void main(String Args[]) {
String[] piecePlaces = new String[65];
piecePlaces[0] = "WR1";
piecePlaces[1] = "WH1";
piecePlaces[2] = "WB1";
piecePlaces[3] = "WK";
piecePlaces[4] = "WQ";
piecePlaces[5] = "WR2";
piecePlaces[6] = "WB2";
piecePlaces[7] = "WH2";
piecePlaces[8] = "WR2";
piecePlaces[9] = "WPAWN1";
piecePlaces[10] = "WPAWN2";
piecePlaces[11] = "WPAWN3";
piecePlaces[12] = "WPAWN4";
piecePlaces[13] = "WPAWN5";
piecePlaces[14] = "WPAWN6";
piecePlaces[15] = "WPAWN7";
piecePlaces[16] = "WPAWN8";
piecePlaces[17] = null;
piecePlaces[18] = null;
piecePlaces[19] = null;
piecePlaces[20] = null;
piecePlaces[21] = null;
piecePlaces[22] = null;
piecePlaces[23] = null;
piecePlaces[24] = null;
piecePlaces[25] = null;
piecePlaces[26] = null;
piecePlaces[27] = null;
piecePlaces[28] = null;
piecePlaces[29] = null;
piecePlaces[30] = null;
piecePlaces[31] = null;
piecePlaces[32] = null;
piecePlaces[33] = null;
piecePlaces[34] = null;
piecePlaces[35] = null;
piecePlaces[36] = null;
piecePlaces[37] = null;
piecePlaces[38] = null;
piecePlaces[39] = null;
piecePlaces[40] = null;
piecePlaces[41] = null;
piecePlaces[42] = null;
piecePlaces[43] = null;
piecePlaces[44] = null;
piecePlaces[45] = null;
piecePlaces[46] = null;
piecePlaces[47] = null;
piecePlaces[48] = null;
piecePlaces[49] = "BPAWN1";
piecePlaces[50] = "BPAWN2";
piecePlaces[51] = "BPAWN3";
piecePlaces[52] = "BPAWN4";
piecePlaces[53] = "BPAWN5";
piecePlaces[54] = "BPAWN6";
piecePlaces[55] = "BPAWN7";
piecePlaces[56] = "BPAWN8";
piecePlaces[57] = "BR1";
piecePlaces[58] = "BH1";
piecePlaces[59] = "BB1";
piecePlaces[60] = "BK";
piecePlaces[61] = "BQ";
piecePlaces[62] = "BB2";
piecePlaces[63] = "BH2";
piecePlaces[64] = "BR2";
int rows = 8;
int cols = 8;
int start = 0;
int start2 = 0;
int[][] pseudoboard = new int[cols][cols];
for (int row = 0; row < rows; row++) {
for(int col = 0; col < cols; col++) {
start++;
}
start2++;
}
workoutloc(int x,int y) {
int temp1 = x / 75;
int temp2 = y / 75;
math.round(temp1);
math.round(temp2);
// lets assume the 2 dimentional array of 8x8 exists already name pseudoboard and contains integers from [0][0]-->0 [8][8]-->64
// what you do is
int targetsquare = pseudoboard[temp1][temp2];
}
}
}
I don't know how to create the 2 dimentional array so that it will accept the X & Y from the main ChessGame class, divide the coordinate by 75 (75 being the width / height of the square) and then round it up or down to a whole number so that it can be placed into the array.
I've hit a wall that I don't think I can get myself away from without a poke... I'm pretty new to programming and this is just giving me a real headache!
If anyone could help I would really appreciate it!
thanks,
Dan