If they choose not to roll again (but their most recent roll was not a zero), add
their round score to their total score using
addPoints()
.
G.
Use
getPoints()
to see if the player’s total is greater than or equal to the stored
victory
number.
If
so,
out
that
the
player
won
and
call
addWin()
,
then
return;
to end the round. If not, go on to the next player.
import java.util.Random;
public class Main {
    public static int victory = 100;
    public static void main(String[] args) {
        Player[] players = new Player[2] {
            new Player(), new Player()
        } ;
        Random random = new Random();
        int cur = 0;
        int roll = 0;
        while (true) {
            if (random.nextBoolean()) {
                roll = random.nextInt(10) + 1;
            } else {
                if (roll > 0) {
                    players[cur].addPoints(roll);
                }
                if (players[cur].getPoints() >= victory) {
                    System.out.println("Player " + players[cur].getName() + " win.");
                    players[cur].addWin();
                    return;
                }
                roll = 0;
                cur = (cur + 1) % 2;
            }
        }
    }
}
Comments