I read serveral similar questions but those answers couldnt help me with my problem:
This is a while(true) loop for a tictactoe game and should run the whole time. But it only runs once I tested it EXCEPT I type a sysout somewhere in the loop...(not in one of the if statements):
Doesn't work like this:
void winCheck() {
while(true) {
if(buttons[0].getValue() == 1 && buttons[1].getValue() == 1 && buttons[2].getValue() == 1) {
dispose();
JOptionPane.showMessageDialog(null, "Crosses win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
} else if(buttons[3].getValue() == 1 && buttons[4].getValue() == 1&& buttons[5].getValue() == 1) {
dispose();
JOptionPane.showMessageDialog(null, "Crosses win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
} else if(buttons[6].getValue() == 1 && buttons[7].getValue() == 1 && buttons[8].getValue() == 1) {
dispose();
JOptionPane.showMessageDialog(null, "Crosses win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
} else if(buttons[0].getValue() == 1 && buttons[3].getValue() == 1 && buttons[6].getValue() == 1 ) {
dispose();
JOptionPane.showMessageDialog(null, "Crosses win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
} else if(buttons[1].getValue() == 1 && buttons[4].getValue() == 1 && buttons[7].getValue() == 1) {
dispose();
JOptionPane.showMessageDialog(null, "Crosses win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
} else if(buttons[2].getValue() == 1 && buttons[5].getValue() == 1 && buttons[8].getValue() == 1 ) {
dispose();
JOptionPane.showMessageDialog(null, "Crosses win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
} else if(buttons[0].getValue() == 1 && buttons[4].getValue() == 1 && buttons[8].getValue() == 1) {
dispose();
JOptionPane.showMessageDialog(null, "Crosses win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
} else if(buttons[6].getValue() == 1 && buttons[4].getValue() == 1 && buttons[2].getValue() == 1 ) {
dispose();
JOptionPane.showMessageDialog(null, "Crosses win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
} else if(buttons[0].getValue() == 2 && buttons[1].getValue() == 2 && buttons[2].getValue() == 2) {
dispose();
JOptionPane.showMessageDialog(null, "Noughts win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
} else if(buttons[3].getValue() == 2 && buttons[4].getValue() == 2 && buttons[5].getValue() ==2 ) {
dispose();
JOptionPane.showMessageDialog(null, "Noughts win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
} else if(buttons[6].getValue() ==2 && buttons[7].getValue() == 2 && buttons[8].getValue() == 2) {
dispose();
JOptionPane.showMessageDialog(null, "Noughts win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
} else if(buttons[0].getValue() == 2 && buttons[3].getValue() == 2 && buttons[6].getValue() == 2 ) {
dispose();
JOptionPane.showMessageDialog(null, "Noughts win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
} else if(buttons[1].getValue() == 2 && buttons[4].getValue() == 2 && buttons[7].getValue() == 2 ) {
dispose();
JOptionPane.showMessageDialog(null, "Noughts win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
} else if(buttons[2].getValue() == 2 && buttons[5].getValue() == 2 && buttons[8].getValue() == 2 ) {
dispose();
JOptionPane.showMessageDialog(null, "Noughts win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
} else if(buttons[0].getValue() == 2 && buttons[4].getValue() == 2 && buttons[8].getValue() == 2 ) {
dispose();
JOptionPane.showMessageDialog(null, "Noughts win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
} else if(buttons[6].getValue() == 2 && buttons[4].getValue() == 2 && buttons[2].getValue() == 2) {
dispose();
JOptionPane.showMessageDialog(null, "Noughts win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
} else if(
(buttons[0].getValue() == 1 || buttons[0].getValue() == 2) &&
(buttons[1].getValue() == 1 || buttons[1].getValue() == 2) &&
(buttons[2].getValue() == 1 || buttons[2].getValue() == 2) &&
(buttons[3].getValue() == 1 || buttons[3].getValue() == 2) &&
(buttons[4].getValue() == 1 || buttons[4].getValue() == 2) &&
(buttons[5].getValue() == 1 || buttons[5].getValue() == 2) &&
(buttons[6].getValue() == 1 || buttons[6].getValue() == 2) &&
(buttons[7].getValue() == 1 || buttons[7].getValue() == 2) &&
(buttons[8].getValue() == 1 || buttons[8].getValue() == 2)) {
dispose();
JOptionPane.showMessageDialog(null, "Draw..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
}
}
}
but it works like this (sysout at the end) so it runs the whole time not just once:
void winCheck() {
while(true) {
if(buttons[0].getValue() == 1 && buttons[1].getValue() == 1 && buttons[2].getValue() == 1) {
dispose();
JOptionPane.showMessageDialog(null, "Crosses win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
}
else if(buttons[3].getValue() == 1 && buttons[4].getValue() == 1&& buttons[5].getValue() == 1) {
dispose();
JOptionPane.showMessageDialog(null, "Crosses win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
}
else if(buttons[6].getValue() == 1 && buttons[7].getValue() == 1 && buttons[8].getValue() == 1) {
dispose();
JOptionPane.showMessageDialog(null, "Crosses win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
}
else if(buttons[0].getValue() == 1 && buttons[3].getValue() == 1 && buttons[6].getValue() == 1 ) {
dispose();
JOptionPane.showMessageDialog(null, "Crosses win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
}
else if(buttons[1].getValue() == 1 && buttons[4].getValue() == 1 && buttons[7].getValue() == 1) {
dispose();
JOptionPane.showMessageDialog(null, "Crosses win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
}
else if(buttons[2].getValue() == 1 && buttons[5].getValue() == 1 && buttons[8].getValue() == 1 ) {
dispose();
JOptionPane.showMessageDialog(null, "Crosses win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
}
else if(buttons[0].getValue() == 1 && buttons[4].getValue() == 1 && buttons[8].getValue() == 1) {
dispose();
JOptionPane.showMessageDialog(null, "Crosses win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
}
else if(buttons[6].getValue() == 1 && buttons[4].getValue() == 1 && buttons[2].getValue() == 1 ) {
dispose();
JOptionPane.showMessageDialog(null, "Crosses win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
}
else if(buttons[0].getValue() == 2 && buttons[1].getValue() == 2 && buttons[2].getValue() == 2) {
dispose();
JOptionPane.showMessageDialog(null, "Noughts win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
}
else if(buttons[3].getValue() == 2 && buttons[4].getValue() == 2 && buttons[5].getValue() ==2 ) {
dispose();
JOptionPane.showMessageDialog(null, "Noughts win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
}
else if(buttons[6].getValue() ==2 && buttons[7].getValue() == 2 && buttons[8].getValue() == 2) {
dispose();
JOptionPane.showMessageDialog(null, "Noughts win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
}
else if(buttons[0].getValue() == 2 && buttons[3].getValue() == 2 && buttons[6].getValue() == 2 ) {
dispose();
JOptionPane.showMessageDialog(null, "Noughts win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
}
else if(buttons[1].getValue() == 2 && buttons[4].getValue() == 2 && buttons[7].getValue() == 2 ) {
dispose();
JOptionPane.showMessageDialog(null, "Noughts win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
}
else if(buttons[2].getValue() == 2 && buttons[5].getValue() == 2 && buttons[8].getValue() == 2 ) {
dispose();
JOptionPane.showMessageDialog(null, "Noughts win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
}
else if(buttons[0].getValue() == 2 && buttons[4].getValue() == 2 && buttons[8].getValue() == 2 ) {
dispose();
JOptionPane.showMessageDialog(null, "Noughts win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
}
else if(buttons[6].getValue() == 2 && buttons[4].getValue() == 2 && buttons[2].getValue() == 2) {
dispose();
JOptionPane.showMessageDialog(null, "Noughts win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
}
else if(
(buttons[0].getValue() == 1 || buttons[0].getValue() == 2) &&
(buttons[1].getValue() == 1 || buttons[1].getValue() == 2) &&
(