A free Java Dice Game

Discussion in 'JavaScript' started by Grimmfang, Sep 9, 2010.

  1. #1
    I made this recently for fun so I'll post it here.

    Note requires Breezyswing, and TerminalIO, also I highly recommend Jcreator as a compiler.

    Enjoy :)

    dicegame.java
    /*********** Dice Game for Javascript ***********/
    /*********** Grimmfang *************************/
    import TerminalIO.*;
    public class dicegame {
        public dicegame() {
        }
        public static void main(String[] args) {
            KeyboardReader kbreader=new KeyboardReader();
            int End=1;
        
      System.out.println("A Java based Dice game.");
      do
        {
       int D=kbreader.readInt("How many Dice would you like to roll? ");
       int S=kbreader.readInt("How many sides would you like those Dice to have? ");
       Dice mydice=new Dice(D,S);
       System.out.println("\n---- You rolled "+D+" Dice, each with "+S+" Sides. ----");
       int Player= mydice.rollDice();
       System.out.println("\nYou rolled a "+Player+"");
       int Computer= mydice.rollDice();
       System.out.println("\nThe Computer rolled a "+Computer+"");
       if (Player>Computer){
        System.out.println("\n(Winner) Great, You won.");
       }else if(Player<Computer){
        System.out.println("\n(Loser) Better luck next time.");
       }else{
        System.out.println("\n(Tie) You tied with the Computer.");
       }
            End=kbreader.readInt("\nWould you like to Play again?(Yes=1, No=0) ");
            }while(End==1);
               
        }
    }
    Code (markup):
    Also the Dice.java class is required to run the game.

    Dice.java
    
    //This class represents information and methods relating to a set of dice
    public class Dice
    {
    
    	//Attributes
    	
    	private int numdice;    //number of dice
    	private int numsides;   //number of sides
    		
    	
    	//Constructors
    	
    	public Dice()      //default constructor
    	{
    		numdice=2;
    		numsides=6;
    	}	
    	
            public Dice(int d, int s)      //allows user to set sides and number of dice
    	{
    		numdice=d;
    		numsides=s;
    	}
    	
    	//Accessors
    	
    	public int getSides() //accesses the number of sides
    	{
    	  return numsides;
    	}
    	
    	public int getNumDice() //accesses the number of dice
    	{
    	  return numdice;
    	}
    	
    	
    	public int rollDice()   // returns a random number representing number rolled by dice
    	{
    	   int total=0;	
    	   for (int i=0; i<numdice; i++)
    	      total+=(int)(Math.random()*numsides)+1;
    	   return total;  
    	}
    		
    	
    				
    	public static void main(String [] args)  //example of how to use the Dice class
    	{
    		
    		Dice mydice=new Dice(3,6);  //creates a set of three six-sided dice
    		System.out.println(mydice.rollDice());  //rolls the dice!
    		
    	}
    	
    	
    }
    
    Code (markup):
     
    Grimmfang, Sep 9, 2010 IP