Results 1 to 6 of 6
  1. #1
    danborgir is offline Member
    Join Date
    Jan 2011
    Posts
    26
    Rep Power
    0

    Default 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:

    Java 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

    Java 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:

    Java 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

  2. #2
    snotmare is offline Member
    Join Date
    Apr 2011
    Posts
    34
    Rep Power
    0

    Default

    Would something like this work?


    Java Code:
    int column = 0;
    int row = 0;
    
    if(x >= 0 && x <= 600){
        column = x / 75
    }else{
        //mouse not on game board
    }
    
    if(y >= 0 && y <= 600){
        row = y / 75
    }else{
        //mouse not on game board
    }
    If x=100 and y=100, column would be 1, and row would be 1.

  3. #3
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    Why does piecePlaces[] have 65 elements?

  4. #4
    danborgir is offline Member
    Join Date
    Jan 2011
    Posts
    26
    Rep Power
    0

    Default

    Sorry for the late reply, thanks snotmare, that worked :D

    I accidentally had one too many pieces in the array however I've corrected that now!

    So the ChessGame class has not changed yet, here is the Pieces class:

    Java Code:
    public class Pieces extends ChessGame {
    
    
    public Pieces() {
    
    }
    	
    	
    public static void workoutloc(int X, int Y) {
    		int column = X;
    		int row = Y;
    		
    	String[] piecePlaces = new String[64];
    
    	piecePlaces[0] = "WR1";
    	piecePlaces[1] = "WH1";
    	piecePlaces[2] = "WB1";
    	piecePlaces[3] = "WK";
    	piecePlaces[4] = "WQ";
    	piecePlaces[5] = "WB2";
    	piecePlaces[6] = "WH2";
    	piecePlaces[7] = "WR2";
    	piecePlaces[8] = "WPAWN1";
    	piecePlaces[9] = "WPAWN2";
    	piecePlaces[10] = "WPAWN3";
    	piecePlaces[11] = "WPAWN4";
    	piecePlaces[12] = "WPAWN5";
    	piecePlaces[13] = "WPAWN6";
    	piecePlaces[14] = "WPAWN7";
    	piecePlaces[15] = "WPAWN8";
    	
    	piecePlaces[16] = null;
    	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] = "BPAWN1";
    	piecePlaces[49] = "BPAWN2";
    	piecePlaces[50] = "BPAWN3";
    	piecePlaces[51] = "BPAWN4";
    	piecePlaces[52] = "BPAWN5";
    	piecePlaces[53] = "BPAWN6";
    	piecePlaces[54] = "BPAWN7";
    	piecePlaces[55] = "BPAWN8";
    	piecePlaces[56] = "BR1";
    	piecePlaces[57] = "BH1";
    	piecePlaces[58] = "BB1";
    	piecePlaces[59] = "BK";
    	piecePlaces[60] = "BQ";
    	piecePlaces[61] = "BB2";
    	piecePlaces[62] = "BH2";
    	piecePlaces[63] = "BR2";
    	
    	int start = 0;
    	int start2 = 0;
    	
    	int[][] pseudoboard = new int[8][8];
    
    	for (int rows = 0; rows < 8; rows++) {
    		for(int cols = 0; cols < 8; cols++) {	
    			start++;
    		}	
    			start2++;
    		}		
    		
    	if(X >= 0 && X <= 600) {
    		column = X / 75;
    	}else{
    			//mouse not on game board
    	}
    
    	if(Y >= 0 && Y <= 600) {
    		row = Y / 75;
    	}else{
    		    //mouse not on game board
    	}	
    	
    	// lets assume the 2 dimencional array of 8x8 exists already name pseudoboard and contains integers from [0][0]-->0 [8][8]-->64
    	// what you do is 
    
    //    int targetsquare = pseudoboard[column][row];	
    	
    	
    	} 
    	
    }
    So now I think I need to link the piecePlaces array and the pseudoboard array. I'm stuck!

    If anyone could help it would be great I have two weeks left to finish this and its driving me insane!!!

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    you could've just held a reference to the piece object in each board square...

    here's an example of what i mean:
    Java Code:
    //the GUI board
    java.awt.Canvas[][] boardArray = new java.awt.Canvas[8][8]();
    //attach event to each square
    for (int i=0; i<boardArray.length; i++) for (int j=0; j<boardArray[i].length; j++)
        boardArray[i][j].addMouseListener(new java.awt.event.MouseAdapter() {
                public void mousePressed(java.awt.event.MouseEvent evt) {
                    boardObjectPicked(i, j, evt);
                }
                public void mouseReleased(java.awt.event.MouseEvent evt) {
                    boardObjectReleased(i, j, evt);
                }
            });
    Java Code:
        private void boardObjectPressed(int x, int y, java.awt.event.MouseEvent evt) {
            //pick up object related to board[x][y]
            updateBoard();
        }
    
        private void boardObjectReleased(int x, int y, java.awt.event.MouseEvent evt) {
            //if any object is picked, drop object at board[x][y]
            updateBoard();
        }

    then the indexes x and y would relate to an array of the placement of Piece Objects, like this:
    Java Code:
    Piece[][] pieceArrangement = new Piece[8][8];
    //initialise board when game is started
    pieceArrangement[0][0] = player2.getRook1();
    pieceArrangement[0][1] = player2.getHorse1();
    pieceArrangement[0][2] = player2.getBishop1();
    ...
    pieceArrangement[8][8] = player1.getRook2();
    and you will need to rearrange the array as needed to relate to the new board with an updateBoard() method
    Java Code:
    public void updateBoard() {
        //first check if move is valid, or if anything died
        evaluateBoard();
    }
    Last edited by ozzyman; 04-18-2011 at 11:50 AM.

  6. #6
    danborgir is offline Member
    Join Date
    Jan 2011
    Posts
    26
    Rep Power
    0

    Default

    thankyou ozzyman, it makes sense kind of but I think if I change my code drastically now I'll cause myself more work! If I had more time I would def change it, thankyou for the advice though!

Similar Threads

  1. problem getting a piece of data out of XML
    By Sparky in forum New To Java
    Replies: 8
    Last Post: 02-14-2011, 09:30 PM
  2. Determining whether a queen can attack another piece
    By sonofshirt in forum New To Java
    Replies: 9
    Last Post: 10-09-2010, 02:23 AM
  3. Code to check if a piece of code is legal.
    By vahshir in forum New To Java
    Replies: 3
    Last Post: 08-30-2010, 04:21 AM
  4. small piece of code: cannot set a max
    By senca in forum New To Java
    Replies: 1
    Last Post: 03-06-2010, 08:26 PM
  5. Decode this piece of Code
    By mikeyl62 in forum New To Java
    Replies: 2
    Last Post: 02-27-2010, 08:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •