0
votes

The purpose of the code is to have 2 players roll a pair of dice. The first player to roll a total of 20 wins the game. I'm having trouble figuring out how to properly track the sum of the rolls; it only gives me the sum of that current turn, and then the game ends when each player rolled 10 times.

How do I properly count the sum of each players game, and then stop the loop when one of the players' sums is equal to 20?

int a, b, c, d;
int playerone=0, playertwo=0;

Random gen = new Random();
a=gen.nextInt(6)+1;
b=gen.nextInt(6)+1;
c=gen.nextInt(6)+1;
d=gen.nextInt(6)+1;

while(playerone!=20 || playertwo!=20) {
    playerone=a+b;
    playertwo=c+d;

    System.out.println("Player 1 rolled " + a + " and a " + b );
    System.out.println("Player 1 now has " + playerone);
    System.out.println("Player 2 rolled " + c + " and a " + d );
    System.out.println("Player 2 now has " + playertwo);

    a=gen.nextInt(6)+1;
    b=gen.nextInt(6)+1;
    c=gen.nextInt(6)+1;
    d=gen.nextInt(6)+1;

    playertwo+=a+b;

    playerone+=c+d;
    if(playerone==20) 
        System.out.println("player one wins ");
    else if (playertwo==20)
        System.out.println("player two wins ");             
    }       
}

2
what happens if my sum is greater than 20? - MT756
Welcome to Stackoverflow. Please remember to look at your post after submitting it, so you can edit any formatting mistakes you made. Like improper code indentation etc (see the markup help page for what proper syntax on Stackoverflow is). - Mike 'Pomax' Kamermans

2 Answers

2
votes

please have a look and compare it with your snippet:

int playerone = 0, playertwo = 0;
while(playerone < 20 && playertwo < 20) {
    a=gen.nextInt(6)+1;
    b=gen.nextInt(6)+1;
    c=gen.nextInt(6)+1;
    d=gen.nextInt(6)+1;

    System.out.println("Player 1 rolled " + a + " and a " + b );
    System.out.println("Player 1 now has " + playerone);
    System.out.println("Player 2 rolled " + c + " and a " + d );
    System.out.println("Player 2 now has " + playertwo);

    playerone+=a+b;
    playertwo+=c+d;
    }

    if(playerone >= playertwo) { // here you have to choose how
        System.out.println("player one wins with " + playerone + " over " + playertwo);
    } else {
    System.out.println("player two wins with " + playertwo + " over " + playerone);
}

In the code above I have corrected a few things, the where condition and a/b are for player 1 and c/d for player 2. After the where loop ends, you have to decide depending on the resulted values or by your logic how to determine the winner since both rolls the dices.

2
votes

You set playerone=a+b and playertwo=c+d inside the loop, which means the totals are only based on the latest rolls. Do that before the loop, instead.

Although, really, it would be better to consolidate all the dice rolling and output inside the loop, so that you would output the new totals after they are updated, rather than before.

You are also inconsistent in whether a and b are for player one or two. You should move all the code for updating the players and rolling the dice into methods.