0

The code doesn't make sense because i and j are not defined.

Here's a start in pseudo code:
is this the first click? (selected square == null)
If so, is the clicked square occupied?
If not occupied, print error message and return
save the clicked square in selected square
return
if not the first click then
check if clicked square is occupied,
if occupied print message and return
move piece from selected square to clicked square
clear selected square

0

My partner has come up with a radically different code. If I were to post it, could somone do their best to dissect it and see what makes it tick and what will make it work?

0
 import javax.swing.JOptionPane;
   import javax.swing.*;
   import java.awt.*;
   import java.awt.event.*;
   import java.util.Scanner;
   public class CheckerBoard extends JFrame
   {
      Scanner scan = new Scanner(System.in);
      public static final int SIDE = 8;
      private Container contents;
      private JButton [][] squares;
      Icon redPiece = new ImageIcon("RedPiece.png");
      Icon blackPiece = new ImageIcon("BlackPiece.png");

      public CheckerBoard( )
      {
         super( "Let's Play Checkers" );
         contents = getContentPane( );

      // set layout to an 8-by-8 Grid
         contents.setLayout( new GridLayout( SIDE, SIDE ) );

         squares = new JButton[SIDE][SIDE];

         ButtonHandler bh = new ButtonHandler( );

         for ( int i = 0; i < SIDE; i++ )
         {
            for ( int j = 0; j < SIDE; j++ )
            {
            // instantiate JButton array
               squares[i][j] = new JButton( );

            // make every other square red
               if ( ( i + j ) % 2 == 0  )
                  squares[i][j].setBackground( Color.RED );

               if ( ( i + j ) % 2 != 0  )   
                  squares[i][j].setBackground( Color.BLACK );

               if(squares[i][j].getBackground()==Color.black && i>=5)
               {
                  squares[i][j].setIcon(blackPiece);
                  squares[i][j].setName("B");
               }
               if(squares[i][j].getBackground()==Color.black && i<3)
                  squares[i][j].setIcon(redPiece);      
            // add the JButton
               contents.add( squares[i][j] );

            // register listener on button
               squares[i][j].addActionListener( bh );
            }
         }

         setSize( 400, 400 );
         setVisible( true );
      }
      public class getCorrijxy
      { 
         int i;
         int j;
         int x;
         int y; 
         public int getX()
         {
            return x;
         } 
         public int getY()
         {
            return y;
         }
         public int getI()
         {
            return i;
         }
         public int getJ()
         {
            return j;
         }      
      } 
      public class movex extends getCorrijxy 
      {

         public int movePiece()
         {  
            int x = x.getX();
            if( x==1)
            {

               for(int k=0;k<2;)
               {
                  i = i + x;
                  k++;
               }

            }
            return i;
         }

      }
      public class movey extends movex
      {
         int j;
         public int movePiece()
         {  
            int y = super.getY();
            if( y==1)
            {

               for(int k=0;k<2;)
               {
                  j = j + y;
                  k++;
               }

            }
            return j;
         }

      }
      public class newArray extends movey
      {
         public int nextArray()
         {
            i = super.getI();
            j = super.getJ();
            int [][] Array2 = new int[i][j];
            return Array2[i][j];
         }

      }    
      private class ButtonHandler extends newArray implements ActionListener
      {
         public void actionPerformed( ActionEvent ae )
         {

            for ( int i = 0; i < SIDE; i++ )
            {
               for ( int j = 0; j < SIDE; j++ )
               {


                  if (ae.getSource()==squares[i][j] && squares[i][j].getIcon()==blackPiece)
                  {
                     String x = JOptionPane.showInputDialog(null , "enter an x value");
                     if( x == "1")
                     {

                        String number = "1";
                        int newNumber = Integer.parseInt(number);
                     }
                     if( x == "-1")
                     {

                        String number = "-1";
                        int newNumber = Integer.parseInt(number);
                     }
                     String y = JOptionPane.showInputDialog(null , "enter a y value");


                   squares[i][j] = super.newArray();                            

                  }

               }

            }

         }
      }

      public static void main( String [] args )
      {
         CheckerBoard myGame = new CheckerBoard( );
         myGame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      }
   }

There are issues in the button handler.

0

The app is GUI and should not prompt the user to enter board locations. Bad idea. It should all be done with mouse clicks.

what errors to fix.

Fix all of them.

Edited by NormR1

0

We are having trouble about how to fix the error on line 162. it keeps saying cannot find symbol for newArray().

0

Where is the method: newArray() defined? The compiler can not find it. Check the spelling

0

