I am writing a Reversi application. I implemented the turns manager class, but I have a little problem in the while loop.
This is my snippet:
while (!table.isFull() || passFlag != 2) {
if (player1.isActive()) {
for (int i = 0; i < table.getSize(); i++) {
for (int j = 0; j < table.getSize(); j++) {
table.getField(i, j).addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof Field) {
((Field) e.getSource()).changeToBlack();
}
}
});
}
}
}
if (player2.isActive()) {
for (int i = 0; i < table.getSize(); i++) {
for (int j = 0; j < table.getSize(); j++) {
table.getField(i, j).addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof Field) {
((Field) e.getSource()).changeToWhite();
}
}
});
}
}
}
sentinel.changeActivePlayer(player1, player2);
The table is a grid of buttons, and the fields are the buttons. The loop does not wait for the player interaction. How can I implement the code so that it waits for the user's mouse click?
This is the full code of this class
package Core;
import GUILayer.Field;
import GUILayer.MainFrame;
import elements.Player;
import elements.Table;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TurnManager {
int passFlag = 0;
int TurnFlag = 0;
Sentinel sentinel = new Sentinel();
public TurnManager() {
}
public void manage(MainFrame mainframe, Table table, Player player1, Player player2) {
while (!table.isFull() || passFlag != 2) {
if (player1.isActive()) {
for (int i = 0; i < table.getSize(); i++) {
for (int j = 0; j < table.getSize(); j++) {
table.getField(i, j).addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof Field) {
((Field) e.getSource()).changeToBlack();
}
}
});
}
}
}
if (player2.isActive()) {
for (int i = 0; i < table.getSize(); i++) {
for (int j = 0; j < table.getSize(); j++) {
table.getField(i, j).addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof Field) {
((Field) e.getSource()).changeToWhite();
}
}
});
}
}
}
sentinel.changeActivePlayer(player1, player2);
}
}
}