Change Part of the string comparison has been resolved, but the code still does not go to the next player when rolling only one 1 out of 2 dice, only when both dice are 1 s, it goes to the next player.
Here is the code so far, the first class was pretty much from the tutorial, and the second class with the main method is what I did.
The program is trying to create a dice game called a pig . Simple rules are at the bottom of the code if they are interested, but the main problems.
I have that it doesn't loop properly when I don't put y in the scanner to scroll it again again as soon as I entered y when I don't. In addition, if do not work correctly, because when a player rolls 1 with one of the dice, he does not move on to the next player.
Sorry if I did not explain the problem properly. Also I do not want to use more methods or classes. I guess there are faster ways to achieve this than how I do it, but this is for later if I want to use additional methods in my code. I also have a problem with point of delivery, because sometimes it does not change them properly, but I can understand it as soon as the rest works.
Here is the first bit of code. This code is not very important for the problem, but if you want to know what methods I call in main , you can look at them:
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 updated some things in the code using .equals now that I am comparing the string, and I changed the while conditions and simplified 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; } } } } }