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






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.models;
//from   w  w w.j  av a 2  s.  com
import java.io.Serializable;
import java.util.ArrayList;

import mn100013d.pmu.controllers.GameController;

public abstract class GamePlayer implements Serializable {
  protected Pawn[] pawns = new Pawn[4];
  protected GameController gController;
  private int color;
  protected Board board;
  private int active_pawns;
  protected String name;
  protected Dice _dice = null;
  protected int eaten = 0;
  protected int beenEaten = 0; 
  
  

  /**
   * GamePlayer will create 4 {@link Pawn}s and will
   * place them on the {@link Board} on the {@link HomeField}s
   * @param color represents the {@link Color} of the player
   * @param board represents the playing board
   * */
  public GamePlayer(String name, int color, Board board, GameController gController){
    this.color = color;
    this.board = board;
    this.name = name;
    this.gController = gController;
    ArrayList<HomeField> home = board.getHomeFields(color);
    for (int i = 0; i<4; i++){
      pawns[i] = new Pawn(this, color, board, home.get(i));
      home.get(i).setPawn(pawns[i]);
    }
  }
  /**
   * Abstract method of player's behavior when he 
   * starts the move
   * */
  protected abstract void _play();
  /**
   * Method that will set the {@link Dice} instance
   * and will call abstract method {@link #_play()} on the GamePlayer
   * that has the right to play current turn
   * */
  public void play(Dice dice){
    _dice = dice;
    _play();
  }
  /**
   * Abstract method of player's behavior when he
   * decides on how to play the move*/
  public abstract void decide();
  /**
   * @return Array of {@link Pawn}s assigned to the player
   * */
  public Pawn[] getPawns() {
    return pawns;
  }
  /**
   * @return The color of the player
   * @see {@link Color}*/
  public int getColor() {
    return color;
  }
  
  /**
   * @return the eaten
   */
  public int getEaten() {
    return eaten;
  }
  /**
   * @param eaten the eaten to set
   */
  public void setEaten(int eaten) {
    this.eaten = eaten;
  }
  /**
   * @return the beenEaten
   */
  public int getBeenEaten() {
    return beenEaten;
  }
  /**
   * @param beenEaten the beenEaten to set
   */
  public void setBeenEaten(int beenEaten) {
    this.beenEaten = beenEaten;
  }
  public void incEaten(){
    eaten++;
  }
  public void incBeenEaten(){
    beenEaten++;
  }
  public String getName(){
    return name;
  }
  /**
   * Moves the {@link Pawn} to the position away from it's
   * current position by the given parameter of moves
   * or activates the pawn if the pawn is {@link Pawn#INNACTIVE}
   * @param pawn represents pawn to be moved
   * @param moves represents number of moves for making
   * @see {@link Pawn}
   * @see {@link Pawn#getState()}
   * */
  public void makeMove(Pawn pawn, int moves) {
    if (!pawn.isActive()) activatePawn(pawn);
    else{
      pawn.move(moves);
    }
    gController.nextTurn();
  }
  /**
   * @return Number of active {@link Pawn}s
   **/
  public int getNumberOfActivePawns() {
    return active_pawns;
  }
  /**
   * @return Number of inactive {@link Pawn}s*/
  public int getNumberOfInactivePawns() {
    return 4 - active_pawns;
  }
  /**
   * Takes the given {@link Pawn} and places him on the 
   * starting position on the {@link Board}
   * @return returns activated pawn
   * @param pawn represents pawn to be activated
   * */
  public Pawn activatePawn(Pawn pawn) {
    active_pawns++;
    pawn.getCurrentField().setPawn(null);
    pawn.activate();
    pawn.getCurrentField().setPawn(pawn);
    return pawn;

  }
  /**
   * @return First empty field on the board that represents
   * the {@link HomeField}s area*/
  private HomeField getEmptyHomeField(){
    ArrayList<HomeField> home = board.getHomeFields(color);
    for (int i = 0; i<4; i++){
      if (home.get(i).getState() == Field.INITIAL_STATE)
        return home.get(i);
    }
    return null;
  }
  /**
   * Takes the pawn deactivates it. Places it on to the empty
   * {@link HomeField} and clears the previos {@link Field}
   * @param pawn represents the {@link Pawn} to be deactivated
   * @see {@link PathField}
   * */
  public void deactivatePawn(Pawn pawn) {
    pawn.deactivate();
    pawn.getCurrentField().setPawn(null);
    Field homeField = getEmptyHomeField();
    homeField.setPawn(pawn);
    pawn.setCurrentField(homeField);
    active_pawns--;

  }
}




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