DEV Community

Mohammed mhanna
Mohammed mhanna

Posted on

🎲 Build a Dice Rolling Simulator in Java

I created a dice simulator in Java that lets you choose how many dice you want to roll and then shows the result as ASCII art 🎨.

πŸ‘‰ Full project on GitHub:
Dice Simulator – Source Code

In this post, I’ll break down the core ideas of the project step by step so even beginners can follow along.


πŸ“ 1. The Goal

The simulator should:

Ask the user how many dice they want to roll.

Generate a random number (1–6) for each die.

Show the result using ASCII art (like βš€, ⚁, βš‚).

Handle mistakes (like typing letters instead of numbers).

This small project helps you practice:

Input handling

Random numbers

Loops

Clean method design


πŸ’» 2. Handling User Input

We use Scanner to read the user’s choice and a loop to make sure the input is valid:

Scanner input = new Scanner(System.in);
boolean appCompleted = false;

do {
    try {
        System.out.println("How many dice would you like to roll?");
        int numberOfDice = input.nextInt();

        if (numberOfDice <= 0) {
            System.out.println("Please enter a positive number.");
            continue;
        }

        appCompleted = true;
        // Roll dice here...

    } catch (InputMismatchException e) {
        System.out.println("This is not a valid number.");
        input.next(); // clear invalid input
    }
} while (!appCompleted)
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Takeaway: Input must be checked! Without this, the program could crash if the user types something unexpected.


🎲 3. Rolling the Dice

We use Random to simulate the dice roll:

Random random = new Random();
int rolledNumber = random.nextInt(6) + 1;

Why +1? Because nextInt(6) gives numbers from 0–5, but dice go from 1–6.

πŸ‘‰ Takeaway: This is how Java simulates randomness.


🎨 4. Displaying Dice Faces

The coolest part: ASCII art!

static String display(int value) {
    return switch (value) {
        case 1 -> "---------\n|       |\n|   o   |\n|       |\n---------";
        case 2 -> "---------\n| o     |\n|       |\n|     o |\n---------";
        // ... and so on for 3, 4, 5, 6
        default -> "Not a valid die face";
    };
}

Enter fullscreen mode Exit fullscreen mode

This method maps numbers to pictures of dice. It uses a modern switch expression for clarity.

πŸ‘‰ Takeaway: Always separate β€œlogic” (rolling) from β€œpresentation” (printing). Makes your code much cleaner.


πŸ›‘οΈ 5. Putting It Together

When you run it:

  1. You’re asked how many dice to roll.

  2. Each roll is random.

  3. ASCII dice are displayed.

  4. Invalid inputs don’t crash the program.

This shows defensive programming in action.


πŸš€ 6. Possible Enhancements

Want to take this further? Try adding:

A roll until doubles feature.

A statistics counter (how often each face shows up).

A GUI version using JavaFX for real dice images.


πŸ”— Full Code

πŸ‘‰ Dice Simulator in Java (GitHub Repo)


🎯 Final Thoughts

This project may be small, but it demonstrates important concepts in Java:

Input validation

Random number generation

Clean method separation

Defensive coding


πŸ’¬ Question for you:
If you were to improve this dice simulator, what would you add first β€” statistics, a GUI, or multiplayer mode?

Top comments (0)