'While loop not terminating when an array equals the terminate condition array. Java. First java class please be patient. Thank you

Was wondering if there was a reason that the while loop was not terminating if the arrays equal eachother, the one condition was an array set to all 2s and then when the Die array reaches all 2s I wanted the while loop to terminate. Thank you for any help.

Stuck in the mud dice game, 2 players will roll 5 dice each, and the person with the highest total wins, if one player rolls a 2 they are stuck in the mud and unable to continue rolling for the rest of the game

import java.util.*;
public class StuckInTheMud {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("What is player ones name?");
        String firstPlayer = keyboard.nextLine();
        System.out.println("What is player twos name?");
        String secondPlayer = keyboard.nextLine();
        Player playerOne = new Player(firstPlayer);
        Player playerTwo = new Player(secondPlayer);
        while (playerOne.getDice() != playerOne.endGame() || playerTwo.getDice() != playerTwo.endGame()) // this is the while loop that wont terminate
        {
            pressEnterKeyToContinue(firstPlayer); // these are the turns looping
            playerOne.playerTurn(firstPlayer);
            pressEnterKeyToContinue(secondPlayer);
            playerTwo.playerTurn(secondPlayer);
        }
    }
    public static void pressEnterKeyToContinue(String playerName) // using a continue method so the game doesn't contine on its own
    {
        System.out.println("\n" + playerName + ", press Enter key when you are ready to roll!");
        Scanner nextTurn = new Scanner(System.in);
        nextTurn.nextLine();
    }
}
import java.util.*;

public class Player { // this is the player class 
   private int score;
   private Die[] dice = new Die[6];
   private String playerName;
   private int roundScore;
   private int totalScore;

   public Player(String playerAlias) {
    playerAlias = playerName;
    score = 0;
    for (int i = 0; i != dice.length; i++) {
        dice[i] = new Die(6);
        dice[i].setValue(1);
    }
   }

   public void playerTurn(String playerName) {
    roundScore = 0;
    int twoRoll = 0;
    System.out.print(playerName + " it is your turn, here come the dice.\n");
    for (int i = 0; i != dice.length; i++) // This for loop will add random dice roll integers into the players array
    {
        if (dice[i].getValue() != 2) {
            dice[i].roll();
            if (dice[i].getValue() != 2) {
                System.out.println("For roll number " + (i + 1) + " you got a " + dice[i].getValue() + "!");
                roundScore(dice[i].getValue());
            } else {
                System.out.println("For roll number " + (i + 1) + ", you got a 2! Die " + (i + 1) + " is now stuck in the mud!");
                twoRoll = 1;
            }
        } else {
            System.out.println("Die " + (i + 1) + " is stuck in the mud since you rolled a 2, it cannot be used for the rest of the game!");
        }
    }
    if (twoRoll == 0) {
        System.out.println("\nYour total score this round was " + getRoundScore() + ".");
        totalScore(roundScore);
        System.out.println(playerName + ", your total score for the game is " + getTotalScore() + "!");
    } else {
        System.out.println("\nSince you rolled a 2, the score for this round is 0");
        roundScore = 0;
        totalScore(roundScore);
        System.out.println(playerName + ", your total score for the game is " + getTotalScore() + "!");
    }
   }

   public void roundScore(int singleRoll) {
    roundScore += singleRoll;
   }

   public int getRoundScore() {
    return roundScore;
   }

   public void totalScore(int roundScore) {
    totalScore += roundScore;
   }
   public int getTotalScore() {
    return totalScore;
   }

   public Die[] getDice() {
    return dice;
   }

   public Die[] endGame() {
    Die[] endGame = new Die[6];
    for (int i = 0; i != endGame.length; i++) {
        endGame[i] = new Die(6);
        endGame[i].roll();
        endGame[i].setValue(2);
    }
    return endGame;
   }
}
// Die Class
// 4/21/22
// Zachary Strickler

import java.util.Random;

/**
   The Die class simulates a six-sided die.
*/

public class Die // this is the die class
{
    private int sides; // Number of sides
    private int value; // The die's value

    /**
       The constructor performs an initial
       roll of the die.
       @param numSides The number of sides for this die.
    */

    public Die(int numSides) {
        sides = numSides;
        roll();
    }

    /**
       The roll method simlates the rolling of
       the die.
    */

    public void roll() {
        // Create a Random object.
        Random rand = new Random();

        // Get a random value for the die.
        value = rand.nextInt(sides) + 1;
    }

    /**
       getSides method
       @return The number of sides for this die.
    */

    public int getSides() {
        return sides;
    }

    /**
       getValue method
       @return The value of the die.
    */

    public int getValue() {
        return value;
    }

    public int setValue(int setValue) {
        value = setValue;
        return value;
    }
}


Solution 1:[1]

You while loop test includes

playerTwo.getDice() != playerTwo.endGame()

By using the = operator, you are saying 'are these two arrays the same array object?'. Looking at the endGame method you can see that can never be true, you are creating a new array object every time you call that method, so could never be the same one as

playerTwo.getDice(). 

Instead of checking to see if they are the same object, you really want to know if they have the same values. There are some other ways of doing that. I'll give you a hint, look at java.util.Arrays package.

 https://www.geeksforgeeks.org/java-util-arrays-equals-java-examples/
 https://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html
 https://www.geeksforgeeks.org/compare-two-arrays-java/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 cogitoboy