示例#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]);
        }
      }
    }
  }
示例#2
0
  /**
   * Move the all the ninjas in the game to random directions.
   *
   * @return true if all ninjas moved successfully, false if foud a spy near by and stabbed him.
   */
  public boolean moveNinja() {

    // Check if the ninja has stepped on any power up last turn, assign them
    // back to their location.
    reAssignPowerUps();

    for (Ninja ninja : ninjas) {
      int row = ninja.getRow();
      int col = ninja.getCol();
      Square location = null;

      ArrayList<Square> validLocations = getValidLocations(ninja);

      // Remove all the rooms and ninjas locations from possible moves.
      Iterator<Square> iterator = validLocations.iterator();
      while (iterator.hasNext()) {
        Square loc = iterator.next();
        if (isRoom(loc) || isNinja(loc)) {
          iterator.remove();
        }
      }

      // The ninja can not stab the spy if the spy has invincibility.
      if (!spy.isInvincible()) {
        // If there's the spy next to this ninja, stab him!
        if (checkForSpy(ninja)) {
          return false;
        }
      } else {
        // Remove the spy location from ninja's possible moves when the
        // spy is invincible.
        Iterator<Square> iter = validLocations.iterator();
        while (iter.hasNext()) {
          Square loc = iter.next();
          if (loc instanceof Spy) {
            iter.remove();
          }
        }
      }

      // If the ninja got place in the dead end corner and has no where to
      // move, it can stay in the same position.
      if (validLocations.size() < 3) {
        validLocations.add(map[row][col]);
      }

      // Choose one random direction from possible locations.
      int index = random.nextInt(validLocations.size());
      location = validLocations.get(index);

      int Lrow = location.getRow();
      int Lcol = location.getCol();

      if (isPowerUp(location)) {
        // If the ninja step on the power up, save the power up and
        // display the ninja.
        powerUps.add((PowerUp) location);
        map[Lrow][Lcol] = ninja;
        ninja.setRow(Lrow);
        ninja.setCol(Lcol);
        map[row][col] = new Square(debug, row, col);
      } else if (location == map[row][col]) {
        // If the ninja stay in the same postion, do nothing.
        map[row][col] = ninja;
      } else {
        map[Lrow][Lcol] = ninja;
        ninja.setRow(Lrow);
        ninja.setCol(Lcol);
        if (!isPowerUp(map[row][col])) {
          map[row][col] = new Square(debug, row, col);
        }
      }
    }

    return true;
  }
