Below is a C# Dice Roller program that allows a user to roll one or more dice of various types (e.g., a 6-sided die, 20-sided die, etc.).
The program uses a console-based interface and generates random numbers to simulate dice rolls.
Full Code with Explanation
using System; class DiceRoller { static void Main(string[] args) { Console.WriteLine("Welcome to the Dice Roller!"); while (true) { Console.WriteLine("\nChoose an option:"); Console.WriteLine("1. Roll a standard 6-sided die"); Console.WriteLine("2. Roll a custom die (choose number of sides)"); Console.WriteLine("3. Roll multiple dice"); Console.WriteLine("4. Exit"); Console.Write("Enter your choice: "); string choice = Console.ReadLine(); switch (choice) { case "1": RollSingleDie(6); break; case "2": RollCustomDie(); break; case "3": RollMultipleDice(); break; case "4": Console.WriteLine("Goodbye!"); return; default: Console.WriteLine("Invalid choice. Please try again."); break; } } } static void RollSingleDie(int sides) { Random random = new Random(); int result = random.Next(1, sides + 1); // Generates a number between 1 and 'sides' Console.WriteLine($"You rolled a {sides}-sided die and got: {result}"); } static void RollCustomDie() { Console.Write("Enter the number of sides for the die: "); if (int.TryParse(Console.ReadLine(), out int sides) && sides > 0) { RollSingleDie(sides); } else { Console.WriteLine("Invalid input. Please enter a positive integer."); } } static void RollMultipleDice() { Console.Write("Enter the number of dice to roll: "); if (!int.TryParse(Console.ReadLine(), out int diceCount) || diceCount <= 0) { Console.WriteLine("Invalid input. Please enter a positive integer."); return; } Console.Write("Enter the number of sides for each die: "); if (!int.TryParse(Console.ReadLine(), out int sides) || sides <= 0) { Console.WriteLine("Invalid input. Please enter a positive integer."); return; } Random random = new Random(); int total = 0; Console.WriteLine("\nRolling the dice..."); for (int i = 1; i <= diceCount; i++) { int roll = random.Next(1, sides + 1); total += roll; Console.WriteLine($"Die {i}: {roll}"); } Console.WriteLine($"Total of all dice: {total}"); } }
Explanation of the Code
- Program Structure:
- The program offers a menu-driven interface for rolling standard 6-sided dice, custom-sided dice, or multiple dice.
- The main loop continues until the user selects the “Exit” option.
Method 1: RollSingleDie(int sides)
- Simulates rolling a single die with a specified number of sides.
- Uses the Random class to generate a random integer between 1 and sides.
- Displays the result of the roll.
static void RollSingleDie(int sides) { Random random = new Random(); int result = random.Next(1, sides + 1); // Generates a number between 1 and 'sides' Console.WriteLine($"You rolled a {sides}-sided die and got: {result}"); }
Example Input/Output:
- Input: Roll a 6-sided die.
- Output: You rolled a 6-sided die and got: 4
Method 2: RollCustomDie()
- Prompts the user to enter the number of sides for the die.
- Validates the input to ensure it is a positive integer.
- Calls RollSingleDie to perform the roll.
static void RollCustomDie() { Console.Write("Enter the number of sides for the die: "); if (int.TryParse(Console.ReadLine(), out int sides) && sides > 0) { RollSingleDie(sides); } else { Console.WriteLine("Invalid input. Please enter a positive integer."); } }
Example Input/Output:
- Input: Enter 20 sides.
- Output: You rolled a 20-sided die and got: 14
Method 3: RollMultipleDice()
- Prompts the user to specify the number of dice and the number of sides per die.
- Rolls each die individually, displays each result, and calculates the total.
- Uses a for loop to perform multiple rolls.
static void RollMultipleDice() { Console.Write("Enter the number of dice to roll: "); if (!int.TryParse(Console.ReadLine(), out int diceCount) || diceCount <= 0) { Console.WriteLine("Invalid input. Please enter a positive integer."); return; } Console.Write("Enter the number of sides for each die: "); if (!int.TryParse(Console.ReadLine(), out int sides) || sides <= 0) { Console.WriteLine("Invalid input. Please enter a positive integer."); return; } Random random = new Random(); int total = 0; Console.WriteLine("\nRolling the dice..."); for (int i = 1; i <= diceCount; i++) { int roll = random.Next(1, sides + 1); total += roll; Console.WriteLine($"Die {i}: {roll}"); } Console.WriteLine($"Total of all dice: {total}"); }
Example Input/Output:
- Input: Roll 3 dice with 8 sides each.
- Output:
Rolling the dice... Die 1: 5 Die 2: 3 Die 3: 7 Total of all dice: 15
Menu and Input Validation
- The Main method provides a user-friendly menu.
- Validates user input using int.TryParse to ensure valid integers.
- Handles invalid input gracefully.
Features
- Random Dice Rolls:
- The Random class is used to generate numbers simulating dice rolls.
- Custom Dice:
- Allows the user to roll dice with any number of sides.
- Multiple Dice Rolls:
- Supports rolling multiple dice, showing individual results, and calculating the total.
- Input Validation:
- Ensures that the user inputs valid numbers for dice count and sides.
Sample Output
Run the program:
Welcome to the Dice Roller! Choose an option: 1. Roll a standard 6-sided die 2. Roll a custom die (choose number of sides) 3. Roll multiple dice 4. Exit Enter your choice: 1 You rolled a 6-sided die and got: 4
Custom-sided dice:
Enter your choice: 2 Enter the number of sides for the die: 20 You rolled a 20-sided die and got: 14
Multiple dice:
Enter your choice: 3 Enter the number of dice to roll: 3 Enter the number of sides for each die: 8 Rolling the dice... Die 1: 6 Die 2: 2 Die 3: 8 Total of all dice: 16
Exit:
Enter your choice: 4 Goodbye!
Ideas for Enhancement
- Add Advanced Options:
- Include features like rolling with modifiers (e.g., 2d6 + 3).
- Save Results:
- Log the results of each roll to a file for future reference.
- Create a GUI:
- Use Windows Forms or WPF to create a graphical version of the dice roller.
- Simulate Real Dice Physics:
- Integrate physics libraries to simulate dice rolls visually.
This program is a fun and interactive way to learn about randomization, user input, and control structures in C#!