Skip to content

Instantly share code, notes, and snippets.

@ft975
Last active December 19, 2015 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ft975/6012616 to your computer and use it in GitHub Desktop.
Save ft975/6012616 to your computer and use it in GitHub Desktop.
/**
* It goes to touching block. If the cond function returns true on that block,
* it goes on and operates on the values of that block using the op function.
* It then executes itself with that block as the base.
* To make a generic TreeCapitator, cond should be
* <code>return wrd.getBlockId(x, y, z) == Block.wood.blockID || wrd.getBlockId(x, y, z) == Block.leaves.blockID</code>
* op should be
* <code>wrd.destroyBlock(x, y, z, true)</code>
*
* For more information see the wikipedia page on recursion, especially Multiple Recursion
*/
public void iterateTouching(int x, int y, int z, World wrd) {
if (cond(x + 1, y, z,world)) {
op(x + 1, y, z,world)
iterateTouching(x + 1, y, z,world)
}
if (cond(x - 1, y, z,world)) {
op(x - 1, y, z,world)
iterateTouching(x - 1, y, z,world)
}
if (cond(x, y + 1, z,world)) {
op(x, y + 1, z,world)
iterateTouching(x, y + 1, z,world)
}
if (cond(x, y - 1, z,world)) {
op(x, y - 1, z,world)
iterateTouching(x, y - 1, z,world)
}
if (cond(x, y, z + 1,world)) {
op(x, y, z + 1,world)
iterateTouching(x, y, z + 1, world)
}
if (cond(x, y, z - 1,world)) {
op(x, y, z - 1,world)
iterateTouching(x, y, z - 1, world)
}
}
private boolean cond(int x,int y,int z,World world){...}
private void op(int x, int y, int z, World world){...}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment