Reputation: 99
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;
}
}
}
}
}
Upvotes: 4
Views: 206
Reputation: 7551
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)
Upvotes: 4
Reputation: 4118
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) {
==
versusequals()
:
==
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
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.
Replace two else if
with equivalent
else if ( die2 == 1 || die1 == 1 ) {
Upvotes: 2