コード例 #1
0
  /**
   * Performs an action from source token to the target token. Actions can be: 1. Nothing (action
   * not permitted) 2. Move (valid movement action) 3. Attack (valid attack action)
   *
   * @param src The source token
   * @param trg The target token
   * @return The outcome of the action was successful. False: no action whatsoever.
   */
  public boolean tokenAction(
      Board board,
      Token src,
      Token trg,
      Vector2D trgloc,
      StrategoAppViewer viewer,
      boolean specialPower) {
    // List<Vector2D> moveLocations = new ArrayList<Vector2D>();
    // Determine if action is valid (is permitted):
    // Token action is already validated by the viewer, but the controller ought to check:
    // (in case viewer is not used):
    boolean found = false;
    // Get available positions for movement/attack:
    if (specialPower) {
      this.moveLocations = tokenSpecialSelection(board, src);
    } else {
      this.moveLocations = tokenSelection(board, src);
    }

    for (int i = 0; i < this.moveLocations.size(); i++) {
      if ((trgloc.y == this.moveLocations.get(i).y) && (trgloc.x == this.moveLocations.get(i).x)) {
        found = true;
        break;
      }
    }
    // if no valid movement/token detected: do not execute action:
    if (!found) {
      return false;
    }
    // Now that the movement is permitted:
    // Check what token action to perform:
    if (trg instanceof BackgroundToken) {
      // Grass is the only move-onto background token:
      this.moveToken(board, (MovablePlayerToken) src, trgloc);
    } else {
      // else this is an attack:
      this.attackToken(board, (MovablePlayerToken) src, (PlayerToken) trg, specialPower);
    }

    MovablePlayerToken saviour = null;
    // Check if a token moved in enemy's back row:

    for (int k = 0; k < getTurn().side().tokens.size(); k++) {
      if (getTurn().side().pname == "Fire") {
        if ((getTurn().side().tokens.get(k).getRow() == 0)) {
          saviour = (MovablePlayerToken) getTurn().side().tokens.get(k);
        }
      } else {
        if ((getTurn().side().tokens.get(k).getRow() == 7)) {
          saviour = (MovablePlayerToken) getTurn().side().tokens.get(k);
        }
      }
    }
    // Scouts can not save tokens:
    if (saviour instanceof Scout) {
      saviour = null;
    } else if (getTurn().side().getTotalSaves() > 2) {
      // Can not save more than twice:
      saviour = null;
    } else if ((saviour != null) && saviour.savedToken()) {
      // Can not save more than twice:
      // Get a random Token to save:
      for (int k = 0; k < getTurn().side().tokens.size(); k++) {
        if (getTurn().side().tokens.get(k).getCol() < 0) {
          if (getTurn().side().tokens.get(k) instanceof MovablePlayerToken) {
            if (saveToken(board, (MovablePlayerToken) getTurn().side().tokens.get(k))) {
              break;
            }
          }
        }
      }
    }

    // if player turn completed successfully, check if winning condition:
    getTurn().setWinner(doWeHaveAWinner(board, getTurn()));
    // Else change sides:
    if (getTurn().getWinner() == null) {
      getTurn().flip();
    } else {
      endGame(getTurn());
    }

    return true;
  }