Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Help printing random dice

  1. #1
    Member vanDarg's Avatar
    Join Date
    Jan 2011
    Location
    Chicago
    Posts
    65
    My Mood
    Mellow
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default 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:

    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!
    Last edited by vanDarg; January 26th, 2011 at 08:50 PM.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default 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.
    Last edited by aussiemcgr; January 26th, 2011 at 10:26 PM.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Member vanDarg's Avatar
    Join Date
    Jan 2011
    Location
    Chicago
    Posts
    65
    My Mood
    Mellow
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default 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

  4. #4
    Junior Member
    Join Date
    Jan 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  5. #5
    Member vanDarg's Avatar
    Join Date
    Jan 2011
    Location
    Chicago
    Posts
    65
    My Mood
    Mellow
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: Help printing random dice

    Quote Originally Posted by nine05 View Post
    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.

  6. #6
    Member vanDarg's Avatar
    Join Date
    Jan 2011
    Location
    Chicago
    Posts
    65
    My Mood
    Mellow
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default 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

  7. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default 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.
    Last edited by aussiemcgr; January 27th, 2011 at 10:33 PM.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  8. #8
    Member vanDarg's Avatar
    Join Date
    Jan 2011
    Location
    Chicago
    Posts
    65
    My Mood
    Mellow
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default 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:

    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?

  9. #9
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default 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.

    //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.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  10. #10
    Member vanDarg's Avatar
    Join Date
    Jan 2011
    Location
    Chicago
    Posts
    65
    My Mood
    Mellow
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default 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.
     
    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
    Last edited by vanDarg; February 13th, 2011 at 10:37 AM.

  11. #11
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default 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.
    Last edited by aussiemcgr; February 1st, 2011 at 09:38 AM.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  12. #12
    Member vanDarg's Avatar
    Join Date
    Jan 2011
    Location
    Chicago
    Posts
    65
    My Mood
    Mellow
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default 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.

  13. #13
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Help printing random dice

    Quote Originally Posted by vanDarg View Post
    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.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 05:46 PM
  2. Dice Counter
    By Xxl0n3w01fxX in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 26th, 2011, 11:49 AM
  3. Dice Program Help
    By Bradshjo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 2nd, 2010, 07:50 AM
  4. random
    By b109 in forum Java Theory & Questions
    Replies: 2
    Last Post: June 3rd, 2010, 04:19 AM
  5. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM