Android Open Source - DroidSweeper Game Config






From Project

Back to project page DroidSweeper.

License

The source code is released under:

MIT License

If you think the Android project DroidSweeper listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package de.nisble.droidsweeper.config;
//  www . ja  v  a  2s . com
import android.content.Context;
import android.content.res.Configuration;
import android.os.Parcel;
import android.os.Parcelable;

/** Representation of the configuration of a game.
 * This class wraps a Level and adds the ability to handle custom difficulty
 * levels. If instantiated from a standard level the grid sizes are set
 * accordingly.
 * It is also able to adapt the dimensions of the game grid to the
 * current orientation of the device. This is simply done by switching the sides
 * of the game grid.
 * <ul>
 * <li>Immutable: Members are public final and capitalized.</li>
 * <li>Parcelable: Can e.g. passed as extra to an intent.</li>
 * </ul>
 * @author Moritz Nisbl moritz.nisble@gmx.de */
public final class GameConfig implements Parcelable {
  public final Level LEVEL;
  public final int X;
  public final int Y;
  public final int BOMBS;

  /** Instantiate from Level.
   * <b>Do not call this constructor with Level.CUSTOM</b>
   * @param l A Level. */
  public GameConfig(Level l) {
    LEVEL = l;
    X = l.X;
    Y = l.Y;
    BOMBS = l.BOMBS;
  }

  /** Instantiate with custom grid size.
   * <b>The internal Level is set to Level.CUSTOM</b>
   * @param x The width of the grid.
   * @param y The height of the grid.
   * @param bombs The bomb count. */
  public GameConfig(int x, int y, int bombs) {
    LEVEL = Level.CUSTOM;
    X = x;
    Y = y;
    BOMBS = bombs;
  }

  private GameConfig(Level l, int x, int y, int bombs) {
    LEVEL = l;
    X = x;
    Y = y;
    BOMBS = bombs;
  }

  /** Get an orientation adjusted version.
   * @param c The application context.
   * @return An orientation corrected copy of the GameConfig. */
  public GameConfig adjustOrientation(Context c) {
    return adjustOrientation(c.getResources().getConfiguration());
  }

  /** Get an orientation adjusted version.
   * @param config The current configuration.
   * @return An orientation corrected copy of the GameConfig. */
  public GameConfig adjustOrientation(Configuration config) {
    if (Configuration.ORIENTATION_LANDSCAPE == config.orientation)
      return toLandscape();
    else
      return toPortrait();
  }

  /** Get the portrait version.
   * @return A copy of the GameConfig that fits best to portrait orientation. */
  public GameConfig toPortrait() {
    int max = Math.max(X, Y);
    int min = (max == X) ? Y : X;
    return new GameConfig(LEVEL, min, max, BOMBS);
  }

  /** Get the landscape version.
   * @return A copy of the GameConfig that fits best to landscape orientation. */
  public GameConfig toLandscape() {
    int max = Math.max(X, Y);
    int min = (max == X) ? Y : X;
    return new GameConfig(LEVEL, max, min, BOMBS);
  }

  @Override
  public String toString() {
    return "GameConfig [LEVEL=" + LEVEL.toString() + ", X=" + X + ", Y=" + Y + ", BOMBS=" + BOMBS + "]";
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + BOMBS;
    result = prime * result + ((LEVEL == null) ? 0 : LEVEL.hashCode());
    result = prime * result + X;
    result = prime * result + Y;
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (!(obj instanceof GameConfig))
      return false;
    GameConfig other = (GameConfig) obj;
    if (BOMBS != other.BOMBS)
      return false;
    if (LEVEL != other.LEVEL)
      return false;
    if (X != other.X)
      return false;
    if (Y != other.Y)
      return false;
    return true;
  }

  public GameConfig(Parcel in) {
    int[] data = new int[4];

    in.readIntArray(data);
    LEVEL = Level.fromInt(data[0]);
    X = data[1];
    Y = data[2];
    BOMBS = data[3];
  }

  @Override
  public int describeContents() {
    return 0;
  }

  @Override
  public void writeToParcel(Parcel dest, int flags) {
    dest.writeIntArray(new int[] { LEVEL.ordinal(), X, Y, BOMBS });
  }

  public static final Creator<GameConfig> CREATOR = new Creator<GameConfig>() {
    public GameConfig createFromParcel(Parcel in) {
      return new GameConfig(in);
    }

    public GameConfig[] newArray(int size) {
      return new GameConfig[size];
    }
  };
}




Java Source Code List

de.nisble.droidsweeper.config.ApplicationConfig.java
de.nisble.droidsweeper.config.Constants.java
de.nisble.droidsweeper.config.GameConfig.java
de.nisble.droidsweeper.config.Level.java
de.nisble.droidsweeper.game.Field.java
de.nisble.droidsweeper.game.GameObserver.java
de.nisble.droidsweeper.game.Game.java
de.nisble.droidsweeper.game.Position.java
de.nisble.droidsweeper.game.database.DSDBAdapter.java
de.nisble.droidsweeper.game.database.DSDBContract.java
de.nisble.droidsweeper.game.database.DSDBGameEntry.java
de.nisble.droidsweeper.game.database.DSDBHelper.java
de.nisble.droidsweeper.game.jni.FieldListener.java
de.nisble.droidsweeper.game.jni.FieldStatus.java
de.nisble.droidsweeper.game.jni.GameStatus.java
de.nisble.droidsweeper.game.jni.MatrixObserver.java
de.nisble.droidsweeper.game.jni.MineSweeperMatrix.java
de.nisble.droidsweeper.game.replay.PlayerObserver.java
de.nisble.droidsweeper.game.replay.Player.java
de.nisble.droidsweeper.game.replay.Recorder.java
de.nisble.droidsweeper.game.replay.Replay.java
de.nisble.droidsweeper.game.replay.TimeStep.java
de.nisble.droidsweeper.gui.DroidSweeperActivity.java
de.nisble.droidsweeper.gui.HighScoreActivity.java
de.nisble.droidsweeper.gui.HighScoreListAdapter.java
de.nisble.droidsweeper.gui.SettingsActivity.java
de.nisble.droidsweeper.gui.grid.FieldDrawables.java
de.nisble.droidsweeper.gui.grid.FieldView.java
de.nisble.droidsweeper.gui.grid.GameGridView.java
de.nisble.droidsweeper.utilities.LogDog.java
de.nisble.droidsweeper.utilities.Timer.java