28 - Virtual Dice Roller with Multiple Dice

Description

Create a React application that provides a virtual dice roller with multiple dice. The application should allow users to choose the number of dice they want to roll (from a dropdown menu or buttons), support various types of dice (e.g., D4, D6, D8, D10, D12, D20) with selectable options, roll the chosen number of dice and display the individual results for each die, and calculate and display the sum of all rolled dice.

Algorithm

  1. Create a state variable to store the number of dice and the type of dice.

  2. Define a function to handle the selection of the number of dice.

  3. Define a function to handle the selection of the type of dice.

  4. Define a function to roll the dice and calculate the results.

  5. Render the virtual dice roller interface.

Classes

  • DiceRoller: A component that represents the virtual dice roller interface.

  • DiceSelector: A component that represents the dice selection menu.

  • Dice RollerButton: A component that represents the button to roll the dice.

  • DiceResults: A component that represents the display of the dice results.

Code

DiceRoller.js

import React, { useState } from 'react';
import DiceSelector from './DiceSelector';
import DiceRollerButton from './DiceRollerButton';
import DiceResults from './DiceResults';

function DiceRoller() {
  const [numDice, setNumDice] = useState(1);
  const [diceType, setDiceType] = useState('D6');
  const [results, setResults] = useState([]);

  const handleNumDiceChange = (event) => {
    setNumDice(event.target.value);
  };

  const handleDiceTypeChange = (event) => {
    setDiceType(event.target.value);
  };

  const handleRollDice = () => {
    const rolledDice = [];
    for (let i = 0; i < numDice; i++) {
      const result = rollDice(diceType);
      rolledDice.push(result);
    }
    setResults(rolledDice);
  };

  const rollDice = (diceType) => {
    const sides = getNumSides(diceType);
    return Math.floor(Math.random() * sides) + 1;
  };

  const getNumSides = (diceType) => {
    switch (diceType) {
      case 'D4':
        return 4;
      case 'D6':
        return 6;
      case 'D8':
        return 8;
      case 'D10':
        return 10;
      case 'D12':
        return 12;
      case 'D20':
        return 20;
      default:
        return 6;
    }
  };

  return (
    <div>
      <h2>Virtual Dice Roller</h2>
      <DiceSelector
        numDice={numDice}
        onNumDiceChange={handleNumDiceChange}
        diceType={diceType}
        onDiceTypeChange={handleDiceTypeChange}
      />
      <DiceRollerButton onRollDice={handleRollDice} />
      <DiceResults results={results} />
    </div>
  );
}

export default DiceRoller;

DiceSelector.js

import React from 'react';

function DiceSelector({ numDice, onNumDiceChange, diceType, onDiceTypeChange }) {
  return (
    <div>
      <label>
        Number of Dice:
        <select value={numDice} onChange={onNumDiceChange}>
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
          <option value="4">4</option>
          <option value="5">5</option>
        </select>
      </label>
      <label>
        Type of Dice:
        <select value={diceType} onChange={onDiceTypeChange}>
          <option value="D4">D4</option>
          <option value="D6">D6</option>
          <option value="D8">D8</option>
          <option value="D10">D10</option>
          <option value="D12">D12</option>
          <option value="D20">D20</option>
        </select>
      </label>
    </div>
  );
}

export default DiceSelector;

DiceRollerButton.js

import React from 'react';

function DiceRollerButton({ onRollDice }) {
  return (
    <button onClick={onRollDice}>
      Roll Dice
    </button>
  );
}

export default DiceRollerButton;

DiceResults.js

import React from 'react';

function DiceResults({ results }) {
  return (
    <div>
      <h3>Dice Results:</h3>
      <ul>
        {results.map((result, index) => (
          <li key={index}>
            <span>Dice {index + 1}: {result}</span>
          </li>
        ))}
      </ul>
      <p>Sum: {results.reduce((a, b) => a + b, 0)}</p>
    </div>
  );
}

export default DiceResults;

Explanation

The DiceRoller component is the main component that renders the virtual dice roller interface. It uses the useState hook to store the number of dice, the type of dice, and the results of the roll.The DiceSelector component is a dropdown menu that allows the user to select the number of dice and the type of dice.The DiceRollerButton component is a button that triggers the roll of the dice.The DiceResults component displays the results of the roll, including the individual results for each die and the sum of all the dice.The rollDice function simulates the roll of a single die, and the getNumSides function returns the number of sides for a given type of die.

Possible Future Enhancements

  • Add more types of dice, such as D100 or custom dice.

  • Allow users to save their favorite dice configurations.

  • Add a feature to roll multiple sets of dice at once.

  • Create a mobile app version of the virtual dice roller.

  • Add sound effects and animations to enhance the user experience.

  • Integrate with popular tabletop gaming platforms.

Last updated