hysteriaistic
hysteriaistic

Reputation: 85

How to take a second actionPerformed for moving a chess piece?

I am making a chess game and need to figure out how to move the pieces. I have my pieces stored in an array squaresGrid[][] and I want to use the method moveTo to move the pieces. Currently this method simply marks a piece selected but I need it to take a second mouse click to choose the square to move the selected piece to but am not sure how best to do this.

    public void actionPerformed(ActionEvent e)
    {
        for(int x = 0; x < 8; x++)
        {
            for(int y = 0; y < 8; y++)
            {
                if(e.getSource() == squaresGrid[x][y])
                {
                    moveTo(e, squaresGrid[x][y]);
                }
            }
        }   
    }

    public void moveTo(ActionEvent e, JButton clicked)
    {
        clicked.setIcon(selected);
    }

Upvotes: 0

Views: 628

Answers (2)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

You don't need a second ActionListener or actionPerformed method, but rather you need a state-ful ActionListener, one that knows whether the button push represents the first push or the 2nd. A boolean variable could be all that is required for this. Another option is to use a variable to represent the first pushed location, set it equal to null initially and then set it equal to the position on the first push. On the 2nd push check if it is null or non-null and if non-null, the button push represents the 2nd push. Then set it back to null.

For example

public void actionPerformed(ActionEvent e) {
    for(int x = 0; x < 8; x++) {
        for(int y = 0; y < 8; y++) {
            if(e.getSource() == squaresGrid[x][y]) {
                if (gridLocation == null) {
                    // class to hold x and y location
                    gridLocation = new GridLocation(x, y);
                }  else {
                    // use gridLocation here
                    int firstX = gridLocation.getX();
                    int firstY = gridLocation.getY();
                    moveTo(e, x, y, firstX, firstY);
                    gridLocation = null;
                }
            }
        }
    }  
}

Upvotes: 3

RealSkeptic
RealSkeptic

Reputation: 34628

You don't do a "second actionPerformed". What you do is keep around state, and when a click happens, look at the state, and decide what the action should be.

For example, keep around a field called currentlySelected, pointing to the currently selected square (containing its coordinates, for example).

In the actionPerformed, when you receive a click, you look at currentlySelected.

  • If it is null, it means you are supposed to select the clicked square and put it in currentlySelected.
  • If it is not null, and the current click is in the same square, the user probably wants to de-select it. De-select and clear (put null) in currentlySelected.
  • If it is not null and not the same square, it means that you have to check if the move is legal. If it is legal, you can do the move, and clear currentlySelected. If it is not legal, you do what you think is the proper thing to do: perhaps de-select the original place and select the new one. Or just de-select and tell the user the move is not legal. Or keep it selected.

Remember to always clear your currentlySelected in the appropriate situations.

Upvotes: 4

Related Questions