Hi lilLB,
Just write a method that will pass as arguments the x and y of the last button pressed, that will browse the array of buttons (horizontally, vertically and diagonally) from the passed location and that will return a boolean set to true if the player won.
There's a
getText method on the
JButton you may use to compare the button letter.
Also, when you initialize the game buttons, you may use the
setName method to give each button a specific name that may be used to determine its location on the board.
Example :
Code:
for (int i=0; i<boardSize; i++) {
for (int j=0; j<boardSize; j++){
buttons[i][j] = new JButton();
boardPanel.add(buttons[i][j]);
buttons[i][j].addActionListener(this);
buttons[i][j].setName(String.valueOf((i*10)+j);
}
} In the
actionPerformed method, you retrieve the x and y of the pressed button like this :
Code:
JButton pressedButton = (JButton)a.getSource();
pressedButton.setText(letter);
pressedButton.setEnabled(false);
int value = Integer.parseInt(pressedButton.getName());
int x = value / 10;
int y = value % 10;
