Results 1 to 3 of 3

Thread: ChessGame

  1. #1
    Dastagir is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default ChessGame

    hello,i'm new in java & i'm facing problems with making a 2 player chess game.i've inserted the pieces & made the pieces move,but i'm stuck there.i'm not finding how to ensure valid moves.anyone pls get me some help...




    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import java.applet.*;
     
    
    public class ChessGame extends JFrame implements MouseListener, MouseMotionListener {
      JLayeredPane layeredPane;
      JPanel chessBoard;
      JLabel chessPiece;
      int xAdjustment;
      int yAdjustment;
     
      public ChessGame(){
      Dimension boardSize = new Dimension(800, 800);
     
      
     layeredPane = new JLayeredPane();
      getContentPane().add(layeredPane);
      layeredPane.setPreferredSize(boardSize);
      layeredPane.addMouseListener(this);
      layeredPane.addMouseMotionListener(this);//for dragging
    
      //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);
     
      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 ? Color.RED : Color.GREEN );
      else
      square.setBackground( i % 2 == 0 ? Color.GREEN : Color.RED );
      }
     
     
     //Black pieces
      JLabel piece = new JLabel( new ImageIcon("D:/`studY/Project/br.gif") );
      JPanel panel = (JPanel)chessBoard.getComponent(0);
      panel.add(piece);
      piece = new JLabel(new ImageIcon("D:/`studY/Project/bn.gif"));
      panel = (JPanel)chessBoard.getComponent(1);
      panel.add(piece);
      piece = new JLabel(new ImageIcon("D:/`studY/Project/bb.gif"));
      panel = (JPanel)chessBoard.getComponent(2);
      panel.add(piece);
      piece = new JLabel(new ImageIcon("D:/`studY/Project/bk.gif"));
      panel = (JPanel)chessBoard.getComponent(3);
      panel.add(piece);
      piece = new JLabel(new ImageIcon("D:/`studY/Project/bq.gif"));
      panel = (JPanel)chessBoard.getComponent(4);
      panel.add(piece);
      piece = new JLabel(new ImageIcon("D:/`studY/Project/bb.gif"));
      panel = (JPanel)chessBoard.getComponent(5);
      panel.add(piece);
      piece = new JLabel(new ImageIcon("D:/`studY/Project/bn.gif"));
      panel = (JPanel)chessBoard.getComponent(6);
      panel.add(piece);
      piece = new JLabel(new ImageIcon("D:/`studY/Project/br.gif"));
      panel = (JPanel)chessBoard.getComponent(7);
      panel.add(piece);
      
      for (int i=8;i<=15;i++){
      piece = new JLabel(new ImageIcon("D:/`studY/Project/bp.gif"));
      panel = (JPanel)chessBoard.getComponent(i);
      panel.add(piece);
      }
      
      //white pieces
      piece = new JLabel( new ImageIcon("D:/`studY/Project/wr.gif") );
      panel = (JPanel)chessBoard.getComponent(56);
      panel.add(piece);
      piece = new JLabel(new ImageIcon("D:/`studY/Project/wn.gif"));
      panel = (JPanel)chessBoard.getComponent(57);
      panel.add(piece);
      piece = new JLabel(new ImageIcon("D:/`studY/Project/wb.gif"));
      panel = (JPanel)chessBoard.getComponent(58);
      panel.add(piece);
      piece = new JLabel(new ImageIcon("D:/`studY/Project/wq.gif"));
      panel = (JPanel)chessBoard.getComponent(59);
      panel.add(piece);
      piece = new JLabel(new ImageIcon("D:/`studY/Project/wk.gif"));
      panel = (JPanel)chessBoard.getComponent(60);
      panel.add(piece);
      piece = new JLabel(new ImageIcon("D:/`studY/Project/wb.gif"));
      panel = (JPanel)chessBoard.getComponent(61);
      panel.add(piece);
      piece = new JLabel(new ImageIcon("D:/`studY/Project/wn.gif"));
      panel = (JPanel)chessBoard.getComponent(62);
      panel.add(piece);
      piece = new JLabel(new ImageIcon("D:/`studY/Project/wr.gif"));
      panel = (JPanel)chessBoard.getComponent(63);
      panel.add(piece);
      piece = new JLabel(new ImageIcon("D:/`studY/Project/wp.gif"));
      panel = (JPanel)chessBoard.getComponent(48);
      panel.add(piece);
      
      for (int i=49;i<=55;i++){
      piece = new JLabel(new ImageIcon("D:/`studY/Project/wp.gif"));
      panel = (JPanel)chessBoard.getComponent(i);
      panel.add(piece);
      }
      
       
      }
     
        @Override
      public void mousePressed(MouseEvent e){
      chessPiece = null;
      Component c =  chessBoard.findComponentAt(e.getX(), e.getY());
     
      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();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setResizable(true);
      frame.setLocationRelativeTo( null );
      frame.setVisible(true);
     }
    }
    Last edited by Norm; 12-12-2011 at 10:15 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: ChessGame

    how to ensure valid moves
    I think I saw a post on this forum that had the logic for checking valid moves. Try doing a Search on the forum.
    One way would be a brute force method. Compute the valid moves for a piece one at a time and as you are doing it see if the move you are testing is one of the computed moves.

  3. #3
    Dastagir is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

Posting Permissions

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