Android Open Source - 2048GameAndroid Game






From Project

Back to project page 2048GameAndroid.

License

The source code is released under:

Apache License

If you think the Android project 2048GameAndroid 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 zhengxiao.myapplication.game;
/*from   www . j  ava2 s. co m*/
import android.support.annotation.NonNull;

/**
 * Created by zhengxiao on 12/15/14.
 */
public class Game {

    private final Callback activity;

    public Board board = new Board(ROW_COUNT);
    public final static int ROW_COUNT = 4;

    public Game(Callback activity) {
        this.activity = activity;
        setUpGame();
    }

    public static interface Callback{
        void render(@NonNull Board board);
        void gameOver(int finalScore);
    }

    private boolean setUpGame() {
        board.insertBlockRandomly();
        board.insertBlockRandomly();
        notifyRenderer();
        return true;
    }

    /**
     * ??Activity????????
     */
    private void notifyRenderer() {
        activity.render(board);
    }

    private void notifyGameOver() {
        activity.gameOver(0);
    }

    /**
     * ????????????
     * @return ??????
     */
    public boolean moveInDirection(Direction direction){
        boolean canMove = board.moveInDirection(direction);
        if(canMove) {
            populateOnceAtEmptyBlock();
        }
        return canMove;
    }

    /**
     * ????????????????????????????? GameOver???????Activity ??
     * @return ?????
     */
    private void populateOnceAtEmptyBlock() {
        boolean hasNextPosition = board.insertBlockRandomly();
        if(!hasNextPosition){
            notifyGameOver();
        }else{
            notifyRenderer();
        }
    }


}




Java Source Code List

zhengxiao.myapplication.ApplicationTest.java
zhengxiao.myapplication.GameActivity.java
zhengxiao.myapplication.MainActivity.java
zhengxiao.myapplication.game.BoardTest.java
zhengxiao.myapplication.game.Board.java
zhengxiao.myapplication.game.Direction.java
zhengxiao.myapplication.game.GameTest.java
zhengxiao.myapplication.game.Game.java
zhengxiao.myapplication.game.Position.java
zhengxiao.myapplication.game.Utils.java
zhengxiao.myapplication.utils.OnGestureListenerAdapter.java