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

    Default Chess Game - Movement

    Hi Guys,

    I am developing a Chess game in Java for a 2nd year project at University. So far I have created the chess board and laid out all the pieces according to co-ordinates I have worked out on the board.

    I am now stuck as to where to go from here to get movement into the pieces!

    Here is my code so far, please feel free to criticise it!

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseEvent;
    
    public class Chessboard extends JFrame implements MouseListener
    {
    Insets insetxy;
    Graphics g1;
    Image img;
    int X,Y,X1,Y1,X2,Y2,TOP_ROW_PIECES_Y,BOTTOM_ROW_PIECES_Y,BOTTOM_BOTTOM_ROW_PIECES_Y,BOTTOM_TOP_ROW_PIECES_Y;
    
    
        /** Starts the application.
    
    
        /* Displays a very simple chessboard.
          */
        public Chessboard() 
        {
           super("Chess"); 		
    	   
        setSize(490,510);
    	setVisible(true);
        setResizable(false);
        addMouseListener(this);
    	
    	
    }
    
    
        public void apiece()	{	
    	
    /***********---TOP ROW CHESS PIECES---************/	
    	
    	//	Position in center of square
    			
    		TOP_ROW_PIECES_Y = 40;	
    		BOTTOM_ROW_PIECES_Y = 90;
    		
    		
    // 		X array	to set position of pieces
     
    	   int[] xCoOrd = new int[16];
    
    //		Top row pieces	   
    	   xCoOrd[0] = 20;
    	   xCoOrd[1] = 80;
    	   xCoOrd[2] = 140;
    	   xCoOrd[3] = 200;
    	   xCoOrd[4] = 260;
    	   xCoOrd[5] = 320;
    	   xCoOrd[6] = 380;
    	   xCoOrd[7] = 440;
    	   
    //  	Image String array - TOP ROW
    
    	   String[] ImageArray_TOP = new String[8];
    
    	   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";
    	   
    //	   Image String Array - BOTTOM ROW
    
    	   String[] ImageArray_BOTTOM = new String[8];
    
    	   ImageArray_BOTTOM[0] = "pieces/top/1bpawn.gif";
    	   ImageArray_BOTTOM[1] = "pieces/top/1bpawn.gif";
    	   ImageArray_BOTTOM[2] = "pieces/top/1bpawn.gif";
    	   ImageArray_BOTTOM[3] = "pieces/top/1bpawn.gif";
    	   ImageArray_BOTTOM[4] = "pieces/top/1bpawn.gif";
    	   ImageArray_BOTTOM[5] = "pieces/top/1bpawn.gif";
    	   ImageArray_BOTTOM[6] = "pieces/top/1bpawn.gif";
    	   ImageArray_BOTTOM[7] = "pieces/top/1bpawn.gif";
    
    	   
    //  Draws TOP ROW of pieces
    	
    	for(int i = 0; i < 8; i++)	{
    
    // X1 is now equal to value in array, passing this value to drawImage
    		
    	X1 = xCoOrd[i];
    
    		
    // Get Image
    
    	Toolkit toolkit = getToolkit();
    	img = toolkit.getImage(ImageArray_TOP[i]);  
    	
    	g1.drawImage(img,X1,TOP_ROW_PIECES_Y,this);
    			
        }
    	
    	
    //	Draws TOP ROWS of pieces (pawns)
    	
    	for(int i = 0; i < 8; i++)	{
    
    // X1 is now equal to value in array, passing this value to drawImage
    		
    	X1 = xCoOrd[i];
    
    		
    // Get Image
    
    	Toolkit toolkit = getToolkit();
    	img = toolkit.getImage(ImageArray_BOTTOM[i]);  
    
    			
    	g1.drawImage(img,X1,BOTTOM_ROW_PIECES_Y,this);
    			
        }	   
    	   
    	   
    	   
    /** BOTTOM CHESS PIECES **/  
    	   
    
    	
    	
    //	Position in center of square
    			
    		BOTTOM_TOP_ROW_PIECES_Y = 390;	
    		BOTTOM_BOTTOM_ROW_PIECES_Y = 460;
    		
    		
    // 		X array	to set position of pieces
     
    	   int[] BOTTOM_xCoOrd = new int[16];
    
    //		Top row pieces	   
    
    	   BOTTOM_xCoOrd[0] = 20;
    	   BOTTOM_xCoOrd[1] = 80;
           BOTTOM_xCoOrd[2] = 140;
    	   BOTTOM_xCoOrd[3] = 200;
    	   BOTTOM_xCoOrd[4] = 260;
    	   BOTTOM_xCoOrd[5] = 320;
    	   BOTTOM_xCoOrd[6] = 380;
    	   BOTTOM_xCoOrd[7] = 440;
    	
    //  	Image String array - TOP ROW
    
    	   String[] ImageArray_BOTTOM_TOP = new String[8];
    
    	   ImageArray_BOTTOM_TOP[0] = "pieces/bottom/rrook.gif";
    	   ImageArray_BOTTOM_TOP[1] = "pieces/bottom/rknight.gif";
    	   ImageArray_BOTTOM_TOP[2] = "pieces/bottom/rbishop.gif";
    	   ImageArray_BOTTOM_TOP[3] = "pieces/bottom/rqueen.gif";
    	   ImageArray_BOTTOM_TOP[4] = "pieces/bottom/rking.gif";
    	   ImageArray_BOTTOM_TOP[5] = "pieces/bottom/rbishop.gif";
    	   ImageArray_BOTTOM_TOP[6] = "pieces/bottom/rknight.gif";
    	   ImageArray_BOTTOM_TOP[7] = "pieces/bottom/rrook.gif";
    	   
    //	   Image String Array - BOTTOM ROW
    
    	   String[] ImageArray_BOTTOM_BOTTOM = new String[8];
    
    	   ImageArray_BOTTOM_BOTTOM[0] = "pieces/bottom/rpawn.gif";
    	   ImageArray_BOTTOM_BOTTOM[1] = "pieces/bottom/rpawn.gif";
    	   ImageArray_BOTTOM_BOTTOM[2] = "pieces/bottom/rpawn.gif";
    	   ImageArray_BOTTOM_BOTTOM[3] = "pieces/bottom/rpawn.gif";
    	   ImageArray_BOTTOM_BOTTOM[4] = "pieces/bottom/rpawn.gif";
    	   ImageArray_BOTTOM_BOTTOM[5] = "pieces/bottom/rpawn.gif";
    	   ImageArray_BOTTOM_BOTTOM[6] = "pieces/bottom/rpawn.gif";
    	   ImageArray_BOTTOM_BOTTOM[7] = "pieces/bottom/rpawn.gif";
    	   
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    //  Draws BOTTOM ROWS of pieces (pawns)
    	
    	for(int i = 0; i < 8; i++)	{
    
    // X1 is now equal to value in array, passing this value to drawImage
    		
    	X1 = BOTTOM_xCoOrd[i];
    
    		
    // Get Image
    
    	Toolkit toolkit = getToolkit();
    	img = toolkit.getImage(ImageArray_BOTTOM_BOTTOM[i]);  
    
    			
    	g1.drawImage(img,X1,BOTTOM_TOP_ROW_PIECES_Y,this);
    			
        }
    	
    //  Draws BOTTOM ROWS of pieces (pawns)
    	
    	for(int i = 0; i < 8; i++)	{
    
    // X1 is now equal to value in array, passing this value to drawImage
    		
    	X1 = BOTTOM_xCoOrd[i];
    
    		
    // Get Image
    
    	Toolkit toolkit = getToolkit();
    	img = toolkit.getImage(ImageArray_BOTTOM_TOP[i]);  // linux referrence
    
    			
    	g1.drawImage(img,X1,BOTTOM_BOTTOM_ROW_PIECES_Y,this);
    			
        }
    	
    	
    	
    	
    	
    }
    	 
    
         public void Board()
    	{
    		Color Green = new Color(119,170,119);
    		Color Cream = new Color(250,235,215);
    		
    	int xbounds, ybounds;
    	 for (int i = 0; i < 8; i++)
                {
                xbounds=i*60;
                for (int j = 0; j < 8; j++)
                    {
                    ybounds=j*60;
                    // Select the color and draw a square
                    if((i+j) % 2 == 0)
                    {
    					g1.setColor(Cream);		}		
    					else { g1.setColor(Green); }
    
              ;
                    g1.fillRect(X+xbounds, Y+ybounds, 60, 60);
                    }
                }
    
    	}
    
        public void paint (Graphics g )
        {
        		insetxy = getInsets();
    		X = insetxy.top + insetxy.bottom-20;
    		Y = 20+insetxy.left + insetxy.right;
    		
    		g1 = g;
    		Board();
    		apiece();
    		
          }
    
          public void mouseClicked(MouseEvent e)
    	  	{
    
    	  		System.out.println("mouseClicked (" + e.getX() + ","+e.getY()+")");
    	  		System.out.println(" the button you pressed was button---> " +e.getButton());
    	  		System.out.println(" the point was ---> " +e.getPoint());
    
    	  		}
    
    
    	  	public void mouseReleased(MouseEvent e)
    	  	{
    
    	  		System.out.println("mouseReleased (" + e.getX() + ","+e.getY()+")");
    
    //repaint();
    	  	}
    	  	public void mouseEntered(MouseEvent e)
    	  	{
    
    	  		System.out.println("mouseEntered (" + e.getX() + ","+e.getY()+")");
    
    	  	}
    	  	public void mouseExited (MouseEvent e)
    	  	{
    
    	  		System.out.println("mouseExited (" + e.getX() + ","+e.getY()+")");
    
    	  	}
    	  	public void mousePressed(MouseEvent e)
    	  	{
    
    	  		System.out.println("mousePressed (" + e.getX() + ","+e.getY()+")");
    	  	X1=e.getX();
    	  	Y1=e.getY();
    		repaint();
    
    
     			}
    
    public static void main (String args[])
    {
    	Chessboard nf=new Chessboard();
    	/*nf.setSize(500,520);
    	       nf.setVisible(true);
           nf.setResizable(false);*/
    	}
    
    
    }
    As you can see there is quite a lot of code for setting out all the pieces! What I need to work out is how I can move the pieces round in the squares. The squares aren't images but 60 by 60 px.

    Do any of you guys know of a good way to do this? I was thinking of creating a grid over the top of the image with coordinates that relate to numbers and letters but I don't really know how I would go about doing that :confused:

    I have attached a screen shot of the program running. :)

    Thanks in advance,

    Dan
    Attached Thumbnails Attached Thumbnails Chess Game - Movement-update.jpg  

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

    Default

    to move a chess piece, piece P needs to move from 0,0 to X,Y (board co-ordinates)

    the board is 8x8 = 64.
    if a Pawn starts at (0,1), and then it is dragged and dropped on (0,2)
    IF (its a valid move) {
    then move(Pawn,0,2)
    }
    re-paint the board

    so you need to make a move() function
    and you need to set the co-ordinates for each square on the board so the game knows where each square is
    i.e. if Piece[1] is dragged to the Point(300,200), which square lies on that point?

    Once movement is allowed, you can add the other function validateMove(piece,X,Y) which will create an array of valid board squares that particular piece can move to, and if its a valid move allow it to continue, otherwise disallow the move
    Last edited by ozzyman; 03-14-2011 at 12:50 PM.

Similar Threads

  1. Car Game - Handling movement
    By Rectal Exambot in forum New To Java
    Replies: 2
    Last Post: 08-24-2010, 03:23 AM
  2. chess game
    By michail in forum New To Java
    Replies: 13
    Last Post: 01-12-2010, 08:24 AM
  3. Chess game
    By michail in forum New To Java
    Replies: 4
    Last Post: 12-23-2009, 07:51 PM
  4. New Chess-Like game
    By jSarK0Y in forum Reviews / Advertising
    Replies: 3
    Last Post: 06-10-2009, 04:28 AM
  5. Snake game movement
    By BeerMonkey in forum New To Java
    Replies: 9
    Last Post: 11-27-2008, 01:48 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
  •