JimiiBee
JimiiBee

Reputation: 159

(ADDED) Noughts and Crosses game. While loop

        for(int i = 0; i < 3; i++){
            for(int j = 0; j < 3; j++){
                visBoard[i][j] = "[ ]";
                board[i][j] = 0;
                check[i][j] = false;
            }
        }for(int i = 0; i < 3; i++){
            for(int j = 0; j < 3; j++){
                System.out.print(visBoard[i][j]);
            }System.out.print("\n");
        }

        //Getting Names
        System.out.println("Player 1 - Enter your name");
        play1 = sc.nextLine();
        System.out.println("Player 2 - Enter your name");
        play2 = sc.nextLine();
        //
        moves = 0;
        symbol = " X ";
        do{
            do{
                //Get Coords
                System.out.println("X Coordinate");
                xcoord = sc.nextInt() -1;
                System.out.println("Y Coordinate");
                ycoord = sc.nextInt() -1;

                if(check[xcoord][ycoord] == true){
                    System.out.println("Not a valid move!");
                }
            }while(check[xcoord][ycoord] == true);

            //Making move
            check[xcoord][ycoord] = true;
            visBoard[xcoord][ycoord] = symbol;
            if(symbol.equals(" X ")){
                board[xcoord][ycoord] = 1;
            }else if(symbol.equals(" O ")){
                board[xcoord][ycoord] = 5;
            }else{
                System.out.println("You've messed up James");
            }

            for(int i = 0; i < 3; i++){
                for(int j = 0; j < 3; j++){
                    System.out.print(visBoard[i][j]);
                }System.out.print("\n");
            }

            //Check if game has won
            //columns
            total = 0;
            for(int i = 0; i < 3; i++){
                for(int j = 0; j < 3; j++){
                    total = total + board[j][i];
                }if(total == 15 || total == 3){
                    gamewon = true;

                }
            }total = 0;
            //rows
            for(int i = 0; i < 3; i++){
                for(int j = 0; j < 3; j++){
                    total = total + board[i][j];
                }if(total == 15 || total == 3){
                    gamewon = true;

                }
            }total = 0;
            //diagonals
            for(int i = 0; i < 3; i++){
                total = total + board[i][i];
            }if(total == 15 || total == 3){
                gamewon = true;

            }total = 0;
            diag = 2;
            for(int i = 0; i < 3; i++){
                total = total + board[i][diag];
                diag--;
            }if(total == 15 || total == 3){
                gamewon = true;

            }
            moves++;
            if(gamewon == false){
                if(moves == 9){
                    System.out.println("Game has been drawn! No one wins!");
                }else{
                    mod = moves % 2;
                    if(mod == 0){
                        symbol = " X ";
                    }else{
                        symbol = " O ";
                    }
                }
            }
        }while(gamewon == false || moves != 9);

        if(gamewon == true){
            if(symbol.equals(" X ")){
                System.out.println("Winner is "+play1);
            }else{
                System.out.println("Winner is "+play2);
            }
        }else{
            System.out.println("Game is drawn");
        }

    }
}

This is a further question from a previous question I had. This game won't end until moves reaches 9 even though the while loop should stop once someone has won. The boolean will turn true, but it will continue to loop.

How do I fix this issue with keeping the while condition, and possibly without using breaks?

Upvotes: 0

Views: 158

Answers (2)

Gus
Gus

Reputation: 6871

You need an and not an or

while(gamewon == false && moves != 9);

Reading that to yourself it says while there is no winner and we are not at move 9. However it's usually better form to code your loops to check that you haven't exceeded a bound rather than you have hit the bound exactly, and it is also nicer to simply test the boolean directly so the following is more stylish:

while(!gamewon && moves < 9);

Upvotes: 2

Drifter64
Drifter64

Reputation: 1123

while(gamewon == false || moves != 9)....

This tells the loop to execute while game isnt won, or moves are not 9. For it to end, BOTH conditions need to change, the game needs to be ended AND moves needs to be 9.

Change your || operator to &&. This way the game will keep going while the game is not won AND the moves is not 9. It seems a bit strange but if you can follow the logic, you'll see that you need the AND operator.

Therefore, you're looking for:

while(gamewon == false && moves != 9)

Upvotes: 0

Related Questions