Tuesday 21 January 2014

Snake dice program in java

The below code is for Snake dice
Snake dice program means i think all of you know snake chart and  dice game,
based on the snake dice game i have implemented the below program for the same.
In many interviews they are asking the below program.

Requirement: Please write a program which performs snake dice game.


       /*  An object of class PairOfDice represents a pair of dice,
         where each die shows a number between 1 and 6.  The dice
         can be rolled, which randomizes the numbers showing on the
         dice.
      */

      /* Roll the dice until they come up snake eyes(if the dice total is 2). */



package com.naga.test;

import java.util.Scanner;

public class PairOfDice {

 private int die1;
 private int die2;

 private static int singleDie; // 6,5,4,3,2,1

 public void rollDice() {
  die1 = (int) (Math.random() * 6) + 1;
  die2 = (int) (Math.random() * 6) + 1;
 }

 public void rollSingleDice() {
  singleDie = (int) (Math.random() * 6) + 1;
 }

 public int getTotal() {
  return die1 + die2;
 }

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  PairOfDice dice; // A variable that will refer to the dice.
  int rollCount; // Number of times the dice have been rolled.

  dice = new PairOfDice(); // Create the PairOfDice object.
  rollCount = 0;

  /*
   * Roll the dice until they come up snake eyes.(dies total is equal to
   * 2)
   */
  int enterdValue;
  String enterdString = null;
  do {
   System.out
     .print("Please select how many dices are you going to use to play this game:  ");
   Scanner sc = new Scanner(System.in);
   enterdValue = sc.nextInt();

   if (enterdValue == 2 || enterdValue == 1) {
    if (enterdValue == 2) {
     do {
      dice.rollDice();
      System.out.println("The dies come up with dies1= "
        + dice.die1 + " and dies2= " + dice.die2);
      rollCount++;

     } while (dice.getTotal() != 2);

     /* Report the number of rolls. */
     System.out.println("\nIt took " + rollCount
       + " rolls to get a 2(means reached snake eyes).");
     System.out
       .print("Do you want to continue the playing this game(yes/no): ");
     enterdString = sc.next();
    } else {
     do {
      dice.rollSingleDice();
      System.out.println("The dies come up with dice= "
        + singleDie);
      rollCount++;

     } while (singleDie != 2);

     /* Report the number of rolls. */
     System.out.println("\nIt took " + rollCount
       + " rolls to get a 2(means reached snake eyes).");
     System.out
       .print("Do you want to continue the playing this game(yes/no): ");
     enterdString = sc.next();
    }

   } else {
    System.out.println("Please enter only 1 or 2.... ");
   }
  } while (enterdString.equalsIgnoreCase("yes") || (enterdValue != 2));

 }

}
OUTPUT:
Please select how many dices are you going to use to play this game: 2 The dies come up with dies1= 6 and dies2= 2 The dies come up with dies1= 3 and dies2= 2 The dies come up with dies1= 4 and dies2= 2 The dies come up with dies1= 4 and dies2= 4 The dies come up with dies1= 5 and dies2= 5 The dies come up with dies1= 1 and dies2= 5 The dies come up with dies1= 3 and dies2= 6 The dies come up with dies1= 3 and dies2= 2 The dies come up with dies1= 6 and dies2= 1 The dies come up with dies1= 5 and dies2= 1 The dies come up with dies1= 3 and dies2= 4 The dies come up with dies1= 6 and dies2= 5 The dies come up with dies1= 4 and dies2= 6 The dies come up with dies1= 5 and dies2= 4 The dies come up with dies1= 3 and dies2= 3 The dies come up with dies1= 2 and dies2= 3 The dies come up with dies1= 1 and dies2= 5 The dies come up with dies1= 1 and dies2= 1 It took 18 rolls to get a 2(means reached snake eyes). Do you want to continue the playing this game(yes/no): yes Please select how many dices are you going to use to play this game: 1 The dies come up with dice= 1 The dies come up with dice= 2 It took 20 rolls to get a 2(means reached snake eyes). Do you want to continue the playing this game(yes/no): 3 Please select how many dices are you going to use to play this game:


No comments:

Post a Comment