Android Open Source - MentalMathX Game Controller Impl






From Project

Back to project page MentalMathX.

License

The source code is released under:

GNU General Public License

If you think the Android project MentalMathX 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 net.schlingel.bplaced.mentalmathx.controller.impl;
//from   ww  w .  j  av a 2  s. com
import net.schlingel.bplaced.mentalmathx.controller.GameController;
import net.schlingel.bplaced.mentalmathx.game.Difficulty;
import net.schlingel.bplaced.mentalmathx.game.Mode;
import net.schlingel.bplaced.mentalmathx.game.logic.GameLogic;
import net.schlingel.bplaced.mentalmathx.game.logic.HoundredRoundsGameLogic;
import net.schlingel.bplaced.mentalmathx.game.logic.InfiniteGameLogic;
import net.schlingel.bplaced.mentalmathx.game.logic.TenRoundsGameLogic;
import net.schlingel.bplaced.mentalmathx.game.strategy.ExerciseStrategy;
import net.schlingel.bplaced.mentalmathx.math.Term;
import net.schlingel.bplaced.mentalmathx.model.Result;
import net.schlingel.bplaced.mentalmathx.view.GameView;

/**
 * Created by zombie on 27.06.14.
 */
public class GameControllerImpl implements GameController {
    private static final String TAG = GameControllerImpl.class.getSimpleName();
    private static class Ticker implements Runnable {
        private GameController ctrl;
        private long seconds;
        private transient boolean isAlive;

        public Ticker(GameController ctrl) {
            this(ctrl, 0L);
        }

        public Ticker(GameController ctrl, long seconds) {
            this.ctrl = ctrl;
            this.seconds = seconds;
            this.isAlive = true;
        }

        public void shutdown() {
            isAlive = false;
        }

        public void setSeconds(long seconds)  {
            this.seconds = seconds;
        }

        @Override
        public void run() {
            while (isAlive) {
                try {
                    Thread thread = Thread.currentThread();

                    synchronized (thread) {
                        Thread.currentThread().wait(1000L);
                        seconds++;

                        ctrl.onTimeChange(seconds);
                    }
                } catch (InterruptedException e) {
                }
            }
        }
    }

    private int correctGuesses;
    private int wrongGuesses;
    private ExerciseStrategy exerciseFactory;
    private Term currentExercise;
    private String currentInput;
    private GameView view;
    private Ticker ticker;
    private long timeInSeconds;
    private GameLogic gameLogic;
    private Mode mode;

    public GameControllerImpl(Difficulty difficulty, Mode mode, GameView view) {
        this.gameLogic = createGameLogic(difficulty, mode);
        this.exerciseFactory = gameLogic.exerciseFactory();
        this.view = view;
        this.mode = mode;

        reset();
        updateView();
    }

    private GameLogic createGameLogic(Difficulty difficulty, Mode mode) {
        switch (mode) {
            case HoundredRounds:
                return new HoundredRoundsGameLogic(difficulty);
            case TenRounds:
                return new TenRoundsGameLogic(difficulty);
            case Marathon:
                return new InfiniteGameLogic(difficulty);
            default:
                throw new IllegalArgumentException();
        }
    }

    @Override
    public void onFigureInput(int figure) {
        String result = Integer.toString(currentExercise.value());
        currentInput = currentInput + Integer.toString(figure);

        if(result.startsWith(currentInput)) {
            if(result.length() == currentInput.length()) {
                correctGuesses++;
                currentInput = "";

                gameLogic.endRound();
                view.onCorrectGuess();
                currentExercise = exerciseFactory.nextProblem(correctGuesses);
            }
        } else {
            wrongGuesses++;
            gameLogic.onWrongGuess();
            view.onWrongGuess();
            currentInput = "";
            updateView();
        }

        updateView();

        if(gameLogic.isGameOver()) {
            ticker.shutdown();
            view.onGameOver();
        }
    }

    @Override
    public void onTimeChange(long timeInSec) {
        this.timeInSeconds = timeInSec;
        updateView();
    }

    private void updateView() {
        Result result = new Result();

        result.setCorrectGuesses(correctGuesses);
        result.setWrongGuesses(wrongGuesses);
        result.setTime(timeInSeconds);
        result.setRounds(correctGuesses);
        result.setMode(mode);

        view.updateInput(currentInput + "_");
        view.updateExercise(currentExercise.toString());
        view.updateStats(result);
    }

    @Override
    public void reset() {
        correctGuesses = 0;
        wrongGuesses = 0;
        currentExercise = exerciseFactory.nextProblem(correctGuesses);
        currentInput = "";
        timeInSeconds = 0;

        if(ticker != null) {
            ticker.shutdown();
        }

        ticker = new Ticker(this);
        new Thread(ticker).start();
    }

    @Override
    public void pause() {
        ticker.shutdown();
    }

    @Override
    public void resume() {
        ticker = new Ticker(this, timeInSeconds);
        new Thread(ticker).start();
    }

    @Override
    public void setValues(Term currentExercise, int correctGuesses, int wrongGuesses, long timeInSeconds) {
        this.currentExercise = currentExercise;
        this.correctGuesses = correctGuesses;
        this.wrongGuesses = wrongGuesses;

        this.currentInput = "";
    }
}




Java Source Code List

net.schlingel.bplaced.mentalmathx.AboutActivity.java
net.schlingel.bplaced.mentalmathx.GameActivity.java
net.schlingel.bplaced.mentalmathx.HighscoresActivity.java
net.schlingel.bplaced.mentalmathx.NewGameActivity.java
net.schlingel.bplaced.mentalmathx.SelectDifficultyActivity.java
net.schlingel.bplaced.mentalmathx.controller.GameController.java
net.schlingel.bplaced.mentalmathx.controller.HighscoreController.java
net.schlingel.bplaced.mentalmathx.controller.impl.GameControllerImpl.java
net.schlingel.bplaced.mentalmathx.controller.impl.HighscoreControllerImpl.java
net.schlingel.bplaced.mentalmathx.game.Difficulty.java
net.schlingel.bplaced.mentalmathx.game.Mode.java
net.schlingel.bplaced.mentalmathx.game.logic.FiniteGameLogic.java
net.schlingel.bplaced.mentalmathx.game.logic.GameLogic.java
net.schlingel.bplaced.mentalmathx.game.logic.HoundredRoundsGameLogic.java
net.schlingel.bplaced.mentalmathx.game.logic.InfiniteGameLogic.java
net.schlingel.bplaced.mentalmathx.game.logic.TenExercisesGameLogic.java
net.schlingel.bplaced.mentalmathx.game.logic.TenRoundsGameLogic.java
net.schlingel.bplaced.mentalmathx.game.strategy.EasyExerciseStrategy.java
net.schlingel.bplaced.mentalmathx.game.strategy.ExerciseStrategyFactory.java
net.schlingel.bplaced.mentalmathx.game.strategy.ExerciseStrategy.java
net.schlingel.bplaced.mentalmathx.game.strategy.HardExerciseStrategy.java
net.schlingel.bplaced.mentalmathx.game.strategy.MediumExerciseStrategy.java
net.schlingel.bplaced.mentalmathx.game.strategy.OneOOneExerciseStrategy.java
net.schlingel.bplaced.mentalmathx.math.Calculation.java
net.schlingel.bplaced.mentalmathx.math.Calculations.java
net.schlingel.bplaced.mentalmathx.math.Number.java
net.schlingel.bplaced.mentalmathx.math.Operator.java
net.schlingel.bplaced.mentalmathx.math.Term.java
net.schlingel.bplaced.mentalmathx.model.Result.java
net.schlingel.bplaced.mentalmathx.model.Score.java
net.schlingel.bplaced.mentalmathx.model.adapters.ScoreAdapter.java
net.schlingel.bplaced.mentalmathx.utils.DatabaseHelper.java
net.schlingel.bplaced.mentalmathx.utils.DelayedTask.java
net.schlingel.bplaced.mentalmathx.utils.LabelHelper.java
net.schlingel.bplaced.mentalmathx.utils.MarathonScoreComparator.java
net.schlingel.bplaced.mentalmathx.utils.RegularScoreComparator.java
net.schlingel.bplaced.mentalmathx.view.DisplayMode.java
net.schlingel.bplaced.mentalmathx.view.GameView.java
net.schlingel.bplaced.mentalmathx.view.HighscoresView.java
net.schlingel.bplaced.mentalmathx.view.ResultsView.java
net.schlingel.bplaced.mentalmathx.view.impl.DialogResultsView.java
net.schlingel.bplaced.mentalmathx.view.impl.FragmentHighscoresSubView.java