Help printing random dice
I started school for CS last semester and it is awesome. I have been able to get through most of the work with little help. I have a question (which I hope is simple) about printing out five random dice in a HORIZONTAL row, rather than vertical:
snippet:
Code Java:
for (int i = 0; i < 5; i++){
randomValue = randomNumberGenerator.nextInt( 6) + 1; // get random number 0..5 and add 1 to it
if (randomValue == 1){ // If statement for random # being 1
System.out.println("------- \n"
+ "| | \n"
+ "| * | \n" // Print out a 1 die
+ "| | \n"
+ "------- \n");
}
else if (randomValue == 2){ // If statement for random # being 2
System.out.println("------- \n"
+ "|* | \n"
+ "| | \n" // Print out a 2 die
+ "| *| \n"
+ "------- \n");
}
else if (randomValue == 3){
System.out.println("------- \n"
+ "|* | \n" // And so on...
+ "| * | \n"
+ "| *| \n"
+ "------- \n");
}
else if (randomValue == 4){
System.out.println("------- \n"
+ "|* *| \n"
+ "| | \n"
+ "|* *| \n"
+ "------- \n");
}
else if (randomValue == 5){
System.out.println("------- \n"
+ "|* *| \n"
+ "| * | \n"
+ "|* *| \n"
+ "------- \n");
}
else if (randomValue == 6){
System.out.println("------- \n"
+ "|* *| \n"
+ "|* *| \n"
+ "|* *| \n"
+ "------- \n");
}
}
This code currently prints out a graphical representation of 5 different dice vertically, I am wondering if somebody can tell me which steps to take to be able to print these out so they are horizontal and all in one row. I have tried reading about the printf() function but am unsure if that is the answer. Any help is appreciated thanks!
Re: Help printing random dice
This is one of those things that I really wish I could do for you. Sounds like fun. But I won't since you seem to be doing this for your own personal interests.
I'll give you some hints on how to accomplish this.
What do you know?
1) Each die is 11 characters in length
2) There is 1 space between each die (or more, whatever)
3) Each die is 5 characters in height
The print statement is a statement you should look into.
There are 3 main methods for printing to the console:
1) print - prints out a continuous line of text
2) println - prints out a continuous line of text, then increments to the next line
3) printf - used primary for formatting (based off of function in C)
So, if you were to use a nested loop and if you were to use the print statement instead of the println statement, you should be able to accomplish your goal based on the information you know about your dice.
Your current design for printing out vertically is based off of printing each object in one go. When printing out horizontally, think of printing out more like a typewriter (across line-by-line) than of printing each die in one statement.
If you have trouble with the code, post it here and I'll help you debug it.
Re: Help printing random dice
Thanks a ton! This is actually for a class so working through it myself will be much more beneficial...I thought it may have something to do with a nested loop (rows and cols). I will continue to work through it and post if I have problems. Thanks again
Re: Help printing random dice
Hello VanDarg,
I'm new also in Java can you give me a sample output of your program I want to try this also and may this symbol below may help you:
Alt + 218 = ┌
Alt + 179 = │
Alt + 192 = └
Alt + 191 = ┐
Alt + 217 = ┘
Alt + 196 = ─
Thanks
Re: Help printing random dice
Quote:
Originally Posted by
nine05
Hello VanDarg,
I'm new also in Java can you give me a sample output of your program I want to try this also and may this symbol below may help you:
Alt + 218 = ┌
Alt + 179 = │
Alt + 192 = └
Alt + 191 = ┐
Alt + 217 = ┘
Alt + 196 = ─
Thanks
Output:
-------
|* *|
| |
|* *|
-------
-------
|* *|
| * |
|* *|
-------
-------
|* |
| |
| *|
-------
-------
|* *|
| |
|* *|
-------
-------
|* |
| * |
| *|
-------
In the actual program all of the characters line up. For some reason copying and pasting into here doesn't work to well in this situation.
And I really am not sure what your symbol is for. If it is special characters while holding ALT, it does not apply to my machine as I am not using windows.
Re: Help printing random dice
Ok, I understand the concept of what needs to be done here. I understand that it will print from left to right, and that I need to use print so that it doesn't automatically start a new line. I also understand that the nested loop will allow me to specify (11 characters length, 5 characters height, etc). What I am having trouble with is :
A. How do I print out the different characters for each die (the '*', '|', '-')
B. How do I make the die random?
If you can help me without totally giving it away I'd appreciate it! Thanks again
Re: Help printing random dice
There really isn't an elegant way for this part. Since you know the number of die that will be printed, I would have an array of ints (representing what type of die each die should be going from left to right). You could then access the array throughout the nested loops and print out each line based on what type of die is being worked on.
If you wanted to get interesting with it, you can actually create a semi-complex algorithm for printing out each line for each die. Consider the patterns for each line in each type of die. I can count a total of six different patterns. You could then cross-reference the current line with the type of die to determine which pattern should be used.
That is actually a pretty cool way of doing it.
Try not to print out each character one at a time, but rather each segment of die at a time. That creates less room for error.
EDIT: I couldn't resist. I gave it a try using the above description and it does work. Pretty awesome.
Re: Help printing random dice
OK I am having trouble figuring out where the arrays come in. I had them set up but I ended up just using the loop counters instead:
Code Java:
import java.util.Random;
public class dicePrint {
public static void main(String[] args){
int[] arrayOne = new int[7];
int[] arrayTwo = new int[14];
for (int a = 0; a < 7; a++){
arrayOne[a] = a;
}
Random randomNumberGenerator = new Random(1);
int randNum = 5;
for (int i = 0; i < 5; i++){
for (int j = 0; j < 5; j++){
if (i == 0 || i == 4){
System.out.print("------- ");
}
else if (i == 1){
if (randNum == 2 || randNum == 3){
System.out.print("|* | ");
}
else if (randNum > 3){
System.out.print("|* *| ");
}
else{
System.out.print("| | ");
}
}
else if (i == 2){
if (randNum % 2 != 0){
System.out.print("| * | ");
}
else if (randNum == 2 || randNum == 4){
System.out.print("| | ");
}
else{
System.out.print("|* *| ");
}
}
else if (i == 3){
if (randNum == 2 || randNum == 3){
System.out.print("| *| ");
}
else if (randNum > 3){
System.out.print("|* *| ");
}
else{
System.out.print("| | ");
}
}
}
System.out.println();
}
}
}
This probably isn't the correct way of doing it but it's all I could come up with. And obviously, making the dice random doesn't work out. I had it coming up with a new random number after each inside loop was completed but that came up with a random number whenever it started a new line. Any additional info you can give me?
Re: Help printing random dice
I will show you how I did my arrays since it doesn't actually give you the logic for creating the dice.
Code java:
//Create an array of dice. This array holds 20 dice
int[] dieType = new int[20];
//Loop through dice array
for(int i=0;i<dieType.length;i++)
{
//Set the current die's value to a random number (1-6)
dieType[i] = (int)(Math.random()*6)+1;
}
//Create a loop for the different patterns (you don't need an array for this)
String[] patterns = new String[]{"------- ","| | ","| * | ","| * | ","| * | ","| * * | "};
You then want to use a nested loop. The outer loop should loop the rows (the height of the dice) and the inner loop should loop the number of dice.
See if you can work from there, you seem to understand the logic based on your current design.
Re: Help printing random dice
Ok I think I got it. Did you do this a different way? And is there a way to have an infinite amount of random numbers? Otherwise I can just choose a larger number for the size of the array.
Code java:
import java.util.Random;
public class dicePrint {
public static void main(String[] args){
Random randomNumberGenerator = new Random(1);
int[] dieType = new int[20];
for (int i = 0; i < dieType.length; i++){
dieType[i] = randomNumberGenerator.nextInt( 6) + 1;
System.out.print(dieType[i] + " ");
}
System.out.println();
String[] diePatterns = new String[]{"------- ","| | ","|* | ","| * | ","| *| ","|* *| "};
for (int rows = 0; rows < 5; rows++){
for (int cols = 0; cols < 5; cols++){
if (rows == 0 || rows == 4){
System.out.print(diePatterns[0]);
}
else if (rows == 1 ){
if (dieType[cols] == 1){
System.out.print(diePatterns[1]);
}
else if (dieType[cols] == 2 || dieType[cols] == 3){
System.out.print(diePatterns[2]);
}
else {
System.out.print(diePatterns[5]);
}
}
else if (rows == 2){
if (dieType[cols] % 2 != 0){
System.out.print(diePatterns[3]);
}
else if (dieType[cols] == 6){
System.out.print(diePatterns[5]);
}
else {
System.out.print(diePatterns[1]);
}
}
else if (rows == 3){
if (dieType[cols] == 1){
System.out.print(diePatterns[1]);
}
else if (dieType[cols] == 2 || dieType[cols] == 3){
System.out.print(diePatterns[4]);
}
else {
System.out.print(diePatterns[5]);
}
}
}
System.out.println();
}
} // end main method
} // end class dicePrint
Re: Help printing random dice
An infinite number of dice? Yes it is possible, but it would not very practical as you could never print them all out (since there are an infinite amount.
To create an infinite number of dice (or an undetermined number) you should use a dynamic data structure. You could use arrays and constantly resize, but a dynamic data structure is easier. Here is a brief introduction:
Background Notes:
There are tons of dynamic data structures to choose from in JAVA. It is important to note that the dynamic data structure in JAVA are considered Objects, not just references to memory (which is what arrays are). Being Objects, all dynamic data structures implement the List Interface. Also, since they are Objects, you access and interact with the indexes of the data structure using Methods, unlike arrays where you access and interact with the indexes via directly accessing the point in memory. Lastly, it is important to note that (*most*) dynamic data structures do not require a specification of the size. They can dynamically grow and shrink at runtime, based on what you do to the data structure.
Dynamic Data Structures In JAVA Introduction Classes:
Most JAVA Introduction Classes will usually teach you at least 2 of the following Dynamic Data Structures: ArrayList (ArrayList (Java Platform SE 6)), Vector (Vector (Java Platform SE 6)), and LinkedList(LinkedList (Java Platform SE 6)). If you have been taught any of these Dynamic Data Structures, these are what I am referring to.
I'll go more indepth with specifics if you want me to.
Re: Help printing random dice
Thanks aussiemcgr you were a huge help. I have not been introduced yet to dynamic data structures just yet (I'm sure it will be this semester) so I think for now I am just going to loop the array creating random numbers until the user wishes to exit. Thanks again, these forums have proven to be very useful! I'll keep them in mind in the future.
Re: Help printing random dice
Quote:
Originally Posted by
vanDarg
Thanks aussiemcgr you were a huge help. I have not been introduced yet to dynamic data structures just yet (I'm sure it will be this semester) so I think for now I am just going to loop the array creating random numbers until the user wishes to exit. Thanks again, these forums have proven to be very useful! I'll keep them in mind in the future.
Alternatively, you can prompt the user in the beginning for the number of dice they want made. You can then set the size of the array according to what they wanted. That way you don't have to worry about resizing the array, null indexes, and bounds issues.