Android Open Source - Do-not-get-annoyed Game Player Factory






From Project

Back to project page Do-not-get-annoyed.

License

The source code is released under:

Apache License

If you think the Android project Do-not-get-annoyed 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 mn100013d.pmu.services;
/*from  w  w w.  j a  va 2 s  .  co  m*/
import java.util.ArrayList;

import mn100013d.pmu.controllers.GameController;
import mn100013d.pmu.models.Board;
import mn100013d.pmu.models.CPUGamePlayer;
import mn100013d.pmu.models.Color;
import mn100013d.pmu.models.GamePlayer;
import mn100013d.pmu.models.HumanGamePlayer;
import android.util.Log;

public class GamePlayerFactory {
  public static final String DEBUG = "mn100013d.pmu.services.DEBUG";
  private static final ArrayList<String> REGULAR_OPTIONS = new ArrayList<String>();
  private ShakeDetector shakeDetector;
  private Randomizer randomizer;
  private int firstPlayerIndex = 0;
  private Boolean FIRST_PLAYER_INDEX_VALUE_AVAILABLE = false;
  static {
    REGULAR_OPTIONS.add("CPU");
    REGULAR_OPTIONS.add("HUMAN");
    REGULAR_OPTIONS.add("OFF");
  }

  public GamePlayerFactory(ShakeDetector shakeDetector, Randomizer randomizer) {
    this.randomizer = randomizer;
    this.shakeDetector = shakeDetector;
    Log.i(DEBUG, "GamePlayerFactory has been created");
  }

  private GamePlayer create(String name, String option, int color, Board board,
      GameController gController) {
    if (option.equalsIgnoreCase("CPU")) {
      Log.i(DEBUG, "CPUGamePlayer created");
      return new CPUGamePlayer(name, color, board, gController, randomizer);
    }

    if (option.equalsIgnoreCase("HUMAN")) {
      Log.i(DEBUG, "HumanGamePlayer created");
      return new HumanGamePlayer(name, color, board, gController, shakeDetector);
    }

    return null;
  }

  public Boolean firstPlayerIndexValueIsAvailable() {
    return FIRST_PLAYER_INDEX_VALUE_AVAILABLE;
  }

  public int getFirstPlayerIndex() {
    if (FIRST_PLAYER_INDEX_VALUE_AVAILABLE)
      return firstPlayerIndex;
    Log.wtf(DEBUG,
        "First Player Index is not available! Please use firstPlayerIndexValueIsAvailable"
            + " to determin if value is available. Return value is -1");
    return -1;
  }

  public GamePlayer[] create(String[] names, String[] options, Board board,
      GameController gController) {
    GamePlayer[] players = new GamePlayer[4];
    Log.i(DEBUG, "Create metod called");
    if (options.length != 4) {
      Log.w(DEBUG, "String[] options length must be 4");
      return null;
    }
    int[] colors = { Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW };
    for (int i = 0; i < 4; i++) {
      if (!REGULAR_OPTIONS.contains(options[i])) {
        Log.w(DEBUG, "option must be one of the values CPU, "
            + "HUMAN, OFF, invalid value on the index of " + i);
        return null;
      }
      
      players[i] = create(names[i], options[i], colors[i], board, gController);
      if (players[i] != null){
        if (!FIRST_PLAYER_INDEX_VALUE_AVAILABLE) {
          firstPlayerIndex = i;
          FIRST_PLAYER_INDEX_VALUE_AVAILABLE = true;
        }
      }
    }
    return players;
  }
}




Java Source Code List

mn100013d.pmu.BeginingActivity.java
mn100013d.pmu.GameTypeFragment.java
mn100013d.pmu.NewGameActivity.java
mn100013d.pmu.NewGameFragment.java
mn100013d.pmu.PauseFragment.java
mn100013d.pmu.PauseGameFragment.java
mn100013d.pmu.ScoresFragment.java
mn100013d.pmu.SettingsFragment.java
mn100013d.pmu.StartActivity.java
mn100013d.pmu.StartGameFragment.java
mn100013d.pmu.controllers.GameController.java
mn100013d.pmu.data.GameDataDbHelper.java
mn100013d.pmu.data.GameSettingsEditor.java
mn100013d.pmu.data.GameTableContract.java
mn100013d.pmu.exceptions.ContextNotSetException.java
mn100013d.pmu.exceptions.GameExceptions.java
mn100013d.pmu.exceptions.PlayerNotRegisteredException.java
mn100013d.pmu.models.Board.java
mn100013d.pmu.models.CPUGamePlayer.java
mn100013d.pmu.models.Color.java
mn100013d.pmu.models.Dice.java
mn100013d.pmu.models.Field.java
mn100013d.pmu.models.FinishField.java
mn100013d.pmu.models.GamePlayer.java
mn100013d.pmu.models.HomeField.java
mn100013d.pmu.models.HumanGamePlayer.java
mn100013d.pmu.models.PathField.java
mn100013d.pmu.models.Pawn.java
mn100013d.pmu.models.Result.java
mn100013d.pmu.services.FragmentProvider.java
mn100013d.pmu.services.GamePlayerFactory.java
mn100013d.pmu.services.PopupService.java
mn100013d.pmu.services.Randomizer.java
mn100013d.pmu.services.ShakeDetector.java
mn100013d.pmu.services.SoundService.java