public List<Tile> getNeighbors(Tile tile, List<Tile> neighbors) { //returns the neighbors of the given tile and adds them to existing list //at most, will return 4 tiles int xCoord = tile.getXCoord(); int yCoord = tile.getYCoord(); //MAYBE TODO, getNeighbors and find moves back to working order //prevent array index out of bounds and check for existence of tile in list if (xCoord > 0 && !neighbors.Contains(map[xCoord - 1][yCoord])) { neighbors.Add(map[xCoord - 1][yCoord]); } if (xCoord < width && !neighbors.Contains(map[xCoord + 1][yCoord])) { neighbors.Add(map[xCoord + 1][yCoord]); } if (yCoord > 0 && !neighbors.Contains(map[xCoord][yCoord - 1])) { neighbors.Add(map[xCoord][yCoord - 1]); } if (yCoord < width && !neighbors.Contains(map[xCoord][yCoord + 1])) { neighbors.Add(map[xCoord][yCoord + 1]); } return neighbors; }
public bool isNeighbor(Tile tile1, Tile tile2) { //returns true if the given tiles are neighbors //two tiles are neighbors if either their xCoords or yCoords are one apart (but not both) int xCoord1 = tile1.getXCoord(); int yCoord1 = tile1.getYCoord(); int xCoord2 = tile2.getXCoord(); int yCoord2 = tile2.getYCoord(); if (Math.Abs(xCoord1 - xCoord2) == 1 && (yCoord1 - yCoord2 == 0)) { return true; } else if (Math.Abs(yCoord1 - yCoord2) == 1 && (xCoord1 - xCoord2 == 0)) { return true; } else { return false; } }