4

Edit. The string comparison part of the problem has been solved, but the code still doesn't go to next player when rolling just one 1 out of the 2 dice, only when both dice are 1s does it go to next player.

Here is the code so far, with the first class being pretty much from the textbook and the second class with the main method is what I have done.

The program is trying to create a dice game called pig. The simple rules are at the bottom of the code if interested, but the main problems.

I'm having is that its not looping properly, when I don't put y in the scanner to signal roll again its just continuing like I've inputted y, when I don't. Also, the if statements aren't working properly, because when the player rolls a 1 with one of the dice its not going to next player.

Sorry if I haven't explained the problem properly. Also I don't want to use any more methods or classes. I assume there are quicker ways to accomplish this than the way I'm doing it but that's for later, if I want to use extra methods in my code. I also have a problem with counting points, because sometimes it doesn't change them appropriately, but I can figure that out once the rest is working.

Here's the first bit of code. This code isn't really important to the problem, but if you want to know what methods I call in main, you can look at these:

import java.util.Random;

public class PairOfDice {

    private final int MAX = 6;
    private int faceValue;
    private int faceValue1;

    Random generator0 = new Random();
    Random generator1 = new Random();

    public PairOfDice(){
        faceValue = 1;
        faceValue1 = 1;
    }

    public int getFaceValue() {
        return faceValue;
    }

    public void setFaceValue(int faceValue) {
        this.faceValue = faceValue;
    }

     public int getFaceValue1() {
        return faceValue1;
    }

    public void setFaceValue1(int faceValue1) {
        this.faceValue1 = faceValue1;
    }

    public int rollOne() {
         faceValue = generator0.nextInt(MAX) + 1;

         return faceValue;
    }

    public int rollTwo() {
        faceValue1 = generator1.nextInt(MAX) + 1;

        return faceValue1;   
    }

    public int sumOfRoll() {
        return faceValue + faceValue1;
    }

    @Override
    public String toString() {
        return "First roll of the die: \t" + rollOne() 
             + "\nSecond roll of the die: " + rollTwo()
             + "\nThe sum of both rolls: \t" + sumOfRoll();
    }
}

The next bit of code is my own. I have update some of the things in the code, using .equals now when comparing string and I changed the while conditions and streamlined the if statements a bit.

public class NewClass {

    public static void main(String[] args) {

        PairOfDice player1 = new PairOfDice();
        PairOfDice player2 = new PairOfDice();
        Scanner scan = new Scanner(System.in);

        int p1 = 33, p2 = 0, turnp1 = 0, turnp2 = 0, signal = 1;

        while (p1 <= 100 || p2 >= 100) {
            int newp1total = p1;
            turnp1 = 0;
            while (turnp1 <= 20 && signal == 1) {

                System.out.println("Player 1s Turn!");
                int die1 = player1.rollOne();
                int die2 = player1.rollTwo();
                int sumofdice = player1.sumOfRoll();
                System.out.println("Player 1: First Die: " + die1 + "     Second Die:" + die2 + "     Sum of Roll:  " + sumofdice);
                if (sumofdice == 2) {
                    p1 = 0;
                    turnp1 = 0;
                    signal = -1;
                    System.out.println("Player rolled a two 1s. All players points are forfeited. Next Players turn.");
                    System.out.println("Points this turn:" + turnp1);
                    System.out.println("Points this game: " + p1);
                    System.out.println();
                } else if (die1 == 1 || die2 == 1) {
                    turnp1 = 0;
                    signal = -1;
                    p1 = newp1total;
                    System.out.println("Player rolled a 1. All points on this round are forfeited. Next Players turn.");
                    System.out.println("Points this turn:" + turnp1);
                    System.out.println("Points this game: " + p1);
                    System.out.println();
                } else {
                    turnp1 += sumofdice;
                    p1 += sumofdice;
                    System.out.println("Points this turn:" + turnp1);
                    System.out.println("Points this game: " + p1);
                    System.out.println();
                    signal = 1;
                }
            }

            signal = 1;
            String yesno = "y";
            int newp2total = p2;
            while (yesno.toLowerCase().equals("y") && signal == 1) {
                System.out.println("Player 2s Turn!");
                int die1 = player2.rollOne();
                int die2 = player2.rollTwo();
                int sumofdice = player2.sumOfRoll();
                System.out.println("Player 2: First Die: " + die1 + "     Second Die:" + die2 + "     Sum of Roll:  " + sumofdice);

                if (sumofdice == 2) {
                    p2 = 0;
                    turnp2 = 0;
                    signal = -1;
                    System.out.println("Player rolled a two 1s. All players points are forfeited. Next Players turn.");
                    System.out.println("Points this turn:" + turnp2);
                    System.out.println("Points this game: " + p2);
                    System.out.println();
                } else if (die1 == 1 || die2 == 1) {
                    signal = -1;
                    turnp2 = 0;
                    p2 = newp2total;
                    System.out.println("Player rolled a 1. All points on this round are forfeited. Next Players turn.");
                    System.out.println("Points this turn:" + turnp2);
                    System.out.println("Points this game: " + p2);
                    System.out.println();
                } else {
                    turnp2 += sumofdice;
                    p2 += sumofdice;
                    System.out.println("Points this turn:" + turnp2);
                    System.out.println("Points this game: " + p2);
                    System.out.println();
                    System.out.println("Try your luck? Y/N");
                    yesno = scan.next();
                    System.out.println();
                    signal = 1;
                }
            }
        }
    }
}
Danielson
  • 2,605
  • 2
  • 28
  • 51
