Results 1 to 4 of 4

Thread: Dice Game

  1. #1
    DD13 is offline Member
    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    Default Dice Game

    hey everyone!
    im new to java and i am having a difficult time with this dice game. It works, however there are a few things i need to add and i do not know how to do them. I have to implement a boolean Snakeeyes as well as a boolean matching pair in the TwoDice class, which i do not know how to do. i also need to place the methods giveDirection, play, and reportWinner in the DiceGame class. i am having a really difficult time, and i do not know how to finish this. any help would be appreciated!

    this is my DiceGame class:


    package dicegame;
    /**
    * This program creates a dice game
    *
    *The user faces the computer in a game of dice
    *and each player rolls both dice
    *
    * The rules are:
    * 1.The player who rolls the highest face value wins
    * 2.If both players roll the same value, it is a draw
    * 3.If a player rolls a pair of ones, they lose(if both players
    * roll a pair of ones, it is a draw)
    * 4.If a player rolls matching pairs wins(if both player rolls
    * doubles, the player who rolled the highest value wins)
    *
    *
    */
    import java.util.Scanner;

    /**
    *
    *
    */

    public class DiceGame
    {

    private static String reportWinner;


    public static void main(String[] args)
    {

    Scanner keyboard = new Scanner(System.in);
    DiceGame game = new DiceGame();

    TwoDice player1 = new TwoDice();
    TwoDice player2 = new TwoDice();

    System.out.println("Would you like to throw first? "
    + "Type y or n: ");
    String response = keyboard.nextLine();

    //If the user wants to go first, they wil throw first
    if (response.charAt(0) == 'y')
    {

    System.out.println("You rolled a " + player1.getFaceValue() +
    " and a " + player1.getFaceValueDieTwo());
    System.out.println("Your total is: " + player1.getDiceSum());
    System.out.println("The computer rolled a " + player2.getFaceValueDieOne()
    + " and a " + player2.getFaceValueDieTwo());
    System.out.println("The computer's total is: " + player2.getDiceSum());

    }
    //If user does not want to go first, the computer throws first
    else if(response.charAt(0)== 'n') {

    System.out.println("The computer rolled a " + player2.getFaceValueDieOne()
    + " and a " + player2.getFaceValueDieTwo());
    System.out.println("The computer's total is: " + player2.getDiceSum());
    System.out.println("You rolled a " + player1.getFaceValueDieOne() +
    " and a " + player1.getFaceValueDieTwo());
    System.out.println("Your total is: " + player1.getDiceSum());

    }

    //If the players sum is greater than the computers sum, the player wins


    if (player1.getDiceSum()> player2.getDiceSum())
    {reportWinner = "You win!";}

    else if(player1.getDiceSum()<player2.getDiceSum() )
    {reportWinner = "You lose!";}

    //If the player and the computer throw the same value,it results in a draw
    else if (player1.getDiceSum() == player2.getDiceSum())
    {reportWinner = "It's a draw!";}




    //Displays the winner
    System.out.println(reportWinner);

    }//end main
    }//end Dice Game






    this is my TwoDice class:

    package dicegame;

    /**
    *
    *
    */
    public class TwoDice {

    //Represents the number of sides on the die
    public int n =6;


    public TwoDice(int n){

    this.n = n;
    }

    //Displays the current value shown on die
    public int faceValue;

    TwoDice() {

    }
    /**
    * Get the value of faceValue
    *
    * @return the value of faceValue
    */
    public int getFaceValue() {
    faceValue = (int)(Math.random() * n) + 1;
    return faceValue;
    }

    /**
    * Set the value of faceValue
    *
    * @param faceValue new value of faceValue
    */
    public void setFaceValue(int faceValue) {
    this.faceValue = faceValue;
    }

    //Rolls the pair of dice
    public int roll;

    public int rollDice (){

    faceValue = (int)(Math.random() * n) + 1;
    return faceValue;
    }

    //Rolls one of the dice
    private int FaceValueDieOne;

    /**
    * Get the value of FaceValueDieOne
    *
    * @return the value of FaceValueDieOne
    */
    public int getFaceValueDieOne() {
    FaceValueDieOne = (int)(Math.random() * n) + 1;
    return FaceValueDieOne;
    }

    //Rolls one of the dice
    private int FaceValueDieTwo;

    /**
    * Get the value of FaceValueDieTwo
    *
    * @return the value of FaceValueDieTwo
    */
    public int getFaceValueDieTwo() {
    FaceValueDieTwo = (int)(Math.random() * n) + 1;
    return FaceValueDieTwo;
    }

    //Displays the face value of the two dice were they
    //were first rolled
    private int ValueOfDice;

    /**
    * Get the value of ValueOfDice
    *
    * @return the value of ValueOfDice
    */
    public int getValueOfDice() {
    ValueOfDice = faceValue;
    return ValueOfDice;
    }

    //Displays a string
    @Override
    public String toString() {
    return "The Dice rolled are a " + FaceValueDieOne
    + " and a " + FaceValueDieTwo;
    }
    private int DiceSum;

    /**
    * Get the value of DiceSum
    *
    * @return the value of DiceSum
    */
    public int getDiceSum() {
    DiceSum = FaceValueDieOne + FaceValueDieTwo;
    return DiceSum;
    }
    private boolean matchingPair;

    /**
    * Get the value of matchingPair
    *
    * @return the value of matchingPair
    */
    public boolean isMatchingPair() {

    return matchingPair;
    }


    private boolean snakeEyes;

    /**
    * Get the value of snakeEyes
    *
    * @return the value of snakeEyes
    */
    public boolean isSnakeEyes() {

    return snakeEyes;
    }

    }//end TwoDice
    Last edited by DD13; 03-28-2013 at 04:51 PM.

  2. #2
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: Dice Game

    first of all please put your code inside "[ CODE ] [ /CODE ]" <-- without the " " ... this will make your code more clear...
    Last edited by ShadowWalker; 03-28-2013 at 04:59 PM.

  3. #3
    DD13 is offline Member
    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    Default Re: Dice Game

    Dice game class

    Java Code:
     
    package dicegame;
     /**
     * This program creates a dice game
     * 
    *The user faces the computer in a game of dice
     *and each player rolls both dice
     *
     * The rules are:
     * 1.The player who rolls the highest face value wins
     * 2.If both players roll the same value, it is a draw
     * 3.If a player rolls a pair of ones, they lose(if both players
     * roll a pair of ones, it is a draw)
     * 4.If a player rolls matching pairs wins(if both player rolls
     * doubles, the player who rolled the highest value wins)
     * 
    * 
    */ 
    import java.util.Scanner;
     
    /**
     *
     * 
    */
     
    public class DiceGame
     {
     
    private static String reportWinner;
     
    
    public static void main(String[] args)
     {
     
    Scanner keyboard = new Scanner(System.in);
     DiceGame game = new DiceGame();
     
    TwoDice player1 = new TwoDice();
     TwoDice player2 = new TwoDice();
     
    System.out.println("Would you like to throw first? "
     + "Type y or n: ");
     String response = keyboard.nextLine();
     
    //If the user wants to go first, they wil throw first 
    if (response.charAt(0) == 'y')
     { 
    
    System.out.println("You rolled a " + player1.getFaceValue() +
     " and a " + player1.getFaceValueDieTwo());
     System.out.println("Your total is: " + player1.getDiceSum());
     System.out.println("The computer rolled a " + player2.getFaceValueDieOne() 
    + " and a " + player2.getFaceValueDieTwo());
     System.out.println("The computer's total is: " + player2.getDiceSum());
     
    }
     //If user does not want to go first, the computer throws first
     else if(response.charAt(0)== 'n') {
     
    System.out.println("The computer rolled a " + player2.getFaceValueDieOne()
     + " and a " + player2.getFaceValueDieTwo());
     System.out.println("The computer's total is: " + player2.getDiceSum());
     System.out.println("You rolled a " + player1.getFaceValueDieOne() +
     " and a " + player1.getFaceValueDieTwo());
     System.out.println("Your total is: " + player1.getDiceSum());
     
    }
     
    //If the players sum is greater than the computers sum, the player wins
     
    
    if (player1.getDiceSum()> player2.getDiceSum())
     {reportWinner = "You win!";}
     
    else if(player1.getDiceSum()<player2.getDiceSum() ) 
    {reportWinner = "You lose!";}
     
    //If the player and the computer throw the same value,it results in a draw
     else if (player1.getDiceSum() == player2.getDiceSum())
     {reportWinner = "It's a draw!";}
     
    
    
    
    //Displays the winner
     System.out.println(reportWinner);
     
    }//end main
     }//end Dice Game

    Two Dice class

    Java Code:
    package dicegame;
     
    /**
     *
     *
     */
     public class TwoDice {
     
    //Represents the number of sides on the die
     public int n =6;
     
    
    public TwoDice(int n){
     
    this.n = n;
     }
     
    //Displays the current value shown on die
     public int faceValue;
     
    TwoDice() {
     
    }
     /**
     * Get the value of faceValue
     *
     * @return the value of faceValue
     */
     public int getFaceValue() {
     faceValue = (int)(Math.random() * n) + 1;
     return faceValue;
     }
     
    /**
     * Set the value of faceValue
     *
     * @param faceValue new value of faceValue
     */
     public void setFaceValue(int faceValue) {
     this.faceValue = faceValue;
     }
     
    //Rolls the pair of dice 
    public int roll;
     
    public int rollDice (){
     
    faceValue = (int)(Math.random() * n) + 1;
     return faceValue;
     }
     
    //Rolls one of the dice
     private int FaceValueDieOne;
     
    /**
     * Get the value of FaceValueDieOne
     *
     * @return the value of FaceValueDieOne
     */
     public int getFaceValueDieOne() {
     FaceValueDieOne = (int)(Math.random() * n) + 1;
     return FaceValueDieOne;
     }
     
    //Rolls one of the dice
     private int FaceValueDieTwo;
     
    /**
     * Get the value of FaceValueDieTwo
     *
     * @return the value of FaceValueDieTwo
     */
     public int getFaceValueDieTwo() {
     FaceValueDieTwo = (int)(Math.random() * n) + 1;
     return FaceValueDieTwo;
     }
     
    //Displays the face value of the two dice were they
     //were first rolled
     private int ValueOfDice;
     
    /**
     * Get the value of ValueOfDice
     *
     * @return the value of ValueOfDice
     */
     public int getValueOfDice() {
     ValueOfDice = faceValue;
     return ValueOfDice;
     }
     
    //Displays a string
     @Override
     public String toString() {
     return "The Dice rolled are a " + FaceValueDieOne 
    + " and a " + FaceValueDieTwo;
     }
     private int DiceSum;
     
    /**
     * Get the value of DiceSum
     *
     * @return the value of DiceSum
     */
     public int getDiceSum() {
     DiceSum = FaceValueDieOne + FaceValueDieTwo;
     return DiceSum;
     }
     private boolean matchingPair;
     
    /**
     * Get the value of matchingPair
     *
     * @return the value of matchingPair
     */
     public boolean isMatchingPair() {
     
    return matchingPair;
     }
     
    
    private boolean snakeEyes;
     
    /**
     * Get the value of snakeEyes
     *
     * @return the value of snakeEyes
     */
     public boolean isSnakeEyes() {
     
    return snakeEyes;
     }
     
    }//end TwoDice
    Last edited by DD13; 03-28-2013 at 08:59 PM.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Dice Game

    Learn how to format code fore readability, and that variable names should start with a lowercase letter: Code Conventions for the Java Programming Language: Contents

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Pig dice game
    By dangyounoobs in forum New To Java
    Replies: 2
    Last Post: 12-09-2013, 04:21 AM
  2. Help With Dice Game Please
    By Visions in forum New To Java
    Replies: 2
    Last Post: 10-07-2012, 08:16 PM
  3. Help with a Dice Game.
    By HalfAZN in forum New To Java
    Replies: 30
    Last Post: 04-30-2012, 06:36 PM
  4. Dice Game
    By Rachel1991 in forum New To Java
    Replies: 1
    Last Post: 11-24-2011, 11:10 PM
  5. Help with a dice game.
    By hero in forum AWT / Swing
    Replies: 14
    Last Post: 07-26-2009, 11:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •