0
import java.util.Scanner; 
public class SneakyDice 
{

    public static void main(String[] args)
    {
        game();
    }
    public static void game()
    {
        int player1;
        int player2;
        int p1Final=0;
        int p2Final=0;
        boolean gameStatus = true;
        Die idie1 = new Die();
        Die idie2 = new Die();
        Die idie3 = new Die();
        Die idie4 = new Die();
        if (idie1.getNum()==idie2.getNum())
        {
            if(idie1.getNum()==1)
            {
                p1Final = 99;
            }
            else if(idie1.getNum()==2)
            {
                p1Final = 8;
            }
            else if(idie1.getNum()==3)
            {
                p1Final = 12;
            }
            else if(idie1.getNum()==4)
            {
                p1Final = 16;
            }
            else if(idie1.getNum()==5)
            {
                p1Final = 20;
            }
            else if(idie1.getNum()==6)
            {
                p1Final = 24;
            }
        }
        else
        {
            p1Final = idie1.getNum() + idie2.getNum();
        }
        player1 = idie1.getNum() + idie2.getNum();
        if (idie3.getNum()==idie4.getNum())
        {
            if(idie3.getNum()==1)
            {
                p2Final = 99;
            }
            else if(idie3.getNum()==2)
            {
                p2Final = 8;
            }
            else if(idie3.getNum()==3)
            {
                p2Final = 12;
            }
            else if(idie3.getNum()==4)
            {
                p2Final = 16;
            }
            else if(idie3.getNum()==5)
            {
                p2Final = 20;
            }
            else if(idie3.getNum()==6)
            {
                p2Final = 24;
            }
        }
        else
        {
            p2Final = idie3.getNum() + idie4.getNum();
        }
        player2 = idie3.getNum() + idie4.getNum();
        while(gameStatus == true)
        {
            System.out.println("Player 1's roll:" + player1);
            System.out.println("Player 2's roll:" + player2);
            System.out.println("Player 1's real roll:" + p1Final);
            System.out.println("Player 2's real roll:" + p2Final);
            System.out.println("Enter the player who rerolls: ");
            Scanner scanner = new Scanner(System.in);
            String input = scanner.nextLine();

            if(input == "p1" || input =="1")
            {
                Die die1 = new Die();
                Die die2 = new Die();
                if (die1.getNum()==die2.getNum())
                {
                    if(die1.getNum()==1)
                    {
                        p1Final = 99;
                    }
                    else if(die1.getNum()==2)
                    {
                        p1Final = 8;
                    }
                    else if(die1.getNum()==3)
                    {
                        p1Final = 12;
                    }
                    else if(die1.getNum()==4)
                    {
                        p1Final = 16;
                    }
                    else if(die1.getNum()==5)
                    {
                        p1Final = 20;
                    }
                    else if(die1.getNum()==6)
                    {
                        p1Final = 24;
                    }
                }
                else
                {
                    p1Final = die1.getNum() + die2.getNum();
                }
                player1 = die1.getNum() + die2.getNum();

            }
            else if(input =="p2" || input =="2")
            {
                Die die3 = new Die();
                Die die4 = new Die();
                if (die3.getNum()==die4.getNum())
                {
                    if(die3.getNum()==1)
                    {
                        p2Final = 99;
                    }
                    else if(die3.getNum()==2)
                    {
                        p2Final = 8;
                    }
                    else if(die3.getNum()==3)
                    {
                        p2Final = 12;
                    }
                    else if(die3.getNum()==4)
                    {
                        p2Final = 16;
                    }
                    else if(die3.getNum()==5)
                    {
                        p2Final = 20;
                    }
                    else if(die3.getNum()==6)
                    {
                        p2Final = 24;
                    }
                }
                else
                {
                    p2Final = die3.getNum() + die4.getNum();
                }
                player2 = die3.getNum() + die4.getNum();

            }
            else if(input == "stop" || input == "s")
            {
                System.out.println("This exists");
                gameStatus = false;
            }
        }
        if(p1Final>p2Final) //player1>player2
        {
            System.out.println("Player One Wins");
        }
        if(p2Final>p1Final)
        {
            System.out.println("Player Two Wins");
        }
        else
        {
            System.out.println("Tie");
        }
    }
}

Initial runs of this code work as it is able to print out what the pure rolls of each player are and if they fit in a special case, the actual value it is. My code is never able to make it the gameStatus = false or change a player's roll however as it seems to ignore whatever I type into the console. However, this is able to work the way I intended it to when I remove the special cases where I make the dice roll larger than it should be.

Bill F
  • 723
  • 11
  • 23
Theguyser
  • 21
  • 2
  • 3
    Instead of a pile of `if` statements use some kind of look-up table or at the very least a [`switch`](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html). This is not a sustainable method of coding. – tadman May 16 '17 at 18:08

2 Answers2

0

Your problem is with the below line of code,

if(input == "p1" || input =="1")

input, "p1" , "1" etc are string objects, you should use the equals method to perform the comparison.

change it to,

if(input.equals("p1") || input.equals("1"))

Make the same changes to the below too,

else if(input =="p2" || input =="2")

else if(input == "stop" || input == "s")

change these to,

else if(input.equals("p2") || input.equals("2"))

else if(input.equals("stop") || input.equals("s"))
dumbPotato21
  • 5,669
  • 5
  • 21
  • 34
Jerin Joseph
  • 1,087
  • 9
  • 17
0

While the use of "==" will sometimes get you want you want during Java String comparison, it's better to simply use the .equals() method.

This answer (https://stackoverflow.com/a/513839/8021378) can explain the full reasoning.

Because of this, none of your inputs would ever equal the conditionals you set, and the loop would run again with the same parameters and inputs it began with. This would give you the impression that your console inputs were being ignored, when they were just simply inequal.

Additionally, you are going to want to be careful with how you set up your final if statements. As of right now you have:

    if(p1Final>p2Final) //player1>player2
    {
        System.out.println("Player One Wins");
    }
    if(p2Final>p1Final)
    {
        System.out.println("Player Two Wins");
    }
    else
    {
        System.out.println("Tie");
    }

With this setup, if player one wins, it will print out "Player One Wins" as well as printing "Tie". This is because you start a whole new if statement block with your second if statement.

To fix this simply change:

if(p2Final>p1Final)

To:

else if(p2Final>p1Final)

That should fix your issue.

Community
  • 1
  • 1