Merge pull request #204 from Xychen1994/code-refactor

simplified getRandomTileWithAdjacent with for loop instead of similar branches
This commit is contained in:
Simon Fortier 2019-06-01 13:48:25 -04:00 committed by GitHub
commit 2982fcbbc1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -412,6 +412,8 @@ public class World implements MongoSerializable {
*/ */
public Point getRandomTileWithAdjacent(int n, int tile) { public Point getRandomTileWithAdjacent(int n, int tile) {
int counter = 0; int counter = 0;
int[] xPositions = {1, 0, -1, 0, 1, -1, 1, -1};
int[] yPositions = {0, 1, 0, -1, 1, 1, -1, -1};
while (true) { while (true) {
counter++; counter++;
@ -425,29 +427,10 @@ public class World implements MongoSerializable {
if (rTile != null) { if (rTile != null) {
int adjacentTiles = 0; int adjacentTiles = 0;
if (tileMap.isInBounds(rTile.x, rTile.y - 1) && !isTileBlocked(rTile.x, rTile.y - 1)) { for (int idx = 0; idx < xPositions.length; idx++) {
if (tileMap.isInBounds(rTile.x + xPositions[idx], rTile.y + yPositions[idx]) && !isTileBlocked(rTile.x + xPositions[idx], rTile.y + yPositions[idx])) {
adjacentTiles++; adjacentTiles++;
} }
if (tileMap.isInBounds(rTile.x + 1, rTile.y) && !isTileBlocked(rTile.x + 1, rTile.y)) {
adjacentTiles++;
}
if (tileMap.isInBounds(rTile.x, rTile.y + 1) && !isTileBlocked(rTile.x, rTile.y + 1)) {
adjacentTiles++;
}
if (tileMap.isInBounds(rTile.x - 1, rTile.y) && !isTileBlocked(rTile.x - 1, rTile.y)) {
adjacentTiles++;
}
if (tileMap.isInBounds(rTile.x + 1, rTile.y + 1) && !isTileBlocked(rTile.x + 1, rTile.y + 1)) {
adjacentTiles++;
}
if (tileMap.isInBounds(rTile.x - 1, rTile.y + 1) && !isTileBlocked(rTile.x - 1, rTile.y + 1)) {
adjacentTiles++;
}
if (tileMap.isInBounds(rTile.x + 1, rTile.y - 1) && !isTileBlocked(rTile.x + 1, rTile.y - 1)) {
adjacentTiles++;
}
if (tileMap.isInBounds(rTile.x - 1, rTile.y - 1) && !isTileBlocked(rTile.x - 1, rTile.y - 1)) {
adjacentTiles++;
} }
if (adjacentTiles >= n) { if (adjacentTiles >= n) {