java - (添加)圈与叉游戏。 While 循环

标签 java arrays while-loop do-while

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

    }
}

这是我之前提出的问题的进一步问题。尽管 while 循环应该在有人获胜后停止,但直到步数达到 9 为止这个游戏不会结束。 boolean 值将变为 true,但它将继续循环。

如何在保持 while 条件的情况下解决此问题,并且可能不使用中断?

最佳答案

你需要一个而不是一个或

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

你自己读一下,它说虽然没有赢家,而且我们还没有进入第 9 步。但是,通常更好的形式是对循环进行编码以检查您没有超出界限,而不是完全达到界限,直接测试 boolean 值也更好,因此以下内容更时尚:

while(!gamewon && moves < 9);

关于java - (添加)圈与叉游戏。 While 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21173297/

相关文章:

c++使用ptr作为数组基础

java - Hangman Java 游戏打印错误和正确的猜测

javascript - 如何在 Javascript 中创建一个模拟 100 行 10 列网格的嵌套循环?

Java - 在本例中如何使用按位移位运算符反转字节数组?

java - 删除 POST 主体 Retrofit 中的可选参数

java - 将整数日期/时间转换为 Java 中的 unix 时间戳?

php - 如何将这个 PHP 对象数组转换为 json?

c - c函数中的二维数组更新

java - 用哨兵填充字符串数组

java - 通过java邮件发送带有包含byte[]附件的电子邮件