示例#1
0
  /**
   * Run a game in asynchronous mode and recorded.
   *
   * @param pacManController The Pac-Man controller
   * @param ghostController The Ghosts controller
   * @param trials The number of trials to be executed
   * @param fileName The file name of the file that saves the replay
   */
  public void runGameTimedRecorded(
      Controller<MOVE> pacManController,
      Controller<EnumMap<GHOST, MOVE>> ghostController,
      boolean visual,
      String fileName) {
    StringBuilder replay = new StringBuilder();

    Game game = new Game(0);

    GameView gv = null;

    if (visual) {
      gv = new GameView(game).showGame();

      if (pacManController instanceof HumanController)
        gv.getFrame().addKeyListener(((HumanController) pacManController).getKeyboardInput());
    }

    new Thread(pacManController).start();
    new Thread(ghostController).start();

    while (!game.gameOver()) {
      pacManController.update(game.copy(), System.currentTimeMillis() + DELAY);
      ghostController.update(game.copy(), System.currentTimeMillis() + DELAY);

      try {
        Thread.sleep(DELAY);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }

      game.advanceGame(pacManController.getMove(), ghostController.getMove());

      if (visual) gv.repaint();

      replay.append(game.getGameState() + "\n");
    }

    pacManController.terminate();
    ghostController.terminate();

    saveToFile(replay.toString(), fileName, false);
  }
示例#2
0
文件: Game.java 项目: jgacitua/PacMan
  /**
   * Gets the game state as a string: all variables are written to a string in a pre-determined
   * order. The string may later be used to recreate a game state using the setGameState() method.
   *
   * <p>Variables not included: enableGlobalReversals
   *
   * @return The game state as a string
   */
  public String getGameState() {
    StringBuilder sb = new StringBuilder();

    sb.append(
        indiceDeLaberinto
            + ","
            + tiempoTotal
            + ","
            + score
            + ","
            + tiempoLvlActual
            + ","
            + cuentaElLvl
            + ","
            + pacman.currentNodeIndex
            + ","
            + pacman.lastMoveMade
            + ","
            + pacman.numberOfLivesRemaining
            + ","
            + pacman.hasReceivedExtraLife
            + ",");

    for (Ghost ghost : fantasmas.values())
      sb.append(
          ghost.currentNodeIndex
              + ","
              + ghost.edibleTime
              + ","
              + ghost.lairTime
              + ","
              + ghost.lastMoveMade
              + ",");

    for (int i = 0; i < laberintoActua.pillIndices.length; i++)
      if (pills.get(i)) sb.append("1");
      else sb.append("0");

    sb.append(",");

    for (int i = 0; i < laberintoActua.powerPillIndices.length; i++)
      if (powerPills.get(i)) sb.append("1");
      else sb.append("0");

    sb.append(",");
    sb.append(timeOfLastGlobalReversal);
    sb.append(",");
    sb.append(pacmanFueComido);
    sb.append(",");

    for (GHOST ghost : GHOST.values()) {
      sb.append(fantasmaComido.get(ghost));
      sb.append(",");
    }

    sb.append(pastillaFueComida);
    sb.append(",");
    sb.append(pildoraPoderFueComida);

    return sb.toString();
  }