Geekpedia Programming Tutorials






Chess Game In Java

A fully functional chess game in Java, without an AI or disallowing moves that are considered to be illegal according to the rules of chess.

On Tuesday, August 5th 2008 at 11:09 PM
By Andrew Pociu (View Profile)
****-   (Rated 4 with 4 votes)
Contextual Ads
More Java Resources
Advertisement
  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.ComponentOrientation;
  4. import java.awt.Cursor;
  5. import java.awt.GridLayout;
  6. import java.awt.event.InputEvent;
  7. import java.awt.image.BufferedImage;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.util.Map;
  11. import java.util.TreeMap;
  12.  
  13. import javax.imageio.ImageIO;
  14. import javax.swing.ImageIcon;
  15. import javax.swing.JButton;
  16. import javax.swing.JFrame;
  17. import javax.swing.JLabel;
  18. import javax.swing.JOptionPane;
  19. import javax.swing.JPanel;
  20. import javax.swing.JToolBar;
  21. import java.awt.Dimension;
  22.  
  23. public class ChessGame extends JFrame {
  24.  
  25.         private static final long serialVersionUID = 1L;
  26.  
  27.         private JPanel jContentPane = null;
  28.  
  29.         private JPanel jPanel = null;
  30.  
  31.         private JToolBar tlbMain = null;
  32.  
  33.         private JLabel lblCells[] = new JLabel[64];
  34.        
  35.         private String jPieces[][] = new String[8][8];  //  @jve:decl-index=0:
  36.  
  37.         private JButton btnNewGame = null;
  38.  
  39.         private JLabel lblStatus = null;
  40.        
  41.         private int heldX, heldY, heldI = -1;
  42.        
  43.         // Map the full names of the pieces to their codenames (wRook, wQueen, etc.)
  44.         private Map pieceName = new TreeMap();  //  @jve:decl-index=0:
  45.  
  46.         private JLabel lblCurrPlayer = null;
  47.        
  48.         // Stores the current player's move - we can easily match it against
  49.         // the first character of the pieces array
  50.         private char currPlayer = ' ';
  51.  
  52.         private JButton btnUndo = null;
  53.        
  54.         private int[][] moves = new int[10][6];
  55.        
  56.         private String movedPieces[] = new String[10];
  57.        
  58.         private int currMove = 0;
  59.  
  60.         /**
  61.          * This is the default constructor
  62.          */
  63.         public ChessGame() {
  64.                 super();
  65.                 initialize();
  66.                 buildBoard();
  67.         }
  68.        
  69.         /**
  70.          * This method initializes btnUndo     
  71.          *      
  72.          * @return javax.swing.JButton 
  73.          */
  74.         private JButton getBtnUndo() {
  75.                 if (btnUndo == null) {
  76.                         btnUndo = new JButton();
  77.                         btnUndo.setText("Undo");
  78.                         btnUndo.setEnabled(false);
  79.                         btnUndo.addMouseListener(new java.awt.event.MouseAdapter() {
  80.                                 public void mouseReleased(java.awt.event.MouseEvent e) {
  81.                                         undoMove();
  82.                                 }
  83.                         });
  84.                 }
  85.                 return btnUndo;
  86.         }
  87.  
  88.         public static void main( String args[] ) {
  89.                 new ChessGame().setVisible(true);
  90.         }
  91.  
  92.         /**
  93.          * This method initializes this
  94.          *
  95.          * @return void
  96.          */
  97.         private void initialize() {
  98.                 this.setSize(671, 555);
  99.                 this.setContentPane(getJContentPane());
  100.                 this.setTitle("Basic Chess");
  101.                 this.setExtendedState(JFrame.MAXIMIZED_BOTH);
  102.         }
  103.  
  104.         /**
  105.          * This method initializes jContentPane
  106.          *
  107.          * @return javax.swing.JPanel
  108.          */
  109.         private JPanel getJContentPane() {
  110.                 if (jContentPane == null) {
  111.                         jContentPane = new JPanel();
  112.                         jContentPane.setLayout(new BorderLayout());
  113.                         jContentPane.add(getJPanel(), BorderLayout.CENTER);
  114.                         jContentPane.add(getTlbMain(), BorderLayout.NORTH);
  115.                 }
  116.                 return jContentPane;
  117.         }
  118.  
  119.         /**
  120.          * This method initializes jPanel       
  121.          *      
  122.          * @return javax.swing.JPanel   
  123.          */
  124.         private JPanel getJPanel() {
  125.                 if (jPanel == null) {
  126.                         GridLayout gridLayout = new GridLayout();
  127.                         gridLayout.setRows(8);
  128.                         gridLayout.setHgap(5);
  129.                         gridLayout.setVgap(5);
  130.                         gridLayout.setColumns(8);
  131.                         jPanel = new JPanel();
  132.                         jPanel.setLayout(gridLayout);
  133.                         //buildBoard();
  134.                 }
  135.                 return jPanel;
  136.         }
  137.        
  138.         private void newGame()
  139.         {
  140.                 resetBoard();
  141.                 resetPieces();
  142.         }
  143.        
  144.         private void resetPieces()
  145.         {
  146.                 jPieces = new String[8][8];
  147.                 jPieces[0][0] = "bRook";
  148.                 jPieces[0][1] = "bKnight";
  149.                 jPieces[0][2] = "bBishop";
  150.                 jPieces[0][3] = "bKing";
  151.                 jPieces[0][4] = "bQueen";
  152.                 jPieces[0][5] = "bBishop";
  153.                 jPieces[0][6] = "bKnight";
  154.                 jPieces[0][7] = "bRook";
  155.                 jPieces[1][0] = "bPawn";
  156.                 jPieces[1][1] = "bPawn";
  157.                 jPieces[1][2] = "bPawn";
  158.                 jPieces[1][3] = "bPawn";
  159.                 jPieces[1][4] = "bPawn";
  160.                 jPieces[1][5] = "bPawn";
  161.                 jPieces[1][6] = "bPawn";
  162.                 jPieces[1][7] = "bPawn";
  163.                
  164.                 jPieces[6][0] = "wPawn";
  165.                 jPieces[6][1] = "wPawn";
  166.                 jPieces[6][2] = "wPawn";
  167.                 jPieces[6][3] = "wPawn";
  168.                 jPieces[6][4] = "wPawn";
  169.                 jPieces[6][5] = "wPawn";
  170.                 jPieces[6][6] = "wPawn";
  171.                 jPieces[6][7] = "wPawn";
  172.                 jPieces[7][0] = "wRook";
  173.                 jPieces[7][1] = "wKnight";
  174.                 jPieces[7][2] = "wBishop";
  175.                 jPieces[7][3] = "wKing";
  176.                 jPieces[7][4] = "wQueen";
  177.                 jPieces[7][5] = "wBishop";
  178.                 jPieces[7][6] = "wKnight";
  179.                 jPieces[7][7] = "wRook";
  180.                 RepaintPieces();
  181.         }
  182.        
  183.         private void PaintPiece(String pieceName, int i)
  184.         {
  185.                 try
  186.                 {
  187.                         if(pieceName != null && pieceName != "")
  188.                         {
  189.                                 InputStream inIcon = ClassLoader.getSystemResourceAsStream("pociu/games/chess/" + pieceName + ".png");
  190.                                 BufferedImage imgIcon = ImageIO.read(inIcon);
  191.                                 lblCells[i].setIcon(new ImageIcon(imgIcon));
  192.                                 //System.out.println("Painted " + pieceName + " at " + i);
  193.                         }
  194.                         else
  195.                         {
  196.                                 lblCells[i].setIcon(null);
  197.                                 //System.out.println("Cleared cell at " + i);
  198.                         }
  199.                 }
  200.                 catch(IOException e)
  201.                 {
  202.                         e.printStackTrace();
  203.                 }
  204.         }
  205.        
  206.         private void RepaintPieces()
  207.         {
  208.                 int i = 0;
  209.                 for(int x = 0