dimanche 16 avril 2023

DnD Dice Roll Simulation

I am Trying to make a simulated dice rolling game for my friends. I want to utilize two classes Dice and a child class VarDice which will let user choose how many sides. However, I am struggling to get my derived class to work properly.

This is my parent class :

import java.util.Random;

public class Dice {
   
   protected int numRolled;
   final Random rand = new Random();
    
    
   public void roll() {
        numRolled = rand.nextInt(5) + 1;
   }
   public int getNumRolled() {
      return numRolled;
   }
   
}

This is the derived class:

    import java.util.Random;
    
    
    public class VarDice extends Dice {
    
       public static void main(String[] args) {
            Scanner scnr = new Scanner(System.in);
            int numSides;
            
            public void setNumSides(int sides) {
                numSides = sides;
            }
            
           public int getNumSides() {
              return numSides;  
            }
            
            @Override
            public void roll() {
            numRolled = rand.nextInt(numSides) + 1;
           }
    
    }
    }

This is finally my main code:

import java.util.Scanner; 

    public class Main {
    
       public static void main(String[] args) {
            Scanner scnr = new Scanner(System.in);
             int numSides;

             VarDice dice = new VarDice();

             numSides = scar.nextInt();

             dice.setNumSides(numSides);
             
             dice.roll();
             System.out.println("The dice come up " + dice.getNumRolled()); 
        
    
        }
    
    }

I want the user to be able to input how many sides are on their dice and how many dice, but my VarDice class is not overriding the roll method very well and I am a little bit lost on the errors I am getting... I appreciate the help and hope the format is appropriate.




Aucun commentaire:

Enregistrer un commentaire