Example #1
0
  /**
   * Run the game with time limit (asynchronous mode). This is how it will be done in the
   * competition. Can be played with and without visual display of game states.
   *
   * @param pacManController The Pac-Man controller
   * @param ghostController The Ghosts controller
   * @param visual Indicates whether or not to use visuals
   */
  public void runGameTimed(
      Controller<MOVE> pacManController,
      Controller<EnumMap<GHOST, MOVE>> ghostController,
      boolean visual) {
    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();
    }

    pacManController.terminate();
    ghostController.terminate();
  }
Example #2
0
  /**
   * Run the game in asynchronous mode but proceed as soon as both controllers replied. The time
   * limit still applies so so the game will proceed after 40ms regardless of whether the
   * controllers managed to calculate a turn.
   *
   * @param pacManController The Pac-Man controller
   * @param ghostController The Ghosts controller
   * @param fixedTime Whether or not to wait until 40ms are up even if both controllers already
   *     responded
   * @param visual Indicates whether or not to use visuals
   */
  public void runGameTimedSpeedOptimised(
      Controller<MOVE> pacManController,
      Controller<EnumMap<GHOST, MOVE>> ghostController,
      boolean fixedTime,
      boolean visual) {
    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 {
        int waited = DELAY / INTERVAL_WAIT;

        for (int j = 0; j < DELAY / INTERVAL_WAIT; j++) {
          Thread.sleep(INTERVAL_WAIT);

          if (pacManController.hasComputed() && ghostController.hasComputed()) {
            waited = j;
            break;
          }
        }

        if (fixedTime) Thread.sleep(((DELAY / INTERVAL_WAIT) - waited) * INTERVAL_WAIT);

        game.advanceGame(pacManController.getMove(), ghostController.getMove());
      } catch (InterruptedException e) {
        e.printStackTrace();
      }

      if (visual) gv.repaint();
    }

    pacManController.terminate();
    ghostController.terminate();
  }
Example #3
0
  /**
   * Replay a previously saved game.
   *
   * @param fileName The file name of the game to be played
   * @param visual Indicates whether or not to use visuals
   */
  public void replayGame(String fileName, boolean visual) {
    ArrayList<String> timeSteps = loadReplay(fileName);

    Game game = new Game(0);

    GameView gv = null;

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

    for (int j = 0; j < timeSteps.size(); j++) {
      game.setGameState(timeSteps.get(j));

      try {
        Thread.sleep(DELAY);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      if (visual) gv.repaint();
    }
  }
Example #4
0
  public void runQLearner(
      Controller<MOVE> pacManController,
      Controller<EnumMap<GHOST, MOVE>> ghostController,
      int iteraciones) {
    System.out.println("Entrenamos nuestro controlador con " + iteraciones + " iteraciones.");
    long start_time = System.nanoTime();

    QLearner learner = new QLearner();
    learner.initialize();
    Game game;

    Estado estado;
    Accion accion;
    Estado lastEstado;
    Accion lastAccion;
    Estado ePrima;
    MOVE siguienteMovimiento;
    float recompensa;

    for (int i = 0; i < iteraciones; i++) {
      System.out.println("Ejecucion del juego: " + i);
      game = new Game(0);

      lastEstado = null;
      lastAccion = null;

      while (!game.gameOver()) {
        recompensa = 0;
        estado = learner.getState(game);
        accion = learner.getAccion(estado);
        siguienteMovimiento = accion.getMovimientoFromAccion(game);
        recompensa = estado.getRecompensa();

        if (lastEstado != null) {
          learner.update(lastEstado, lastAccion, recompensa, estado);
        }

        lastEstado = estado;
        lastAccion = accion;

        // Le pasamos el siguiente movimiento al controlador
        ((QController) ghostController).setSiguienteMovimiento(siguienteMovimiento);

        game.advanceGame(
            pacManController.getMove(game.copy(), System.currentTimeMillis() + DELAY),
            ghostController.getMove(game.copy(), System.currentTimeMillis() + DELAY));
      }

      estado = learner.getState(game);
      recompensa = estado.getRecompensa();
      learner.update(lastEstado, lastAccion, recompensa, estado);
    }

    long end_time = System.nanoTime();
    double tiempo = (((end_time - start_time) / 1e6) / 1000);
    System.out.println(learner);

    System.out.printf("Tiempo de entrenamiento: %.2f segundos%n", tiempo);

    System.out.printf(
        "Ejecucion del juego en modo grafico con la QTable generada en el entrenamiento.");

    game = new Game(0);
    GameView gv = new GameView(game).showGame();

    while (!game.gameOver()) {

      pacManController.update(game.copy(), System.currentTimeMillis() + DELAY);
      ghostController.update(game.copy(), System.currentTimeMillis() + DELAY);

      // Obtenemos la mejor accion segun el estado actual (miramos en la QTabla la accion con mas
      // puntuacion para ese estado)
      Accion mejorAccionFromTQtable = learner.getMejorAccion(learner.getState(game));
      siguienteMovimiento = mejorAccionFromTQtable.getMovimientoFromAccion(game);

      // Le pasamos el siguiente movimiento al controlador
      ((QController) ghostController).setSiguienteMovimiento(siguienteMovimiento);

      game.advanceGame(
          pacManController.getMove(game.copy(), System.currentTimeMillis() + DELAY),
          ghostController.getMove(game.copy(), System.currentTimeMillis() + DELAY));

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

      gv.repaint();
    }
  }