public CellGenerator move(CellGenerator cell, int direction) { switch (direction) { case 0: if (!cell.getNorth()) { return(getCell(cell.getX() - 1, cell.getY())); } break; case 1: if (!cell.getEast()) { return(getCell(cell.getX(), cell.getY() + 1)); } break; case 2: if (!cell.getSouth()) { return(getCell(cell.getX() + 1, cell.getY())); } break; case 3: if (!cell.getWest()) { return(getCell(cell.getX(), cell.getY() - 1)); } break; } return(cell); }
public int GetStraightLenghtInDirection(CellGenerator position, int direction) { int res = -1; bool continuer = true; while (continuer) { switch (direction) { case 0: if (!position.getNorth() && !getCell(position.getX() - 1, position.getY()).isVisited()) { position = getCell(position.getX() - 1, position.getY()); } else { continuer = false; } break; case 1: if (!position.getEast() && !getCell(position.getX(), position.getY() + 1).isVisited()) { position = getCell(position.getX(), position.getY() + 1); } else { continuer = false; } break; case 2: if (!position.getSouth() && !getCell(position.getX() + 1, position.getY()).isVisited()) { position = getCell(position.getX() + 1, position.getY()); } else { continuer = false; } break; case 3: if (!position.getWest() && !getCell(position.getX(), position.getY() - 1).isVisited()) { position = getCell(position.getX(), position.getY() - 1); } else { continuer = false; } break; } res++; } return(res); }
public List <int> getPossibleWay(CellGenerator cell) { List <int> res = new List <int>(); if (!cell.getNorth() && !getCell(cell.getX() - 1, cell.getY()).isVisited()) { res.Add(0); } if (!cell.getSouth() && !getCell(cell.getX() + 1, cell.getY()).isVisited()) { res.Add(2); } if (!cell.getEast() && !getCell(cell.getX(), cell.getY() + 1).isVisited()) { res.Add(1); } if (!cell.getWest() && !getCell(cell.getX(), cell.getY() - 1).isVisited()) { res.Add(3); } return(res); }
public bool shorterDistance(CellGenerator cell) { int direction = 0; CellGenerator position = cell; position.setVisited(); while (true) { directionStraight[0] = (position.getNorth() || getCell(position.getX() - 1, position.getY()).isVisited()) ? 0 : GetStraightLenghtInDirection(position, 0); directionStraight[1] = (position.getEast() || getCell(position.getX(), position.getY() + 1).isVisited()) ? 0 : GetStraightLenghtInDirection(position, 1); directionStraight[2] = (position.getSouth() || getCell(position.getX() + 1, position.getY()).isVisited()) ? 0 : GetStraightLenghtInDirection(position, 2); directionStraight[3] = (position.getWest() || getCell(position.getX(), position.getY() - 1).isVisited()) ? 0 : GetStraightLenghtInDirection(position, 3); if (directionStraight[3] > 0 && (directionStraight[3] <= directionStraight[1] || directionStraight[1] == 0) && (directionStraight[3] <= directionStraight[2] || directionStraight[2] == 0) && (directionStraight[3] <= directionStraight[0] || directionStraight[0] == 0)) { direction = 3; Debug.Log(direction + " " + directionStraight[0]); position = MoveInDirection(position, direction); } else if (directionStraight[2] > 0 && (directionStraight[2] <= directionStraight[1] || directionStraight[1] == 0) && (directionStraight[2] <= directionStraight[0] || directionStraight[0] == 0)) { direction = 2; Debug.Log(direction + " " + directionStraight[1]); position = MoveInDirection(position, direction); } else if (directionStraight[1] > 0 && (directionStraight[1] <= directionStraight[0] || directionStraight[0] == 0)) { direction = 1; Debug.Log(direction + " " + directionStraight[2]); position = MoveInDirection(position, direction); } else if (directionStraight[0] > 0) { direction = 0; Debug.Log(direction + " " + directionStraight[3]); position = MoveInDirection(position, direction); } else { lastVisitedIntersection.Remove(position); if (lastVisitedIntersection.Count > 0) { position = lastVisitedIntersection[lastVisitedIntersection.Count - 1]; } } if (position.isDeadEnd() && error == 0 && position != start) { endX = position.getX(); endY = position.getY(); return(true); } if (position.isDeadEnd() && error == 0 && position == start) { return(false); } if (position.isDeadEnd() && position != start) { error -= 1; if (lastVisitedIntersection.Count > 0) { position = lastVisitedIntersection[lastVisitedIntersection.Count - 1]; } } } }
public bool inOrder(CellGenerator cell) { cell.setVisited(); if (cell.isDeadEnd() && error == 0 && cell != start) { endX = cell.getX(); endY = cell.getY(); return(true); } if (cell.isDeadEnd() && error == 0 && cell == start) { return(false); } if (cell.isDeadEnd() && cell != start) { error -= 1; return(false); } if (cell.getX() > 0 && !getCell(cell.getX() - 1, cell.getY()).isVisited() && !cell.getNorth() && inOrder(getCell(cell.getX() - 1, cell.getY()))) { return(true); } if (cell.getY() < width && !getCell(cell.getX(), cell.getY() + 1).isVisited() && !cell.getEast() && inOrder(getCell(cell.getX(), cell.getY() + 1))) { return(true); } if (cell.getY() > 0 && !getCell(cell.getX(), cell.getY() - 1).isVisited() && !cell.getWest() && inOrder(getCell(cell.getX(), cell.getY() - 1))) { return(true); } if (cell.getX() < lenght && !getCell(cell.getX() + 1, cell.getY()).isVisited() && !cell.getSouth() && inOrder(getCell(cell.getX() + 1, cell.getY()))) { return(true); } return(false); }