コード例 #1
0
  /**
   * Moves from the games current Room to the Room on the given Direction, provided in the
   * constructor An opened Door must exist between both Doors to be moved. Otherwise, an exception
   * is thrown.
   */
  public void execute() throws CommandExecutionException {
    try {
      Room actualRoom = this.game.getCurrentMap().getCurrentRoom();
      Door aux = this.game.getCurrentMap().getDoor(actualRoom, direction);
      if (aux != null) {
        Room target = aux.nextRoom(actualRoom);
        if (aux.isOpen()) {
          if (aux.connect(actualRoom, target)) {
            System.out.print(Msg.game_mooving.replace("{Directions}", direction.name()));
            this.game.getCurrentMap().setCurrentRoom(aux.nextRoom(actualRoom));
            if (this.game.getCurrentMap().getCurrentRoom().isExit()) {
              this.game.getPlayer().addHealth(-5);
              System.out.print(Msg.game_goodbye);
              System.out.print(
                  "HEALTH = "
                      + this.game.getPlayer().getHealth()
                      + ", SCORE ="
                      + this.game.getPlayer().getPoints()
                      + "\n");
              this.game.requestQuit();
            } else {
              this.game.getPlayer().addHealth(-5);
              if (this.game.getPlayer().dead()) {
                System.out.print(
                    "HEALTH = "
                        + this.game.getPlayer().getHealth()
                        + ", SCORE ="
                        + this.game.getPlayer().getPoints()
                        + "\n");
                this.game.requestQuit();
                throw new CommandExecutionException("You are dead");

              } else {
                System.out.print(this.game.getCurrentMap().getCurrentRoom().getDescription());
                System.out.print(
                    "HEALTH = "
                        + this.game.getPlayer().getHealth()
                        + ", SCORE ="
                        + this.game.getPlayer().getPoints()
                        + "\n");
              }
            }
          } else throw new CommandExecutionException(Msg.game_notconnect);
        } else
          throw new CommandExecutionException(
              Msg.game_doorclosed.replace("{Directions}", direction.name()));
      } else
        throw new CommandExecutionException(
            Msg.game_nodoor.replace("{Directions}", this.direction.name()));
    } catch (Exception e) {
      throw new CommandExecutionException(e.getMessage());
    }
  }