Exemple #1
0
  /**
   * Run a game in asynchronous mode: the game waits until a move is returned. In order to slow
   * thing down in case the controllers return very quickly, a time limit can be used. If fasted
   * gameplay is required, this delay should be put as 0.
   *
   * @param pacManController The Pac-Man controller
   * @param ghostController The Ghosts controller
   * @param visual Indicates whether or not to use visuals
   * @param delay The delay between time-steps
   */
  public void runGame(
      Controller<MOVE> pacManController,
      Controller<EnumMap<GHOST, MOVE>> ghostController,
      boolean visual,
      int delay) {
    try {
      PrintStream out = new PrintStream("replay.txt");
      Game game = new Game(0);

      GameView gv = null;

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

      while (!game.gameOver()) {
        game.advanceGame(
            pacManController.getMove(game.copy(), -1),
            ghostController.getMove(game.copy(), System.currentTimeMillis() + Long.MAX_VALUE));

        try {
          Thread.sleep(delay);
        } catch (Exception e) {
        }

        if (visual) gv.repaint();
        out.println(game.getGameState());
        out.flush();
      }
      out.close();
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
Exemple #2
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);
  }