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 w w. ja v a2s . com*/ import java.io.Serializable; import java.util.ArrayList; import mn100013d.pmu.R; import mn100013d.pmu.controllers.GameController; import mn100013d.pmu.exceptions.PlayerNotRegisteredException; import mn100013d.pmu.services.SoundService; import android.content.Context; import android.util.Log; import android.view.View; import android.widget.Toast; public class Board implements Serializable{ private static final String INFO_TAG = "mn100013d.pmu.models.Board.INFO_TAG"; private Context context; private View view; private GameController gameController; private ArrayList<Field> gamePath = new ArrayList<Field>(); private ArrayList<HomeField> redHome; private ArrayList<HomeField> blueHome; private ArrayList<HomeField> greenHome; private ArrayList<HomeField> yellowHome; private ArrayList<FinishField> redFinish = new ArrayList<FinishField>(); private ArrayList<FinishField> blueFinish = new ArrayList<FinishField>(); private ArrayList<FinishField> greenFinish = new ArrayList<FinishField>(); private ArrayList<FinishField> yellowFinish = new ArrayList<FinishField>(); /** * Board contructor <strong>initializes</strong> * <ul> * <ol>gamePath </ol> * <ol>finishFields for each player </ol> * <ol>homeFields for each player </ol> * </ul> * <p> * Initializes the HomeFields for every player of the game * @param gController {@link GameController} * @param view {@link View} * @param context {@link Context} * @see Board#initHomeFields(int) * */ public Board(GameController gController, View view, Context context){ this.gameController = gController; this.view = view; this.context = context; for (int i = 1; i<= 56; i++){ String name = "path_field_"+i; int id = context.getResources().getIdentifier(name, "id", "mn100013d.pmu"); View field_view = this.view.findViewById(id); if (i>40){ if (i>40 && i<45){ gamePath.add(new FinishField(this, field_view, context, Color.RED)); redFinish.add((FinishField) gamePath.get(i-1)); } if (i>44 && i<49){ gamePath.add(new FinishField(this, field_view, context, Color.BLUE)); blueFinish.add((FinishField) gamePath.get(i-1)); } if (i>48 && i<53){ gamePath.add(new FinishField(this, field_view, context, Color.GREEN)); greenFinish.add((FinishField) gamePath.get(i-1)); } if (i>52 && i<57){ gamePath.add(new FinishField(this, field_view, context, Color.YELLOW)); yellowFinish.add((FinishField) gamePath.get(i-1)); } } else gamePath.add(new PathField(this, field_view, context)); redHome = initHomeFields(Color.RED); blueHome = initHomeFields(Color.BLUE); greenHome = initHomeFields(Color.GREEN); yellowHome = initHomeFields(Color.YELLOW); } } /** * @return {@link Field} from the board on the given position * @param index represents the index of the field on the {@link Board} * <p> * index is decreased due to pawn's path starts with index of 1 * @see {@link Pawn}*/ public Field getField(int index){ return gamePath.get(index-1); } /** * Initializes the homeFields for the {@link GamePlayer} with the given {@link Color} * @return ArrayList of {@link HomeField}s for given color * @param color represents the color of the player*/ public ArrayList<HomeField> initHomeFields(int color){ ArrayList<HomeField> home = new ArrayList<HomeField>(); switch(color){ case Color.RED: home.add(new HomeField(this, view.findViewById(R.id.relative_layout_game_type_holder), context)); home.add(new HomeField(this, view.findViewById(R.id.home_field_2), context)); home.add(new HomeField(this, view.findViewById(R.id.home_field_3), context)); home.add(new HomeField(this, view.findViewById(R.id.home_field_4), context)); break; case Color.BLUE: home.add(new HomeField(this, view.findViewById(R.id.home_field_5), context)); home.add(new HomeField(this, view.findViewById(R.id.home_field_6), context)); home.add(new HomeField(this, view.findViewById(R.id.home_field_7), context)); home.add(new HomeField(this, view.findViewById(R.id.home_field_8), context)); break; case Color.GREEN: home.add(new HomeField(this, view.findViewById(R.id.home_field_9), context)); home.add(new HomeField(this, view.findViewById(R.id.home_field_10), context)); home.add(new HomeField(this, view.findViewById(R.id.home_field_11), context)); home.add(new HomeField(this, view.findViewById(R.id.home_field_12), context)); break; case Color.YELLOW: home.add(new HomeField(this, view.findViewById(R.id.home_field_13), context)); home.add(new HomeField(this, view.findViewById(R.id.home_field_14), context)); home.add(new HomeField(this, view.findViewById(R.id.home_field_15), context)); home.add(new HomeField(this, view.findViewById(R.id.home_field_16), context)); break; } return home; } /** * @return ArrayList of {@link HomeField}s for the specific * {@link Color} given as the parameter * @param color represents the color of the {@link GamePlayer} * */ public ArrayList<HomeField> getHomeFields(int color){ switch (color){ case Color.RED: return redHome; case Color.BLUE: return blueHome; case Color.GREEN: return greenHome; case Color.YELLOW: return yellowHome; } return null; } /** * Sets the right state of the {@link Field} using the color of the * {@link Pawn} placed on the field * @param field represents the field to be changed * */ public void highlightField(Field field){ if (field.getPawn() == null){ field.setState(Field.HIGHLIGHTED, -1); } else{ field.setState(Field.ATTACKED, field.getPawn().getColor()); } } private GamePlayer _player; private int _moves; /** * Method will highlight properly every {@link Field} available for playing to * and will set OnClickListener for each of those * @return ArrayList of {@link Field}s that can be played by the player with * each of his {@link Pawn}s * @param player represents the {@link GamePlayer} whose options are * requested * @param moves represents the number of fields player is requested to mvoe his * pawns */ public ArrayList<Field> getOptions(GamePlayer player, int moves){ ArrayList<Field> options = new ArrayList<Field>(); _player = player; _moves = moves; Pawn[] pawns = player.getPawns(); for (int i = 0; i<4; i++){ Pawn pawn = pawns[i]; Field option = pawn.getPathField(moves); if (option == null) continue; if (option.getState() == Field.OCCUPIED) if (option.getPawn().getColor() != pawn.getColor()){ option.setState(Field.ATTACKED, option.getPawn().getColor()); options.add(option); option.registerOnClickListener(pawn); } if (option.getState() == Field.INITIAL_STATE){ option.setState(Field.HIGHLIGHTED, pawn.getColor()); options.add(option); option.registerOnClickListener(pawn); } } return options; } /** * Method will delegate the action to the active {@link GamePlayer} * giving him an information on which {@link Pawn} should he move and * for how many moves*/ public void fieldClicked(Field field, Pawn pawn){ if (_moves == -1 || _player == null){ Log.wtf("ERROR fieldClicked METHOD", "moves = "+_moves+" - and _player might be null"); return; } if (field.getState() == Field.ATTACKED){ Pawn eatenPawn = field.getPawn(); GamePlayer opositePlayer = eatenPawn.getPlayer(); opositePlayer.deactivatePawn(eatenPawn); opositePlayer.incBeenEaten(); _player.incEaten(); } _player.makeMove(pawn, _moves); refreshBoard(); _moves = -1; _player = null; } public void refreshBoard(){ for (int i=0; i<gamePath.size(); i++){ gamePath.get(i).refresh(); } int redCount = 0; int blueCount = 0; int greenCount = 0; int yellowCount = 0; for (int i = 0; i<4; i++){ if (redFinish.get(i).getPawn()!=null) redCount++; } for (int i = 0; i<4; i++){ if (blueFinish.get(i).getPawn()!=null) blueCount++; } for (int i = 0; i<4; i++){ if (greenFinish.get(i).getPawn()!=null) greenCount++; } for (int i = 0; i<4; i++){ if (yellowFinish.get(i).getPawn()!=null) yellowCount++; } if (redCount == 4) { gameController.gameOver(); Toast.makeText(context, "GAME DONE", Toast.LENGTH_SHORT).show(); } if (blueCount == 4) { gameController.gameOver(); Toast.makeText(context, "GAME DONE", Toast.LENGTH_SHORT).show(); } if (greenCount == 4) { gameController.gameOver(); Toast.makeText(context, "GAME DONE", Toast.LENGTH_SHORT).show(); } if (yellowCount == 4) { gameController.gameOver(); Toast.makeText(context, "GAME DONE", Toast.LENGTH_SHORT).show(); } } }