Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Chess Game

  1. #1
    Junior Member KTJW88's Avatar
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Chess Game

    Hello,

    I am working on my project with my partner for my school and we faced some problems, since we can move the x's and the 0'x across but we can't move the X's or the 0'x down. Also, when I exit the program, won't work with getSource() but works with getActionCommand.

    Any help or feedback will be helpful.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
     
    public class Breakthrough extends JFrame implements ActionListener
    {
       private JButton[][] gridUnits; // array holding grid square buttons
       private int x = 0;
       private int y = 0;
       private int y2 = 7;
       private boolean found = false;
     
     
       public Breakthrough()
       {
     
          //JMenuBar objects
          JMenuBar jmb = new JMenuBar(); 
          JMenu jmFile = new JMenu("File");
          JMenu jmHelp = new JMenu("Help");
          JMenuItem jmiExit = new JMenuItem("Exit");
          JMenuItem jmiAbout = new JMenuItem("About");
     
          //adding JMenuBar objects to the JFrame, gui
          jmFile.add(jmiExit); 
          jmHelp.add(jmiAbout); 
          jmb.add(jmFile); 
          jmb.add(jmHelp); 
          setJMenuBar(jmb);
     
          //Mnemonic objects
          jmFile.setMnemonic(KeyEvent.VK_F);
          jmHelp.setMnemonic(KeyEvent.VK_H);
          jmiExit.setMnemonic(KeyEvent.VK_X);
          jmiAbout.setMnemonic(KeyEvent.VK_A);
     
          //Adding ActionListener
          jmiExit.addActionListener(this); 
          jmiAbout.addActionListener(this);
     
     
          //creates grid
          JPanel grid = new JPanel();
          grid.setLayout(new GridLayout(8,8));
          grid.setSize(700,700);
     
          //creates grid squares
     
          gridUnits = new JButton[8][8];
     
          //creates 64 grid square buttons
          for(int rows = 0; rows< gridUnits.length;rows++)
          {
             for(int cols = 0; cols< gridUnits.length; cols++)
             { 
                gridUnits[rows][cols] = new JButton();
             }
     
          } 
     
          //adds 64 buttons to the grid
     
          for (int rows = 0; rows < gridUnits.length; rows++)
          {
     
             for(int cols = 0; cols < gridUnits.length; cols++)
             {
                grid.add(gridUnits[rows][cols]);
     
             }
     
          }
     
     
          //sets text for 16 buttons to the letter "X"
          for( int rows = 0; rows< gridUnits.length; rows++)
          {
     
             for(int cols = 0;cols <2; cols++)
             {
                gridUnits[rows][cols].setText("X");
             }
          }
     
     
          //sets text for 16 buttons to the letter "O"
     
          for(int rows = 0; rows< gridUnits.length; rows++)
          {
     
             for(int cols = 6;cols <8; cols++)
             {
                gridUnits[rows][cols].setText("O");
             }
     
          }      
     
          add(grid);
     
          for (int rows = 0; rows < gridUnits.length; rows++)
          {
     
             for(int cols = 0; cols < gridUnits.length; cols++)
             {
                gridUnits[rows][cols].addActionListener(this);
     
             }
     
          }
     
     
     
       }
     
       public void actionPerformed(ActionEvent ae)
       {
          Object choice = ae.getSource();
          //Object choice = ae.getActionCommand();
     
          if(choice == "Exit")
          {
             System.exit(0);
          }
          else if (choice == "About")
          {
             JOptionPane.showMessageDialog(null,"121 MiniPrject: Chess" +
    				"\nFebruary 19, 2014" + "\nDeveloped By Hassan Ndow & Kevin Whetstone", "Chess", JOptionPane.INFORMATION_MESSAGE);
          }
     
          try
          { 
             for (int rows = 0; rows < gridUnits.length; rows++)
             {
     
                for(int cols = 0; cols < gridUnits.length; cols++)
                {
     
     
                   if(choice == gridUnits[rows][cols] && (gridUnits[rows][cols].getText().equals("X")) &&(x == 0 && y == 0))
                   {
                      x = rows;
                      y = cols;
     
                   }
     
     
                   if(choice == gridUnits[x][y+1] && (gridUnits[x][y].getText().equals("X")) && (gridUnits[x][y+1].getText().equals("")))
                   {
                      gridUnits[x][y].setText("");
                      gridUnits[x][y+1].setText("X");
     
                   }
     
     
                   if(choice == gridUnits[rows][cols] && (gridUnits[rows][cols].getText().equals("X")))
                   {
                      x = rows;
                      y = cols;
     
                   }
     
     
                   if(choice == gridUnits[rows][cols] && (gridUnits[rows][cols].getText().equals("O")) &&(x == 0 && y2 == 7))
                   {
                      x = rows;
                      y2 = cols;
     
                   }
     
     
                   if(choice == gridUnits[x][y2-1] && (gridUnits[x][y2].getText().equals("O")) && (gridUnits[x][y2-1].getText().equals("")))
                   {
                      gridUnits[x][y2].setText("");
                      gridUnits[x][y2-1].setText("O");
     
                   }
     
     
                   if(choice == gridUnits[rows][cols] && (gridUnits[rows][cols].getText().equals("O")))
                   {
                      x = rows;
                      y2 = cols;
     
                   }
     
                }
     
             }//end of outer for loop
          }
     
          catch(NullPointerException npe)
          {
             npe.printStackTrace();
          }
          catch(ArrayIndexOutOfBoundsException aio)
          {
             aio.printStackTrace();
          }
     
          System.out.println(x);
          System.out.println(y);
     
     
     
       }//end of actionPerformed
     
     
     
     
       public static void main (String[] args)
       {
     
          //creates a Breakthrough object
          Breakthrough bt = new Breakthrough();
          bt.setTitle("Breakthrough");
          bt.setSize(700,700);
          bt.setLocationRelativeTo(null);
          bt.setVisible(true);
          bt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       }
    }


  2. #2
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Chess Game

    getSource() returns an object and you are trying to compare said object to "Exit", but you don't have any "Exit" objects. However, you do have a bmiExit object, but the scope of that variable wouldn't allow it to be referenced in the actionPerformed method. So change the scope of the object(s) you want to check via getSource() and then test the result against the object itself instead of a literal string. As for the x's and o's movement, I am too tired to be of much help there.

  3. #3
    Junior Member KTJW88's Avatar
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Chess Game

    Thank you for your help on the getSource method, and for the x's and 0'x movement, no problem.

    Thanks again,

    Kevin

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,115
    My Mood
    Mellow
    Thanks
    244
    Thanked 755 Times in 741 Posts

    Default Re: Chess Game

    You and your partner should learn to comment your code, especially if you want to show it to someone else to get their help. Please don't tell me "We do it after the code is done." That's pointless on so many levels.

    Please give your variables better names. What's a gridUnit? Why not chessPiece? Even better, whitePawn, blackPawn, whiteKing, blackQueen, etc. Using piece-specific names will lead you to creating a ChessPiece class - a good thing - in which the piece's name, original location, current location, move rules, possible moves, move history, etc. could be stored.

    You have a very busy actionPerformed() method without a clue given about what it's doing. Comments would be nice. A single actionPerformed() method for every possible user action may not be the best approach. Perhaps one for the menu items and another for the "chess" pieces might be simpler. If you expand the ability of the game to keep track of pieces as I suggested above, then a separate action listener for pieces will be appreciated.

    Comment your code so that you and we can see what it's supposed to be doing, and then come back and post it, IF you still need help.

  5. #5
    Junior Member KTJW88's Avatar
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Chess Game

    Quote Originally Posted by GregBrannon View Post
    You and your partner should learn to comment your code, especially if you want to show it to someone else to get their help. Please don't tell me "We do it after the code is done." That's pointless on so many levels.

    Please give your variables better names. What's a gridUnit? Why not chessPiece? Even better, whitePawn, blackPawn, whiteKing, blackQueen, etc. Using piece-specific names will lead you to creating a ChessPiece class - a good thing - in which the piece's name, original location, current location, move rules, possible moves, move history, etc. could be stored.

    You have a very busy actionPerformed() method without a clue given about what it's doing. Comments would be nice. A single actionPerformed() method for every possible user action may not be the best approach. Perhaps one for the menu items and another for the "chess" pieces might be simpler. If you expand the ability of the game to keep track of pieces as I suggested above, then a separate action listener for pieces will be appreciated.

    Comment your code so that you and we can see what it's supposed to be doing, and then come back and post it, IF you still need help.
    Thank you for your patience. I understand the frustration and I will bring that issue when I talk with my partner about that.

    Thanks again,

    Kevin

Similar Threads

  1. Chess Game. Code to move pieces.
    By Vas87thRD in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 12th, 2014, 03:41 AM
  2. If you had a chess-like boardgame....
    By integer9 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 2nd, 2014, 12:49 PM
  3. Help! Error with a save and load feature of a java chess game
    By davidchan in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 22nd, 2013, 08:27 AM
  4. Chess Program
    By gokuball in forum Java Theory & Questions
    Replies: 218
    Last Post: August 19th, 2013, 05:47 PM
  5. Chess game help
    By that_guy in forum Java Theory & Questions
    Replies: 3
    Last Post: December 4th, 2011, 08:42 PM