Switch Adjacent Blocks in tiled terrain

Hi!

I am trying to write a code to draw a tiled terrain on screen.

The problem occurs when I try, for every block, to check adjacent blocks and switch them to certain block if current block is, for example, type Water.

I think the algorithm is ok, but my code doesn’t work.

._.

Is there a way to edit my code properly?
Or maybe you can share with me another working algorithm?

Here the Terrain class and the Layer class.

Sorry for my bad english, tell me if something isn’t clear.

Hey,

I did have a look on it and I changed it on how I would do it.
Some general things: Your Block Array is public, why ?
Do you really need to extend the Terrain Class ? I mean, is there gonna be anything else that is extending the Terrain class ? If yes, it´s completely fine, else you can just take the variables and the methods of it to the Layer class.

So, here´s your snippet of the code:
[spoiler]


    private Block[][] switchBlock(Block[][] map){
        int currentX, currentY;
 
        for(currentX = 0; currentX < getWidth(); currentX++){
            for(currentY = 0; currentY < getHeight(); currentY++){
 
                if(map[currentX][currentY].getType() == BlockType.WATER){
                   
                    int finalX, finalY;
                    int tempX = -1;
                    int tempY = -1;
                   
                    if(currentX == 0) tempX = 0;
                    if(currentY == 0) tempY = 0;
                   
                    for(finalX = tempX; finalX <= 1; finalX++){
                        for(finalY = tempY; finalY <= 1; finalY++){
                           
                            if(map[finalX][finalY].getType() != BlockType.WATER){
                                if(map[finalX][finalY].getType() != BlockType.GRASS_1){
                                    map[finalX][finalY] = blockList.get(1);
                                }
                            }
                        }
                    }
                }
            }
        }
        return map;
    }

[/spoiler]

And here´s mine:
Please be aware that I did not any check of being in bounds so 4 Blocks won´t be checked. This code was not written in a editor so there might be some errors in spelling, also I don´t know your Block class so I did just call the methods as I thought the might be named.
Should work for you
[spoiler]

  
    //Can be a void, no need to return the map
    private void switchBlock(Block[][] map){
        //No need to predefine the variables
        //Set currX and currY to 1 because I would have to do a check for being inside the layer else. This will change all block except the upper left, upper right, lower left and lower right ones.
        for(int currentX = 1; currentX < getWidth() - 1; currentX++){
            for(int currentY = 1; currentY < getHeight() - 1; currentY++){
                Block block = map[currentX][currentY];
                if(block.getType() == BlockType.WATER){
                    if(map[currentX + 1][currentY].getType() != BlockType.WATER)map[currentX + 1][currentY].setType(BlockType.GRASS);
                    if(map[currentX - 1][currentY].getType() != BlockType.WATER)map[currentX - 1][currentY].setType(BlockType.GRASS);
                    if(map[currentX][currentY + 1].getType() != BlockType.WATER)map[currentX][currentY + 1].setType(BlockType.GRASS);
                    if(map[currentX][currentY - 1].getType() != BlockType.WATER)map[currentX][currentY - 1].setType(BlockType.GRASS);
                }
            }
        }
    }

[/spoiler]

EDIT: Why do you set the block to blockList(1) ? Just create a enum with all the BlockTypes and render the block according to the type he has.
If you need some help with your project, I´m happy to help you, just send me a message and we can get in touch.

Best regards,
Major_Sauce

[quote]Some general things: Your Block Array is public, why ?
[/quote]
I was testing the code, i would have chance it later >.<

[quote]Do you really need to extend the Terrain Class?
[/quote]
In the beginning i had a different idea… but maybe you are right. Later on i might chance it, 'cause i don’t really need it.

[quote]This code was not written in a editor so there might be some errors in spelling
[/quote]
Actually it works fine!

[quote]Just create a enum with all the BlockTypes and render the block according to the type he has.
[/quote]
Sure >.<

Thank you for your help! It’s been 3 days since i started working on this, my brain was about to explode @_@

Hi,

I’m happy that I could help you…
Trust me, I know the feeling when you’re sitting in front of 10 lines of code for hours and still can’t get it to work, and to be honest, this won’t change too fast but you did a important step and reached out for help.
The only thing you’ve got to learn is how to get the information you need to get it working and soon those hours will reduce to minutes, at least it worked like this for me…

Best regards
Major