Results 1 to 12 of 12
  1. #1
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    4

    Default World Generation?

    i'm just wondering if I could get any help with world generation in Java? As some people may know, I will realease the game Loaie soon. And for that I will need world generation. I have an array of numbers, which you will see in the code in a second (Btw, I am doing this in the cmd because when I test stuff, like doing world gen, I'm doing it in the command prompt.) What I am trying to do by world generation (If statements) is to create an island looking thing. I've been working on this for a couple of hours now, but cannot find a soloution. :( Please help!

    Java Code:
    import java.util.*;
    
    public class Engine {
    	public static void main(String[] args) {
    		Random Rand = new Random();
    		
    		int[][] World = new int[20][30];
    		
    		int i=0;
    		int x=0;
    		int Ra;
    		
    		while(i < World.length) {
    			while(x < World[0].length) {
    				Ra = Rand.nextInt(1000);
    				
    				//Start of the ground generation - "The Grid:ed Layer!"
    				
    				if(i == 0 || x == World[0].length-1 || x == 0 || i == World.length-1) {
    					World[i][x] = 7;
    				} else {
    					if(x < World[0].length && i < World.length && x > 0 && i > 0 && World[i-1][x] == 7 || World[i+1][x] == 7 || World[i][x] == 7 || World[i][x+1] == 7 || World[i][x-1] == 7) {
    
    						if(Ra > 700) {
    							World[i][x] = 7;
    						} else {
    							World[i][x] = 3;
    						}
    						
    					} else {
    						World[i][x] = 2;
    					}
    				}
    				
    				//End of the ground generation;
    				
    				System.out.print(World[i][x]);
    				x++;
    			}
    			System.out.println();
    			
    			x=0;
    			i++;
    		} //End of the normal world generation, not very important to know;
    	}
    }
    Some stuff that is good to know.
    What the numbers means: 7 is water. 2 is grass. 3 is sand.

    Please help me out! I really need help. (Some people is prob just gonna say, oh now he's posting something again, who cares. But I really appreciate help, just give me a tips, you don't have to give me the code).

    Alex

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    but cannot find a soloution.
    Can you explain your problem in java programming terms? I'm not familiar with your application.
    For example show the input to the program, the output from the program and add some comments saying what is wrong with the output and show what the output should look like.

    Also add comments to the code describing the algorithm it is trying to follow and what it is supposed to do.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    What the numbers means: 7 is water. 2 is grass. 3 is sand.
    A better coding technique is to use self documenting variable names for those constants:
    final int Water = 7;
    etc
    instead of hardcoding literal/magic numbers.

  4. #4
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    4

    Default

    The output should be like:

    777777777777777777777777777777
    773373773373373773737373733777
    732232332232277773733232322377
    732222222222233332773222222237
    732222222222222222332222222227
    732222222222222222222222222227
    732222222222222222222222222227
    732222222222222222222222222227
    773222222222222222222222222227
    773222222222222222222222222227
    732222222222222222222222222227
    732222222222222222222222222227
    732222222222222222222222222227
    732222222222222222222222222227
    773222222222222222222222222227
    777732222222222222222222222227
    733322222222222222222222222227
    732222222222222222222222222227
    732222222222222222222222222227
    777777777777777777777777777777

    As you can see the 2:s on the right side & the bottom just goes like a straight like O.O I want them to change to 3:s and look a littlebit like the 3:s on the right side & the top. Anything specific I can do to fix that? I though I had that in the code, but I don't know why it's not working? Hmm... Maybe you could take a quick look at the code in Eclipse?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    What is the equation or algorithm or formula for generating the contents of each row and each column?

  6. #6
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    4

    Default

    You should have a look. I placed out comments so that it's easy to understand. Please have a look yourself.

    Java Code:
    /*
     *Value List:
     *2: Grass
     *3: Sand
     *7: Water
     */
    
    //One more thing, I need that I don't have to use the ";" in end of comments, I'm just used to it;
    
    
    import java.util.*;
    
    public class Engine {
    	public static void main(String[] args) {
    		Random Rand = new Random();
    		
    		//Here all of the world numbers are stored, the numbers that I am generating in order to get the world;
    		int[][] World = new int[20][30];
    		
    		//Here I create and giving variables for the counting values;
    		int i=0;
    		int x=0;
    		int Ra;
    		
    		//Here we have the "Double-Loop" which will generate the entire world;
    		while(i < World.length) {
    			while(x < World[0].length) {
    				Ra = Rand.nextInt(1000);
    				
    				//First it is pretty much just creating a square around the entire world, with 7:s (7 = Water, look on top for more values)
    				if(i == 0 || x == World[0].length-1 || x == 0 || i == World.length-1) {
    					
    					//Here we are setting the number of water, go up 3 rows in the code and you will see what we are doing;
    					World[i][x] = 7;
    					
    				//When we're not generating Java, here is what we want java to do;
    				} else {
    					
    					//So, now we're testing if there are any water close to what we're generating; 
    					/**PROBLEM HERE! Java Should Generate A Nice World With Sand (3) All Around It, But It Does'nt **/
    					if(x < World[0].length && i < World.length && x > 0 && i > 0 && World[i-1][x] == 7 || World[i+1][x] == 7 || World[i][x] == 7 || World[i][x+1] == 7 || World[i][x-1] == 7) {
    						
    						//So here we tells Java that, instead of giving it sand (3) directly, we may want some water (7)
    						if(Ra > 700) {
    							World[i][x] = 7;
    						} else {
    							World[i][x] = 3;
    						}
    						
    					//If none of these would be true, give it Grass (2) by default;
    					} else {
    						World[i][x] = 2;
    					}
    				}
    				
    				//Printing the number that we just generated through all this out on the screen;
    				System.out.print(World[i][x]);
    				x++;
    			}
    			
    			//Printing a new row, so that it actually looks like a world;
    			System.out.println();
    			
    			x=0;
    			i++;
    		}
    	}
    }

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    I placed out comments so that it's easy to understand
    Sorry I still see a 7 and a 3 and a 2 in this code. Water, Grass and Sand would be easier and quicker to understand
    Java Code:
                                              World[i][x] = 7;
    						} else {
    							World[i][x] = 3;
    						}
    						
    					//If none of these would be true, give it Grass (2) by default;
    					} else {
    						World[i][x] = 2;
    What is the answer to this question:
    What is the equation or algorithm or formula for generating the contents of each row and each column?

    This looks like an algorithm problem, not a java programming problem.
    Define your algorithm and someone can help you code it in java.

  8. #8
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    4

    Default

    What is the equation or algorithm or formula for generating the contents of each row and each column?
    How do you mean? Explain.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    How do you decide when to output a 2 or a 3 or a 7?

  10. #10
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    4

    Default

    Im using if statements, also a Random for giving making it change some of the time.
    So for an example if the World[i][x+1] == Water.
    Then it's bigger percentage of chance for the block I am generating to be water.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    How do you decide when to output a 2 or a 3 or a 7?
    You need an algorithm to decide what to output at each location.
    The one you currently using doesn't give you what you want.
    Are you asking for someone to give you a better algorithm?
    This is not really a java programming problem.
    Its a find/create a good algorithm problem.

  12. #12
    Skiller is offline Member
    Join Date
    Jan 2011
    Posts
    67
    Rep Power
    0

    Default

    If you aren't too concerned about the time it takes to generate then you can fill the areas you want to be an island with 2s the rest with 7s then iterate over the world and anywhere a 2 is adjacent to a 7 make it a 3. It's probably the most reliable way to ensure you have a sandy border around the island, but it can take a little extra time to do.
    Currently developing Cave Dwellers, a Dwarf Fortress/Minecraft style game for Android.

Similar Threads

  1. Midi Generation
    By Singing Boyo in forum New To Java
    Replies: 7
    Last Post: 07-05-2011, 08:45 AM
  2. payroll generation
    By kriti in forum New To Java
    Replies: 5
    Last Post: 06-02-2010, 03:21 PM
  3. War file generation
    By rummy in forum New To Java
    Replies: 1
    Last Post: 02-08-2010, 12:57 PM
  4. War file generation
    By vislawath.jawaharlal in forum Threads and Synchronization
    Replies: 0
    Last Post: 04-07-2009, 01:12 PM
  5. String generation
    By codingisfun22 in forum Advanced Java
    Replies: 3
    Last Post: 12-03-2007, 05:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •