Java While循环中的奇怪行为

Java While循环中的奇怪行为,java,while-loop,infinite-loop,system.out,Java,While Loop,Infinite Loop,System.out,我正在写一个基本的井字游戏,单人游戏使用基本的摆动图形。我完成了游戏,但我面临一个奇怪的问题。在一个地方,我使用了一个带有SOP语句的while循环。若我省略了这条语句,那个么程序的工作方式就不同了,什么也并没有发生(比如某种无限循环),若我保留它,它就可以正常工作。我不知道代码里发生了什么。请帮忙 下面是导致问题的源代码。对不起,我的业余编码风格 import java.util.Random; public class SinglePlayer implements Runnable{

我正在写一个基本的井字游戏,单人游戏使用基本的摆动图形。我完成了游戏,但我面临一个奇怪的问题。在一个地方,我使用了一个带有SOP语句的while循环。若我省略了这条语句,那个么程序的工作方式就不同了,什么也并没有发生(比如某种无限循环),若我保留它,它就可以正常工作。我不知道代码里发生了什么。请帮忙

下面是导致问题的源代码。对不起,我的业余编码风格

import java.util.Random;

public class SinglePlayer implements Runnable{

    public final int MINIMUM = -1000000;
    private GameBoard game;

    public SinglePlayer(){
        game = new GameBoard("Single Player");
    }

    public static void main(String[] args){
        SinglePlayer gameSingle = new SinglePlayer();
        gameSingle.run();
    }

    public void run(){
        boolean machinePlayed = true, userPlayed = false;

        // Outer loop is to maintain re-match option of program
        while(this.game.quitTwoPlayer == false){
            // Inner loop is a single game b/w user and machine
            while(this.game.GameQuitStatus() == false){

                /* I kept two conditions to switch b/w machine and user mode 
                 * of game and they just keep changing to simulate the game
                 * b/w machine and user.
                 */
                if(machinePlayed == false && userPlayed){
                    try {
                        MachineMove("O");
                    } catch (CloneNotSupportedException e) {
                        e.printStackTrace();
                        break;
                    }
                    this.game.ChangePlayerLabels();
                    machinePlayed = true;
                    userPlayed = false;
                }
                else if(machinePlayed && userPlayed == false){
                    int earlierCount = this.game.CountSteps();

                    /* THIS IS THE WHILE LOOP I AM TALKING ABOUT. 
                     * If I omit the print statement inside the body of loop,
                     * program behaves differently, but when I keep it,
                     * it working just fine. 
                     * */ 
                    while(earlierCount == this.game.CountSteps()){
                        System.out.println("Player User thinking");
                    }

                    this.game.ChangePlayerLabels();
                    machinePlayed = false;
                    userPlayed = true;                  
                }
                this.game.DeclareResult();
            }
            this.game.dispose();
        }
    }

    public void MachineMove(String player) throws CloneNotSupportedException{

        /* If board is empty, play at center of the board */
        if(this.game.CountSteps() == 0){
            this.game.MakeMove(1, 1);
        }

        /* If center is blank, play it there. Otherwise, pick a corner randomly */
        else if(this.game.CountSteps() == 1){
            if(this.game.IsEmpty(1, 1))
                this.game.MakeMove(1, 1);
            else{
                Random randomNum = new Random();
                int num = randomNum.nextInt(4);
                if(num == 0)
                    this.game.MakeMove(0, 0);
                else if(num == 1)
                    this.game.MakeMove(2, 0);
                else if(num == 2)
                    this.game.MakeMove(0, 2);
                else if(num == 3)
                    this.game.MakeMove(2, 2);
            }
        }
        else{           
            /* If the next move is such that it should be taken, otherwise opponent will win */
            String opponent = "";
            if(this.game.GetCurrentPlayer().equals("O"))
                opponent = "X";
            else
                opponent = "O";
            for(int i = 0; i<3; i++){
                for(int j = 0; j<3; j++){
                    if(this.game.IsEmpty(i,j)){
                        GameBoard tempGame = new GameBoard(this.game, "Single Player");
                        tempGame.MakePossibleMove(i, j, opponent);
                        if(tempGame.GameWinner().equals(opponent + " wins")){
                            this.game.MakeMove(i,j);
                            return;
                        }
                    }
                }
            }

            /* If the next move is not such that if missed, game is lost, then play most optimal move towards winning */
            Move tempMove = new Move(MINIMUM, 0, 0);
            Move bestMove = new Move(MINIMUM, 0, 0);
            for(int i = 0; i<3; i++){
                for(int j = 0; j<3; j++){
                    if(this.game.IsEmpty(i,j)){
                        GameBoard tempGame = new GameBoard(this.game, "Single Player");
                        tempMove = MakeMoves(tempGame, i, j);
                        if(tempMove.score > bestMove.score){
                            bestMove.row = tempMove.row;
                            bestMove.col = tempMove.col;
                            bestMove.score = tempMove.score;
                        }
                    }
                }
            }
            this.game.MakeMove(bestMove.row, bestMove.col);
        }
    }

    public Move MakeMoves(GameBoard tempGame, int row, int col){
        String player = tempGame.GetCurrentPlayer(); 
        tempGame.MakeMove(row, col);
        if(tempGame.GameWinner().equals("Match Draw")){
            return new Move(0, row, col);
        }
        else if(tempGame.GameWinner().equals("X wins")){
            if(player.equals("X")){ 
                return new Move(1, row, col);
            }
            else{
                return new Move(-1, row, col);
            }
        }
        else if(tempGame.GameWinner().equals("O wins")){
            if(player.equals("O")){
                return new Move(1, row, col);
            }
            else{
                return new Move(-1, row, col);
            }
        }
        else{
            Move bestMove = new Move(MINIMUM, 0, 0);
            Move tempBestMove = new Move(0, 0, 0);
            for(int i = 0; i<3; i++){
                for(int j = 0; j<3; j++){
                    if(tempGame.IsEmpty(i,j)){
                        GameBoard newGame = new GameBoard(tempGame, "Single Player");
                        tempBestMove = MakeMoves(newGame, i, j);
                        if(tempBestMove.score > bestMove.score)
                            bestMove = tempBestMove;
                    }
                }
            }
            return bestMove;
        }
    }
}

class Move{
    public int score;
    public int row;
    public int col;

    public Move(int score, int row, int col){
        this.score = score;
        this.row = row;
        this.col = col;
    }
}
import java.util.Random;
公共类SinglePlayer实现了Runnable{
公共最终整数最小值=-1000000;
私人棋盘游戏;
公共单人游戏机(){
游戏=新游戏板(“单人”);
}
公共静态void main(字符串[]args){
SinglePlayer GameSSingle=新的SinglePlayer();
gamessingle.run();
}
公开募捐{
布尔值machinePlayed=true,userPlayed=false;
//外循环用于维护程序的重新匹配选项
while(this.game.quitTwoPlayer==false){
//内环是一个单独的游戏b/w用户和机器
while(this.game.GameQuitStatus()==false){
/*我保留了两个条件来切换b/w机器和用户模式
*他们只是不断地改变来模拟游戏
*b/w机器和用户。
*/
if(machinePlayed==false&&userPlayed){
试一试{
机械移动(“O”);
}捕获(CloneNotSupportedException e){
e、 printStackTrace();
打破
}
this.game.ChangePlayerLabels();
machinePlayed=真;
userPlayed=false;
}
else if(machinePlayed&&userPlayed==false){
int earlierCount=this.game.CountSteps();
/*这就是我所说的WHILE循环。
*如果我省略循环体中的print语句,
*程序的行为不同,但当我保留它时,
*它工作得很好。
* */ 
while(earlierCount==this.game.CountSteps()){
System.out.println(“玩家用户思维”);
}
this.game.ChangePlayerLabels();
machinePlayed=假;
userPlayed=true;
}
此.game.DeclareResult();
}
this.game.dispose();
}
}
public void MachineMove(字符串播放器)抛出CloneNotSupportedException{
/*若棋盘为空,则在棋盘的中心进行游戏*/
if(this.game.CountSteps()==0){
this.game.MakeMove(1,1);
}
/*如果“中心”为空,则在那里播放。否则,随机选择一个角*/
else if(this.game.CountSteps()==1){
如果(这个游戏是空的(1,1))
this.game.MakeMove(1,1);
否则{
随机数=新随机数();
int num=randomNum.nextInt(4);
如果(num==0)
this.game.MakeMove(0,0);
else if(num==1)
这个.game.MakeMove(2,0);
else if(num==2)
这个.game.MakeMove(0,2);
else if(num==3)
这个.game.MakeMove(2,2);
}
}
否则{
/*如果下一步是应该采取的,否则对手将获胜*/
字符串对手=”;
if(this.game.GetCurrentPlayer().equals(“O”))
对手=“X”;
其他的
对手=“O”;

对于(int i=0;i您的循环可能正在键入处理器,SOP会使循环速度减慢到足以允许其他进程发生。但无论如何,最重要的是,您首先不希望出现此循环。您声明您有一个

Tic Tac Toe单人游戏,使用基本swing图形

请记住,Swing是一个事件驱动的GUI库,因此不要像在线性控制台程序中那样循环,而是让事件发生,但根据程序的状态对其作出响应


换句话说,给你的类几个字段,包括一个布尔变量,它告诉你该轮到谁了,比如
布尔playersTurn
,一个布尔变量gameOver,…,并在游戏进行时更改这些变量的状态,并根据这些状态来确定游戏行为。例如,游戏会忽略玩家的行为如果没有轮到他,请输入。

你认为你的无限循环在这堆代码中的什么地方?sysout可能包含某种屈服,允许执行其他线程。@MarshallTigerus先生,请在代码中稍下一步。它在第二条多行注释的下面。@njzk2你能再解释一下吗?请检查