1 Replies - 1739 Views - Last Post: 02 February 2011 - 04:40 PM Rate Topic: -----

#1 smthakkar23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-February 11

petals around the rose game

Posted 01 February 2011 - 06:55 PM

I am creating a petals around the rose game for cs class and im lost, im unsure of how to generate six random numbers and how present 5 dice that correspond to those random numbers
this is my code so far

import java.util.Scanner;		  // allows user input from the keyboard
import java.util.Random;          // allows getting a random number


public class petals {
	 public static void main(String[] args)
	    {       
	        // variable declarations
	        Scanner keyboard = new Scanner( System.in);     // used to read user input
	        Random randomNumberGenerator = new Random(1); 	// Use 1 as the random seed, to give reproducible results 
	        											    //     between program runs.  Removing the 1 will give different
	        												//     results each time.
	        int userInput;
	        
	        // Display identifying information
						System.out.println();
	                 
	        // Display game instructions 
			System.out.println("Welcome to the Petals of the Rose puzzle.  On each turn you will  \n" 
			                 + "be presented with 5 dice values which are randomly chosen.  These  \n"
			                 + "dice represent a numerical value, which will always be even.  If   \n"
			                 + "you can correctly identify the right answer 6 times in a row, you  \n"
			                 + "become an official \"Keeper of the Rose.\" " 
			                 + "   \n"
			                 + "For example consider the following dice:  \n"
			                 + "-------  \n"
			                 + "|*    |  \n"
			                 + "|  *  |  \n"
			                 + "|    *|  \n"
			                 + "-------  \n"
			                 + "  \n"
			                 + "-------  \n"
			                 + "|*   *|  \n"
			                 + "|  *  |  \n"
			                 + "|*   *|  \n"
			                 + "-------  \n"
			                 + "  \n"
			                 + "-------  \n"
			                 + "|*   *|  \n"
			                 + "|*   *|  \n"
			                 + "|*   *|  \n"
			                 + "-------  \n"
			                 + "  \n"
			                 + "-------  \n"
			                 + "|     |  \n"
			                 + "|  *  |  \n"
			                 + "|     |  \n"
			                 + "-------  \n"
			                 + "  \n"
			                 + "-------  \n"
			                 + "|*   *|  \n"
			                 + "|     |  \n"
			                 + "|*   *|  \n"
			                 + "-------  \n"
			                 + "  \n"
			                 + "The answer to this would be 6. OK, here we go:   \n");
			
			// Get a random number
			int randomValue = randomNumberGenerator.nextInt( 6) + 1;   // get random number 0..5 and add 1 to it
			System.out.println("randomValue is: " + randomValue);
			
			// Display the "graphical" representation for the number 5
			System.out.println("-------  \n"
					         + "|*   *|  \n"
					         + "|  *  |  \n"
					         + "|*   *|  \n"
					         + "-------  \n");
			// and so on...
			
			System.out.print("Enter some number to use in the following loop: ");
			userInput = keyboard.nextInt();
			
			if( userInput == 5) {
				System.out.println("You entered a 5.");
			}
			else {
				System.out.println("You did NOT enter a 5.");
			}

			
			// If you repeatedly call the random number generator, you will start to get different values
			int counter = 0;
			System.out.print("Random values are: ");
			while( counter < userInput) {
				randomValue = randomNumberGenerator.nextInt( 6) + 1;   // get random number 0..5 and add 1 to it
				System.out.print( randomValue + " ");
				counter++;	// add 1 to the counter to avoid having an infinite loop
			}
			
			System.out.println("Exiting program...");
	    }//end method main()



}


Edited by Dogstopper: :code:

Is This A Good Question/Topic? 0
  • +

Replies To: petals around the rose game

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: petals around the rose game

Posted 02 February 2011 - 04:40 PM

You can use a String array constant to hold your String representation for the dice:
public static final String[] DICE = { "draw dice1 here", "draw dice2 here", "draw dice3 here" ... };

"draw dice here" refers to the dices you print in your code.

use a Random object to generate a random nuber between 0 - 5, like:
[code]Random r = new Random();
int rand = r.nextInt(5);


after getting a value in rand, print from the DICE constant array the correct index of the dice. i.e. if you got 0, ptint the String object stored in index 0 in the DICE array.

since array indices are between 0 - 5, I refer to dice values as 0 - 5. but you can always use rand + 1 to refer to the actual dice values 1 - 6.

don't forget to import:
import java.util.Random;

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1