Search code examples
javaarraystile

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

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; 
        }