Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9017947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:23:51+00:00 2026-06-16T04:23:51+00:00

I have this code right now: private void generateLevel() { for (int y =

  • 0

I have this code right now:

private void generateLevel() {
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            tiles[x + y * width] = random.nextInt(4);
        }
    }
}

Which allows this method to work:

public Tile getTile(int x, int y) {
    if (x < 0 || y < 0 || x >= width || y >= height) return Tile.voidTile;
    if (tiles[x + y * width] == 0) return Tile.grass;
    if (tiles[x + y * width] == 1) return Tile.stone;
    else return Tile.water; 
    //return Tile.voidTile;
}

Which returns this result:
2D Tile Generation

What I want is smooth, rounded islands with random stone deposits here and there. Would perlin noise be overkill for this? I figured I could just generate a tile, check the tile id next to it and then place it down if the tile adjacent is of the same type. But that would create endless expanses of the same tile. Any help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:23:53+00:00Added an answer on June 16, 2026 at 4:23 am

    The first step I would take is to create objects out of anything on your domain level:

    public class Island {
        private Point2D center = null;
        private int radius = 0;
    
        private List<Deposit> deposits = new ArrayList<Deposit>();
    
        public Island(Point2D center, int radius) {
            this.center = center;
            this.radius = radius;
        }
    
        public void generateDeposits(int numDeposits) {
            for (int i = 0; i < numDeposits; i++) {
                // TODO: I leave it to you to find an x and y inside the island's
                // boundary.
                int x = getIntInsideCircle(center, radius);
                int y = getIntInsideCircle(center, radius);
                if (!depositInLocation(x, y)) {
                    deposits.add(new StoneDeposit(x, y));
                } else {
                    i--; // TODO: This code could potentially go on forever,
                         // if we keep generating locations that have been used, 
                         // but I'll leave this for you to handle.
                }
            }
        }
    }
    
    public abstract class Deposit {
        private Point2D location = null;
    
        public Deposit(Point2D location) {
            this.location = location;
        }
    }
    
    public class StoneDeposit extends Deposit {
        // TODO: You can fill this with StoneDeposit specifics.
    }
    

    Now we have code that will generate an island with all it’s random deposits. The only thing that is left to do is actually place these islands. I’m going to keep it simple and only add one to the map but I’m sure you can figure out how to add more than one (I’ll leave some comments with my ideas):

    public class Map {
        private final int WIDTH = 1000;
        private final int HEIGHT = 1000;
    
        private List<Island> islands = new ArrayList<Island>();
    
        public void generate() {
            // TODO: If you want to make more, make a for loop.
            int radius = 100;
            Island island = new Island(new Point2D(WIDTH / 2, HEIGHT / 2), radius);
            // TODO: If you are going to add more, then you can't simply add them
            // all willy-nilly. You are going to have to check if the islands collide
            // and, if they do, find a way to handle that.
            // You could let them collide and create a mountain range where they do, or,
            // you could try to place the next island in a different position (similar
            // to what we used above placing deposits, but both situations require
            // code a bit better than what I've included).
            islands.add(island);
         }
    }
    

    Ok, now we’ve got all the data we need. This brings us to the final point of actually drawing it onto the screen using tiles. I’m not too experienced with this subject, so this might be inefficient, but it should serve as a launching point.

    Some of the functions I’ve generalized (like drawTile(int x, int y, TileType type) because I don’t know how you are drawing the tiles to the screen).

    // Generate our data.
    Map map = new Map();
    map.generate();
    // Draw to the screen.
    
    // 1. Fill the entire screen with water.
    for (int y = 0; y < HEIGHT; y++) {
        for (int x = 0; x < WIDTH; x++) {
            drawTile(x, y, Type.WATER);
        }
    }
    
    // 2. Draw the islands.
    // We're going to use this algorithm to draw the circle:
    // http://en.wikipedia.org/wiki/Midpoint_circle_algorithm
    for (Island island : map.getIslands()) {
        int f = 1 - island.getRadius();
        int ddF_x = 1;
        int ddF_y = -2 * island.getRadius();
        int x = 0;
        int y = 0;
    
        Point2D center = island.getCenter();
        int radius = island.getRadius();
        drawTile(center.getX(), center.getY() + radius, TileType.LAND);
        drawTile(center.getX(), center.getY() - radius, TileType.LAND);
        drawTile(center.getX() + radius, center.getY(), TileType.LAND);
        drawTile(center.getX() - radius, center.getY(), TileType.LAND);
    
        while(x < y) {
            if(f >= 0) {
              y--;
              ddF_y += 2;
              f += ddF_y;
            }
            x++;
            ddF_x += 2;
            f += ddF_x;    
            drawTile(center.getX() + x, center.getY() + y, TileType.LAND);
            drawTile(center.getX() - x, center.getY() + y, TileType.LAND);
            drawTile(center.getX() + x, center.getY() - y, TileType.LAND);
            drawTile(center.getX() - x, center.getY() - y, TileType.LAND);
            drawTile(center.getX() + y, center.getY() + x, TileType.LAND);
            drawTile(center.getX() - y, center.getY() + x, TileType.LAND);
            drawTile(center.getX() + y, center.getY() - x, TileType.LAND);
            drawTile(center.getX() - y, center.getY() - x, TileType.LAND);
          }
    
          // TODO: Now you have to figure out how to fill in the guts of the island.
        }
    
    // 3. Draw the deposits.
    // TODO: I'll leave this one for you.
    

    So that’s basically it – it’s not too bad.

    You could always go a step further and add tiles that are mostly water but with some shore line. In order to do that you would have to check if the tile you are drawing is an edge or a corner:

    +-+-+-+
    |1|2|3|
    +-+-+-+
    +-+-+-+-+
    |4|5|6|7|
    +-+-+-+-+
    +-+-+-+
    |8|9|0|
    +-+-+-+
    

    Here you can see 2,9, and 4 are all edge tiles. 1, 3, 8, 0 are all corner tiles, and 5 is an interior tile. When you recognize a tile as being a corner then you have to pick all the attached water tiles and draw them as “coast” tiles:

      +-+-+
      | |x|
      +-+-+-+-+
      |x|1|2|3|
      +-+-+-+-+
        +-+-+-+-+
        |4|5|6|7|
        +-+-+-+-+
        +-+-+-+
        |8|9|0|
        +-+-+-+
    

    There all the x’s would be coastal tiles.

    I hope this helps a bit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code right now to get the row value from jquery grid..
Right now, I have this code where $obj_arr maybe contain array and an object.
Right now, I have my code looking like this: SqlTables.java: // Separate file public
I have this code which compiles and works as expected: class Right {}; class
I have a problem with this code right here: - (void)fetchedData:(NSData *)responseData { //parse
I have this code right here, where the $friends variable is an array with
i have this code right here: <select name=group> <option value=>Choose a group....</option> <?php foreach($groups
Hey I have this code right here: http://pastie.org/534470 And on line 109 I get
I have this code <a href=#> <span style=border-right: medium none; class=Yes>Yes</span> </a> <a href=#>
So, I have this simple code $('#info_name'+index).show(slide, {direction: right}, 1000, function(){ $('#info_info'+index).show('slide',{direction: up}, 1000);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.