2

I am trying to make a simple chess GUI, that consists only in a gridLayout where each square is made of a button, each button has an ActionListener and works independantly of the actual game, only "printing out" what is happening in the Board class.

private JButton[][] squares = new JButton[8][8];
private Color darkcolor = Color.decode("#D2B48C");
private Color lightcolor = Color.decode("#A0522D");
public ChessGUI(){
    super("Chess");
    contents = getContentPane();
    contents.setLayout(new GridLayout(8,8));

    ButtonHandler buttonHandler = new ButtonHandler();

    for(int i = 0; i< 8; i++){
        for(int j = 0;j < 8;j++){
            squares[i][j] = new JButton();
            if((i+j) % 2 != 0){
                squares[i][j].setBackground(lightcolor);
            }else{
                squares[i][j].setBackground(darkcolor);
            }
            contents.add(squares[i][j]);
            squares[i][j].addActionListener(buttonHandler);
            squares[i][j].setSize(75, 75);

        }
    }
    setSize(600, 600);
    setResizable(true);
    setLocationRelativeTo(null);
    setVisible(true);
}
private class ButtonHandler implements ActionListener{
    public void actionPerformed(ActionEvent e){
        Object source = e.getSource();
        for(int i = 0;i< 8; i++){
            for(int j = 0;j < 8;j++){
                if(source == squares[i][j]){
                    //Pass set of coordinates to game.Move(...)
                    return;
                }
            }
        }
    }
}

I need to pass 2 sets of coordinates, the "from" and the "to" to the game.Move(...) function(function that aplies game movement logic), where each set of coordinates is given by a click in a button.

How should i handle the fact that i need to wait for the user to make 2 clicks before calling the game.Move(...) function? It seems it can only pass 1 set of coordinates at a time.

Any help would be apricieated.

MiguelD
  • 409
  • 1
  • 7
  • 16

1 Answers1

2

In game object you should probably have field variables that will store the from and to coordinates instead of passing them into game.move function. You can make a counter that starts at 0. When it is 0, it will process the "from" click (set the "from" variable in Game to the coordinate that was clicked), and when it is 1, it will process the "to" click (set "to" variable in Game), call move, and reset counter back to 0.

hoobit
  • 36
  • 2
  • 1
    You'd also probably would want to implement a way to check if a move is valid. If so, set the field variable back to 1. If they wish to deselect, set field variable back to 0. – Haris Nadeem Apr 21 '18 at 19:15
  • but how can i call move only after i made the "clicks"? i tried to implement what you suggested like this: if(clicked == 0){ game.setFromX(i); game.setFromY(j); clicked++; }else if(clicked == 1){ game.setToX(i); game.setToY(j); clicked = 0; } – MiguelD Apr 21 '18 at 19:23
  • The logic for handling the game might get more complex as you keep developing. Maybe you need a new class that checks the move for validity etc. I would implement a "GameManager" as a listener (would wait for "move events" (For example MoveStarted, MoveEnded etc and the buttonListener would only fire those events. Some sort of callback could then update the UI – Eirini Graonidou Apr 21 '18 at 19:30
  • @Eirini Graonidou So when pressing a button for the "from" click it would instead call "MoveStarted" and the "to" click would call "MoveEnded", but what exactly would those functions do? – MiguelD Apr 21 '18 at 19:43
  • 1
    https://stackoverflow.com/questions/6270132/create-a-custom-event-in-java Please take a look at this link. The "Initializer" would be in your case the GameManager and the "HelloListener" would be the "MoveListener":) Instead of sayHallo(), you could do something like gameManager.executeMove(moveStarted). Then the MoveListener should contain logic, as already suggested to handle the move. For example check if the event is of type "MoveEnded" or "MoveStarted"(Those would be event classes with meaningfull information) – Eirini Graonidou Apr 21 '18 at 20:24