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;
}
}
}
}
}