Java:猪游戏变得无休止的循环?

时间:2017-10-16 20:05:55

标签: java

所以我的编程类有这个任务,玩家和计算机玩Pig游戏最多50分,并且多个类用于掷骰子。计算机可以在一个回合中玩到20点,并且提示玩家输入y或n以继续滚动或不滚动。如果任何骰子滚动为1,则该轮的所有点都将丢失。

然而,使用while时游戏变成无限循环,我不确定为什么。我对java很新,所以我不知道怎么说这个。

模具课

    public class Die {

    private final int MAX = 6; //maximum face value
    private int faceValue; //current value showing on the die

    public Die() {
        //Constuctor: Sets the initial face value
        faceValue = 1;

    }

    public int roll() {
        //Rolls the die and returns the result
        faceValue = (int)(Math.random() * MAX) + 1;
        return faceValue;

    }

    public void setFaceValue(int value) {
        //Face value mutator
        faceValue = value;

    }

    public int getFaceValue() {
        //Face value accessor
        return faceValue;

    }

    public String toString() {
        //Returns a string representation of this die.
        String result = Integer.toString(faceValue);
        return result;

    }
}

PairOfDice类

public class PairOfDice {
    //Creates two Die objects
    Die die1 = new Die();
    Die die2 = new Die();

    private int faceValue1;
    private int faceValue2;

    //Initialize sum variable as an integer
    int sum;

    public int getDie1() {
        //Get value of first die
        faceValue1 = die1.getFaceValue();
        return faceValue1;
    }

    public int getDie2() {
        //Get value of second die
        faceValue2 = die2.getFaceValue();
        return faceValue2;
    }

    public void setDie1(int value) {
        //Set value of first die
        die1.setFaceValue(1);
    }

    public void setDie2(int value2) {
        //Set value of second die
        die2.setFaceValue(1);
    }

    public int sumDice() {
        //Adds values of both dice into a sum
        sum = die1.getFaceValue() + die2.getFaceValue();
        return sum;
    }

    public int rollDice() {
        //Rolls both dice and returns results
        int roll = die1.roll() + die2.roll();
        return roll;
    }

    public String toString() {
        //Returns a string representation of the dice
        String result = Integer.toString(sum);
        return result;
    }
}

Pig class / Hog class(我的教授称之为Hog game fyi)

import java.util.Scanner;

    public class Hog {
        public static void main(String[] args) {
            //Declaring data variables
            char answer = 'n';
            PairOfDice dice;
            Die die1;
            Die die2;

            Scanner playerAnswer = new Scanner(System.in);

            dice = new PairOfDice();
            die1 = new Die();
            die2 = new Die();

            int computerTotalScore = 0;
            int humanTotalScore = 0;

            int computerRoundTotal;
            int humanRoundTotal;

            int die1FaceValue = 0;
            int die2FaceValue = 0;

            //Main loop of the game
            while (computerTotalScore < 50 && humanTotalScore < 50) {
                //The computer's turn
                computerRoundTotal = 0; //resetting for computer's next turn

                System.out.println("Current Status: ");
                System.out.println("Computer: " + computerTotalScore);
                System.out.println("You: " + humanTotalScore);

                while (computerRoundTotal < 20 && (die1FaceValue > 1 && die2FaceValue > 1)) {
                   die1.roll();
                   die2.roll();

                   die1FaceValue = die1.getFaceValue();
                   die2FaceValue = die2.getFaceValue();

                   System.out.println("Die 1: " + die1FaceValue + ", Die 2: " + die2FaceValue);

                   computerRoundTotal = computerRoundTotal + (die1FaceValue + die2FaceValue);
                   System.out.println("Current Round: " + computerRoundTotal);
                }

                if (die1FaceValue == 1 || die2FaceValue == 1) {
                   System.out.println("Busted!");
                }
                  else {
                     computerTotalScore = computerTotalScore + computerRoundTotal;
                  }

                //Player's turn
                humanRoundTotal = 0; //resetting for player's next turn

                System.out.println("Current Status: ");
                System.out.println("Computer: " + computerTotalScore);
                System.out.println("You: " + humanTotalScore);

                while (answer != 'y' && (die1FaceValue > 1 || die2FaceValue > 1)) {
                    die1.roll();
                    die2.roll();

                    die1FaceValue = die1.getFaceValue();
                    die2FaceValue = die2.getFaceValue();

                    System.out.println("Die 1: " + die1FaceValue + ", Die 2: " + die2FaceValue);

                    humanRoundTotal = humanRoundTotal + (die1FaceValue + die2FaceValue);
                    System.out.println("Current Round: " + humanRoundTotal);

                    System.out.println("Take another turn (y/n)?");
                    answer = playerAnswer.next().charAt(0);
                }

                if (die1FaceValue == 1 || die2FaceValue == 1) {
                    System.out.println("Busted!");
                }
                  else {
                      humanTotalScore = humanTotalScore + humanRoundTotal;
                  }
            }

            if (computerTotalScore >= 50) {
                System.out.println("The computer has won!");
            }
              else {
                  System.out.println("You won!");
              }

            System.out.println("Final Results:");
            System.out.println("Computer: " + computerTotalScore);
            System.out.println("You: " + humanTotalScore);
        }
    }

1 个答案:

答案 0 :(得分:1)

我没有调试您的整个代码,但无限循环的原因是die1FaceValuedie2FaceValue0初始化。现在,布尔表达式computerRoundTotal < 20 && (die1FaceValue > 1 && die2FaceValue > 1)(这也适用于玩家回合)将始终为falsedie1FaceValue == 0 die2FaceValue == 0。要快速解决此问题,您可以在while循环中添加if-case,如下所示

 while (computerRoundTotal < 20) {
   //roll your dice here

   if(die1FaceValue == 1 || die2FaceValue == 1){
     //reset points here and break, since the computers turn is over
     break;

   }
   //implement the "normal" turn logic here

}

希望这有助于修复您的计划。请注意,我没有检查您的代码是否存在其他错误。

您还要检查answer != 'y'是否正确。我不知道这是否有意,但玩家只有在输入'n'时才会继续滚动(我假设y代表是,n代表否)。所以answer == 'y'肯定会是更合适的选择。