I need the program to end the loop after player1 has won but the program continues to run until the loop is done i was wondering i know i need a break statement but i do not know where to put statement at.Any help is appreciated.
import javax.swing.JOptionPane;
public class ticTacToeGame {
/**
* @param args
*/
public static void main(String[] args) {
int response;
String player1 = null;
String player2 = null;
player1 = createUser1(player1);
player2 = creatUser2(player2);
do{
int i = 0;
do{
String boardCreator = JOptionPane.showInputDialog(null, "Please enter the size of your game board?",
"", JOptionPane.QUESTION_MESSAGE);
i = Integer.parseInt(boardCreator);
if (i < 0)
{
JOptionPane.showMessageDialog(null, "Sorry! Please enter a positive number. " +
"Please enter again.", "", JOptionPane.ERROR_MESSAGE);
}
if (1 == i)
{
JOptionPane.showMessageDialog(null, "Sorry! The size of the board cannot be less than 2 x 2. " +
"Please enter again.", "", JOptionPane.ERROR_MESSAGE);
}
}while (i < 2);
char [][] board = new char [i][i];
int arrayLength = i * i;
String output = "";
for(int u = 0; u < arrayLength; u = u + 2 ){
String userRowSelection = JOptionPane.showInputDialog(null, "" + player1 + " (X): " + "Please enter the row#",
"", JOptionPane.QUESTION_MESSAGE);
int j = Integer.parseInt(userRowSelection);
String userColumnSelection = JOptionPane.showInputDialog(null, "" + player1 + " (X): " + "Please enter the column#",
"", JOptionPane.QUESTION_MESSAGE);
int k = Integer.parseInt(userColumnSelection);
board[j][k] = (char)88;
String userRowSelection2 = JOptionPane.showInputDialog(null, "" + player2 + " (O): " + "Please enter the row#",
"", JOptionPane.QUESTION_MESSAGE);
int p = Integer.parseInt(userRowSelection2);
String userColumnSelection2 = JOptionPane.showInputDialog(null, "" + player2 + " (O): " + "Please enter the column#",
"", JOptionPane.QUESTION_MESSAGE);
int o = Integer.parseInt(userColumnSelection2);
board[p][o] = (char)79;
}
output += createTicTacToeBoard(board);
output += checkRow(board, i, player1);
displayTicTacToeBoard(board, output);
response = JOptionPane.showConfirmDialog(null, "Play another game?",
"", JOptionPane.YES_NO_OPTION);
}while (response != 1);
}
private static String checkRow(char[][] board, int i, String player1) {
String output = "";
for (int row = 0; row < i; row++){
int column = 0; boolean allOne = true;
while(column < i && allOne){
if (board[row][column] != (char)88)
allOne = false;
else
column++;
};
if (allOne){
output += ("" + (player1) + ", You win\n");
}
}
return output;
}
private static String creatUser2(String player2) {
do{
player2 = JOptionPane.showInputDialog(null, "Please enter your name, Player 2: ",
"", JOptionPane.QUESTION_MESSAGE);
if(player2.isEmpty()){
JOptionPane.showMessageDialog(null, "Sorry! Your name is empty. " +
"Please enter again.", "", JOptionPane.ERROR_MESSAGE);
}
}while(player2.isEmpty());
return player2;
}
private static String createUser1(String player1) {
do{
player1 = JOptionPane.showInputDialog(null, "Please enter your name, Player 1: ",
"", JOptionPane.QUESTION_MESSAGE);
if(player1.isEmpty()){
JOptionPane.showMessageDialog(null, "Sorry! Your name is empty. " +
"Please enter again.", "", JOptionPane.ERROR_MESSAGE);
}
}while(player1.isEmpty())
;
return player1;
}
private static void displayTicTacToeBoard(char[][] board, String output) {
JOptionPane.showMessageDialog(null, output, "", JOptionPane.INFORMATION_MESSAGE);
}
private static String createTicTacToeBoard(char[][] board) {
String output = "";
for (int row = 0; row < board.length; row++){
for (int column = 0; column < board[row].length; column++)
output = output + ( board [row][column]);
output = output + ("\n");
}
return output;
}
}