Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Java Tile Array Issue

I am creating a 2D TileMap.I am using integers to determine tile type. I wanted to turn every grass tile(4) that neighbours at least one ocean tile(3) in to sand(1).

public void geography() {
        
        
    for(int x=0; x<width; x++) { //Iterates through array
    for(int y=0; y<height; y++) {
        
        
            
            int counter = sametile(x,y,1,3); // uses a method to count neighbouring tiles
            
            if(counter>0 && map[x][y]==4) { //if it at least has 1 
                map[x][y]=1;            //turns that tile into sand
            }
            
        }
        }
}

        public int sametile(int locx,int locy,int diameter,int tile) {  //location x,y,search diameter and tile type                                                  
        int counter=0;  
        for(int x=locx-diameter; x<locx+diameter; x++) { //where we start and end
            for(int y=locy-diameter; y<locy+diameter; y++) {
                 if (x < diameter || y < diameter || x+diameter>width || y+diameter>height) {
                             counter++;       
                        continue; 

 //to avoid reaching index numbers that array doesnt have 
                    }           
                if(map[x][y]==tile) { //if it is the tile we are looking for
                counter++;  //we increase the counter
                }
            }
        }   
        return counter; 
        }

Weird thing is it works as intented some part of the map,but works partly wrong or totally wrong on some parts of the map.

I literally checked every line of code but didnt really catch anything

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

tilemap

>Solution :

So assuming diameter 1, one of the things I want to do is check is to my left and to my right.

for(int x=locx-diameter; x<locx+diameter; x++)

Looking at this is starts to my left and then never checks to my right because it doesn’t use <=. Similar logic for y and above/below.

for(int y=locy-diameter; y<locy+diameter; y++) {

OK, now if locx is 1 and I need to look to my left, then I need to took at tile 0, however this prevents that:

if (x < diameter

And similarly for other bounds, so I assume you want:

if (x < 0 || y < 0 || x >= width || y >= height) {
    counter++;
    continue; 
}

Additionally, we don’t do anything to prevent us counting the tile we’re looking around.

All together:

    public int sametile(int locx,int locy,int diameter,int tile) {  //location x,y,search diameter and tile type                                                  
        int counter=0;  
        for(int x=locx-diameter; x <= locx+diameter; x++) { //where we start and end
            for(int y=locy-diameter; y <= locy+diameter; y++) {
                if (x < 0 || y < 0 || x >= width || y >= height) {
                    counter++;       
                    continue; 
                    //to avoid reaching index numbers that array doesnt have 
                }
                if (x == locx && y == locy) {
                    continue; // don't count the central tile, only neighbours
                }
                if(map[x][y]==tile) { //if it is the tile we are looking for
                    counter++;  //we increase the counter
                }
            }
        }   
        return counter; 
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading