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.controllers; /*from w w w . j a va 2 s . com*/ import java.io.Serializable; import java.util.ArrayList; import mn100013d.pmu.GameTypeFragment; import mn100013d.pmu.R; import mn100013d.pmu.data.GameDataDbHelper; import mn100013d.pmu.data.GameSettingsEditor; import mn100013d.pmu.models.Board; import mn100013d.pmu.models.CPUGamePlayer; import mn100013d.pmu.models.Dice; import mn100013d.pmu.models.Field; import mn100013d.pmu.models.GamePlayer; import mn100013d.pmu.services.FragmentProvider; import mn100013d.pmu.services.GamePlayerFactory; import mn100013d.pmu.services.PopupService; import mn100013d.pmu.services.Randomizer; import mn100013d.pmu.services.ShakeDetector; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.CountDownTimer; import android.util.Log; import android.view.View; import android.widget.Toast; public class GameController implements Serializable { private Board board; private Dice dice; private Context context; private View view; private ShakeDetector shakeDetector; private Randomizer randomizer; private GameSettingsEditor gsEditor; private GamePlayer[] players = new GamePlayer[4]; private GamePlayer currentPlayer; private int currentPlayerIndex; private Boolean debugMode = false; private PopupService pleaseWaitPopup; private Boolean gameOn = true; public GameController(final View view, final Context context) { this.context = context; this.view = view; board = new Board(this, this.view, this.context); dice = new Dice(view.findViewById(R.id.dice_holder), this.context); pleaseWaitPopup = new PopupService(PopupService.PLEASE_WAIT, view.findViewById(R.id.newgame_layout), context) { @Override public Boolean setHideOnOutsideClick() { return false; } @Override public void onShow() { } @Override public void onHide() { } }; shakeDetector = new ShakeDetector(context) { @Override public void onShakeDetected() { dice.startRoll(); } @Override public void onShakeCompletion() { dice.stopRoll(); currentPlayer.decide(); } @Override public int setTimeout() { return 0; } }; randomizer = new Randomizer() { @Override public void produce() { int val = (int) (Math.random() * 6); dice.setValue(val + 1); currentPlayer.decide(); } }; Intent i = ((Activity) context).getIntent(); String redName = i.getStringExtra(GameTypeFragment.RED_NAME); String blueName = i.getStringExtra(GameTypeFragment.BLUE_NAME); String greenName = i.getStringExtra(GameTypeFragment.GREEN_NAME); String yellowName = i.getStringExtra(GameTypeFragment.YELLOW_NAME); Log.i("Nikola", redName); Log.i("Nikola", blueName); Log.i("Nikola", greenName); Log.i("Nikola", yellowName); String redOption = i.getStringExtra(GameTypeFragment.RED_OPTION); String blueOption = i.getStringExtra(GameTypeFragment.BLUE_OPTION); String greenOption = i.getStringExtra(GameTypeFragment.GREEN_OPTION); String yellowOption = i.getStringExtra(GameTypeFragment.YELLOW_OPTION); GamePlayerFactory gFactory = new GamePlayerFactory(shakeDetector, randomizer); String[] options = { redOption, blueOption, greenOption, yellowOption }; String[] names = { redName, blueName, greenName, yellowName }; players = gFactory.create(names, options, board, this); gsEditor = new GameSettingsEditor(context); if (gFactory.firstPlayerIndexValueIsAvailable()) { currentPlayer = players[gFactory.getFirstPlayerIndex()]; currentPlayerIndex = gFactory.getFirstPlayerIndex(); } setSensitivity(gsEditor.getSensitivity()); currentPlayer.play(dice); } public void setSensitivity(int sensitivity) { shakeDetector.setSensitivity(gsEditor.getSensitivity()); } public void nextTurn() { if (gameOn) new CountDownTimer(300, 300) { @Override public void onTick(long millisUntilFinished) { // TODO Auto-generated method stub } @Override public void onFinish() { hidePopup(PopupService.PLEASE_WAIT); board.refreshBoard(); if (dice.getValue() != 6) { currentPlayer = nextPlayer(); } if (currentPlayer instanceof CPUGamePlayer) { showPopup(PopupService.PLEASE_WAIT); } if (!debugMode) { currentPlayer.play(dice); } } }.start(); } public void pause() { shakeDetector.deactivate(); } public void resume() { shakeDetector.activate(shakeDetector.getMode()); } public Boolean toggleDebugMode() { debugMode = !debugMode; if (debugMode) { shakeDetector.deactivate(); } else { shakeDetector.activate(shakeDetector.getMode()); } return debugMode; } public void forceDice(int number) { if (debugMode == false) { Toast.makeText(context, "Debug mode is not active", Toast.LENGTH_SHORT).show(); return; } Log.i("DEBUG", "Dice forced to number " + number); ArrayList<Field> options = board.getOptions(currentPlayer, number); if (options.size() == 0) nextTurn(); dice.setValue(number); } private GamePlayer nextPlayer() { do { currentPlayerIndex = (currentPlayerIndex + 1) % 4; currentPlayer = players[currentPlayerIndex]; Log.i("Nikola", "Next"); } while (currentPlayer == null); return currentPlayer; } public void gameOver() { GameDataDbHelper dbHelper = new GameDataDbHelper(context); for (int i = 0; i < 4; i++) { GamePlayer p = players[i]; if (p != null) { dbHelper.addNewEntry(p.getName(), p.getEaten(), p.getBeenEaten()); } } dbHelper.getAllEntries(); gameOn = false; FragmentProvider.getInstance().changeFragment(FragmentProvider.SCORES); } public void showPopup(int popup) { switch (popup) { case PopupService.PLEASE_WAIT: pleaseWaitPopup.show(); break; } } public void hidePopup(int popup) { switch (popup) { case PopupService.PLEASE_WAIT: pleaseWaitPopup.hide(); break; } } public void refreshBoard() { board.refreshBoard(); } public void turnGameOff(){ gameOn = false; } }