So, I'm working on this assignment for a class. It's a Java class, and I'm supposed to make a game where it rolls two dice, adds them up, and adds them to your turn score. It then asks if you want to keep playing. When your turn score hits 20, or when you decide to pass, it goes to a computer. It's supposed to print the scores each turn, and then when someone hits 100 points, it announces the winner. However, no matter what, the score at the end of each turn is 0, no matter how many times I run it. When a player rolls a 1, their turn score is cancelled, and it moves on to the other player, and if they roll double 1's, they lose all of their points for the game so far. Here is my code, can you figure out why the score variables don't update? Thank you.
import java.util.Scanner;
import java.util.Random;
public class PlayPig {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
int player1 = 0;
int player2 = 0;
int a, b, c, player1turn, player2turn, input;
int pig = 1;
Random r = new Random();
do{
do {
player1turn=0;
a = r.nextInt(6)+1;
b = r.nextInt(6)+1;
if( a==1 || b==1){
if (a == 1 && b == 1){
player1=0;
break;}
else if (a==1 || b==1){
player1turn=0;
break;}
else {
player1turn= a+b ;
}}
player1= player1+player1turn;
System.out.println("Player1 score is " + player1 + " and player2 score is " + player2);
System.out.print("Do you want to keep playing? Enter 1 to continue. Enter any other number to pass.");
input = scan.nextInt();
if (input != 1)
break;
}
while
(player1turn <= 20);
do{
player2turn=0;
a = r.nextInt(6)+1;
b = r.nextInt(6)+1;
if( a==1 || b==1){
if (a == 1 && b == 1){
player2=0;
break;}
else if (a==1 || b==1){
player2turn=0;
break;}
else {
player1turn= a+b ;
player2= player2+player2turn;}}
}
while
(player2turn<=20);
}
while
(player1 < 100 || player2 < 100);
if (player1>player2)
System.out.print("Player 1 wins");
else
System.out.print("Player 2 wins");
}}
The main problem was, that your else
conditions in which you assigned the current score were in the wrong block. (These ones):
else {
player1turn = a+b ;
}
Try this code:
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
int player1 = 0;
int player2 = 0;
int a, b, c, player1turn, player2turn, input;
int pig = 1;
Random r = new Random();
do{
do {
player1turn=0;
a = r.nextInt(6)+1;
b = r.nextInt(6)+1;
if( a==1 || b==1){
if (a == 1 && b == 1){
player1 = 0;
break;
}
else if (a==1 || b==1){
player1turn=0;
break;
}
}else {
player1turn = a+b ;
}
player1 += player1turn;
System.out.println("Player1 score is " + player1 + " and player2 score is " + player2);
System.out.print("Do you want to keep playing? Enter 1 to continue. Enter any other number to pass.");
input = scan.nextInt();
if (input != 1){
break;
}
} while (player1turn <= 20);
do{
player2turn=0;
a = r.nextInt(6)+1;
b = r.nextInt(6)+1;
if( a==1 || b==1){
if (a == 1 && b == 1){
player2=0;
break;
} else if (a==1 || b==1){
player2turn=0;
break;
}
}else {
player2turn = a+b ;
player2 += player2turn;
}
}while (player2turn<=20);
} while (player1 < 100 || player2 < 100);
if (player1>player2)
System.out.print("Player 1 wins");
else
System.out.print("Player 2 wins");
}
I have modified the if loop. You can try this:
import java.util.Scanner;
import java.util.Random;
public class PlayPig {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
int player1 = 0;
int player2 = 0;
int a, b, c, player1turn, player2turn, input;
int pig = 1;
Random r = new Random();
do{
do {
player1turn=0;
a = r.nextInt(6)+1;
b = r.nextInt(6)+1;
if (a == 1 && b == 1){
player1=0;
break;
}
else if((a== 1 && b!= 1) || (a!=1 && b== 1){
player1turn=0;
break;
}
else{
player1turn= a+b ;
}
player1= player1+player1turn;
System.out.println("Player1 score is " + player1 + " and player2 score is " + player2);
System.out.print("Do you want to keep playing? Enter 1 to continue. Enter any other number to pass.");
input = scan.nextInt();
if (input != 1)
break;
}
while
(player1turn <= 20);
do{
player2turn=0;
a = r.nextInt(6)+1;
b = r.nextInt(6)+1;
if (a == 1 && b == 1){
player2=0;
break;
}
else if((a== 1 && b!= 1) || (a!=1 && b== 1){
player2turn=0;
break;
}
else{
player2turn= a+b ;
}
player2= player2+player2turn;
}
while
(player2turn<=20);
}
while
(player1 < 100 || player2 < 100);
if (player1>player2)
System.out.print("Player 1 wins");
else
System.out.print("Player 2 wins");
}}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.