Exemple #1
0
  /**
   * Play one game of 2048, updating the maximum score. Return true iff play should continue with
   * another game, or false to exit.
   */
  boolean play() {

    setRandomPiece();

    while (true) {
      setRandomPiece();
      if (gameOver()) {
        if (_score > _maxScore) {
          _maxScore = _score;
        }
        _game.setScore(_score, _maxScore);
        _game.endGame();
      }
      GetMove:
      while (true) {
        String key = _game.readKey();
        if (key.equals("\u2191")) {
          key = "Up";
        }
        if (key.equals("\u2190")) {
          key = "Left";
        }
        if (key.equals("\u2192")) {
          key = "Right";
        }
        if (key.equals("\u2193")) {
          key = "Down";
        }
        switch (key) {
          case "Up":
          case "Down":
          case "Left":
          case "Right":
            if (!gameOver() && tiltBoard(keyToSide(key))) {
              break GetMove;
            }
            break;

          case "New Game":
            _game.setScore(_score, _maxScore);
            if (_score > _maxScore) {
              _maxScore = _score;
            }
            _game.setScore(_score, _maxScore);
            clear();
            play();
            break;
          case "Quit":
            return false;
          default:
            break;
        }
      }
    }
  }
Exemple #2
0
  /**
   * The main program. ARGS may contain the options --seed=NUM, (random seed); --log (record moves
   * and random tiles selected.); --testing (take random tiles and moves from standard input); and
   * --no-display.
   */
  public static void main(String... args) {
    CommandArgs options = new CommandArgs("--seed=(\\d+) --log --testing --no-display", args);
    if (!options.ok()) {
      System.err.println(
          "Usage: java game2048.Main [ --seed=NUM ] " + "[ --log ] [ --testing ] [ --no-display ]");
      System.exit(1);
    }

    Main game = new Main(options);

    while (game.play()) {
      /* No action */
    }
    System.exit(0);
  }