# petals around the rose game

Page 1 of 1

## 1 Replies - 2862 Views - Last Post: 02 February 2011 - 04:40 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=213429&amp;s=b4802fd0375d132007d679adab9fed74&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 smthakkar23

Reputation: 0
• 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:

Is This A Good Question/Topic? 0

## Replies To: petals around the rose game

### #2 japanir

• jaVanir

Reputation: 1012
• 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;
```