What is "right above the button handler class"? I see a class with that name but no method. The code with the error is calling a method.

0

Do you know the difference between calling a method and creating a new instance of a class?
You don't call a class. You only call methods. The exception is when you create a new instance of a class, then you call that class's constructor.

Edited by NormR1

0

nextArray() is a method in the class newArray. nextArray() is not a constructor.

Edited by NormR1

0

I have no idea what the lines of code you are working on is supposed to do.
Can you describe what you are trying to do? What do you want the method to do when it is called?
What do you want returned by the method?

0

This is our peniultimate code

   import javax.swing.JOptionPane;
   import javax.swing.*;
   import java.awt.*;
   import java.awt.event.*;
   import java.util.Scanner;
   public class CheckerBoard extends JFrame
   {
      Scanner scan = new Scanner(System.in);
      public static final int SIDE = 8;
      private Container contents;
      private JButton [][] squares;
      private JButton [][] Array2;
      Icon redPiece = new ImageIcon("RedPiece.png");
      Icon blackPiece = new ImageIcon("BlackPiece.png");
        Icon redKing = new ImageIcon("Coin.png");
        Icon blackKing = new ImageIcon("Ring.png");


      public CheckerBoard( )
      {
         super( "Let's Play Checkers" );
         contents = getContentPane( );

      // set layout to an 8-by-8 Grid
         contents.setLayout( new GridLayout( SIDE, SIDE ) );

         squares = new JButton[SIDE][SIDE];

         ButtonHandler bh = new ButtonHandler( );

         for ( int i = 0; i < SIDE; i++ )
         {
            for ( int j = 0; j < SIDE; j++ )
            {
            // instantiate JButton array
               squares[i][j] = new JButton( );



            // make every other square red
               if ( ( i + j ) % 2 == 0  )
                  squares[i][j].setBackground( Color.RED );

               if ( ( i + j ) % 2 != 0  )   
                  squares[i][j].setBackground( Color.BLACK );

               if(squares[i][j].getBackground()==Color.black && i>=5)
               {
                  squares[i][j].setIcon(blackPiece);
                  squares[i][j].setName("B");
               }
               if(squares[i][j].getBackground()==Color.black && i<3)
                  squares[i][j].setIcon(redPiece);      
            // add the JButton
               contents.add( squares[i][j] );

            // register listener on button
               squares[i][j].addActionListener( bh );
            }
         }
         setSize( 400, 400 );
         setVisible( true );
      }
      public class getCorrijxy 
      { 
         int x;
         int y;
         int i;
         int j; 
         public int getX()
         {
            return x;           
         } 
         public int getY()
         {
            return y;
         }
         public int getI()
         {
            return i;
         }
         public int getJ()
         {
            return j;
         }  
      } 
      public class movex extends getCorrijxy 
      {

         public int movePiece()
         {  
            int i = super.getI();
            int x = super.getX();
            if( x==1)
            {

               for(int k=0;k<2;)
               {
                  i = i + x;
                  k++;
               }

            }
            return i;
         }

      }
      public class movey extends movex
      {

         public int movePiece()
         {  
            int j = super.getJ();
            int y = super.getY();
            if( y==1)
            {

               for(int k=0;k<2;)
               {
                  j = j + y;
                  k++;
               }

            }
            return j;
         }

      }

      private class ButtonHandler  implements ActionListener
      {
         public void actionPerformed( ActionEvent ae )
         {

            for ( int i = 0; i < SIDE; i++ )
            {
               for ( int j = 0; j < SIDE; j++ )
               {

                  int q = 0;
                  int z  = 0;                                                            
                  if (ae.getSource()==squares[i][j] && squares[i][j].getIcon()==blackPiece)
                  {



                     String x = JOptionPane.showInputDialog(null , "enter an x value");
                     String y = JOptionPane.showInputDialog(null , "enter a y value");
                     if(x != null && y != null)
                     {               
                        q = Integer.parseInt(x);
                        z = Integer.parseInt(y);
                     }
                     if( ((q + i <= 8) && (z+j) <= 8) && ((q+ i > -1) && (z+j) > -1) && (q < 2 && q > -2) && (z < 2 && z > -2) && (q != 0 && z != 0) && squares[q+i][z+j] != blackPiece)
                     {     
                        if(squares[q+i][z+j].getIcon() == redPiece && squares[q+q+i][z+z+j].getIcon() == null && squares[i+q][j+z].getIcon()==null)
                        {
                           Array2 = new JButton[i+q+q][j+z+z];                  
                           JButton temp = squares[i][j];                        
                           squares[i][j].setIcon(null);
                           squares[q+q+i][z+z+j].setIcon(blackPiece);
                           squares[q+i][z+j].setIcon(null);
                        }
                                else if(squares[q+i][z+j].getIcon() == null && i==0)
                                {
                           Array2 = new JButton[i+q][j+z];                  
                           JButton temp = squares[i][j];
                           squares[i][j].setIcon(null);
                           squares[q+i][z+j].setIcon(blackKing);                                

                                }
                        else
                        {       
                           Array2 = new JButton[i+q][j+z];                  
                           JButton temp = squares[i][j];
                           squares[i][j].setIcon(null);
                           squares[q+i][z+j].setIcon(blackPiece); 
                        } 
                     }           





                  }
                  if (ae.getSource()==squares[i][j] && squares[i][j].getIcon()==redPiece)
                  {



                     String x = JOptionPane.showInputDialog(null , "enter an x value");
                     String y = JOptionPane.showInputDialog(null , "enter a y value");
                     if(x != null && y != null)
                     {               
                        q = Integer.parseInt(x);
                        z = Integer.parseInt(y);
                     }
                     if( ((q + i < 8) && (z+j) < 8) && ((q+ i > -1) && (z+j) > -1) && (q < 2 && q > -2) && (z < 2 && z > -2) && (q != 0 && z != 0) && squares[q+i][z+j] != redPiece)
                     {     
                        if(squares[q+i][z+j].getIcon() == blackPiece && squares[q+q+i][z+z+j].getIcon() == null)
                        {
                           Array2 = new JButton[i+q+q][j+z+z];                  
                           JButton temp = squares[i][j];
                           squares[i][j].setIcon(null);
                           squares[q+q+i][z+z+j].setIcon(redPiece);
                           squares[q+i][z+j].setIcon(null);
                        }
                        else
                        {       
                           Array2 = new JButton[i+q][j+z];                  
                           JButton temp = squares[i][j];
                           squares[i][j].setIcon(null);
                           squares[q+i][z+j].setIcon(redPiece); 
                        } 
                     }           





                  }     
               }

            }

         }
      }

      public static void main( String [] args )
      {
         CheckerBoard myGame = new CheckerBoard( );
         myGame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      }
   }

