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;
    }

}

Edited 3 Years Ago by Dani: Formatting fixed

Well a break statement needs to be within a loop... so as stultuske said, find the loop that checks if the player has won, and if he has put a break;

Edited 3 Years Ago by pyTony: unnecessary quote removed

so you are saying it should be apart of the checkRow method?

I'd think so right in this:

if (allOne){
output += ("" + (player1) + ", You win\n");
break;
}

Edited 4 Years Ago by DavidKroukamp: n/a

where in your code do you check whether or not the player has won?

the program checks to see if player wins in the checkRow method

I'd think so right in this:

if (allOne){
output += ("" + (player1) + ", You win\n");
break;
}

I tried this but it did not end the program

I tried this but it did not end the program

well look at you if statement it will only execute when allOne is true... where in your code besides intialization is allOne =true? you'd have to make your code set allOne equal to true so that the if statement may execute, and to be honest i tried testing the code, but without any visual reference i dont know what input would be a win... perhaps you could give that, so i can test when a win occurs

Edited 4 Years Ago by DavidKroukamp: n/a

well look at you if statement it will only execute when allOne is true... where in your code besides intialization is allOne =true? you'd have to make your code set allOne equal to true so that the if statement may execute, and to be honest i tried testing the code, but without any visual reference i dont know what input would be a win... perhaps you could give that, so i can test when a win occurs

A win would be putting the all X's in one row

A win would be putting the all X's in one row

yes but i cant see it visually and im really lazy to go think of thr co-ords but what about setting allOne to true?... I'd initialize the boolean value allOne to false, then change your while statement to loop for while(column < i && !allOne). Then when all x's or o's are in a row set this allOne value to true thereby stopping the while loop and 'breaking' it will then go to the if statement in which the value of allOne is true and thus the if statement will be executed... add your output like you did but after put a return output; but this is all taking it that you have a correct algorithm for checking when a win is made... use some println's to understand whats happening with your codes inner working... what happens once the while statement quits and the if statement is executed and returns the output?...whats happening in the for statement that surrounds your while loop etc..

Edited 4 Years Ago by DavidKroukamp: n/a

This article has been dead for over six months. Start a new discussion instead.