Esempio 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]);
        }
      }
    }
  }
Esempio n. 2
0
  /**
   * Check if the given Square is a legal attack for this Piece
   *
   * @param threatened The Square to attack
   * @return If this Square is a legal attack
   */
  public boolean isLegalAttack(Square threatened) {
    if (m_name.equals("Pawn")) {
      if (m_board.getGame().isStaleLegalDests()) m_board.getGame().genLegalDests();

      if (m_captured) return false;

      if (threatened.getCol() == m_curSquare.getCol()) return false;
      else
        return (isLegalDest(threatened)
            || (threatened.getRow() - m_curSquare.getRow() == ((isBlack()) ? -1 : 1)
                && Math.abs(threatened.getCol() - m_curSquare.getCol()) == 1));
    }
    return isLegalDest(threatened);
  }
Esempio n. 3
0
  /**
   * Add a legal destination to the ArrayList.
   *
   * @param dest The Square to be added to the List
   * @return If the Square was successfully added to the ArrayList
   */
  public boolean addLegalDest(Square dest) {
    // If the Square is not habitable, don't add it, but return true so we
    // can move past it.
    if (!dest.isHabitable()) return true;

    if (dest.getRow() == m_curSquare.getRow() && dest.getCol() == m_curSquare.getCol())
      return false;

    if (dest.isOccupied() && dest.getPiece().isBlack() == isBlack()) {
      // If the destination has a Piece from the same team, we must be
      // guarding that Piece
      getGuardSquares().add(dest);
      return false;
    }

    // Otherwise, add the Piece and return true.
    getLegalDests().add(dest);
    return true;
  }
Esempio n. 4
0
  /**
   * Check for array out of bound, get only the moveable locations from the current location.
   *
   * @param object the Square object of the location on the map.
   * @return the ArrayList of valid locations.
   */
  private ArrayList<Square> getValidLocations(Square object) {
    int row = object.getRow();
    int col = object.getCol();
    ArrayList<Square> validLocations = new ArrayList<Square>();

    // Only add reachable directions to the array.
    if (row - 1 >= 0 && col <= 8 & col >= 0) {
      validLocations.add(map[row - 1][col]);
    }
    if (row + 1 <= 8 && col <= 8 & col >= 0) {
      validLocations.add(map[row + 1][col]);
    }
    if (col - 1 >= 0 && row >= 0 && row <= 8) {
      validLocations.add(map[row][col - 1]);
    }
    if (col + 1 <= 8 && row >= 0 && row <= 8) {
      validLocations.add(map[row][col + 1]);
    }

    return validLocations;
  }
Esempio n. 5
0
    private void populate() {
      // populate with MovePawns
      for (int i = -2; i <= 2; i++) {
        for (int j = -2; j <= 2; j++) {
          Square tmp = setting.getPawn(mover, setting).getSquare();
          Board settingClone = new BoardImpl((BoardImpl) setting);
          Player moverClone = new PlayerImpl(mover);
          MovePawn tentative =
              new MovePawnImpl(tmp.getCol() + j, tmp.getRow() + i, moverClone, settingClone);

          if (tentative.isValid()) {

            pawnBackerList.add(new StateImpl(settingClone, moverClone, tentative));
          }
        }
      }

      for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
          Board settingClone = new BoardImpl((BoardImpl) setting);
          Player moverClone = new PlayerImpl(mover);

          PlaceWall vTentative = new PlaceWallImpl(i, j, Wall.VERTICAL, moverClone, settingClone);

          settingClone = new BoardImpl((BoardImpl) setting);
          moverClone = new PlayerImpl(mover);

          PlaceWall hTentative = new PlaceWallImpl(i, j, Wall.HORIZONTAL, moverClone, settingClone);

          if (vTentative.isValid()) {
            wallBackerList.add(new StateImpl(settingClone, moverClone, vTentative));
          }

          if (hTentative.isValid()) {
            wallBackerList.add(new StateImpl(settingClone, moverClone, hTentative));
          }
        }
      }
    }
Esempio n. 6
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;
  }
Esempio n. 7
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;
  }