A Simple Python Dice Rolling Simulator
Tags: python, portfolioprojects, programming
Published: 2023-09-04 17:08:16

Dice games have been popular for centuries, and rolling dice is a fundamental part of many board games and tabletop role-playing games. In this blog, we'll explore a simple Python program that simulates rolling a 6-sided die and displays the result using ASCII art. This entertaining project can be a fun addition to your Python coding repertoire and is a great way to practice your programming skills.

Github link:
[terminal-dice-roller](https://github.com/Gerry-Aballa/terminal-dice-roller)

The Code:
Let's break down the code step by step.

1. **Dice Art**:
   ```python
   DICE_ART = {
       1: (
           "┌─────────┐",
           "│         │",
           "│    ●    │",
           "│         │",
           "└─────────┘",
       ),
       # ... (similar entries for values 2 to 6)
   }
   ```
   This dictionary (`DICE_ART`) contains ASCII art representations of the six faces of a standard six-sided die. Each face is represented as a tuple of strings, which will be used later to display the dice face when rolled.

2. **Constants**:
   ```python
   DIE_HEIGHT = len(DICE_ART[1])
   DIE_WIDTH = len(DICE_ART[1][0])
   DIE_FACE_SEPARATOR = " "
   ```
   These constants define the height and width of a die face and the separator character used between multiple dice faces when displayed.

3. **`roll_dice()` Function**:
   ```python
   def roll_dice():
       """Simulate rolling a 6-sided die and return the result."""
       return random.randint(1, 6)
   ```
   The `roll_dice()` function generates a random integer between 1 and 6 to simulate rolling a standard six-sided die.

4. **`display_dice(die_value)` Function**:
   ```python
   def display_dice(die_value):
       """Display the ASCII art for the given die value."""
       if die_value in DICE_ART:
           die_face = DICE_ART[die_value]
           for line in die_face:
               print(line)
   ```
   This function takes the result of rolling the die and displays the corresponding ASCII art representation. It checks if the provided `die_value` exists in the `DICE_ART` dictionary and prints the associated art line by line.

5. **`main()` Function**:
   ```python
   def main():
       print("Welcome to the Dice Rolling Simulator!")

       while True:
           user_input = input("Press Enter to roll the dice or 'q' to quit: ").strip()
           
           if user_input == 'q':
               print("Goodbye!")
               break
           
           if user_input == '':
               die_value = roll_dice()
               display_dice(die_value)
           else:
               print("Invalid input. Press Enter to roll or 'q' to quit.")
   ```
   The `main()` function serves as the entry point of the program. It displays a welcome message and enters a loop that waits for user input. The user can press Enter to roll the dice or 'q' to quit. It handles user input validation and calls the appropriate functions.

6. **`if __name__ == "__main__":`**:
   This conditional statement ensures that the `main()` function is executed only if the script is run directly, not if it's imported as a module into another script.

**Example**
![A simple python dice rolling simulator](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t2slx6qgoob16qiiv88n.png)

**Attention**
You can further enhance this project by adding features like keeping track of the roll history or integrating it into a larger game. You can contribute to this [repo](https://github.com/Gerry-Aballa/terminal-dice-roller) by implementing these. Happy coding and rolling!