Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Java
  3. Question
Java

Explain the Java dice game.

Asked by Nishant Chauhan Oct 12, 2022 2.2K views 7 answers
Share

About this question

I'm writing a program in Java, a game called Pig. The rules are: There are two players. Each take turn to roll a 6-sided die; a player repeatedly rolls it until the player chooses to stop rolling and pass the dice to the other player, and add the sum of previous rolls to their points, or the player rolls a 1 during their turn, which all of their previous rolls are discarded and their turn ends immediately. The first to reach 100 points wins. I've finished the code, tried to make it as robust as possible, and ended up with this:


import static java.lang.System.*;

import java.util.*;


public class PigDiceGame {


public static Scanner scan = new Scanner (in);

public static Random generator = new Random ();


public static int p1Score = 0;

public static int p2Score = 0;

public static int counter;


public static void main(String args[]){
    out.println("Welcome to Pig, the two-player dice game!");
    out.println("Player 1 go first");
    p1 Mechanics ();
}
public static int Roll() {
    int Low = 1;
    int High = 7;
    int Roll = generator.nextInt (High - Low) + Low;
    return Roll;
}
public static void p1 Mechanics() {
    counter = 0;
    out.println("Do you want to play this turn? (Y/N)");
    String answer = scan.nextLine ();
    answer = answer.replaceAll (" ", "") ;
    answer = answer.toUpperCase ();
    if (answer.equals("N")) {
        out.println ("Player 1 skipped their turn.");
        out.println ("Player 2's turn to roll");
        p2 Mechanics ();
    }
    int Roll = Roll();
    out.println ("You rolled a " + Roll);
    while (p1 Score + counter < 100>        while (Roll != 1) {
            counter += Roll;
            out.println ("Do you want to roll more? (Y/N)");
            answer = scan.nextLine ();
            answer = answer.replaceAll (" ", "") ;
            answer = answer.toUpperCase ();
            while(!(answer.equals("Y")) && !(answer.equals("N"))){
                out.println("Please enter only Y/N:");
                answer = scan.nextLine ();
                answer = answer.replaceAll (" ", "") ;
                answer = answer.toUpperCase ();
            }
            if (answer.equals("N")) {
                p1 Score += counter;
                out.println ("Player 1 ended their turn.");
                out.println ("P1 vs P2: " + p1 Score + "-" + p Score);
                out.println ("Player 2's turn to roll");
                p2 Mechanics ();
            }
            Roll = Roll ();
            out.println ("You rolled a " + Roll);
        }
        if (Roll == 1) {
            counter = 0;
        }
        out.println ("Player 1 rolled a '1'.");
        out.println ("P1 vs P2: " + p1 Score + "-" + p Score);
        out.println ("Player 2's turn to roll");
        p2 Mechanics ();
    }
    out.println("Player 1's score reached 100!");
    out.println("P1 wins by " + (p1 Score - p Score) + " points!");
    exit (0);
}
public static void p2 Mechanics() {
    counter = 0;
    out.println("Do you want to play this turn? (Y/N)");
    String answer = scan.nextLine ();
    answer = answer.replaceAll (" ", "") ;
    answer = answer.toUpperCase ();
    if (answer.equals("N")) {
        out.println ("Player 2 skipped their turn.");
        out.println ("Player 1's turn to roll");
        p1 Mechanics ();
    }
    int Roll = Roll();
    out.println ("You rolled a " + Roll);
    maxScore: while (p2 Score < 100>        while (Roll != 1) {
            counter += Roll;
            out.println ("Do you want to roll more? (Y/N)");
            answer = scan.nextLine ();
            answer = answer.replaceAll (" ", "") ;
            answer = answer.toUpperCase ();
            while(!(answer.equals("Y")) && !(answer.equals("N"))){
                out.println("Please enter only Y/N:");
                answer = scan.nextLine ();
                answer = answer.replaceAll (" ", "") ;
                answer = answer.toUpperCase ();
            }
            if (answer.equals("N")) {
                p2 Score += counter;
                out.println ("Player 2 ended their turn.");
                out.println ("P1 vs P2: " + p1 Score + "-" + p Score);
                out.println ("Player 1's turn to roll");
                p1 Mechanics ();
            }
            Roll = Roll ();
            out.println ("You rolled a " + Roll);
        }
        if (Roll == 1) {
            counter = 0;
        }
        out.println ("Player 2 rolled a '1'.");
        out.println ("P1 vs P2: " + p1 Score + "-" + p Score);
        out.println ("Player 1's turn to roll");
        p1 Mechanics ();
    }
    out.println("Player 2's score reached 100!");
    out.println("P2 wins by " + (p2Score - p1Score) + " points!");
    exit (0);
}
}

I've run this code twice and swapped back and forth between two player's turns and it works correctly. I haven't had time to play a full game, though. I am looking for ways that I can improve the existing code. I never came up with any procedural decomposition before writing it so it's more like patching holes one by one. It may function, but it could probably be improved. Any help would be appreciated. I've been learning Java for a while now and really need some "expert" opinions.

Your answer

7 Answers

Dave Ginger Latest answer

Answered on Jul 19, 2026

Before signing up anywhere, I usually spend some time reading reviews. https://jackpotanglers.com/ had plenty of useful information without being overly promotional. That's something I always appreciate.

Was this helpful?

Emma Brown

Answered on Jul 3, 2026

https://jugabet.cl/page/casino-con-astropay

Was this helpful?

Dave Ginger

Answered on Jun 29, 2026

Jeśli polujesz na bonus powitalny, warto sprawdzić https://slottywayscasino.com/bonusy/. Znalazłem tam aktualne informacje bez przekopywania kilku różnych stron. Oszczędziło mi to trochę czasu.

Was this helpful?

Dave Ginger

Answered on Jun 24, 2026

 I like testing new platforms from time to time to see what they offer. Recently I checked out richardcasino-slots.com and found it easy to use. The website has a modern design and clear menu structure. It made getting started very simple.

Was this helpful?

Alvin

Answered on Feb 23, 2026

After a day of exhaustive labor staring relentlessly at the glaring blue light of your monitor, it's time to pivot into a sphere brimming with exhilaration and financial excitement! Dive headfirst into the electrifying universe of Fast Payout Casinos where each spin, roll, and deal promises not only riveting entertainment but also rapid monetary rewards. Fast Payout Casinos https://techguide.io/ are engineered to cater to thrill-seekers hungry for swift victories and immediate gratification. Say goodbye to languorous withdrawal times because here, winnings flow seamlessly into your pocket almost as fast as you win them! Engage in an array of pulsating games that merge skill and chance, fueling relentless excitement amidst high stakes. Relinquish the fatigue of your workday behind fiery slots or strategic card games designed with cutting-edge technology and flawless interfaces. Energize your passions while fortifying your bankroll in this perfect combination of leisure and lucrative hustle!

Was this helpful?

More Java discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Java Blogs

Guides, tips and career advice on Java from JanBask experts.