1. ## 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;

int i=0;
int x=0;
int Ra;

while(i < World.length) {
while(x < World.length) {
Ra = Rand.nextInt(1000);

//Start of the ground generation - "The Grid:ed Layer!"

if(i == 0 || x == World.length-1 || x == 0 || i == World.length-1) {
World[i][x] = 7;
} else {
if(x < World.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  Reply With Quote

2. ## 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.  Reply With Quote

3. ## 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  Reply With Quote

4. ## 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?  Reply With Quote

5. ## What is the equation or algorithm or formula for generating the contents of each row and each column?  Reply With Quote

6. ## 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;

//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.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.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.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++;
}
}
}```  Reply With Quote

7. ## 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.  Reply With Quote

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

9. ## How do you decide when to output a 2 or a 3 or a 7?  Reply With Quote

10. ## 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.  Reply With Quote

11. ## 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.  Reply With Quote

12. Member Join Date
Jan 2011
Posts
67
Rep Power
0

## 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.  Reply With Quote

#### Posting Permissions

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