Exemplo n.º 1
0
  /**
   * Put 6 ninjas into different random positions on the map or randomize current ninjas locations.
   *
   * @param initial if first fill up the map with 6 ninjas, set this to true.
   */
  private void assignNinjas(boolean initial) {
    int rRow;
    int rCol;

    if (initial) {

      for (int i = 0; i < 6; i++) {
        do {
          rRow = random.nextInt(8);
          rCol = random.nextInt(8);
        } while (isOccupied(rRow, rCol));

        Ninja ninja = new Ninja(rRow, rCol);
        map[rRow][rCol] = ninja;

        if (debug) {
          ninja.setVisible(true);
        }

        // Store the ninja to the array, and mark the new location as
        // occupied.
        ninjas.add(ninja);
        occupiedLocations.add(map[rRow][rCol]);
      }
    } else {

      // If there are ninjas surrond the original position, move them.
      for (Square location : surroundSpy) {
        if (isNinja(map[location.getRow()][location.getCol()])) {
          do {
            rRow = random.nextInt(8);
            rCol = random.nextInt(8);
            if (isNinja(map[rRow][rCol])) {
              occupiedLocations.add(map[rRow][rCol]);
            }
          } while (isOccupied(rRow, rCol));

          map[rRow][rCol] = map[location.getRow()][location.getCol()];
          Ninja ninja = (Ninja) map[rRow][rCol];
          map[location.getRow()][location.getCol()] = new Square(debug, rRow, rCol);
          ninja.setRow(rRow);
          ninja.setCol(rCol);

          if (debug) {
            ninja.setVisible(true);
          }

          occupiedLocations.add(map[rRow][rCol]);
        }
      }
    }
  }