示例#3
0
  /**
   * Hard mode: If the spy is on the ninja's line of sight, the ninja will chase the spy until the
   * line of sight is broke. If no spy on the line of sight, the ninja will move randomly.
   *
   * @return true if all ninjas moved successfully, false if foud a spy near by and stabbed him.
   */
  public boolean moveSmartNinja() {
    // Check if the ninja has stepped on any power up last turn, assign them
    // back to their location.
    reAssignPowerUps();

    Square roomLoc = null;

    int spyRow = spy.getRow();
    int spyCol = spy.getCol();

    for (Ninja ninja : ninjas) {
      ArrayList<Square> validLocations = getValidLocations(ninja);

      int ninjaRow = ninja.getRow();
      int ninjaCol = ninja.getCol();
      Square location = null;

      // Remove all the rooms and ninjas locations from possible moves.
      Iterator<Square> iterator = validLocations.iterator();
      while (iterator.hasNext()) {
        Square loc = iterator.next();
        if (isRoom(loc) || isNinja(loc)) {
          iterator.remove();
        }
      }

      // The ninja can not stab the spy if the spy has invincibility.
      if (!spy.isInvincible()) {
        // If there's the spy next to this ninja, stab him!
        if (checkForSpy(ninja)) {
          return false;
        }
      } else {
        // Remove the spy location from ninja's possible moves when the
        // spy is invincible.
        Iterator<Square> iter = validLocations.iterator();
        while (iter.hasNext()) {
          Square loc = iter.next();
          if (loc instanceof Spy) {
            iter.remove();
          }
        }
      }

      // If the ninja got place in the dead end corner and has no where to
      // move, it can stay in the same position.
      if (validLocations.size() < 3) {
        validLocations.add(map[ninjaRow][ninjaCol]);
      }

      // If the ninja is in the same row or the same column, evaluate
      // which (row or column) and
      // find if they are above or below you
      if (spyRow == ninjaRow || spyCol == ninjaCol) {

        // scenario 1: ninja and spy are in the same column, spy is
        // above the ninja
        if (spyCol == ninjaCol && spyRow < ninjaRow) {

          for (Square room : roomLocations) {
            if (ninjaRow < room.getRow() && room.getRow() < spyRow) {
              roomLoc = room;
            }
          }

          if (spyRow < roomLoc.getRow() && roomLoc.getRow() < ninjaRow) {
            moveNinja();

          } else if (validLocations.contains(map[ninjaRow - 1][ninjaCol])) {

            // This is SUPPOSED to move the ninja to the
            // location
            // one spot closer to
            // the spy and handle the power up properly
            if (isPowerUp(map[ninjaRow - 1][ninjaCol])) {
              powerUps.add((PowerUp) map[ninjaRow - 1][ninjaCol]);
              map[ninjaRow - 1][ninjaCol] = ninja;
              ninja.setRow(ninjaRow - 1);
              ninja.setCol(ninjaCol);
              map[ninjaRow][ninjaCol] = new Square(debug, ninjaRow, ninjaCol);
            } else {
              map[ninjaRow - 1][ninjaCol] = ninja;
              ninja.setRow(ninjaRow - 1);
              ninja.setCol(ninjaCol);
              if (!isPowerUp(map[ninjaRow][ninjaCol])) {
                map[ninjaRow][ninjaCol] = new Square(debug, ninjaRow, ninjaCol);
              }
            }
          }
        }

        // scenario 2: ninja and spy are in the same col, spy is below
        // the ninja
        if (spyCol == ninjaCol && spyRow > ninjaRow) {

          for (Square room : roomLocations) {
            if (ninjaRow < room.getRow() && room.getRow() < spyRow) {
              roomLoc = room;
            }
          }

          if (ninjaRow < roomLoc.getRow() && roomLoc.getRow() < spyRow) {
            moveNinja();
          } else if (validLocations.contains(map[ninjaRow + 1][ninjaCol])) {
            if (isPowerUp(map[ninjaRow + 1][ninjaCol])) {
              powerUps.add((PowerUp) map[ninjaRow + 1][ninjaCol]);
              map[ninjaRow + 1][ninjaCol] = ninja;
              ninja.setRow(ninjaRow + 1);
              ninja.setCol(ninjaCol);
              map[ninjaRow][ninjaCol] = new Square(debug, ninjaRow, ninjaCol);
            } else {
              map[ninjaRow + 1][ninjaCol] = ninja;
              ninja.setRow(ninjaRow + 1);
              ninja.setCol(ninjaCol);
              if (!isPowerUp(map[ninjaRow][ninjaCol])) {
                map[ninjaRow][ninjaCol] = new Square(debug, ninjaRow, ninjaCol);
              }
            }
          }
        }

        // scenario 3: ninja and spy are in the same row, spy is to the
        // left of the ninja
        if (spyRow == ninjaRow && spyCol < ninjaCol) {

          for (Square room : roomLocations) {
            if (ninjaRow < room.getRow() && room.getRow() < spyRow) {
              roomLoc = room;
            }
          }

          if (spyCol < roomLoc.getCol() && roomLoc.getCol() < ninjaCol) {
            moveNinja();
          } else if (validLocations.contains(map[ninjaRow][ninjaCol - 1])) {
            if (isPowerUp(map[ninjaRow][ninjaCol - 1])) {
              powerUps.add((PowerUp) map[ninjaRow][ninjaCol - 1]);
              map[ninjaRow][ninjaCol - 1] = ninja;
              ninja.setRow(ninjaRow);
              ninja.setCol(ninjaCol - 1);
              map[ninjaRow][ninjaCol] = new Square(debug, ninjaRow, ninjaCol);
            } else {
              map[ninjaRow][ninjaCol - 1] = ninja;
              ninja.setRow(ninjaRow);
              ninja.setCol(ninjaCol - 1);
              if (!isPowerUp(map[ninjaRow][ninjaCol])) {
                map[ninjaRow][ninjaCol] = new Square(debug, ninjaRow, ninjaCol);
              }
            }
          }

          // scenario 4: ninja and spy are in the same row, spy is to
          // the right of the ninja
          if (spyRow == ninjaRow && spyCol > ninjaCol) {

            for (Square room : roomLocations) {
              if (ninjaRow < room.getRow() && room.getRow() < spyRow) {
                roomLoc = room;
              }
            }

            if (spyCol < roomLoc.getCol() && roomLoc.getCol() < ninjaCol) {
              moveNinja();
            } else if (validLocations.contains(map[ninjaRow][ninjaCol + 1])) {
              if (isPowerUp(map[ninjaRow][ninjaCol + 1])) {
                powerUps.add((PowerUp) map[ninjaRow][ninjaCol + 1]);
                map[ninjaRow][ninjaCol + 1] = ninja;
                ninja.setRow(ninjaRow);
                ninja.setCol(ninjaCol + 1);
                map[ninjaRow][ninjaCol] = new Square(debug, ninjaRow, ninjaCol);
              } else {
                map[ninjaRow][ninjaCol + 1] = ninja;
                ninja.setRow(ninjaRow);
                ninja.setCol(ninjaCol + 1);
                if (!isPowerUp(map[ninjaRow][ninjaCol])) {
                  map[ninjaRow][ninjaCol] = new Square(debug, ninjaRow, ninjaCol);
                }
              }
            }
          }
        }
      } else {
        int index = random.nextInt(validLocations.size());
        location = validLocations.get(index);

        int Lrow = location.getRow();
        int Lcol = location.getCol();

        if (isPowerUp(location)) {
          // If the ninja step on the power up, save the power up
          // and
          // display the ninja.
          powerUps.add((PowerUp) location);
          map[Lrow][Lcol] = ninja;
          ninja.setRow(Lrow);
          ninja.setCol(Lcol);
          map[ninjaRow][ninjaCol] = new Square(debug, ninjaRow, ninjaCol);
        } else if (location == map[ninjaRow][ninjaCol]) {
          // If the ninja stay in the same postion, do nothing.
          map[ninjaRow][ninjaCol] = ninja;
        } else {
          map[Lrow][Lcol] = ninja;
          ninja.setRow(Lrow);
          ninja.setCol(Lcol);
          if (!isPowerUp(map[ninjaRow][ninjaCol])) {
            map[ninjaRow][ninjaCol] = new Square(debug, ninjaRow, ninjaCol);
          }
        }
      }
    }
    return true;
  }