Пример #1
0
  // moves the ants and then checks where they are
  public void moveAnts() {
    // iterate through the ants
    for (int i = 0; i < Parameters.NUM_OF_ANTS; ++i) {
      // Check to see if the ant is carrying food. If it is, drop the pheromone
      if (nest1.theAnts[i].isHasFood()
          && boardLayout[nest1.theAnts[i].getX()][nest1.theAnts[i].getY()].isEmpty) {
        boardLayout[nest1.theAnts[i].getX()][nest1.theAnts[i].getY()].setPheromone(
            Parameters.PHEROMONE_STRENGTH + 1);
      }

      // Check if the cell has a pheromone on it. If it does, check the cells around it
      // and move in the direction of the food (weaker direction)
      if (boardLayout[nest1.theAnts[i].getX()][nest1.theAnts[i].getY()].getPheromone() > 0
          && !nest1.theAnts[i].isHasFood()) {
        int antX = nest1.theAnts[i].getX();
        int antY = nest1.theAnts[i].getY();
        int pherStrength = boardLayout[antX][antY].getPheromone();

        if (boardLayout[antX][antY + 1].getPheromone() < pherStrength
            && boardLayout[antX][antY + 1].getPheromone() > 0) {
          nest1.theAnts[i].move(antX, antY + 1);
        } else if (boardLayout[antX + 1][antY + 1].getPheromone() < pherStrength
            && boardLayout[antX + 1][antY + 1].getPheromone() > 0) {
          nest1.theAnts[i].move(antX + 1, antY + 1);
        } else if (boardLayout[antX + 1][antY].getPheromone() < pherStrength
            && boardLayout[antX + 1][antY].getPheromone() > 0) {
          nest1.theAnts[i].move(antX + 1, antY);
        } else if (boardLayout[antX + 1][antY - 1].getPheromone() < pherStrength
            && boardLayout[antX + 1][antY - 1].getPheromone() > 0) {
          nest1.theAnts[i].move(antX + 1, antY - 1);
        } else if (boardLayout[antX][antY - 1].getPheromone() < pherStrength
            && boardLayout[antX][antY - 1].getPheromone() > 0) {
          nest1.theAnts[i].move(antX, antY - 1);
        } else if (boardLayout[antX - 1][antY - 1].getPheromone() < pherStrength
            && boardLayout[antX - 1][antY - 1].getPheromone() > 0) {
          nest1.theAnts[i].move(antX - 1, antY - 1);
        } else if (boardLayout[antX - 1][antY].getPheromone() < pherStrength
            && boardLayout[antX - 1][antY].getPheromone() > 0) {
          nest1.theAnts[i].move(antX - 1, antY);
        } else if (boardLayout[antX - 1][antY + 1].getPheromone() < pherStrength
            && boardLayout[antX - 1][antY + 1].getPheromone() > 0) {
          nest1.theAnts[i].move(antX - 1, antY + 1);
        } else {
          // If no more surrounding cells have pheromones and the pheromone trail
          // doesn't lead to a food pile, move the ants to next space
          nest1.theAnts[i].move();
        }
      } else {
        // move the ants to next space
        nest1.theAnts[i].move();
      }

      // check if ant is at the nest
      if (nest1.theAnts[i].getX() == nest1.getX() && nest1.theAnts[i].getY() == nest1.getY()) {
        // check to see if the ant is carrying food
        if (nest1.theAnts[i].isHasFood()) {
          // drop off food at the nest
          nest1.theAnts[i].setHasFood(false);
          nest1.incrementFood();
        }

        // Check if ant is hungry and if there is food at the nest
        if (nest1.theAnts[i].getRemainingLife() < Parameters.ANT_LIFESPAN / 2
            && nest1.getFoodStored() > 1) {
          // restores the ants life remaining to full value
          nest1.theAnts[i].feedAnt();
          nest1.decrementFood();
        }
      }
      // check to see if an ant found food
      else if (boardLayout[nest1.theAnts[i].getX()][nest1.theAnts[i].getY()].hasFood) {
        // only pick up food if the ant isn't already carrying food
        if (!nest1.theAnts[i].isHasFood()) {
          nest1.theAnts[i].setHasFood(true); // set carrying food attribute to true on the ant
          int temp = getFoodPileIndex(nest1.theAnts[i]); // get index number of given food pile
          theFood[temp].decrementPile(); // decrement the food remaining
        }
      }
    }
  }