Mr_Fishy52
  • 99
  • 11
  • 2
    possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – ajb Aug 21 '15 at 04:00
  • 1
    You're using `==` to check whether the string is `"y"`. Big fat no-no. Use `.equals()`. – ajb Aug 21 '15 at 04:01
  • 1
    What is the point of the excessive getters and setters, why don't you just return it to a specific variable, after calling it. Read this : http://www.javaworld.com/article/2073723/core-java/why-getter-and-setter-methods-are-evil.html – Ruchir Baronia Aug 21 '15 at 04:03
  • 1
    @Rich It is possible (based on how the OP writes "the next bit of code is my own") that those getters/setters are given as part of an assignment. – apnorton Aug 21 '15 at 04:04
  • 1
    i didn't write the first class, that's straight from the text book – Mr_Fishy52 Aug 21 '15 at 04:05
  • 1
    @apnorton you're right. Maybe as a practice, OP can practice doing this without the multiple getters and setters. I think we can all agree they are a big pain! – Ruchir Baronia Aug 21 '15 at 04:09
  • Did my answer work? Feel free to ask me any questions if it didn't. If it did, make sure to mark it best answer! :) – Ruchir Baronia Nov 14 '15 at 02:21

2 Answers2

4

First thing's first, and this is an important concept, when comparing strings ALWAYS use .equals, not ==. For example

yesno.toLowerCase() == "y" && signal == 1

should be

 yesno.toLowerCase().equals("y") && signal.equals 1

This is because y is a string. If y was int y, then you would use ==. If you were comparing y and you were constantly changing values you would use =. Next thing; I understand that using getters and setters may be part of an assignment from your textbook, but unless it is; DONT USE THEM.

Read this article.

It makes code very hard to follow. Other wise great job in your code, I like that you are respecting java code convention!

Also, make sure to use .equals for strings, and == for integers. If you are comparing integers which, I believe you are for int variable, you need to use ==. Also, I think you may be confused on and or concepts. If you have an if statement and are comparing things and need BOTH statements to be true use "&&" if you only need ONE statement to be true, use || (this key is the key above the enter with a shift)

user229044
  • 232,980
  • 40
  • 330
  • 338
Ruchir Baronia
  • 7,406
  • 5
  • 48
  • 83
  • 1
    I have changed the string comparison in the code, but is there any reason why using .equals for the checking the int variable signal should be used? – Mr_Fishy52 Aug 21 '15 at 04:10
  • 1
    Good question, just make sure to use .equals for strings, and == for integers. If you are comparing **integers** which, I believe you are for int variable, you need to use ==. Also, I think you may be confused on and or concepts. If you have an if statement and are comparing things and need BOTH statements to be true use "&&" if you only need ONE statement to be true, use || (this key is the key above the enter with a shift) – Ruchir Baronia Aug 21 '15 at 04:13
  • signal is an integer not a string to use equals – TryinHard Aug 21 '15 at 04:18
  • Would || or ^ be preferable in the first while loop? as i understand ^ is an exclusive or, wouldn't this be preferred because i only want one to be over 100, as that would be the winner. or in this case is that not really necessary as only one will get to 100 first? – Mr_Fishy52 Aug 21 '15 at 04:23
  • Personally I have never used ^, I always use ||. Honestly, the best way to check is to try. First run the program with ||, then run it with ^, see which one works. If the || isn't functioning properly just tell me, and I will be more than happy to help you out! – Ruchir Baronia Aug 21 '15 at 04:38
2
  1. You're comparing the string using == which leads to comparison of the the reference equality: Replace

    while (yesno.toLowerCase() == "y" && signal == 1) {
    

    with

    while (yesno.toLowerCase().equals("y") && signal == 1) {
    

    or

    while (yesno.equalsIgnoreCase("y") && signal == 1) {
    

    == versus equals():

    == tests for reference equality (i.e. whether they are the same objects i.e refer to same string in string pool).

    But, .equals() checks for value equality (whether their contents are actually same or not).

    But beware of the null strings. == can handle these null strings fine, but invoking .equals() from a null string will cause an exception since the method is called using null object.

    For more information you can refer String Pools

  2. Replace the statement:

    while (p1 <= 100 ^ p2 >= 100) {
    

    with

    while (p1 <= 100 || p2 >= 100) {
    

    Consider:

    int p1=1;
    int p2=2;
    if( p1==1 ^ p2==2){ // False here since it is TRUE^TRUE which is FALSE
        System.out.println("Same");
    }
    
    if( p1==1 || p2==3){ // TRUE if either one is TRUE
        System.out.println("Same Again");
    }
    

    This prints only

    Same Again

    This is because ^ is bitwise XOR operator which is false if and only if both the operands are of same truth value.

  3. Replace two else if with equivalent

    else if ( die2 == 1 || die1 == 1 ) {
    
Community
  • 1
  • 1
TryinHard
  • 4,078
  • 3
  • 28
  • 54