Back to project page Do-not-get-annoyed.
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.
package mn100013d.pmu.models; /*from w ww . j av a 2s . c om*/ import java.io.Serializable; import java.util.ArrayList; import mn100013d.pmu.exceptions.PlayerNotRegisteredException; import mn100013d.pmu.services.SoundService; /** * @author Milutinovic * */ public class Pawn implements Serializable{ private static int[][] paths = { {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44}, {11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,1,2,3,4,5,6,7,8,9,10,45,46,47,48}, {21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,49,50,51,52}, {31,32,33,34,35,36,37,38,39,40,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,53,54,55,56} }; /** * Value of this constant is {@value} * */ public static final int ACTIVE = 1; /** * Value of this constant is {@value} * */ public static final int INNACTIVE = 2; /** * Value of this constant is {@value} * */ public static final int FINISHED = 3; /** * Value of this constant is {@value} representing that move tried to be played * is not possible due to INDEX OUT OF BOUND * @see Pawn#makeMove(int) */ public static final int MOVE_IMPOSSIBLE = -2; /** * Value of this constant is {@value} * <p> * Represents the maximum number of moves {@link Pawn} can make * @see Pawn * */ public static final int MAX_MOVES = 44; private GamePlayer player; private Board board; private ArrayList<Field> path; private int position; // relative position on the pawns path private int state = INNACTIVE; private int color; private Field currentField; public Pawn(GamePlayer player, int color, Board board, Field field){ this.player = player; // Assign player to the pawn this.board = board; // Assign board to the pawn this.position = -1; // Set current position ON THE HOME FIELD path = new ArrayList<Field>(); // Create path from board fields for (int i = 0; i<paths[color].length; i++){ path.add(board.getField(paths[color][i])); } this.color = color; // Sets the color of the pawn this.currentField = field; // Assigns the field to the pawn, initially it is HomeField } /** * @return player assigned to the pawn * */ public GamePlayer getPlayer(){ return player; } /** * @return color assigned to the pawn * */ public int getColor(){ return player.getColor(); } /** * @return current position relative to its starting position * */ public int getPosition(){ return position; } /** * @return the currentField */ public Field getCurrentField() { return currentField; } /** * @param currentField the currentField to set */ public void setCurrentField(Field currentField) { this.currentField = currentField; } /** * @return Absolute index of the starting * position of the pawn on the {@link Board}*/ public int getStartingPosition(){ return paths[color][0]; } /** * Activates the pawn by setting its state to {@link #ACTIVE} and * placing it on stating position 0 - relative to its path * on the board * <p> * Sets currentField of the pawn to corresponding {@link Field} on * the {@link Board} * * @see GamePlayer * @see {@link GamePlayer#activatePawn(Pawn pawn)} * */ public void activate(){ state = ACTIVE; position = 0; currentField = getPathField(0); try { SoundService.getInstance().play(SoundService.PLAYER_MOVE_1, false); } catch (PlayerNotRegisteredException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * Deactivates the pawn by setting its state to {@link #INNACTIVE} and * placing it on stating position -1 - corresponding to HomeField * on the board * <p> * {@link GamePlayer} is responsible for setting the corresponding {@link HomeField} * to the {@link Pawn} upon it's deactivation * * @see GamePlayer * @see {@link GamePlayer#deactivatePawn(Pawn pawn)} * */ public void deactivate(){ state = INNACTIVE; position = -1; try { SoundService.getInstance().play(SoundService.PLAYER_MOVE_2, false); } catch (PlayerNotRegisteredException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * @return Current state of the pawn * <p> * <strong>Possible states * <ol>{@link #ACTIVE}</ol> * <ol>{@link #INNACTIVE}</ol> * <ol>{@link #FINISHED}</ol> * */ public int getState(){ return state; } /** * @return TRUE if state of the pawn is {@link #INNACTIVE} * @see #getState() * */ public Boolean isFinished(){ if (position>40) return true; return false; } /** * @return ArrayList of {@link Field}s that are the * path of the pawn. * */ public ArrayList<Field> getPath(){ return path; } /** * @return TRUE is {@link Pawn} state is set to {@link #ACTIVE} FALSE otherwise * */ public Boolean isActive(){ return (state == ACTIVE); } /** * @return {@link Field} from the path on the board assigned to the pawn if move is possible * or NULL if move is not possible and is not aware of other pawns on the board * @param moves represents number of moves ahead pawns current position; * @see {@link Board#getField(int)} * */ public Field getPathField(int moves){ if (!isActive()){ if (moves != 6) return null; // Pawn cannot be activated return board.getField(paths[color][0]); } if ((position+moves)>=44) return null; int absolutePosition = paths[color][position+moves]; if ((position+moves)>44) return null; return board.getField(absolutePosition); } /** * Method will set the field's state to {@link Field#OCCUPIED} and * corresponding {@link Color} * <p> * {@link Pawn} is not responsible for attacking and removing other pawns from * the {@link Board} * @return State of the {@link Pawn} after the move is played * @param moves represents the number of moves the pawn should move in this move * @see {@link GamePlayer#makeMove(Pawn pawn, int number_of_moves)} * @see {@link Field#setPawn(Pawn)} * * */ public int move(int moves){ if (moves+position>44) return MOVE_IMPOSSIBLE; currentField.setPawn(null); currentField = getPathField(moves); position+=moves; currentField.setPawn(this); try { SoundService.getInstance().play(SoundService.TP, false); } catch (PlayerNotRegisteredException e) { // TODO Auto-generated catch block e.printStackTrace(); } return 0; } }