This moves the pieces, but now we are trying to program the king. Any suggestions? Sorry if the code seems long and blocky.

0

The prompting for x and y is terrible. There is no hint what to enter. I enter 1 and 2 and nothing happens.

0

My mistake. this is the latest code.

 import javax.swing.JOptionPane;
   import javax.swing.*;
   import java.awt.*;
   import java.awt.event.*;
   import java.util.Scanner;
   public class CheckerBoard extends JFrame
   {
      Scanner scan = new Scanner(System.in);
      public static final int SIDE = 8;
      private Container contents;
      private JButton [][] squares;
      private JButton [][] Array2;
      Icon redPiece = new ImageIcon("RedPiece.png");
      Icon blackPiece = new ImageIcon("BlackPiece.png");
        Icon redKing = new ImageIcon("Coin.png");
        Icon blackKing = new ImageIcon("Ring.png");


      public CheckerBoard( )
      {
         super( "Let's Play Checkers" );
         contents = getContentPane( );

      // set layout to an 8-by-8 Grid
         contents.setLayout( new GridLayout( SIDE, SIDE ) );

         squares = new JButton[SIDE][SIDE];

         ButtonHandler bh = new ButtonHandler( );

         for ( int i = 0; i < SIDE; i++ )
         {
            for ( int j = 0; j < SIDE; j++ )
            {
            // instantiate JButton array
               squares[i][j] = new JButton( );



            // make every other square red
               if ( ( i + j ) % 2 == 0  )
                  squares[i][j].setBackground( Color.RED );

               if ( ( i + j ) % 2 != 0  )   
                  squares[i][j].setBackground( Color.BLACK );

               if(squares[i][j].getBackground()==Color.black && i>=5)
               {
                  squares[i][j].setIcon(blackPiece);
                  squares[i][j].setName("B");
               }
               if(squares[i][j].getBackground()==Color.black && i<3)
                  squares[i][j].setIcon(redPiece);      
            // add the JButton
               contents.add( squares[i][j] );

            // register listener on button
               squares[i][j].addActionListener( bh );
            }
         }
         setSize( 400, 400 );
         setVisible( true );
      }
      public class getCorrijxy 
      { 
         int x;
         int y;
         int i;
         int j; 
         public int getX()
         {
            return x;           
         } 
         public int getY()
         {
            return y;
         }
         public int getI()
         {
            return i;
         }
         public int getJ()
         {
            return j;
         }  
      } 
      public class movex extends getCorrijxy 
      {

         public int movePiece()
         {  
            int i = super.getI();
            int x = super.getX();
            if( x==1)
            {

               for(int k=0;k<2;)
               {
                  i = i + x;
                  k++;
               }

            }
            return i;
         }

      }
      public class movey extends movex
      {

         public int movePiece()
         {  
            int j = super.getJ();
            int y = super.getY();
            if( y==1)
            {

               for(int k=0;k<2;)
               {
                  j = j + y;
                  k++;
               }

            }
            return j;
         }

      }

      private class ButtonHandler  implements ActionListener
      {
         public void actionPerformed( ActionEvent ae )
         {

            for ( int i = 0; i < SIDE; i++ )
            {
               for ( int j = 0; j < SIDE; j++ )
               {

                  int q = 0;
                  int z  = 0;                                                            
                  if (ae.getSource()==squares[i][j] && squares[i][j].getIcon()==blackPiece)
                  {



                     String x = JOptionPane.showInputDialog(null , "enter an x value");
                     String y = JOptionPane.showInputDialog(null , "enter a y value");
                     if(x != null && y != null)
                     {               
                        q = Integer.parseInt(x);
                        z = Integer.parseInt(y);
                     }
                     if( ((q + i <= 8) && (z+j) <= 8) && ((q+ i > -1) && (z+j) > -1) && (q < 2 && q > -2) && (z < 2 && z > -2) && (q != 0 && z != 0) && squares[q+i][z+j] != blackPiece)
                     {     
                        if(squares[q+i][z+j].getIcon() == redPiece && squares[q+q+i][z+z+j].getIcon() == null && squares[i+q][j+z].getIcon()==null)
                        {
                           Array2 = new JButton[i+q+q][j+z+z];                  
                           JButton temp = squares[i][j];                        
                           squares[i][j].setIcon(null);
                           squares[q+q+i][z+z+j].setIcon(blackPiece);
                           squares[q+i][z+j].setIcon(null);
                        }
                                else if(squares[q+i][z+j].getIcon() == null && i==0)
                                {
                           Array2 = new JButton[i+q][j+z];                  
                           JButton temp = squares[i][j];
                           squares[i][j].setIcon(null);
                           squares[q+i][z+j].setIcon(blackKing);                                

                                }
                        else
                        {       
                           Array2 = new JButton[i+q][j+z];                  
                           JButton temp = squares[i][j];
                           squares[i][j].setIcon(null);
                           squares[q+i][z+j].setIcon(blackPiece); 
                        } 
                     }           





                  }
                  if (ae.getSource()==squares[i][j] && squares[i][j].getIcon()==redPiece)
                  {



                     String x = JOptionPane.showInputDialog(null , "enter an x value");
                     String y = JOptionPane.showInputDialog(null , "enter a y value");
                     if(x != null && y != null)
                     {               
                        q = Integer.parseInt(x);
                        z = Integer.parseInt(y);
                     }
                     if( ((q + i < 8) && (z+j) < 8) && ((q+ i > -1) && (z+j) > -1) && (q < 2 && q > -2) && (z < 2 && z > -2) && (q != 0 && z != 0) && squares[q+i][z+j] != redPiece)
                     {     
                        if(squares[q+i][z+j].getIcon() == blackPiece && squares[q+q+i][z+z+j].getIcon() == null)
                        {
                           Array2 = new JButton[i+q+q][j+z+z];                  
                           JButton temp = squares[i][j];
                           squares[i][j].setIcon(null);
                           squares[q+q+i][z+z+j].setIcon(redPiece);
                           squares[q+i][z+j].setIcon(null);
                        }
                        else
                        {       
                           Array2 = new JButton[i+q][j+z];                  
                           JButton temp = squares[i][j];
                           squares[i][j].setIcon(null);
                           squares[q+i][z+j].setIcon(redPiece); 
                        } 
                     }           





                  }     
               }

            }

         }
      }

      public static void main( String [] args )
      {
         CheckerBoard myGame = new CheckerBoard( );
         myGame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      }
   }
0

The code still has prompts to the user instead of using mouse clicks.. How does a user know what to enter?
Why are there nested loops in the actionListener? There should be a method that is called with a square as argument that returns the x,y location of the square in a simple object like a Point.
What do the movey and movex classes do?

0

Got it done. 2 player game. Still a few bugs but much better than before. all pieces are moving using the coordinate system, kings moving as well, along with correct jumping.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.